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

Negative numbers are not correctly identified as literals inside macros #70050

Closed
connorskees opened this issue Mar 16, 2020 · 4 comments · Fixed by #70058
Closed

Negative numbers are not correctly identified as literals inside macros #70050

connorskees opened this issue Mar 16, 2020 · 4 comments · Fixed by #70058
Assignees
Labels
A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@connorskees
Copy link
Contributor

connorskees commented Mar 16, 2020

Negative numbers are no longer parsed as literals when used as a parameter to a macro inside a macro. This appears to be a regression from stable to nightly.

According to cargo-bisect-rustc, #69023 is responsible (thanks to @jebrosen for helping with this on the Rust discord)
Specifically, the changes to this function look suspicious.

The below fails to compile:

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

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

fn main() {
    // fails on nightly, but not stable
    foo!(-2);
    // doesn't fail
    bar!(-2);
}

with the following error on nightly-2020-02-14,

~ >cargo b
error: no rules expected the token `-2`
  --> src\main.rs:3:14
   |
3  |         bar!($a)
   |              ^^ no rules expected this token in macro call
...
7  | macro_rules! bar {
   | ---------------- when calling this macro
...
12 |     foo!(-2);
   |     --------- in this macro invocation
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
@connorskees connorskees added the C-bug Category: This is a bug. label Mar 16, 2020
@jebrosen
Copy link
Contributor

rustc 1.42.0-nightly (8417d68de 2020-02-03) (compiles)
rustc 1.43.0-nightly (58b834344 2020-02-05) (does not compile)

Strange, with 58b834344 2020-02-05 I can compile this successfully. Here's my cargo-bisect-rustc report, after originally narrowing it down from 2020-01-25 to 2020-02-14:

searched nightlies: from nightly-2020-02-13 to nightly-2020-02-14
regressed nightly: nightly-2020-02-14
searched commits: from https://github.com/rust-lang/rust/commit/a1912f2e89b77cfe2a0e64b96f444848fe4e2d49 to https://github.com/rust-lang/rust/commit/5d04ce67fd14538d03fa47a2598f80d49fd564c6
regressed commit: https://github.com/rust-lang/rust/commit/be493fe8cc40c3d3f6030a1313c1ff747fce770d

Specifically, the changes to this function look suspicious.

My reason for interest in that particular line is that it appears to be the only change that isn't related to async or const, but it is related to literals.

@jonas-schievink jonas-schievink added I-nominated P-high High priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-parser Area: The parsing of Rust source code to an AST labels Mar 16, 2020
@jonas-schievink
Copy link
Contributor

cc @Centril

@Centril Centril self-assigned this Mar 16, 2020
@Centril
Copy link
Contributor

Centril commented Mar 16, 2020

Note to self: "this function" refers to pub fn can_begin_literal_or_bool(&self) -> bool.

@Centril Centril added regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed I-nominated labels Mar 16, 2020
@M-PC

This comment has been minimized.

Centril added a commit to Centril/rust that referenced this issue Mar 21, 2020
can_begin_literal_maybe_minus: `true` on `"-"? lit` NTs.

Make `can_begin_literal_or_bool` (renamed to `can_begin_literal_maybe_minus`) accept `NtLiteral(e) | NtExpr(e)` where `e` is either a literal or a negated literal.

Fixes rust-lang#70050.

r? @petrochenkov
@bors bors closed this as completed in 67e418c Mar 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants