@@ -551,8 +551,18 @@ pub fn try_evaluate_const<'tcx>(
551551 | ty:: ConstKind :: Placeholder ( _)
552552 | ty:: ConstKind :: Expr ( _) => Err ( EvaluateConstErr :: HasGenericsOrInfers ) ,
553553 ty:: ConstKind :: Unevaluated ( uv) => {
554- // Postpone evaluation of constants that depend on generic parameters or inference variables.
555- let ( args, param_env) = if tcx. features ( ) . generic_const_exprs ( )
554+ // Postpone evaluation of constants that depend on generic parameters or
555+ // inference variables.
556+ //
557+ // We use `TypingMode::PostAnalysis` here which is not *technically* correct
558+ // to be revealing opaque types here as borrowcheck has not run yet. However,
559+ // CTFE itself uses `TypingMode::PostAnalysis` unconditionally even during
560+ // typeck and not doing so has a lot of (undesirable) fallout (#101478, #119821).
561+ // As a result we always use a revealed env when resolving the instance to evaluate.
562+ //
563+ // FIXME: `const_eval_resolve_for_typeck` should probably just modify the env itself
564+ // instead of having this logic here
565+ let ( args, typing_env) = if tcx. features ( ) . generic_const_exprs ( )
556566 && uv. has_non_region_infer ( )
557567 {
558568 // `feature(generic_const_exprs)` causes anon consts to inherit all parent generics. This can cause
@@ -568,13 +578,17 @@ pub fn try_evaluate_const<'tcx>(
568578 // the generic arguments provided for it, then we should *not* attempt to evaluate it.
569579 return Err ( EvaluateConstErr :: HasGenericsOrInfers ) ;
570580 } else {
571- ( replace_param_and_infer_args_with_placeholder ( tcx, uv. args ) , param_env)
581+ let args = replace_param_and_infer_args_with_placeholder ( tcx, uv. args ) ;
582+ let typing_env = infcx
583+ . typing_env ( tcx. erase_regions ( param_env) )
584+ . with_post_analysis_normalized ( tcx) ;
585+ ( args, typing_env)
572586 }
573587 }
574588 Err ( _) | Ok ( None ) => {
575589 let args = GenericArgs :: identity_for_item ( tcx, uv. def ) ;
576- let param_env = tcx . param_env ( uv. def ) ;
577- ( args, param_env )
590+ let typing_env = ty :: TypingEnv :: post_analysis ( tcx , uv. def ) ;
591+ ( args, typing_env )
578592 }
579593 }
580594 } else if tcx. def_kind ( uv. def ) == DefKind :: AnonConst && uv. has_non_region_infer ( ) {
@@ -593,27 +607,20 @@ pub fn try_evaluate_const<'tcx>(
593607 ) ;
594608
595609 let args = GenericArgs :: identity_for_item ( tcx, uv. def ) ;
596- let param_env = tcx . param_env ( uv. def ) ;
597- ( args, param_env )
610+ let typing_env = ty :: TypingEnv :: post_analysis ( tcx , uv. def ) ;
611+ ( args, typing_env )
598612 } else {
599613 // FIXME: This codepath is reachable under `associated_const_equality` and in the
600614 // future will be reachable by `min_generic_const_args`. We should handle inference
601615 // variables and generic parameters properly instead of doing nothing.
602- ( uv. args , param_env)
616+ let typing_env = infcx
617+ . typing_env ( tcx. erase_regions ( param_env) )
618+ . with_post_analysis_normalized ( tcx) ;
619+ ( uv. args , typing_env)
603620 } ;
604621 let uv = ty:: UnevaluatedConst :: new ( uv. def , args) ;
605622
606- // It's not *technically* correct to be revealing opaque types here as borrowcheck has
607- // not run yet. However, CTFE itself uses `TypingMode::PostAnalysis` unconditionally even
608- // during typeck and not doing so has a lot of (undesirable) fallout (#101478, #119821).
609- // As a result we always use a revealed env when resolving the instance to evaluate.
610- //
611- // FIXME: `const_eval_resolve_for_typeck` should probably just modify the env itself
612- // instead of having this logic here
613- let typing_env =
614- tcx. erase_regions ( infcx. typing_env ( param_env) ) . with_post_analysis_normalized ( tcx) ;
615623 let erased_uv = tcx. erase_regions ( uv) ;
616-
617624 use rustc_middle:: mir:: interpret:: ErrorHandled ;
618625 match tcx. const_eval_resolve_for_typeck ( typing_env, erased_uv, DUMMY_SP ) {
619626 Ok ( Ok ( val) ) => Ok ( ty:: Const :: new_value (
0 commit comments