Skip to content

mGCA: Validate const literal against expected type#152001

Draft
reddevilmidzy wants to merge 1 commit intorust-lang:mainfrom
reddevilmidzy:mgca-i
Draft

mGCA: Validate const literal against expected type#152001
reddevilmidzy wants to merge 1 commit intorust-lang:mainfrom
reddevilmidzy:mgca-i

Conversation

@reddevilmidzy
Copy link
Member

@reddevilmidzy reddevilmidzy commented Feb 2, 2026

close: #151625
close: #150983

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 2, 2026
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU self-assigned this Feb 3, 2026
tcx.at(span).lit_to_const(input)
}

fn const_lit_matches_ty(&self, kind: &LitKind, ty: Ty<'tcx>, neg: bool) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

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_ty to THIR building for patterns (the only place other than const-generics that calls lit_to_const) and create a ty::ConstKind::Error if it's false (or if lit_to_const returns None)
  • Here in HIR ty lowering we can emit a proper error about needing more type information for the literal if lit_to_const returns None. And we can entirely ignore the possibility of type mismatches because type checking should take care of that for us

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 4, 2026

very cool :) thanks for working on this

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

[ICE]: ty::ConstKind::Error constructed but no error reported ICE: ty::ConstKind::Error constructed but no error reported

4 participants