-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New Attribute Syntax (#2569) #12538
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
New Attribute Syntax (#2569) #12538
Changes from all commits
22f3838
6abffcd
e6b4258
340f729
e21c2b1
bbf3ddb
5f13fe2
736cdec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,9 +40,6 @@ impl ParserAttr for Parser { | |
attrs.push(self.parse_attribute(false)); | ||
} | ||
token::POUND => { | ||
if self.look_ahead(1, |t| *t != token::LBRACKET) { | ||
break; | ||
} | ||
attrs.push(self.parse_attribute(false)); | ||
} | ||
token::DOC_COMMENT(s) => { | ||
|
@@ -70,6 +67,7 @@ impl ParserAttr for Parser { | |
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute { | ||
debug!("parse_attributes: permit_inner={:?} self.token={:?}", | ||
permit_inner, self.token); | ||
let mut warned = false; | ||
let (span, value) = match self.token { | ||
INTERPOLATED(token::NtAttr(attr)) => { | ||
assert!(attr.node.style == ast::AttrOuter); | ||
|
@@ -79,9 +77,22 @@ impl ParserAttr for Parser { | |
token::POUND => { | ||
let lo = self.span.lo; | ||
self.bump(); | ||
|
||
if self.eat(&token::NOT) { | ||
if !permit_inner { | ||
self.fatal("an inner attribute was not permitted in this context."); | ||
} | ||
} else { | ||
warned = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @flaper87 There was two warning calls for using the old syntax. The second warn is just about the extra semicolon though, so I see your point. But, any old syntax will now print one warning instead of two. |
||
// NOTE: uncomment this after a stage0 snap | ||
//self.warn("The syntax for inner attributes have changed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be an error or at least become an error before 1.0 I'd prefer making it an error right away as we did with the s/mod/crate/ change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will obviously require fixing all those errors around rust after the next snap There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old syntax will be long forgotten by 1.0. In any case, I agree, it would be preferable to be a warning. (But... it doesn't have to be changed now: only when activated.) |
||
// Use `#![lang(foo)]` instead."); | ||
} | ||
|
||
self.expect(&token::LBRACKET); | ||
let meta_item = self.parse_meta_item(); | ||
self.expect(&token::RBRACKET); | ||
|
||
let hi = self.span.hi; | ||
(mk_sp(lo, hi), meta_item) | ||
} | ||
|
@@ -91,12 +102,23 @@ impl ParserAttr for Parser { | |
token_str)); | ||
} | ||
}; | ||
let style = if permit_inner && self.token == token::SEMI { | ||
self.bump(); | ||
|
||
let style = if permit_inner { | ||
|
||
if self.eat(&token::SEMI) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Judging by the previous There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic should also change, the semicolon no longer dictates whether it's an inner attribute, something will need to get done when the |
||
// Only warn the user once if the syntax is the old one. | ||
if !warned { | ||
// NOTE: uncomment this after a stage0 snap | ||
//self.warn("This uses the old attribute syntax. Semicolons | ||
// are not longer required."); | ||
} | ||
} | ||
|
||
ast::AttrInner | ||
} else { | ||
ast::AttrOuter | ||
}; | ||
|
||
return Spanned { | ||
span: span, | ||
node: ast::Attribute_ { | ||
|
@@ -127,10 +149,6 @@ impl ParserAttr for Parser { | |
self.parse_attribute(true) | ||
} | ||
token::POUND => { | ||
if self.look_ahead(1, |t| *t != token::LBRACKET) { | ||
// This is an extension | ||
break; | ||
} | ||
self.parse_attribute(true) | ||
} | ||
token::DOC_COMMENT(s) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,9 +270,21 @@ pub fn bump(rdr: &StringReader) { | |
rdr.curr.set(None); | ||
} | ||
} | ||
|
||
// EFFECT: Peek 'n' characters ahead. | ||
pub fn peek(rdr: &StringReader, n: uint) -> Option<char> { | ||
let offset = byte_offset(rdr, rdr.pos.get()).to_uint() + (n - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be a dubious method, because this isn't looking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, makes sense. I basically followed what |
||
if offset < (rdr.filemap.src).len() { | ||
Some(rdr.filemap.src.char_at(offset)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn is_eof(rdr: &StringReader) -> bool { | ||
rdr.curr.get().is_none() | ||
} | ||
|
||
pub fn nextch(rdr: &StringReader) -> Option<char> { | ||
let offset = byte_offset(rdr, rdr.pos.get()).to_uint(); | ||
if offset < (rdr.filemap.src).len() { | ||
|
@@ -369,6 +381,12 @@ fn consume_any_line_comment(rdr: &StringReader) | |
} | ||
} else if rdr.curr_is('#') { | ||
if nextch_is(rdr, '!') { | ||
|
||
// Parse an inner attribute. | ||
if peek(rdr, 2).unwrap() == '[' { | ||
return None; | ||
} | ||
|
||
// I guess this is the only way to figure out if | ||
// we're at the beginning of the file... | ||
let cmap = @CodeMap::new(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2014 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. | ||
|
||
fn main() {} | ||
|
||
#![lang(foo)] //~ ERROR An inner attribute was not permitted in this context. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/An/an/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. Good catch. |
||
fn foo() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2014 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. | ||
|
||
#[foo(bar)] | ||
mod foo { | ||
#![feature(globs)] | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![allow(unknown_features)] | ||
#![feature(bogus)] | ||
fn main() { } | ||
// ignore-license |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2014 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. | ||
|
||
#[main] | ||
fn foo() { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as a side comment, but it would be nice for this not to be a fatal error so the parser can keep going.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, should this be
... attribute is not permitted ...
?