rust-lang / rust-lang/rust

`$lit:literal` chokes on `-(1)`

Open
#117,300 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-discussion
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

macro_rules! is_literal {
    ($l:literal) => { true };
    ($e:expr) => { false };
}

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

I expected to see this happen: one of true or false being printed

Given that -(1) is a valid expression, it should be picked up by $e:expr rather than failing at $lit:literal. Or it should be considered a valid literal expression.

Note that it also chokes on similar expressions, such as -(x) and -(x.method()).

Instead, this happened:

error: unexpected token: `(`
 --> src/main.rs:8:33
  |
2 |     ($l:literal) => { true };
  |      ---------- while parsing argument for this `literal` macro fragment
...
8 |     println!("{}", is_literal!(-(1)));
  |                                 ^
Meta

rustc --version --verbose:

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: aarch64-apple-darwin
release: 1.73.0
LLVM version: 17.0.2

Happens on beta and nightly too.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the macro-fragment parsing failure in src/main.rs using the is_literal! example and the reported rustc version. Investigate how literal and expression fragments handle parenthesized unary expressions; done means -(1), -(x), and -(x.method()) no longer fail during macro matching and produce a defined result.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.