Skip to content

Commit

Permalink
fix issue #3535 and add colon between mode and type when dumping func…
Browse files Browse the repository at this point in the history
…ion prototype
  • Loading branch information
Vincent-Belliard authored and nikomatsakis committed Sep 26, 2012
1 parent 95bc32d commit ef23d77
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
45 changes: 28 additions & 17 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -584,6 +584,29 @@ impl parser {
} else { infer(self.get_id()) }
}

fn is_named_argument() -> bool {
let offset = if self.token == token::BINOP(token::AND) {
1
} else if self.token == token::BINOP(token::MINUS) {
1
} else if self.token == token::ANDAND {
1
} else if self.token == token::BINOP(token::PLUS) {
if self.look_ahead(1) == token::BINOP(token::PLUS) {
2
} else {
1
}
} else { 0 };
if offset == 0 {
is_plain_ident(self.token)
&& self.look_ahead(1) == token::COLON
} else {
is_plain_ident(self.look_ahead(offset))
&& self.look_ahead(offset + 1) == token::COLON
}
}

fn parse_capture_item_or(parse_arg_fn: fn(parser) -> arg_or_capture_item)
-> arg_or_capture_item {

Expand All @@ -605,29 +628,17 @@ impl parser {
// This version of parse arg doesn't necessarily require
// identifier names.
fn parse_arg_general(require_name: bool) -> arg {
let m = self.parse_arg_mode();
let i = if require_name {
let mut m;
let i = if require_name || self.is_named_argument() {
m = self.parse_arg_mode();
let name = self.parse_value_ident();
self.expect(token::COLON);
name
} else {
if is_plain_ident(self.token)
&& self.look_ahead(1u) == token::COLON {
let name = self.parse_value_ident();
self.bump();
name
} else { special_idents::invalid }
m = infer(self.get_id());
special_idents::invalid
};

match m {
expl(_) => {
if i == special_idents::invalid {
self.obsolete(copy self.span, ObsoleteModeInFnType);
}
}
_ => {}
}

let t = self.parse_ty(false);

{mode: m, ty: t, ident: i, id: self.get_id()}
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/util/ppaux.rs
Expand Up @@ -261,7 +261,7 @@ fn ty_to_str(cx: ctxt, typ: t) -> ~str {
m == ty::default_arg_mode_for_ty(cx, ty) {
~""
} else {
mode_to_str(ast::expl(m))
mode_to_str(ast::expl(m)) + ":"
}
}
};
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/unnamed_argument_mode.rs
@@ -0,0 +1,14 @@
//error-pattern: mismatched types

fn bad(&a: int) {
}

// unnamed argument &int is now parsed x: &int
// it's not parsed &x: int anymore

fn called(f: fn(&int)) {
}

fn main() {
called(bad);
}
11 changes: 11 additions & 0 deletions src/test/run-pass/unnamed_argument_mode.rs
@@ -0,0 +1,11 @@
fn good(a: &int) {
}

// unnamed argument &int is now parse x: &int

fn called(f: fn(&int)) {
}

fn main() {
called(good);
}

0 comments on commit ef23d77

Please sign in to comment.