@@ -126,6 +126,7 @@ enum Scope<'a> {
126126 /// fn foo(x: impl for<'a> Trait<'a, Assoc = impl Copy + 'a>) {}
127127 /// ```
128128 where_bound_origin : Option < hir:: PredicateOrigin > ,
129+ in_where_predict : bool ,
129130 } ,
130131
131132 /// Lifetimes introduced by a fn are scoped to the call-site for that fn,
@@ -195,7 +196,7 @@ struct TruncatedScopeDebug<'a>(&'a Scope<'a>);
195196impl < ' a > fmt:: Debug for TruncatedScopeDebug < ' a > {
196197 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
197198 match self . 0 {
198- Scope :: Binder { bound_vars, scope_type, hir_id, where_bound_origin, s : _ } => f
199+ Scope :: Binder { bound_vars, scope_type, hir_id, where_bound_origin, s : _, .. } => f
199200 . debug_struct ( "Binder" )
200201 . field ( "bound_vars" , bound_vars)
201202 . field ( "scope_type" , scope_type)
@@ -394,6 +395,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
394395 s : self . scope ,
395396 scope_type,
396397 where_bound_origin : None ,
398+ in_where_predict : false ,
397399 } ;
398400 self . with ( scope, |this| {
399401 walk_list ! ( this, visit_generic_param, trait_ref. bound_generic_params) ;
@@ -478,14 +480,14 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
478480 . unzip ( ) ;
479481
480482 deny_non_region_late_bound ( self . tcx , & mut bound_vars, "closures" ) ;
481-
482483 self . record_late_bound_vars ( e. hir_id , binders) ;
483484 let scope = Scope :: Binder {
484485 hir_id : e. hir_id ,
485486 bound_vars,
486487 s : self . scope ,
487488 scope_type : BinderScopeType :: Normal ,
488489 where_bound_origin : None ,
490+ in_where_predict : false ,
489491 } ;
490492
491493 self . with ( scope, |this| {
@@ -577,6 +579,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
577579 s : this. scope ,
578580 scope_type : BinderScopeType :: Normal ,
579581 where_bound_origin : None ,
582+ in_where_predict : false ,
580583 } ;
581584 this. with ( scope, |this| {
582585 let scope = Scope :: TraitRefBoundary { s : this. scope } ;
@@ -630,14 +633,14 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
630633 . unzip ( ) ;
631634
632635 deny_non_region_late_bound ( self . tcx , & mut bound_vars, "function pointer types" ) ;
633-
634636 self . record_late_bound_vars ( ty. hir_id , binders) ;
635637 let scope = Scope :: Binder {
636638 hir_id : ty. hir_id ,
637639 bound_vars,
638640 s : self . scope ,
639641 scope_type : BinderScopeType :: Normal ,
640642 where_bound_origin : None ,
643+ in_where_predict : false ,
641644 } ;
642645 self . with ( scope, |this| {
643646 // a bare fn has no bounds, so everything
@@ -907,6 +910,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
907910 s : self . scope ,
908911 scope_type : BinderScopeType :: Normal ,
909912 where_bound_origin : Some ( origin) ,
913+ in_where_predict : true ,
910914 } ;
911915 self . with ( scope, |this| {
912916 walk_list ! ( this, visit_generic_param, bound_generic_params) ;
@@ -963,14 +967,14 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
963967 // of "if there isn't a Binder scope above us, add one", but I
964968 // imagine there's a better way to go about this.
965969 let ( binders, scope_type) = self . poly_trait_ref_binder_info ( ) ;
966-
967970 self . record_late_bound_vars ( * hir_id, binders) ;
968971 let scope = Scope :: Binder {
969972 hir_id : * hir_id,
970973 bound_vars : FxIndexMap :: default ( ) ,
971974 s : self . scope ,
972975 scope_type,
973976 where_bound_origin : None ,
977+ in_where_predict : false ,
974978 } ;
975979 self . with ( scope, |this| {
976980 intravisit:: walk_param_bound ( this, bound) ;
@@ -992,6 +996,26 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
992996
993997 fn visit_generic_param ( & mut self , p : & ' tcx GenericParam < ' tcx > ) {
994998 match p. kind {
999+ GenericParamKind :: Const { ty, default : Some ( default) } => {
1000+ self . resolve_type_ref ( p. def_id , p. hir_id ) ;
1001+
1002+ // For expr in default of Const expr in where predict.
1003+ // We may get unexpected bound var resolution when converts a hir id
1004+ // corresponding to a type parameter to a early-bound `ty::Param` or late-bound `ty::Bound`
1005+ // fn foo<T>() where for<const N: u8 = { T::<0>::A as u8 }> T: Default {}
1006+ // ^ generic param ty
1007+ // ^^^^^^ hir_id
1008+ // ^^^^^^^^^^^^^^^^^^^ default
1009+ // ^^^ parent_id after twice iterations
1010+ // ^ generics of parent
1011+ if let Scope :: Binder { in_where_predict, .. } = self . scope && * in_where_predict {
1012+ let BoundVarContext { tcx, map, .. } = self ;
1013+ let wrap_scope = Scope :: LateBoundary { s : self . scope , what : "constant" } ;
1014+ let mut this = BoundVarContext { tcx : * tcx, map, scope : & wrap_scope } ;
1015+ this. visit_id ( default. hir_id ) ;
1016+ this. visit_nested_body ( default. body ) ;
1017+ }
1018+ }
9951019 GenericParamKind :: Type { .. } | GenericParamKind :: Const { .. } => {
9961020 self . resolve_type_ref ( p. def_id , p. hir_id ) ;
9971021 }
@@ -1144,6 +1168,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
11441168 s : self . scope ,
11451169 scope_type : BinderScopeType :: Normal ,
11461170 where_bound_origin : None ,
1171+ in_where_predict : false ,
11471172 } ;
11481173 self . with ( scope, walk) ;
11491174 }
@@ -1160,6 +1185,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
11601185 s : self . scope ,
11611186 scope_type : BinderScopeType :: Normal ,
11621187 where_bound_origin : None ,
1188+ in_where_predict : false ,
11631189 } ;
11641190 self . with ( scope, |this| {
11651191 let scope = Scope :: TraitRefBoundary { s : this. scope } ;
@@ -1369,7 +1395,6 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
13691395 let mut late_depth = 0 ;
13701396 let mut scope = self . scope ;
13711397 let mut crossed_late_boundary = None ;
1372-
13731398 let result = loop {
13741399 match * scope {
13751400 Scope :: Body { s, .. } => {
0 commit comments