rust-lang / rust-lang/rust

Spurious “constant provided when a type was expected” upon syntax error in a generic parameter list

Open
#124,298 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-parser D-confusing D-verbose T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct Foo;
struct Bar;

impl From<Option<Foo> x> for Bar {
}
Current output
error: expected one of `,` or `>`, found `x`
 --> src/lib.rs:4:23
  |
4 | impl From<Option<Foo> x> for Bar {
  |                       ^ expected one of `,` or `>`
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
4 | impl From<{ Option<Foo> x }> for Bar {
  |           +               +

error[E0747]: constant provided when a type was expected
 --> src/lib.rs:4:11
  |
4 | impl From<Option<Foo> x> for Bar {
  |           ^^^^^^^^^^^^^
Desired output

The “error[E0747]: constant provided when a type was expected” should be omitted.

Rationale and extra context

The secondary error is a distraction from the syntax error, because it is unlikely that the user in fact meant to write a const expression.

As an implementation strategy: perhaps E0747 should not be shown if the const-ness of the argument is a result of syntax error recovery, or perhaps if there were any syntax errors at all since there are lots of ways to typo a generic type.

Or there could be a heuristic like "if the candidate expression has got paired < and >, then it probably isn't an expression at all".

Other cases

This seems to only occur if the generic parameter text is parseable as an expression.

Rust Version
rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-apple-darwin
release: 1.77.2
LLVM version: 17.0.6
Anything else?

No response

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

Start by compiling the provided generic-parameter example with the reported Rust version and compare the diagnostics with the desired output. Trace how syntax-error recovery leads to the E0747 diagnostic, then add or update a compiler regression test so the primary syntax error remains without the spurious constant/type error.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.