Skip to content

Commit e36cb0d

Browse files
committed
syntax: parse inner attributes on impls.
Fixes #3614.
1 parent fb279aa commit e36cb0d

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

src/libsyntax/parse/parser.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -3871,9 +3871,14 @@ impl Parser {
38713871
Some(inner_attrs))
38723872
}
38733873

3874-
// parse a method in a trait impl
3875-
fn parse_method(&self) -> @method {
3876-
let attrs = self.parse_outer_attributes();
3874+
// parse a method in a trait impl, starting with `attrs` attributes.
3875+
fn parse_method(&self, already_parsed_attrs: Option<~[Attribute]>) -> @method {
3876+
let next_attrs = self.parse_outer_attributes();
3877+
let attrs = match already_parsed_attrs {
3878+
Some(mut a) => { a.push_all_move(next_attrs); a }
3879+
None => next_attrs
3880+
};
3881+
38773882
let lo = self.span.lo;
38783883

38793884
let visa = self.parse_visibility();
@@ -3966,16 +3971,21 @@ impl Parser {
39663971
};
39673972

39683973
let mut meths = ~[];
3969-
if self.eat(&token::SEMI) {
3974+
let inner_attrs = if self.eat(&token::SEMI) {
39703975
self.obsolete(*self.last_span, ObsoleteEmptyImpl);
3976+
None
39713977
} else {
39723978
self.expect(&token::LBRACE);
3979+
let (inner_attrs, next) = self.parse_inner_attrs_and_next();
3980+
let mut method_attrs = Some(next);
39733981
while !self.eat(&token::RBRACE) {
3974-
meths.push(self.parse_method());
3982+
meths.push(self.parse_method(method_attrs));
3983+
method_attrs = None;
39753984
}
3976-
}
3985+
Some(inner_attrs)
3986+
};
39773987

3978-
(ident, item_impl(generics, opt_trait, ty, meths), None)
3988+
(ident, item_impl(generics, opt_trait, ty, meths), inner_attrs)
39793989
}
39803990

39813991
// parse a::B<~str,int>

src/libsyntax/print/pprust.rs

+1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ pub fn print_item(s: @ps, item: &ast::item) {
596596

597597
space(s.s);
598598
bopen(s);
599+
print_inner_attributes(s, item.attrs);
599600
for meth in methods.iter() {
600601
print_method(s, *meth);
601602
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
struct Foo;
13+
14+
impl Foo {
15+
#[cfg(cfg_that_surely_doesnt_exist)];
16+
17+
fn method(&self) -> bool { false }
18+
}
19+
20+
impl Foo {
21+
#[cfg(not(cfg_that_surely_doesnt_exist))];
22+
23+
// check that we don't eat attributes too eagerly.
24+
#[cfg(cfg_that_surely_doesnt_exist)]
25+
fn method(&self) -> bool { false }
26+
27+
fn method(&self) -> bool { true }
28+
}
29+
30+
31+
pub fn main() {
32+
assert!(Foo.method());
33+
}

0 commit comments

Comments
 (0)