Don't allow binding patterns to bind keywords

Closes #1586
This commit is contained in:
Marijn Haverbeke 2012-02-10 15:48:25 +01:00
parent fe8a31e569
commit 7f1ea3ef6a
3 changed files with 14 additions and 24 deletions

View file

@ -666,6 +666,15 @@ fn parse_path(p: parser) -> @ast::path {
{global: global, idents: ids, types: []}); {global: global, idents: ids, types: []});
} }
fn parse_value_path(p: parser) -> @ast::path {
let pt = parse_path(p);
let last_word = pt.node.idents[vec::len(pt.node.idents)-1u];
if p.bad_expr_words.contains_key(last_word) {
p.fatal("found " + last_word + " in expression position");
}
pt
}
fn parse_path_and_ty_param_substs(p: parser, colons: bool) -> @ast::path { fn parse_path_and_ty_param_substs(p: parser, colons: bool) -> @ast::path {
let lo = p.span.lo; let lo = p.span.lo;
let path = parse_path(p); let path = parse_path(p);
@ -1510,11 +1519,11 @@ fn parse_pat(p: parser) -> @ast::pat {
pat = ast::pat_lit(val); pat = ast::pat_lit(val);
} }
} else if is_plain_ident(p) && } else if is_plain_ident(p) &&
alt p.look_ahead(1u) { alt p.look_ahead(1u) {
token::LPAREN | token::LBRACKET | token::LT { false } token::LPAREN | token::LBRACKET | token::LT { false }
_ { true } _ { true }
} { } {
let name = parse_path(p); let name = parse_value_path(p);
let sub = if eat(p, token::AT) { some(parse_pat(p)) } let sub = if eat(p, token::AT) { some(parse_pat(p)) }
else { none }; else { none };
pat = ast::pat_ident(name, sub); pat = ast::pat_ident(name, sub);

View file

@ -1,10 +0,0 @@
// error-pattern: overwriting x will invalidate reference y
fn main() {
let x = [];
let &y = x;
while true {
log(error, y);
x = [1];
}
}

View file

@ -1,9 +0,0 @@
// Ensures that invalidating a reference in one branch doesn't
// influence other branches.
fn main() {
let x = [];
let &y = x;
if true { x = [1]; }
else { log(error, y); }
}