Skip to content

"convert path expression -> struct expression" points at the path, instead of the path expression #143220

Open
@sodiboo

Description

@sodiboo

Code

macro_rules! m {
    ($p:path) => {
        ($p)
    }
}

struct T {}

fn main() {
    m!(T);
}

Current output

error[E0423]: expected value, found struct `T`
  --> src/main.rs:10:8
   |
7  | struct T {}
   | ----------- `T` defined here
...
10 |     m!(T);
   |        ^ help: use struct literal syntax instead: `T {}`

Desired output

error[E0423]: expected value, found struct `T`
  --> src/main.rs:3:10
   |
3  |         ($p)
   |          ^^ help: use struct literal syntax instead: `$p {}`
...
7  | struct T {}
   | ----------- `T` defined here

Rationale and extra context

This diagnostic points at the span of the path that resolved to a struct.

But that path is being parsed as a $:path fragment, so there cannot be a struct expression in this location.

The diagnostic should point at the span of where the path was first interpreted as an expression, not at the span of the path itself.

Rust Version

rustc 1.90.0-nightly (11ad40bb8 2025-06-28)
binary: rustc
commit-hash: 11ad40bb839ca16f74784b4ab72596ad85587298
commit-date: 2025-06-28
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Anything else?

It's also worth noting that the $p {} suggestion i included would also not be valid, because of a parsing bug in the compiler. That bug is tracked in #143221

While that parser bug persists, a valid suggestion would instead be like so:

error: expected value, found struct `T`
  --> src/main.rs:3:10
   |
3  |         ($p)
   |          ^^ help: use struct literal syntax instead: `{ $p {} }`
...
7  | struct T {}
   | ----------- `T` defined here

The details of why those braces are necessary are explained in the issue for the parser bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions