Skip to content

Fix where-bound suggestion with legacy const generics#158981

Open
IsaiahCoroama wants to merge 5 commits into
rust-lang:mainfrom
coroama-contrib:fix-const-generics-bound-suggestion
Open

Fix where-bound suggestion with legacy const generics#158981
IsaiahCoroama wants to merge 5 commits into
rust-lang:mainfrom
coroama-contrib:fix-const-generics-bound-suggestion

Conversation

@IsaiahCoroama

@IsaiahCoroama IsaiahCoroama commented Jul 8, 2026

Copy link
Copy Markdown

Summary

During lowering of legacy const generics in lower_legacy_const_generics, the function's span was passed when creating the associated LocalDefId instead of the const argument span. This later caused diagnostics to pull the function's name instead of the const argument for suggestions when emitting an unconstrained generic constant error.

Also adds regression tests for all basic integer types. The full set isn't redundant: non-usize types exercise the as usize cast in the suggestion, while usize exercises the no-cast path. The tests use a cross-crate aux crate rather than a target-specific intrinsic (the rewrite only fires cross-crate), so they run on all platforms.

Sample

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

use std::arch::x86_64::*;
fn uwu<const N: i32>() {
    unsafe {
        let a = _mm_setzero_ps();
        let b = _mm_setzero_ps();
        _mm_shuffle_ps(a, b, N + 1);
    }
}

Previous Output

error: unconstrained generic constant
 --> src/lib.rs:9:30
  |
9 |         _mm_shuffle_ps(a, b, N + 1);
  |                              ^^^^^
  |
help: try adding a `where` bound
  |
5 | fn uwu<const N: i32>() where [(); _mm_shuffle_ps as usize]: {
  |                        ++++++++++++++++++++++++++++++++++++

Current Output

error: unconstrained generic constant
 --> src/lib.rs:9:30
  |
9 |         _mm_shuffle_ps(a, b, N + 1);
  |                              ^^^^^
  |
help: try adding a `where` bound
  |
5 | fn uwu<const N: i32>() where [(); N + 1 as usize]: {
  |                        +++++++++++++++++++++++++++

AI Assistance Disclosure

This is my first time contributing to the compiler, and I used Claude Opus 4.8 to assist with finding the root cause and implementing the fix, and to navigate the contributing docs, build system, and debugging tooling. The regression tests were designed and written entirely by me. I used Grok to proofread the written materials (commit bodies and this PR) for spelling and grammar. I reviewed and verified all changes myself.

Related Issues

Closes #108382

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 8, 2026
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 8, 2026
@rustbot

rustbot commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @khyperia (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 18 candidates

@khyperia

khyperia commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

thanks for looking into this! as noted by @asquared31415 in the issue, generic_const_exprs is on its way out, to be replaced by generic_const_args (min_generic_const_args to start with). It would be nice if we could get a repro with min_generic_const_args instead if at all possible, but it's fine to keep this testing generic_const_exprs as well I suppose.

Some feedback:

  • the suggestions for the non-usize ones don't actually compile - where [(); N + 1 as usize] should be where [(); (N + 1) as usize]. This feels like potentially an unrelated issue though, and if you'd like, it's fine to leave it unresolved IMO as the current output is miles better than the previous, especially if you can find a repro of bad paren suggestions unrelated to this issue (making sure it's tracked with an issue would be nice!)
  • could you remove #![feature(generic_const_exprs)] (and #![allow(incomplete_features)]) from the aux crate? the aux crate is pretending to be std/core, which doesn't have GCE enabled, and removing it doesn't affect the test case, just makes it a little closer to the real world I suppose.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 9, 2026
@rustbot

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

During lowering of legacy const generic arguments, it pointed to the function's span instead of the const argument's span, which led to the diagnostics suggesting bounding the function's name instead of the const argument itself.
…gestions

Checks that the `unconstrained generic constant` suggestion points to the correct const argument instead of the function name. Ran across all basic integer types with the help of an auxiliary crate.
Requested by `khyperia`, removing these doesn't affect the test case.
Appending ` as usize` directly to the const snippet produced `[(); N + 1 as usize]:`, which fails to compile (as pointed out by `khyperia`). Instead, wrap each non-atomic expression in parentheses on the anon const's body with the help of `rustc_hir::expr_needs_parens` so the suggestion is `[(); (N + 1) as usize]:` instead.
Removing this doesn't break the test in any way, and having it doesn't
make sense for a private test. More of a nitpick than a necessity.
@rustbot

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Jul 9, 2026
@IsaiahCoroama IsaiahCoroama force-pushed the fix-const-generics-bound-suggestion branch from dd697af to 9ea03db Compare July 9, 2026 19:34
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generic_const_exprs suggestion to add bound interacts poorly with legacy const generics

3 participants