Skip to content

Commit 2c2919b

Browse files
author
Jorge Aparicio
committed
---
yaml --- r: 177563 b: refs/heads/auto c: 76362f0 h: refs/heads/master i: 177561: df33580 177559: ee3dc8f v: v3
1 parent acd0f6f commit 2c2919b

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
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: 5e1820f34611e311759518440fd4129c5f0c322a
13+
refs/heads/auto: 76362f0a0ee1b66d7df524c4c8dea9b15b45cf78
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/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/auto/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/auto/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/auto/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/auto/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/auto/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)