Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept TyError in patterns to avoid ICE on bad input #51696

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,16 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
let def = self.tables.qpath_def(qpath, pat.hir_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be less fragile to check at the assignment of the tables field, whether tables.tainted_by_errors is true and then just not recurse.

Copy link
Contributor Author

@estebank estebank Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm no. You're right. your original thing seemed better. I thought we could check before calling from_hir or new, but I just checked and that seems messy.

r=me with your original thing

let adt_def = match ty.sty {
ty::TyAdt(adt_def, _) => adt_def,
_ => span_bug!(pat.span, "tuple struct pattern not applied to an ADT"),
ty::TyError => { // Avoid ICE (#50585)
return Pattern {
span: pat.span,
ty,
kind: Box::new(PatternKind::Wild),
};
}
_ => span_bug!(pat.span,
"tuple struct pattern not applied to an ADT {:?}",
ty.sty),
};
let variant_def = adt_def.variant_of_def(def);

Expand Down Expand Up @@ -637,6 +646,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
let substs = match ty.sty {
ty::TyAdt(_, substs) |
ty::TyFnDef(_, substs) => substs,
ty::TyError => { // Avoid ICE (#50585)
return PatternKind::Wild;
}
_ => bug!("inappropriate type for def: {:?}", ty.sty),
};
PatternKind::Variant {
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/issue-50585.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
|y: Vec<[(); for x in 0..2 {}]>| {};
//~^ ERROR mismatched types
}
14 changes: 14 additions & 0 deletions src/test/ui/issue-50585.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0308]: mismatched types
--> $DIR/issue-50585.rs:12:18
|
LL | fn main() {
| - expected `()` because of default return type
LL | |y: Vec<[(); for x in 0..2 {}]>| {};
| ^^^^^^^^^^^^^^^^ expected usize, found ()
|
= note: expected type `usize`
found type `()`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.