@@ -14,7 +14,7 @@ use crate::errors::{
1414 AmbiguousLifetimeBound , MultipleRelaxedDefaultBounds , TraitObjectDeclaredWithNoTraits ,
1515 TypeofReservedKeywordUsed , ValueOfAssociatedStructAlreadySpecified ,
1616} ;
17- use crate :: middle:: resolve_lifetime as rl ;
17+ use crate :: middle:: resolve_bound_vars as rbv ;
1818use crate :: require_c_abi_if_c_variadic;
1919use rustc_ast:: TraitObjectSyntax ;
2020use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
@@ -225,10 +225,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
225225 let tcx = self . tcx ( ) ;
226226 let lifetime_name = |def_id| tcx. hir ( ) . name ( tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ) ;
227227
228- match tcx. named_region ( lifetime. hir_id ) {
229- Some ( rl :: Region :: Static ) => tcx. lifetimes . re_static ,
228+ match tcx. named_bound_var ( lifetime. hir_id ) {
229+ Some ( rbv :: ResolvedArg :: StaticLifetime ) => tcx. lifetimes . re_static ,
230230
231- Some ( rl :: Region :: LateBound ( debruijn, index, def_id) ) => {
231+ Some ( rbv :: ResolvedArg :: LateBound ( debruijn, index, def_id) ) => {
232232 let name = lifetime_name ( def_id. expect_local ( ) ) ;
233233 let br = ty:: BoundRegion {
234234 var : ty:: BoundVar :: from_u32 ( index) ,
@@ -237,15 +237,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
237237 tcx. mk_region ( ty:: ReLateBound ( debruijn, br) )
238238 }
239239
240- Some ( rl :: Region :: EarlyBound ( def_id) ) => {
240+ Some ( rbv :: ResolvedArg :: EarlyBound ( def_id) ) => {
241241 let name = tcx. hir ( ) . ty_param_name ( def_id. expect_local ( ) ) ;
242242 let item_def_id = tcx. hir ( ) . ty_param_owner ( def_id. expect_local ( ) ) ;
243243 let generics = tcx. generics_of ( item_def_id) ;
244244 let index = generics. param_def_id_to_index [ & def_id] ;
245245 tcx. mk_region ( ty:: ReEarlyBound ( ty:: EarlyBoundRegion { def_id, index, name } ) )
246246 }
247247
248- Some ( rl :: Region :: Free ( scope, id) ) => {
248+ Some ( rbv :: ResolvedArg :: Free ( scope, id) ) => {
249249 let name = lifetime_name ( id. expect_local ( ) ) ;
250250 tcx. mk_region ( ty:: ReFree ( ty:: FreeRegion {
251251 scope,
@@ -1604,7 +1604,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16041604 self . ast_region_to_region ( lifetime, None )
16051605 } else {
16061606 self . compute_object_lifetime_bound ( span, existential_predicates) . unwrap_or_else ( || {
1607- if tcx. named_region ( lifetime. hir_id ) . is_some ( ) {
1607+ if tcx. named_bound_var ( lifetime. hir_id ) . is_some ( ) {
16081608 self . ast_region_to_region ( lifetime, None )
16091609 } else {
16101610 self . re_infer ( None , span) . unwrap_or_else ( || {
@@ -2598,6 +2598,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25982598 & self ,
25992599 opt_self_ty : Option < Ty < ' tcx > > ,
26002600 path : & hir:: Path < ' _ > ,
2601+ hir_id : hir:: HirId ,
26012602 permit_variants : bool ,
26022603 ) -> Ty < ' tcx > {
26032604 let tcx = self . tcx ( ) ;
@@ -2661,11 +2662,25 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26612662 }
26622663 } ) ;
26632664
2664- let def_id = def_id. expect_local ( ) ;
2665- let item_def_id = tcx. hir ( ) . ty_param_owner ( def_id) ;
2666- let generics = tcx. generics_of ( item_def_id) ;
2667- let index = generics. param_def_id_to_index [ & def_id. to_def_id ( ) ] ;
2668- tcx. mk_ty_param ( index, tcx. hir ( ) . ty_param_name ( def_id) )
2665+ match tcx. named_bound_var ( hir_id) {
2666+ Some ( rbv:: ResolvedArg :: LateBound ( debruijn, index, _) ) => {
2667+ let name =
2668+ tcx. hir ( ) . name ( tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ) ;
2669+ let br = ty:: BoundTy {
2670+ var : ty:: BoundVar :: from_u32 ( index) ,
2671+ kind : ty:: BoundTyKind :: Param ( def_id, name) ,
2672+ } ;
2673+ tcx. mk_ty ( ty:: Bound ( debruijn, br) )
2674+ }
2675+ Some ( rbv:: ResolvedArg :: EarlyBound ( _) ) => {
2676+ let def_id = def_id. expect_local ( ) ;
2677+ let item_def_id = tcx. hir ( ) . ty_param_owner ( def_id) ;
2678+ let generics = tcx. generics_of ( item_def_id) ;
2679+ let index = generics. param_def_id_to_index [ & def_id. to_def_id ( ) ] ;
2680+ tcx. mk_ty_param ( index, tcx. hir ( ) . ty_param_name ( def_id) )
2681+ }
2682+ arg => bug ! ( "unexpected bound var resolution for {hir_id:?}: {arg:?}" ) ,
2683+ }
26692684 }
26702685 Res :: SelfTyParam { .. } => {
26712686 // `Self` in trait or type alias.
@@ -2868,27 +2883,50 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
28682883 hir:: TyKind :: BareFn ( bf) => {
28692884 require_c_abi_if_c_variadic ( tcx, bf. decl , bf. abi , ast_ty. span ) ;
28702885
2871- tcx. mk_fn_ptr ( self . ty_of_fn (
2886+ let fn_ptr_ty = tcx. mk_fn_ptr ( self . ty_of_fn (
28722887 ast_ty. hir_id ,
28732888 bf. unsafety ,
28742889 bf. abi ,
28752890 bf. decl ,
28762891 None ,
28772892 Some ( ast_ty) ,
2878- ) )
2893+ ) ) ;
2894+
2895+ if let Some ( guar) =
2896+ deny_non_region_late_bound ( tcx, bf. generic_params , "function pointer" )
2897+ {
2898+ tcx. ty_error_with_guaranteed ( guar)
2899+ } else {
2900+ fn_ptr_ty
2901+ }
28792902 }
28802903 hir:: TyKind :: TraitObject ( bounds, lifetime, repr) => {
28812904 self . maybe_lint_bare_trait ( ast_ty, in_path) ;
28822905 let repr = match repr {
28832906 TraitObjectSyntax :: Dyn | TraitObjectSyntax :: None => ty:: Dyn ,
28842907 TraitObjectSyntax :: DynStar => ty:: DynStar ,
28852908 } ;
2886- self . conv_object_ty_poly_trait_ref ( ast_ty. span , bounds, lifetime, borrowed, repr)
2909+
2910+ let object_ty = self . conv_object_ty_poly_trait_ref (
2911+ ast_ty. span ,
2912+ bounds,
2913+ lifetime,
2914+ borrowed,
2915+ repr,
2916+ ) ;
2917+
2918+ if let Some ( guar) = bounds. iter ( ) . find_map ( |trait_ref| {
2919+ deny_non_region_late_bound ( tcx, trait_ref. bound_generic_params , "trait object" )
2920+ } ) {
2921+ tcx. ty_error_with_guaranteed ( guar)
2922+ } else {
2923+ object_ty
2924+ }
28872925 }
28882926 hir:: TyKind :: Path ( hir:: QPath :: Resolved ( maybe_qself, path) ) => {
28892927 debug ! ( ?maybe_qself, ?path) ;
28902928 let opt_self_ty = maybe_qself. as_ref ( ) . map ( |qself| self . ast_ty_to_ty ( qself) ) ;
2891- self . res_to_ty ( opt_self_ty, path, false )
2929+ self . res_to_ty ( opt_self_ty, path, ast_ty . hir_id , false )
28922930 }
28932931 & hir:: TyKind :: OpaqueDef ( item_id, lifetimes, in_trait) => {
28942932 let opaque_ty = tcx. hir ( ) . item ( item_id) ;
@@ -3344,3 +3382,24 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
33443382 }
33453383 }
33463384}
3385+
3386+ fn deny_non_region_late_bound (
3387+ tcx : TyCtxt < ' _ > ,
3388+ params : & [ hir:: GenericParam < ' _ > ] ,
3389+ where_ : & str ,
3390+ ) -> Option < ErrorGuaranteed > {
3391+ params. iter ( ) . find_map ( |bad_param| {
3392+ let what = match bad_param. kind {
3393+ hir:: GenericParamKind :: Type { .. } => "type" ,
3394+ hir:: GenericParamKind :: Const { .. } => "const" ,
3395+ hir:: GenericParamKind :: Lifetime { .. } => return None ,
3396+ } ;
3397+
3398+ let mut diag = tcx. sess . struct_span_err (
3399+ bad_param. span ,
3400+ format ! ( "late-bound {what} parameter not allowed on {where_} types" ) ,
3401+ ) ;
3402+
3403+ Some ( if tcx. features ( ) . non_lifetime_binders { diag. emit ( ) } else { diag. delay_as_bug ( ) } )
3404+ } )
3405+ }
0 commit comments