@@ -11,6 +11,7 @@ use rustc_infer::infer::resolve::OpportunisticRegionResolver;
1111use rustc_infer:: traits:: { ObligationCauseCode , PredicateObligations } ;
1212use rustc_middle:: traits:: select:: OverflowError ;
1313use rustc_middle:: traits:: { BuiltinImplSource , ImplSource , ImplSourceUserDefinedData } ;
14+ use rustc_middle:: ty:: abstract_const:: NotConstEvaluatable ;
1415use rustc_middle:: ty:: fast_reject:: DeepRejectCtxt ;
1516use rustc_middle:: ty:: {
1617 self , FieldInfo , Term , Ty , TyCtxt , TypeFoldable , TypeVisitableExt , TypingMode , Upcast ,
@@ -549,9 +550,17 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>(
549550 ty:: AliasTermKind :: InherentTy { def_id } => {
550551 tcx. type_of ( def_id) . instantiate ( tcx, args) . skip_norm_wip ( ) . into ( )
551552 }
552- ty:: AliasTermKind :: InherentConst { def_id } => {
553+ ty:: AliasTermKind :: InherentConst { def_id } if tcx . is_type_const ( def_id ) => {
553554 tcx. const_of_item ( def_id) . instantiate ( tcx, args) . skip_norm_wip ( ) . into ( )
554555 }
556+ ty:: AliasTermKind :: InherentConst { .. } => {
557+ // FIXME(gca): This is dead code at the moment. It should eventually call
558+ // super::evaluate_const like projected consts do in confirm_impl_candidate in this
559+ // file. However, how generic args are represented for IACs is up in the air right now.
560+ // Will super::evaluate_const eventually take the inherent_args or the impl_args form of
561+ // args? It might be either.
562+ panic ! ( "References to inherent associated consts should have been blocked" ) ;
563+ }
555564 kind => panic ! ( "expected inherent alias, found {kind:?}" ) ,
556565 } ;
557566
@@ -617,11 +626,13 @@ pub fn compute_inherent_assoc_term_args<'a, 'b, 'tcx>(
617626 alias_term. rebase_inherent_args_onto_impl ( impl_args, tcx)
618627}
619628
629+ #[ derive( Debug ) ]
620630enum Projected < ' tcx > {
621631 Progress ( Progress < ' tcx > ) ,
622632 NoProgress ( ty:: Term < ' tcx > ) ,
623633}
624634
635+ #[ derive( Debug ) ]
625636struct Progress < ' tcx > {
626637 term : ty:: Term < ' tcx > ,
627638 obligations : PredicateObligations < ' tcx > ,
@@ -652,7 +663,7 @@ impl<'tcx> Progress<'tcx> {
652663/// IMPORTANT:
653664/// - `obligation` must be fully normalized
654665// FIXME(mgca): While this supports constants, it is only used for types by default right now
655- #[ instrument( level = "info" , skip( selcx) ) ]
666+ #[ instrument( level = "info" , ret , skip( selcx) ) ]
656667fn project < ' cx , ' tcx > (
657668 selcx : & mut SelectionContext < ' cx , ' tcx > ,
658669 obligation : & ProjectionTermObligation < ' tcx > ,
@@ -2060,7 +2071,29 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20602071 ty:: AliasTermKind :: ProjectionConst { .. } => {
20612072 let uv = ty:: UnevaluatedConst :: new ( assoc_term. item . def_id , args) ;
20622073 let ct = ty:: Const :: new_unevaluated ( tcx, uv) ;
2063- super :: evaluate_const ( selcx. infcx , ct, param_env) . into ( )
2074+ // We don't want to use super::evaluate_const, because that returns its parameter
2075+ // unchanged if it is too generic to evaluate. We are passing the `impl` form of the
2076+ // constant to evaluate_const (with generic arguments corresponding to the impl
2077+ // block), but we want to return the original, non-rebased, trait `Self` form of the
2078+ // constant (with generic arguments being the trait `Self` type) in Projected::NoProgress.
2079+ match super :: try_evaluate_const ( selcx. infcx , ct, param_env) {
2080+ Ok ( evaluated) => evaluated. into ( ) ,
2081+ Err (
2082+ super :: EvaluateConstErr :: EvaluationFailure ( e)
2083+ | super :: EvaluateConstErr :: InvalidConstParamTy ( e) ,
2084+ ) => ty:: Const :: new_error ( tcx, e) . into ( ) ,
2085+ Err ( super :: EvaluateConstErr :: HasGenericsOrInfers ) => {
2086+ if selcx. infcx . resolve_vars_if_possible ( uv) . has_non_region_infer ( ) {
2087+ return Err ( ProjectionError :: TraitSelectionError (
2088+ SelectionError :: NotConstEvaluatable (
2089+ NotConstEvaluatable :: MentionsInfer ,
2090+ ) ,
2091+ ) ) ;
2092+ } else {
2093+ return Ok ( Projected :: NoProgress ( obligation. predicate . to_term ( tcx) ) ) ;
2094+ }
2095+ }
2096+ }
20642097 }
20652098 kind => panic ! ( "expected projection alias, found {kind:?}" ) ,
20662099 } ;
0 commit comments