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

concat!() rejects negative number literal #106837

Closed
johnmave126 opened this issue Jan 14, 2023 · 1 comment · Fixed by #106844
Closed

concat!() rejects negative number literal #106837

johnmave126 opened this issue Jan 14, 2023 · 1 comment · Fixed by #106844
Assignees
Labels
C-bug Category: This is a bug.

Comments

@johnmave126
Copy link

johnmave126 commented Jan 14, 2023

I tried this code:

fn main() {
    println!("{}", concat!(-1));
}

I expected to see this happen: code compiles.

Since the following code compiles:

macro_rules! t {
    ($item: literal) => { stringify!($item) }
}

fn main() {
    println!("{}", t!(-1));
}

The macro system recognizes -1 as a single literal in this case.

Instead, this happened:

error: expected a literal
 --> src/main.rs:2:28
  |
2 |     println!("{}", concat!(-1));
  |                            ^^
  |
  = note: only literals (like `"foo"`, `42` and `3.14`) can be passed to `concat!()`

error: could not compile `playground` due to previous error

Meta

rustc --version --verbose:
Tried on playground, all channels:
Stable 1.66.1
Beta 1.67.0-beta.7 (2023-01-11 275123c)
Nightly 1.68.0-nightly (2023-01-13 0b90256)

Backtrace

@johnmave126 johnmave126 added the C-bug Category: This is a bug. label Jan 14, 2023
@Ezrashaw
Copy link
Contributor

Hmm, this actually looks like fun to fix.

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 11, 2023
…r=dtolnay

allow negative numeric literals in `concat!`

Fixes rust-lang#106837

While *technically* negative numeric literals are implemented as unary operations, users can reasonably expect that negative literals are treated the same as positive literals.
@bors bors closed this as completed in d47d4ad Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants