Skip to content

Commit 0301d53

Browse files
author
Jorge Aparicio
committed
---
yaml --- r: 182904 b: refs/heads/beta c: 76362f0 h: refs/heads/master v: v3
1 parent 5643cee commit 0301d53

File tree

7 files changed

+39
-10
lines changed

7 files changed

+39
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ refs/heads/automation-fail: 1bf06495443584539b958873e04cc2f864ab10e4
3131
refs/heads/issue-18208-method-dispatch-3-quick-reject: 2009f85b9f99dedcec4404418eda9ddba90258a2
3232
refs/heads/batch: b7fd822592a4fb577552d93010c4a4e14f314346
3333
refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
34-
refs/heads/beta: 5e1820f34611e311759518440fd4129c5f0c322a
34+
refs/heads/beta: 76362f0a0ee1b66d7df524c4c8dea9b15b45cf78
3535
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3636
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
3737
refs/heads/tmp: eb836bf767aa1d8d4cba488a9091cde3c0ab4b2f

branches/beta/src/librustc/lint/builtin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ impl LintPass for UnusedParens {
11631163
ast::MatchSource::Normal => (head, "`match` head expression", true),
11641164
ast::MatchSource::IfLetDesugar { .. } => (head, "`if let` head expression", true),
11651165
ast::MatchSource::WhileLetDesugar => (head, "`while let` head expression", true),
1166+
ast::MatchSource::ForLoopDesugar => (head, "`for` head expression", true),
11661167
},
11671168
ast::ExprRet(Some(ref value)) => (value, "`return` value", false),
11681169
ast::ExprAssign(_, ref value) => (value, "assigned value", false),

branches/beta/src/librustc/middle/check_match.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &ast::Expr) {
221221
.flat_map(|arm| arm.0.iter())
222222
.map(|pat| vec![&**pat])
223223
.collect();
224-
check_exhaustive(cx, ex.span, &matrix);
224+
check_exhaustive(cx, ex.span, &matrix, source);
225225
},
226226
ast::ExprForLoop(ref pat, _, _, _) => {
227227
let mut static_inliner = StaticInliner::new(cx.tcx);
@@ -327,6 +327,14 @@ fn check_arms(cx: &MatchCheckCtxt,
327327
span_err!(cx.tcx.sess, span, E0165, "irrefutable while-let pattern");
328328
},
329329

330+
ast::MatchSource::ForLoopDesugar => {
331+
// this is a bug, because on `match iter.next()` we cover
332+
// `Some(<head>)` and `None`. It's impossible to have an unreachable
333+
// pattern
334+
// (see libsyntax/ext/expand.rs for the full expansion of a for loop)
335+
cx.tcx.sess.span_bug(pat.span, "unreachable for-loop pattern")
336+
},
337+
330338
ast::MatchSource::Normal => {
331339
span_err!(cx.tcx.sess, pat.span, E0001, "unreachable pattern")
332340
},
@@ -351,18 +359,37 @@ fn raw_pat<'a>(p: &'a Pat) -> &'a Pat {
351359
}
352360
}
353361

354-
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix) {
362+
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix, source: ast::MatchSource) {
355363
match is_useful(cx, matrix, &[DUMMY_WILD_PAT], ConstructWitness) {
356364
UsefulWithWitness(pats) => {
357365
let witness = match &pats[] {
358366
[ref witness] => &**witness,
359367
[] => DUMMY_WILD_PAT,
360368
_ => unreachable!()
361369
};
362-
span_err!(cx.tcx.sess, sp, E0004,
363-
"non-exhaustive patterns: `{}` not covered",
364-
pat_to_string(witness)
365-
);
370+
match source {
371+
ast::MatchSource::ForLoopDesugar => {
372+
// `witness` has the form `Some(<head>)`, peel off the `Some`
373+
let witness = match witness.node {
374+
ast::PatEnum(_, Some(ref pats)) => match &pats[] {
375+
[ref pat] => &**pat,
376+
_ => unreachable!(),
377+
},
378+
_ => unreachable!(),
379+
};
380+
381+
span_err!(cx.tcx.sess, sp, E0297,
382+
"refutable pattern in `for` loop binding: \
383+
`{}` not covered",
384+
pat_to_string(witness));
385+
},
386+
_ => {
387+
span_err!(cx.tcx.sess, sp, E0004,
388+
"non-exhaustive patterns: `{}` not covered",
389+
pat_to_string(witness)
390+
);
391+
},
392+
}
366393
}
367394
NotUseful => {
368395
// This is good, wildcard pattern isn't reachable

branches/beta/src/librustc/util/ppaux.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
100100
ast::ExprMethodCall(..) => "method call",
101101
ast::ExprMatch(_, _, ast::MatchSource::IfLetDesugar { .. }) => "if let",
102102
ast::ExprMatch(_, _, ast::MatchSource::WhileLetDesugar) => "while let",
103+
ast::ExprMatch(_, _, ast::MatchSource::ForLoopDesugar) => "for",
103104
ast::ExprMatch(..) => "match",
104105
_ => "expression",
105106
},

branches/beta/src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ pub enum MatchSource {
788788
Normal,
789789
IfLetDesugar { contains_else_clause: bool },
790790
WhileLetDesugar,
791+
ForLoopDesugar,
791792
}
792793

793794
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]

branches/beta/src/libsyntax/ext/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
288288
fld.cx.expr_call(span, fld.cx.expr_path(next_path), vec![ref_mut_iter]);
289289
let arms = vec![pat_arm, break_arm];
290290

291-
// FIXME(japaric) This should use `ForLoopDesugar` as MatchSource
292-
fld.cx.expr_match(pat_span, next_expr, arms)
291+
fld.cx.expr(pat_span,
292+
ast::ExprMatch(next_expr, arms, ast::MatchSource::ForLoopDesugar))
293293
};
294294

295295
// `[opt_ident]: loop { ... }`

branches/beta/src/test/compile-fail/for-loop-refutable-pattern-error-message.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
1211
fn main() {
1312
for
1413
&1is //~ ERROR refutable pattern in `for` loop binding

0 commit comments

Comments
 (0)