@@ -551,8 +551,18 @@ pub fn try_evaluate_const<'tcx>(
551
551
| ty:: ConstKind :: Placeholder ( _)
552
552
| ty:: ConstKind :: Expr ( _) => Err ( EvaluateConstErr :: HasGenericsOrInfers ) ,
553
553
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 ( )
556
566
&& uv. has_non_region_infer ( )
557
567
{
558
568
// `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>(
568
578
// the generic arguments provided for it, then we should *not* attempt to evaluate it.
569
579
return Err ( EvaluateConstErr :: HasGenericsOrInfers ) ;
570
580
} 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)
572
586
}
573
587
}
574
588
Err ( _) | Ok ( None ) => {
575
589
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 )
578
592
}
579
593
}
580
594
} else if tcx. def_kind ( uv. def ) == DefKind :: AnonConst && uv. has_non_region_infer ( ) {
@@ -593,27 +607,20 @@ pub fn try_evaluate_const<'tcx>(
593
607
) ;
594
608
595
609
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 )
598
612
} else {
599
613
// FIXME: This codepath is reachable under `associated_const_equality` and in the
600
614
// future will be reachable by `min_generic_const_args`. We should handle inference
601
615
// 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)
603
620
} ;
604
621
let uv = ty:: UnevaluatedConst :: new ( uv. def , args) ;
605
622
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) ;
615
623
let erased_uv = tcx. erase_regions ( uv) ;
616
-
617
624
use rustc_middle:: mir:: interpret:: ErrorHandled ;
618
625
match tcx. const_eval_resolve_for_typeck ( typing_env, erased_uv, DUMMY_SP ) {
619
626
Ok ( Ok ( val) ) => Ok ( ty:: Const :: new_value (
0 commit comments