From f2a137829e07505aaaa2be4ae97d6ecfb6ef6823 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Mon, 26 May 2014 22:01:09 +1000 Subject: syntax: desugar a `for` loop to a let binding to get better error messages when the pattern is refutable. This means the compiler points directly to the pattern and said that the problem is the pattern being refutable (rather than just saying that some value isn't covered in the `match` as it did previously). Fixes #14390. --- src/libsyntax/ext/expand.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 762363f3abe..b8766200ccd 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -147,11 +147,17 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr { // [':] loop { // match i.next() { // None => break, - // Some() => + // Some(mut value) => { + // let = value; + // + // } // } // } // } // } + // + // (The use of the `let` is to give better error messages + // when the pattern is refutable.) let local_ident = token::gensym_ident("__i"); // FIXME #13573 let next_ident = fld.cx.ident_of("next"); @@ -167,11 +173,33 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr { fld.cx.arm(span, vec!(none_pat), break_expr) }; - // `Some() => ` + // let = value; + let value_ident = token::gensym_ident("__value"); + // this is careful to use src_pat.span so that error + // messages point exact at that. + let local = @ast::Local { + ty: fld.cx.ty_infer(src_pat.span), + pat: src_pat, + init: Some(fld.cx.expr_ident(src_pat.span, value_ident)), + id: ast::DUMMY_NODE_ID, + span: src_pat.span, + source: ast::LocalFor + }; + let local = codemap::respan(src_pat.span, ast::DeclLocal(local)); + let local = @codemap::respan(span, ast::StmtDecl(@local, ast::DUMMY_NODE_ID)); + + // { let ...; } + let block = fld.cx.block(span, vec![local], + Some(fld.cx.expr_block(src_loop_block))); + + // `Some(mut value) => { ... }` + // Note the _'s in the name will stop any unused mutability warnings. + let value_pat = fld.cx.pat_ident_binding_mode(span, value_ident, + ast::BindByValue(ast::MutMutable)); let some_arm = fld.cx.arm(span, - vec!(fld.cx.pat_enum(span, some_path, vec!(src_pat))), - fld.cx.expr_block(src_loop_block)); + vec!(fld.cx.pat_enum(span, some_path, vec!(value_pat))), + fld.cx.expr_block(block)); // `match i.next() { ... }` let match_expr = { -- cgit 1.4.1-3-g733a5