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

Fsk 8468 allow underscore paramname in trait default method #8940

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
13 changes: 11 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ impl Drop for Parser {
fn drop(&self) {}
}

fn is_plain_ident_or_underscore(t: &token::Token) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to write this as is_plan_ident(...) || *t == UNDERSCORE just to reduce the duplication of logic between parse::token and here?

Otherwise r=me

is_plain_ident(t) || *t == token::UNDERSCORE
}

impl Parser {
// convert a token to a string using self's reader
pub fn token_to_str(&self, token: &token::Token) -> ~str {
Expand Down Expand Up @@ -1242,11 +1246,13 @@ impl Parser {
_ => 0
};

debug!("parser is_named_argument offset:%u", offset);

if offset == 0 {
is_plain_ident(&*self.token)
is_plain_ident_or_underscore(&*self.token)
&& self.look_ahead(1, |t| *t == token::COLON)
} else {
self.look_ahead(offset, |t| is_plain_ident(t))
self.look_ahead(offset, |t| is_plain_ident_or_underscore(t))
&& self.look_ahead(offset + 1, |t| *t == token::COLON)
}
}
Expand All @@ -1256,6 +1262,8 @@ impl Parser {
pub fn parse_arg_general(&self, require_name: bool) -> arg {
let is_mutbl = self.eat_keyword(keywords::Mut);
let pat = if require_name || self.is_named_argument() {
debug!("parse_arg_general parse_pat (require_name:%?)",
require_name);
self.parse_arg_mode();
let pat = self.parse_pat();

Expand All @@ -1266,6 +1274,7 @@ impl Parser {
self.expect(&token::COLON);
pat
} else {
debug!("parse_arg_general ident_to_pat");
ast_util::ident_to_pat(self.get_id(),
*self.last_span,
special_idents::invalid)
Expand Down
15 changes: 15 additions & 0 deletions src/test/run-pass/default-method-parsing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

trait Foo {
fn m(&self, _:int) { }
}

fn main() { }