mGCA: Validate const literal against expected type#152001
Draft
reddevilmidzy wants to merge 1 commit intorust-lang:mainfrom
Draft
mGCA: Validate const literal against expected type#152001reddevilmidzy wants to merge 1 commit intorust-lang:mainfrom
reddevilmidzy wants to merge 1 commit intorust-lang:mainfrom
Conversation
reddevilmidzy
commented
Feb 2, 2026
This comment has been minimized.
This comment has been minimized.
BoxyUwU
reviewed
Feb 4, 2026
| tcx.at(span).lit_to_const(input) | ||
| } | ||
|
|
||
| fn const_lit_matches_ty(&self, kind: &LitKind, ty: Ty<'tcx>, neg: bool) -> bool { |
Member
There was a problem hiding this comment.
I think in most cases we actually don't want to emit errors during HIR ty lowering for type mismatches here. E.g. if we have a true literal it doesn't really matter if the expected type is u32, we can still lower to a 0x1 valtree of type bool and then get a type checking error later.
Separately from this, we already do these checks in fn lit_to_const (just ICEing if they don't hold instead of a proper error) so it probably doesn't make sense to duplicate them here too 🤔
I think it would make sense for lit_to_const to return an Option<ty::Value<'tcx>> and stop ICEing/erroring altogether.
You could then:
- Move
const_lit_matches_tyto THIR building for patterns (the only place other than const-generics that callslit_to_const) and create aty::ConstKind::Errorif it'sfalse(or iflit_to_constreturnsNone) - Here in HIR ty lowering we can emit a proper error about needing more type information for the literal if
lit_to_constreturnsNone. And we can entirely ignore the possibility of type mismatches because type checking should take care of that for us
Member
|
very cool :) thanks for working on this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
close: #151625
close: #150983