Skip to content

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

Closed
Closed
36 changes: 27 additions & 9 deletions src/libsyntax/parse/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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.");
Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Contributor

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 ... ?

}
} else {
warned = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think warned is necessary. The message here is different from the warn below. Although the refer to the same new syntax, they are warning about different things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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)
}
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Judging by the previous if statement, it looks like this should be structured as if permit_inner && self.eat(&token::SEMI) rather than permit_inner always parsing an inner attribute.

Copy link
Member

Choose a reason for hiding this comment

The 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 ! is parsed.

// 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_ {
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 6 additions & 2 deletions src/libsyntax/parse/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ast;
use codemap::{BytePos, CharPos, CodeMap, Pos};
use diagnostic;
use parse::lexer::{is_whitespace, with_str_from, Reader};
use parse::lexer::{StringReader, bump, is_eof, nextch_is, TokenAndSpan};
use parse::lexer::{StringReader, bump, peek, is_eof, nextch_is, TokenAndSpan};
use parse::lexer::{is_line_non_doc_comment, is_block_non_doc_comment};
use parse::lexer;
use parse::token;
Expand Down Expand Up @@ -332,7 +332,11 @@ fn consume_comment(rdr: &StringReader,
} else if rdr.curr_is('/') && nextch_is(rdr, '*') {
read_block_comment(rdr, code_to_the_left, comments);
} else if rdr.curr_is('#') && nextch_is(rdr, '!') {
read_shebang_comment(rdr, code_to_the_left, comments);
// Make sure the following token is **not** the beginning
// of an inner attribute, which starts with the same syntax.
if peek(rdr, 2).unwrap() != '[' {
read_shebang_comment(rdr, code_to_the_left, comments);
}
} else { fail!(); }
debug!("<<< consume comment");
}
Expand Down
18 changes: 18 additions & 0 deletions src/libsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be a dubious method, because this isn't looking n characters ahead, but rather n bytes ahead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, makes sense. I basically followed what nextch had, but also having the ability to specify more than one to be peeked. This was the simpler option to forget/rollback.

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() {
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/attr.rs
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/An/an/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Good catch.

fn foo() {}
16 changes: 16 additions & 0 deletions src/test/run-pass/attr-mix-new.rs
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() {}
4 changes: 4 additions & 0 deletions src/test/run-pass/attr-shebang.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![allow(unknown_features)]
#![feature(bogus)]
fn main() { }
// ignore-license
13 changes: 13 additions & 0 deletions src/test/run-pass/attr.rs
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() {
}