Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #3535 and add colon between mode and type when dumping function #3595

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}