Skip to content

Fix where-bound suggestion with legacy const generics#158981

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
coroama-contrib:fix-const-generics-bound-suggestion
Jul 14, 2026
Merged

Fix where-bound suggestion with legacy const generics#158981
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
coroama-contrib:fix-const-generics-bound-suggestion

Conversation

@IsaiahCoroama

@IsaiahCoroama IsaiahCoroama commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

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.

@rustbot

This comment has been minimized.

@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
@IsaiahCoroama

Copy link
Copy Markdown
Contributor Author

Thank you for the review. I forgot to compile the suggestions and blindly trusted them. I updated the fix to wrap parentheses around non-atomic expressions with the help of rustc_hir::expr_needs_parens. This was suggested to me by Claude Fable 5. To reassure you, the LLM did not write directly to the codebase. Instead, I had it suggest fixes and find issues, then implemented them myself after review. The regression tests were updated to reflect the new parenthesized suggestion. I also removed #![feature(generic_const_exprs)] and #![allow(incomplete_features)] from the auxiliary crate, since it only needs #![feature(rustc_attrs)], matching how core_arch defines these intrinsics.

About min_generic_const_args

From my testing and review, the scenario this PR fixes can't be reproduced under mgca for two independent reasons.

  • First, mgca rejects N + 1 as a const argument during lowering with the error generic parameters may not be used in const operations, before any ConstEvaluatable obligation is ever created.
  • Second, even if it reached the const-evaluation path, the min_generic_const_args branch of is_const_evaluatable calls evaluate_const for its side effects, discards the result, and returns Ok(()) unconditionally, with a comment explicitly stating This will NOT error if the const uses generics, as desired. NotConstEvaluatable::MentionsParam, which triggers the unconstrained generic constant error and where-bound suggestion, is only produced in the generic_const_exprs branch, so a generic const like N + 1 never yields MentionsParam under mgca.

Either way, the regression test stays on generic_const_exprs.

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 [(); N + 1 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]: {
  |                        +++++++++++++++++++++++++++++

Disclaimer

This is my first open-source contribution of any kind, so I'm learning both the process and the compiler as I go. I'm using an LLM (Claude Fable 5 and Claude Opus 4.8) to help me understand the different pieces to the best of my ability, and I've verified what I can by testing locally. Please take my explanations, especially the min_generic_const_args analysis, with a grain of salt. I'm not 100% certain I've read everything correctly, and corrections are very welcome.

@rustbot ready

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 9, 2026
@khyperia

Copy link
Copy Markdown
Contributor

apologies, I've fallen ill and probably won't get around to reviewing this for a while, so I'm rerolling

r? compiler

@rustbot rustbot assigned mu001999 and unassigned khyperia Jul 13, 2026
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs Outdated

@mu001999 mu001999 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just some nits.

Considering that we cannot do the equivalent with mgca for now, I think this is good enough for generic_const_exprs :)

And, please squash the commits before merging this.

View changes since this review

@mu001999

Copy link
Copy Markdown
Member

@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 13, 2026
@IsaiahCoroama
IsaiahCoroama force-pushed the fix-const-generics-bound-suggestion branch from 9ea03db to 5798f26 Compare July 13, 2026 20:05
@rustbot

rustbot commented Jul 13, 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.

@IsaiahCoroama
IsaiahCoroama force-pushed the fix-const-generics-bound-suggestion branch from 5798f26 to 77a7d21 Compare July 13, 2026 20:34
@IsaiahCoroama

Copy link
Copy Markdown
Contributor Author

I appreciate your suggestions, I've implemented them all.

One more thing, since the suggestion is MachineApplicable, would you like me to add //@ run-rustfix coverage so the applied fixes are checked as well? I'd be happy to do so if you would like.

@rustbot ready

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

Copy link
Copy Markdown
Member

One more thing, since the suggestion is MachineApplicable, would you like me to add //@ run-rustfix coverage so the applied fixes are checked as well?

That would be nice :)

I will approve this after adding run-rustfix.

@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 14, 2026
Point the suggestion at the const argument's span instead of the whole obligation, and parenthesize the snippet when the const expression needs it (e.g. `(N + 1) as usize`) so the suggested bound parses and compiles.

Also add regression tests for legacy const generic where-bound suggestions.
@IsaiahCoroama
IsaiahCoroama force-pushed the fix-const-generics-bound-suggestion branch from 77a7d21 to 4882893 Compare July 14, 2026 02:08
@IsaiahCoroama

Copy link
Copy Markdown
Contributor Author

//@ run-rustfix has been added.

@rustbot ready

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

@mu001999 mu001999 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rust-bors

rust-bors Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4882893 has been approved by mu001999

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 14, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 14, 2026
…-bound-suggestion, r=mu001999

Fix where-bound suggestion with legacy const generics

## 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
```rust
#![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
```rust
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
```rust
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 rust-lang#108382
rust-bors Bot pushed a commit that referenced this pull request Jul 14, 2026
Rollup of 6 pull requests

Successful merges:

 - #159039 (resolve: fix effective visibilities for items in ambiguous glob sets)
 - #156047 (Fix trait method resolution on an adjusted never type)
 - #158981 (Fix where-bound suggestion with legacy const generics)
 - #159061 (Skip trivial cast lint for trait object upcasts)
 - #159248 (compiler: Remove `-Zmutable-noalias`)
 - #159250 (Rename `errors.rs` file to `diagnostics.rs` (13/N))
rust-bors Bot pushed a commit that referenced this pull request Jul 14, 2026
Rollup of 6 pull requests

Successful merges:

 - #159039 (resolve: fix effective visibilities for items in ambiguous glob sets)
 - #158981 (Fix where-bound suggestion with legacy const generics)
 - #159061 (Skip trivial cast lint for trait object upcasts)
 - #159241 (Update books)
 - #159248 (compiler: Remove `-Zmutable-noalias`)
 - #159250 (Rename `errors.rs` file to `diagnostics.rs` (13/N))
@rust-bors
rust-bors Bot merged commit 28e67e0 into rust-lang:main Jul 14, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 14, 2026
rust-timer added a commit that referenced this pull request Jul 14, 2026
Rollup merge of #158981 - coroama-contrib:fix-const-generics-bound-suggestion, r=mu001999

Fix where-bound suggestion with legacy const generics

## 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
```rust
#![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
```rust
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
```rust
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
@IsaiahCoroama
IsaiahCoroama deleted the fix-const-generics-bound-suggestion branch July 14, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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

4 participants