Skip to content

Fix bad span for attributes #43933

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

Merged
merged 3 commits into from
Aug 19, 2017
Merged
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
23 changes: 9 additions & 14 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,26 +1064,21 @@ impl MetaItem {
},
_ => return None,
};
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi);
let node = match MetaItemKind::from_tokens(tokens) {
Some(node) => node,
_ => return None,
};
if let Some(last_span) = node.last_span() {
span.hi = last_span.hi;
}
span.hi = match node {
MetaItemKind::NameValue(ref lit) => lit.span.hi,
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(span.hi),
_ => span.hi,
};
Some(MetaItem { name: name, span: span, node: node })
}
}

impl MetaItemKind {
fn last_span(&self) -> Option<Span> {
match *self {
MetaItemKind::Word => None,
MetaItemKind::List(ref list) => list.last().map(NestedMetaItem::span),
MetaItemKind::NameValue(ref lit) => Some(lit.span),
}
}

pub fn tokens(&self, span: Span) -> TokenStream {
match *self {
MetaItemKind::Word => TokenStream::empty(),
Expand Down Expand Up @@ -1130,7 +1125,7 @@ impl MetaItemKind {
let mut result = Vec::new();
while let Some(..) = tokens.peek() {
match NestedMetaItemKind::from_tokens(&mut tokens) {
Some(item) => result.push(Spanned { span: item.span(), node: item }),
Some(item) => result.push(respan(item.span(), item)),
None => return None,
}
match tokens.next() {
Expand Down Expand Up @@ -1163,7 +1158,7 @@ impl NestedMetaItemKind {
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
if let Some(node) = LitKind::from_token(token) {
tokens.next();
return Some(NestedMetaItemKind::Literal(Spanned { node: node, span: span }));
return Some(NestedMetaItemKind::Literal(respan(span, node)));
}
}

Expand Down Expand Up @@ -1256,7 +1251,7 @@ pub trait HasAttrs: Sized {
impl<T: HasAttrs> HasAttrs for Spanned<T> {
fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
Spanned { node: self.node.map_attrs(f), span: self.span }
respan(self.span, self.node.map_attrs(f))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/span/E0536.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0536]: expected 1 cfg-pattern
--> $DIR/E0536.rs:11:7
|
11 | #[cfg(not())] //~ ERROR E0536
| ^^^
| ^^^^^

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/span/E0537.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0537]: invalid predicate `unknown`
--> $DIR/E0537.rs:11:7
|
11 | #[cfg(unknown())] //~ ERROR E0537
| ^^^^^^^
| ^^^^^^^^^

error: aborting due to previous error