Skip to content

Commit

Permalink
syntax: parse inner attributes on impls.
Browse files Browse the repository at this point in the history
Fixes #3614.
  • Loading branch information
huonw committed Nov 25, 2013
1 parent fb279aa commit e36cb0d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3871,9 +3871,14 @@ impl Parser {
Some(inner_attrs))
}

// parse a method in a trait impl
fn parse_method(&self) -> @method {
let attrs = self.parse_outer_attributes();
// parse a method in a trait impl, starting with `attrs` attributes.
fn parse_method(&self, already_parsed_attrs: Option<~[Attribute]>) -> @method {
let next_attrs = self.parse_outer_attributes();
let attrs = match already_parsed_attrs {
Some(mut a) => { a.push_all_move(next_attrs); a }
None => next_attrs
};

let lo = self.span.lo;

let visa = self.parse_visibility();
Expand Down Expand Up @@ -3966,16 +3971,21 @@ impl Parser {
};

let mut meths = ~[];
if self.eat(&token::SEMI) {
let inner_attrs = if self.eat(&token::SEMI) {
self.obsolete(*self.last_span, ObsoleteEmptyImpl);
None
} else {
self.expect(&token::LBRACE);
let (inner_attrs, next) = self.parse_inner_attrs_and_next();
let mut method_attrs = Some(next);
while !self.eat(&token::RBRACE) {
meths.push(self.parse_method());
meths.push(self.parse_method(method_attrs));
method_attrs = None;
}
}
Some(inner_attrs)
};

(ident, item_impl(generics, opt_trait, ty, meths), None)
(ident, item_impl(generics, opt_trait, ty, meths), inner_attrs)
}

// parse a::B<~str,int>
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ pub fn print_item(s: @ps, item: &ast::item) {

space(s.s);
bopen(s);
print_inner_attributes(s, item.attrs);
for meth in methods.iter() {
print_method(s, *meth);
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/run-pass/inner-attrs-on-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.


struct Foo;

impl Foo {
#[cfg(cfg_that_surely_doesnt_exist)];

fn method(&self) -> bool { false }
}

impl Foo {
#[cfg(not(cfg_that_surely_doesnt_exist))];

// check that we don't eat attributes too eagerly.
#[cfg(cfg_that_surely_doesnt_exist)]
fn method(&self) -> bool { false }

fn method(&self) -> bool { true }
}


pub fn main() {
assert!(Foo.method());
}

5 comments on commit e36cb0d

@bors
Copy link
Contributor

@bors bors commented on e36cb0d Nov 25, 2013

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at huonw@e36cb0d

@bors
Copy link
Contributor

@bors bors commented on e36cb0d Nov 25, 2013

Choose a reason for hiding this comment

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

merging huonw/rust/3614 = e36cb0d into auto

@bors
Copy link
Contributor

@bors bors commented on e36cb0d Nov 25, 2013

Choose a reason for hiding this comment

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

huonw/rust/3614 = e36cb0d merged ok, testing candidate = 55201ed

@bors
Copy link
Contributor

@bors bors commented on e36cb0d Nov 25, 2013

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on e36cb0d Nov 25, 2013

Choose a reason for hiding this comment

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

fast-forwarding master to auto = 55201ed

Please sign in to comment.