This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +13
-6
lines changed
compiler/rustc_const_eval/src/const_eval Expand file tree Collapse file tree 3 files changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,7 @@ pub(crate) fn mk_eval_cx<'mir, 'tcx>(
110110pub ( super ) fn op_to_const < ' tcx > (
111111 ecx : & CompileTimeEvalContext < ' _ , ' tcx > ,
112112 op : & OpTy < ' tcx > ,
113+ for_diagnostics : bool ,
113114) -> ConstValue < ' tcx > {
114115 // Handle ZST consistently and early.
115116 if op. layout . is_zst ( ) {
@@ -132,8 +133,14 @@ pub(super) fn op_to_const<'tcx>(
132133 // functionality.)
133134 _ => false ,
134135 } ;
135- let immediate = if force_as_immediate && let Ok ( imm) = ecx. read_immediate ( op) {
136- Right ( imm)
136+ let immediate = if force_as_immediate {
137+ match ecx. read_immediate ( op) {
138+ Ok ( imm) => Right ( imm) ,
139+ Err ( err) if !for_diagnostics => {
140+ panic ! ( "normalization works on validated constants: {err:?}" )
141+ }
142+ _ => op. as_mplace_or_imm ( ) ,
143+ }
137144 } else {
138145 op. as_mplace_or_imm ( )
139146 } ;
@@ -205,7 +212,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
205212 ) ;
206213
207214 // Turn this into a proper constant.
208- op_to_const ( & ecx, & mplace. into ( ) )
215+ op_to_const ( & ecx, & mplace. into ( ) , /* for diagnostics */ false )
209216}
210217
211218#[ instrument( skip( tcx) , level = "debug" ) ]
Original file line number Diff line number Diff line change @@ -99,7 +99,7 @@ pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
9999 let fields_iter = ( 0 ..field_count)
100100 . map ( |i| {
101101 let field_op = ecx. project_field ( & down, i) . ok ( ) ?;
102- let val = op_to_const ( & ecx, & field_op) ;
102+ let val = op_to_const ( & ecx, & field_op, /* for diagnostics */ true ) ;
103103 Some ( ( val, field_op. layout . ty ) )
104104 } )
105105 . collect :: < Option < Vec < _ > > > ( ) ?;
Original file line number Diff line number Diff line change @@ -232,7 +232,7 @@ pub fn valtree_to_const_value<'tcx>(
232232 let mut ecx = mk_eval_cx ( tcx, DUMMY_SP , param_env, CanAccessStatics :: No ) ;
233233 let imm = valtree_to_ref ( & mut ecx, valtree, * inner_ty) ;
234234 let imm = ImmTy :: from_immediate ( imm, tcx. layout_of ( param_env_ty) . unwrap ( ) ) ;
235- op_to_const ( & ecx, & imm. into ( ) )
235+ op_to_const ( & ecx, & imm. into ( ) , /* for diagnostics */ false )
236236 }
237237 ty:: Tuple ( _) | ty:: Array ( _, _) | ty:: Adt ( ..) => {
238238 let layout = tcx. layout_of ( param_env_ty) . unwrap ( ) ;
@@ -265,7 +265,7 @@ pub fn valtree_to_const_value<'tcx>(
265265 dump_place ( & ecx, & place) ;
266266 intern_const_alloc_recursive ( & mut ecx, InternKind :: Constant , & place) . unwrap ( ) ;
267267
268- op_to_const ( & ecx, & place. into ( ) )
268+ op_to_const ( & ecx, & place. into ( ) , /* for diagnostics */ false )
269269 }
270270 ty:: Never
271271 | ty:: Error ( _)
You can’t perform that action at this time.
0 commit comments