Skip to content
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

can_begin_literal_maybe_minus: true on "-"? lit NTs. #70058

Merged
merged 1 commit into from
Mar 21, 2020
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
17 changes: 13 additions & 4 deletions src/librustc_ast/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl Token {
NtExpr(..) | NtBlock(..) | NtLiteral(..) => true,
_ => false,
},
_ => self.can_begin_literal_or_bool(),
_ => self.can_begin_literal_maybe_minus(),
}
}

Expand All @@ -448,13 +448,22 @@ impl Token {
/// Returns `true` if the token is any literal, a minus (which can prefix a literal,
/// for example a '-42', or one of the boolean idents).
///
/// Keep this in sync with `Lit::from_token`.
pub fn can_begin_literal_or_bool(&self) -> bool {
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
///
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
pub fn can_begin_literal_maybe_minus(&self) -> bool {
match self.uninterpolate().kind {
Literal(..) | BinOp(Minus) => true,
Ident(name, false) if name.is_bool_lit() => true,
Interpolated(ref nt) => match &**nt {
NtExpr(e) | NtLiteral(e) => matches!(e.kind, ast::ExprKind::Lit(_)),
NtLiteral(_) => true,
NtExpr(e) => match &e.kind {
ast::ExprKind::Lit(_) => true,
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
matches!(&e.kind, ast::ExprKind::Lit(_))
}
_ => false,
},
_ => false,
},
_ => false,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast/util/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Lit {

/// Converts arbitrary token into an AST literal.
///
/// Keep this in sync with `Token::can_begin_literal_or_bool`.
/// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation.
pub fn from_token(token: &Token) -> Result<Lit, LitError> {
let lit = match token.uninterpolate().kind {
token::Ident(name, false) if name.is_bool_lit() => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ fn may_begin_with(token: &Token, name: Name) -> bool {
}
sym::ty => token.can_begin_type(),
sym::ident => get_macro_ident(token).is_some(),
sym::literal => token.can_begin_literal_or_bool(),
sym::literal => token.can_begin_literal_maybe_minus(),
sym::vis => match token.kind {
// The follow-set of :vis + "priv" keyword + interpolated
token::Comma | token::Ident(..) | token::Interpolated(_) => true,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ impl<'a> Parser<'a> {
}

/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
/// Keep this in sync with `Token::can_begin_literal_maybe_minus`.
pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
maybe_whole_expr!(self);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ impl<'a> Parser<'a> {
})
// `extern ABI fn`
|| self.check_keyword(kw::Extern)
&& self.look_ahead(1, |t| t.can_begin_literal_or_bool())
&& self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
&& self.look_ahead(2, |t| t.is_keyword(kw::Fn))
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ impl<'a> Parser<'a> {
self.look_ahead(dist, |t| {
t.is_path_start() // e.g. `MY_CONST`;
|| t.kind == token::Dot // e.g. `.5` for recovery;
|| t.can_begin_literal_or_bool() // e.g. `42`.
|| t.can_begin_literal_maybe_minus() // e.g. `42`.
|| t.is_whole_expr()
})
}
Expand Down
22 changes: 21 additions & 1 deletion src/test/ui/parser/extern-abi-from-mac-literal-frag.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// check-pass

// In this test we check that the parser accepts an ABI string when it
// comes from a macro `literal` fragment as opposed to a hardcoded string.
// comes from a macro `literal` or `expr` fragment as opposed to a hardcoded string.

fn main() {}

Expand All @@ -17,10 +17,30 @@ macro_rules! abi_from_lit_frag {
}
}

macro_rules! abi_from_expr_frag {
($abi:expr) => {
extern $abi {
fn _import();
}

extern $abi fn _export() {}

type _PTR = extern $abi fn();
};
}

mod rust {
abi_from_lit_frag!("Rust");
}

mod c {
abi_from_lit_frag!("C");
}

mod rust_expr {
abi_from_expr_frag!("Rust");
}

mod c_expr {
abi_from_expr_frag!("C");
}
16 changes: 16 additions & 0 deletions src/test/ui/parser/issue-70050-ntliteral-accepts-negated-lit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// check-pass

macro_rules! foo {
($a:literal) => {
bar!($a)
};
}

macro_rules! bar {
($b:literal) => {};
}

fn main() {
foo!(-2);
bar!(-2);
}