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

Uninhabited while-let pattern fix #39526

Merged
merged 3 commits into from
Feb 5, 2017
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
27 changes: 22 additions & 5 deletions src/librustc_const_eval/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
let mut seen = Matrix::empty();
let mut catchall = None;
let mut printed_if_let_err = false;
for &(ref pats, guard) in arms {
for (arm_index, &(ref pats, guard)) in arms.iter().enumerate() {
for &(pat, hir_pat) in pats {
let v = vec![pat];

Expand Down Expand Up @@ -302,10 +302,27 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
let &(ref first_arm_pats, _) = &arms[0];
let first_pat = &first_arm_pats[0];
let span = first_pat.0.span;
struct_span_err!(cx.tcx.sess, span, E0165,
"irrefutable while-let pattern")
.span_label(span, &format!("irrefutable pattern"))
.emit();

// check which arm we're on.
match arm_index {
// The arm with the user-specified pattern.
0 => {
let mut diagnostic = Diagnostic::new(Level::Warning,
"unreachable pattern");
diagnostic.set_span(pat.span);
cx.tcx.sess.add_lint_diagnostic(
lint::builtin::UNREACHABLE_PATTERNS,
hir_pat.id, diagnostic);
},
// The arm with the wildcard pattern.
1 => {
struct_span_err!(cx.tcx.sess, span, E0165,
"irrefutable while-let pattern")
.span_label(span, &format!("irrefutable pattern"))
.emit();
},
_ => bug!(),
}
},

hir::MatchSource::ForLoopDesugar |
Expand Down
8 changes: 8 additions & 0 deletions src/test/compile-fail/uninhabited-patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct NotSoSecretlyEmpty {
_priv: !,
}

fn foo() -> Option<NotSoSecretlyEmpty> {
None
}

fn main() {
let x: &[!] = &[];

Expand All @@ -45,5 +49,9 @@ fn main() {
Err(Err(_y)) => (),
Err(Ok(_y)) => (), //~ ERROR unreachable pattern
}

while let Some(_y) = foo() {
//~^ ERROR unreachable pattern
}
}