Skip to content

Commit ef23d77

Browse files
Vincent-Belliardnikomatsakis
authored andcommitted
fix issue #3535 and add colon between mode and type when dumping funcion prototype
1 parent 95bc32d commit ef23d77

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

src/libsyntax/parse/parser.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,29 @@ impl parser {
584584
} else { infer(self.get_id()) }
585585
}
586586

587+
fn is_named_argument() -> bool {
588+
let offset = if self.token == token::BINOP(token::AND) {
589+
1
590+
} else if self.token == token::BINOP(token::MINUS) {
591+
1
592+
} else if self.token == token::ANDAND {
593+
1
594+
} else if self.token == token::BINOP(token::PLUS) {
595+
if self.look_ahead(1) == token::BINOP(token::PLUS) {
596+
2
597+
} else {
598+
1
599+
}
600+
} else { 0 };
601+
if offset == 0 {
602+
is_plain_ident(self.token)
603+
&& self.look_ahead(1) == token::COLON
604+
} else {
605+
is_plain_ident(self.look_ahead(offset))
606+
&& self.look_ahead(offset + 1) == token::COLON
607+
}
608+
}
609+
587610
fn parse_capture_item_or(parse_arg_fn: fn(parser) -> arg_or_capture_item)
588611
-> arg_or_capture_item {
589612

@@ -605,29 +628,17 @@ impl parser {
605628
// This version of parse arg doesn't necessarily require
606629
// identifier names.
607630
fn parse_arg_general(require_name: bool) -> arg {
608-
let m = self.parse_arg_mode();
609-
let i = if require_name {
631+
let mut m;
632+
let i = if require_name || self.is_named_argument() {
633+
m = self.parse_arg_mode();
610634
let name = self.parse_value_ident();
611635
self.expect(token::COLON);
612636
name
613637
} else {
614-
if is_plain_ident(self.token)
615-
&& self.look_ahead(1u) == token::COLON {
616-
let name = self.parse_value_ident();
617-
self.bump();
618-
name
619-
} else { special_idents::invalid }
638+
m = infer(self.get_id());
639+
special_idents::invalid
620640
};
621641

622-
match m {
623-
expl(_) => {
624-
if i == special_idents::invalid {
625-
self.obsolete(copy self.span, ObsoleteModeInFnType);
626-
}
627-
}
628-
_ => {}
629-
}
630-
631642
let t = self.parse_ty(false);
632643

633644
{mode: m, ty: t, ident: i, id: self.get_id()}

src/rustc/util/ppaux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fn ty_to_str(cx: ctxt, typ: t) -> ~str {
261261
m == ty::default_arg_mode_for_ty(cx, ty) {
262262
~""
263263
} else {
264-
mode_to_str(ast::expl(m))
264+
mode_to_str(ast::expl(m)) + ":"
265265
}
266266
}
267267
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//error-pattern: mismatched types
2+
3+
fn bad(&a: int) {
4+
}
5+
6+
// unnamed argument &int is now parsed x: &int
7+
// it's not parsed &x: int anymore
8+
9+
fn called(f: fn(&int)) {
10+
}
11+
12+
fn main() {
13+
called(bad);
14+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn good(a: &int) {
2+
}
3+
4+
// unnamed argument &int is now parse x: &int
5+
6+
fn called(f: fn(&int)) {
7+
}
8+
9+
fn main() {
10+
called(good);
11+
}

0 commit comments

Comments
 (0)