Skip to content

Multiple errors on binding missing ; followed by implicitly returning a borrow of that binding #151149

@estebank

Description

@estebank

Code

#![feature(const_trait_impl)]

const trait ConstDefault {
    fn const_default() -> Self;
}

impl const ConstDefault for u8 {
    fn const_default() -> Self { 0 }
}

const fn foo() -> &'static u8 {
    const C: u8 = u8::const_default()
    &C
}

Current output

error: expected `;`, found `}`
  --> src/lib.rs:13:7
   |
13 |     &C
   |       ^ help: add `;` here
14 | }
   | - unexpected token

error[E0308]: mismatched types
  --> src/lib.rs:11:19
   |
11 | const fn foo() -> &'static u8 {
   |          ---      ^^^^^^^^^^^ expected `&u8`, found `()`
   |          |
   |          implicitly returns `()` as its body has no tail or `return` expression

error[E0391]: cycle detected when simplifying constant for the type system `foo::C`
  --> src/lib.rs:12:5
   |
12 |     const C: u8 = u8::const_default()
   |     ^^^^^^^^^^^
   |
note: ...which requires const-evaluating + checking `foo::C`...
  --> src/lib.rs:13:6
   |
13 |     &C
   |      ^
   = note: ...which again requires simplifying constant for the type system `foo::C`, completing the cycle
   = note: cycle used when running analysis passes on crate `playground`
   = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

Desired output

Only one error, mentioning the likely missing `;` on line 12.

Rationale and extra context

None of the error points at the right place.

The parser accepted this as const C: Ty = expr & ident, which means that there's a missing ; for the const, but should recognize it likely was missing earlier. The statement should be marked as Ty::Err instead of Ty, to avoid the return type error.

Other cases

const trait ConstDefault {
    fn const_default() -> Self;
}

impl const ConstDefault for u8 {
    fn const_default() -> Self { 0 }
}

const fn foo() -> &'static u8 {
    let C = u8::const_default()
    &C
}

produces

error: expected `;`, found `}`
  --> src/lib.rs:13:7
   |
13 |     &C
   |       ^ help: add `;` here
14 | }
   | - unexpected token

error[E0425]: cannot find value `C` in this scope
  --> src/lib.rs:13:6
   |
13 |     &C
   |      ^ not found in this scope
   |
help: you might be missing a const parameter
   |
11 | const fn foo<const C: /* Type */>() -> &'static u8 {
   |             +++++++++++++++++++++

error[E0308]: mismatched types
  --> src/lib.rs:11:19
   |
11 | const fn foo() -> &'static u8 {
   |          ---      ^^^^^^^^^^^ expected `&u8`, found `()`
   |          |
   |          implicitly returns `()` as its body has no tail or `return` expression

Rust Version

1.94.0-nightly
2026-01-13 2850ca8295bc253186b2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.T-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