Skip to content

fix: report const generic args of the wrong type - #23062

Open
hongjr03 wants to merge 7 commits into
rust-lang:masterfrom
hongjr03:fix/const-arg-of-wrong-type
Open

fix: report const generic args of the wrong type#23062
hongjr03 wants to merge 7 commits into
rust-lang:masterfrom
hongjr03:fix/const-arg-of-wrong-type

Conversation

@hongjr03

@hongjr03 hongjr03 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

This pr fixes #22953 (supersedes #23045). The panic from the issue was already fixed upstream by removing borrowck in #23043 .

The root cause is that although the ConstArgHasType constraint is registered when indexing an array, the resulting SelectionError::ConstArgHasWrongType was dropped implicitly in solver_errors::from_fulfillment_error, so no diagnostic was produced.

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

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More diagnostics is always a good thing and I would not oppose it, but... are you sure this really fixes the root cause? We have no logic in MIR lowering to skip lowering if there are diagnostics (we should have though, but it's tricky to decide what errors should prevent lowering).

Also about this diagnostic specifically, rustc does emit a diagnostic here but I'm not personally sure we should: of course we should emit a diagnostic, but when the the mismatch actually occurs (i.e. when you're using a i64 as an array length or similar) and not when the constant is used (rustc emits at both).

View changes since this review

Comment thread crates/hir-ty/src/tests/regression.rs Outdated
Comment thread crates/hir/src/diagnostics.rs Outdated
@hongjr03

hongjr03 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

More diagnostics is always a good thing and I would not oppose it, but... are you sure this really fixes the root cause? We have no logic in MIR lowering to skip lowering if there are diagnostics (we should have though, but it's tricky to decide what errors should prevent lowering).

You're right. The change now just plugs ConstArgHasWrongType into the same type-mismatch path that lower_to_mir_with_store already uses to bail out — emit_trait_errors now reports it and inference marks it in nodes_with_type_mismatches. So no new policy needed, it just treats const-arg mismatches as another type mismatch.

Also about this diagnostic specifically, rustc does emit a diagnostic here but I'm not personally sure we should: of course we should emit a diagnostic, but when the mismatch actually occurs (i.e. when you're using an i64 as an array length or similar) and not when the constant is used (rustc emits at both).

I dropped the ConstArgHasWrongType diagnostic path and instead emit from type lowering, at the point where the constant is lowered against its expected type. So we now get the actual "mismatch occurrence" site.

Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: const generics with N: i64 instead of usize

3 participants