@@ -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_re_late_bound ( 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_re_early_bound ( 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_re_free ( scope, ty:: BrNamed ( id, name) )
251251
@@ -1607,7 +1607,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16071607 self . ast_region_to_region ( lifetime, None )
16081608 } else {
16091609 self . compute_object_lifetime_bound ( span, existential_predicates) . unwrap_or_else ( || {
1610- if tcx. named_region ( lifetime. hir_id ) . is_some ( ) {
1610+ if tcx. named_bound_var ( lifetime. hir_id ) . is_some ( ) {
16111611 self . ast_region_to_region ( lifetime, None )
16121612 } else {
16131613 self . re_infer ( None , span) . unwrap_or_else ( || {
@@ -2600,6 +2600,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26002600 & self ,
26012601 opt_self_ty : Option < Ty < ' tcx > > ,
26022602 path : & hir:: Path < ' _ > ,
2603+ hir_id : hir:: HirId ,
26032604 permit_variants : bool ,
26042605 ) -> Ty < ' tcx > {
26052606 let tcx = self . tcx ( ) ;
@@ -2663,11 +2664,25 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26632664 }
26642665 } ) ;
26652666
2666- let def_id = def_id. expect_local ( ) ;
2667- let item_def_id = tcx. hir ( ) . ty_param_owner ( def_id) ;
2668- let generics = tcx. generics_of ( item_def_id) ;
2669- let index = generics. param_def_id_to_index [ & def_id. to_def_id ( ) ] ;
2670- tcx. mk_ty_param ( index, tcx. hir ( ) . ty_param_name ( def_id) )
2667+ match tcx. named_bound_var ( hir_id) {
2668+ Some ( rbv:: ResolvedArg :: LateBound ( debruijn, index, _) ) => {
2669+ let name =
2670+ tcx. hir ( ) . name ( tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ) ;
2671+ let br = ty:: BoundTy {
2672+ var : ty:: BoundVar :: from_u32 ( index) ,
2673+ kind : ty:: BoundTyKind :: Param ( def_id, name) ,
2674+ } ;
2675+ tcx. mk_ty ( ty:: Bound ( debruijn, br) )
2676+ }
2677+ Some ( rbv:: ResolvedArg :: EarlyBound ( _) ) => {
2678+ let def_id = def_id. expect_local ( ) ;
2679+ let item_def_id = tcx. hir ( ) . ty_param_owner ( def_id) ;
2680+ let generics = tcx. generics_of ( item_def_id) ;
2681+ let index = generics. param_def_id_to_index [ & def_id. to_def_id ( ) ] ;
2682+ tcx. mk_ty_param ( index, tcx. hir ( ) . ty_param_name ( def_id) )
2683+ }
2684+ arg => bug ! ( "unexpected bound var resolution for {hir_id:?}: {arg:?}" ) ,
2685+ }
26712686 }
26722687 Res :: SelfTyParam { .. } => {
26732688 // `Self` in trait or type alias.
@@ -2870,27 +2885,50 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
28702885 hir:: TyKind :: BareFn ( bf) => {
28712886 require_c_abi_if_c_variadic ( tcx, bf. decl , bf. abi , ast_ty. span ) ;
28722887
2873- tcx. mk_fn_ptr ( self . ty_of_fn (
2888+ let fn_ptr_ty = tcx. mk_fn_ptr ( self . ty_of_fn (
28742889 ast_ty. hir_id ,
28752890 bf. unsafety ,
28762891 bf. abi ,
28772892 bf. decl ,
28782893 None ,
28792894 Some ( ast_ty) ,
2880- ) )
2895+ ) ) ;
2896+
2897+ if let Some ( guar) =
2898+ deny_non_region_late_bound ( tcx, bf. generic_params , "function pointer" )
2899+ {
2900+ tcx. ty_error_with_guaranteed ( guar)
2901+ } else {
2902+ fn_ptr_ty
2903+ }
28812904 }
28822905 hir:: TyKind :: TraitObject ( bounds, lifetime, repr) => {
28832906 self . maybe_lint_bare_trait ( ast_ty, in_path) ;
28842907 let repr = match repr {
28852908 TraitObjectSyntax :: Dyn | TraitObjectSyntax :: None => ty:: Dyn ,
28862909 TraitObjectSyntax :: DynStar => ty:: DynStar ,
28872910 } ;
2888- self . conv_object_ty_poly_trait_ref ( ast_ty. span , bounds, lifetime, borrowed, repr)
2911+
2912+ let object_ty = self . conv_object_ty_poly_trait_ref (
2913+ ast_ty. span ,
2914+ bounds,
2915+ lifetime,
2916+ borrowed,
2917+ repr,
2918+ ) ;
2919+
2920+ if let Some ( guar) = bounds. iter ( ) . find_map ( |trait_ref| {
2921+ deny_non_region_late_bound ( tcx, trait_ref. bound_generic_params , "trait object" )
2922+ } ) {
2923+ tcx. ty_error_with_guaranteed ( guar)
2924+ } else {
2925+ object_ty
2926+ }
28892927 }
28902928 hir:: TyKind :: Path ( hir:: QPath :: Resolved ( maybe_qself, path) ) => {
28912929 debug ! ( ?maybe_qself, ?path) ;
28922930 let opt_self_ty = maybe_qself. as_ref ( ) . map ( |qself| self . ast_ty_to_ty ( qself) ) ;
2893- self . res_to_ty ( opt_self_ty, path, false )
2931+ self . res_to_ty ( opt_self_ty, path, ast_ty . hir_id , false )
28942932 }
28952933 & hir:: TyKind :: OpaqueDef ( item_id, lifetimes, in_trait) => {
28962934 let opaque_ty = tcx. hir ( ) . item ( item_id) ;
@@ -3346,3 +3384,24 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
33463384 }
33473385 }
33483386}
3387+
3388+ fn deny_non_region_late_bound (
3389+ tcx : TyCtxt < ' _ > ,
3390+ params : & [ hir:: GenericParam < ' _ > ] ,
3391+ where_ : & str ,
3392+ ) -> Option < ErrorGuaranteed > {
3393+ params. iter ( ) . find_map ( |bad_param| {
3394+ let what = match bad_param. kind {
3395+ hir:: GenericParamKind :: Type { .. } => "type" ,
3396+ hir:: GenericParamKind :: Const { .. } => "const" ,
3397+ hir:: GenericParamKind :: Lifetime { .. } => return None ,
3398+ } ;
3399+
3400+ let mut diag = tcx. sess . struct_span_err (
3401+ bad_param. span ,
3402+ format ! ( "late-bound {what} parameter not allowed on {where_} types" ) ,
3403+ ) ;
3404+
3405+ Some ( if tcx. features ( ) . non_lifetime_binders { diag. emit ( ) } else { diag. delay_as_bug ( ) } )
3406+ } )
3407+ }
0 commit comments