mGCA: Confusing error on ill-formed const projection (e.g., "expected `usize`, found a different `usize`")
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
Description
In Const Param Defaults that Reference Params
#![feature(min_generic_const_args)]
use core::direct_const_arg as lift;
struct Ty<T, const N: usize = { lift!(<T as Trait>::N) }>(T);
trait Trait { type const N: usize; }
In the code above, type parameter T doesn't implement trait Trait rendering the const projection <T as Trait>::N ill-formed … except that we intentionally don't check generic parameter defaults for well-formedness if they reference generic parameters (via)[^1]. Therefore, we won't detect & report the trait bound `T: Trait` is not satisfied. That's expected but interestingly we emit a very weird+broken error afterward:
error: the constant `<T as Trait>::N` is not of type `usize`
--> src/lib.rs:7:14
|
7 | struct Ty<T, const N: usize = { lift!(<T as Trait>::N) }>(T);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found a different `usize`
Elsewhere
This error gets emitted in (all?) other (relevant) places, too. Consider:
#![feature(min_generic_const_args)]
trait Trait { type const N: usize; }
type const _: usize = <() as Trait>::N;
error[E0277]: the trait bound `(): Trait` is not satisfied
--> src/lib.rs:5:1
|
5 | type const _: usize = <() as Trait>::N;
| ^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> src/lib.rs:3:1
|
3 | trait Trait { type const N: usize; }
| ^^^^^^^^^^^
error: the constant `<() as Trait>::N` is not of type `usize`
--> src/lib.rs:5:1
|
5 | type const _: usize = <() as Trait>::N;
| ^^^^^^^^^^^^^^^^^^^ expected `usize`, found a different `usize`
However, since we do perform well-formedness checking in locations like this, we get a more reasonable error diagnostic alongside the problematic one. So it's only half-bad.
[^1]: If you ask me, I don't like that decision.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the examples in src/lib.rs, then start with compiler/rustc_hir_analysis/src/check/wfcheck.rs around the linked generic-parameter-default check. Trace the const projection type-checking and diagnostic path in the reported locations. Done means the ill-formed projection no longer produces the misleading “expected usize, found a different usize” error while the relevant trait-bound diagnostic remains.
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
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100