rust-lang / rust-lang/rust

const generics use generic param names in nonsensical ways in error messages, leading to bogus suggestions

Open
#127,582 1 comment 0 reactions 1 assignee View on GitHub

@long-long-float is already working on this.

Since Jul 13, 2024.

A-diagnostics F-generic_const_exprs T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

fn foo<const I: usize, const J: usize>() where [(); I / 4]: {
    bar::<J, I>(); // I and J are accidentally swapped
}

fn bar<const I: usize, const J: usize>() where [(); I / 4]: {
    [(); I / 4];
}
Current output
error: unconstrained generic constant
 --> src/lib.rs:5:11
  |
5 |     bar::<J, I>(); // I and J are accidentally swapped
  |           ^
  |
note: required by a bound in `bar`
 --> src/lib.rs:8:53
  |
8 | fn bar<const I: usize, const J: usize>() where [(); I / 4]: {
  |                                                     ^^^^^ required by this bound in `bar`
help: try adding a `where` bound
  |
4 | fn foo<const I: usize, const J: usize>() where [(); I / 4]:, [(); I / 4]: {
  |                                                            ++++++++++++++
Desired output

the error message should suggest adding [(); J / 4]:, because J in foo corresponds to I in bar. Bonus points if the error message says something like

help: try adding a `where` bound
  |
4 | fn foo<const I: usize, const J: usize>() where [(); I / 4]:, [(); J / 4]: {
  |                                                            ++++++++++++++
note: const param `I` in `bar` corresponds to expression `J` in `foo`

extra bonus points if the error message says something like note: foo and bar both have const generic param named I, but bar::I is not the same as foo::I

Rust Version

nightly on playground as of 2024-7-10

Anything else?

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9b8fd4687496c3786fe09a38c3a87e38

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.