@@ -14,7 +14,7 @@ use crate::errors::{
14
14
AmbiguousLifetimeBound , MultipleRelaxedDefaultBounds , TraitObjectDeclaredWithNoTraits ,
15
15
TypeofReservedKeywordUsed , ValueOfAssociatedStructAlreadySpecified ,
16
16
} ;
17
- use crate :: middle:: resolve_lifetime as rl ;
17
+ use crate :: middle:: resolve_bound_vars as rbv ;
18
18
use crate :: require_c_abi_if_c_variadic;
19
19
use rustc_ast:: TraitObjectSyntax ;
20
20
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
@@ -225,10 +225,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
225
225
let tcx = self . tcx ( ) ;
226
226
let lifetime_name = |def_id| tcx. hir ( ) . name ( tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ) ;
227
227
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 ,
230
230
231
- Some ( rl :: Region :: LateBound ( debruijn, index, def_id) ) => {
231
+ Some ( rbv :: ResolvedArg :: LateBound ( debruijn, index, def_id) ) => {
232
232
let name = lifetime_name ( def_id. expect_local ( ) ) ;
233
233
let br = ty:: BoundRegion {
234
234
var : ty:: BoundVar :: from_u32 ( index) ,
@@ -237,15 +237,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
237
237
tcx. mk_region ( ty:: ReLateBound ( debruijn, br) )
238
238
}
239
239
240
- Some ( rl :: Region :: EarlyBound ( def_id) ) => {
240
+ Some ( rbv :: ResolvedArg :: EarlyBound ( def_id) ) => {
241
241
let name = tcx. hir ( ) . ty_param_name ( def_id. expect_local ( ) ) ;
242
242
let item_def_id = tcx. hir ( ) . ty_param_owner ( def_id. expect_local ( ) ) ;
243
243
let generics = tcx. generics_of ( item_def_id) ;
244
244
let index = generics. param_def_id_to_index [ & def_id] ;
245
245
tcx. mk_region ( ty:: ReEarlyBound ( ty:: EarlyBoundRegion { def_id, index, name } ) )
246
246
}
247
247
248
- Some ( rl :: Region :: Free ( scope, id) ) => {
248
+ Some ( rbv :: ResolvedArg :: Free ( scope, id) ) => {
249
249
let name = lifetime_name ( id. expect_local ( ) ) ;
250
250
tcx. mk_region ( ty:: ReFree ( ty:: FreeRegion {
251
251
scope,
@@ -1604,7 +1604,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1604
1604
self . ast_region_to_region ( lifetime, None )
1605
1605
} else {
1606
1606
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 ( ) {
1608
1608
self . ast_region_to_region ( lifetime, None )
1609
1609
} else {
1610
1610
self . re_infer ( None , span) . unwrap_or_else ( || {
@@ -2598,6 +2598,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2598
2598
& self ,
2599
2599
opt_self_ty : Option < Ty < ' tcx > > ,
2600
2600
path : & hir:: Path < ' _ > ,
2601
+ hir_id : hir:: HirId ,
2601
2602
permit_variants : bool ,
2602
2603
) -> Ty < ' tcx > {
2603
2604
let tcx = self . tcx ( ) ;
@@ -2661,11 +2662,25 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2661
2662
}
2662
2663
} ) ;
2663
2664
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
+ }
2669
2684
}
2670
2685
Res :: SelfTyParam { .. } => {
2671
2686
// `Self` in trait or type alias.
@@ -2868,27 +2883,50 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2868
2883
hir:: TyKind :: BareFn ( bf) => {
2869
2884
require_c_abi_if_c_variadic ( tcx, bf. decl , bf. abi , ast_ty. span ) ;
2870
2885
2871
- tcx. mk_fn_ptr ( self . ty_of_fn (
2886
+ let fn_ptr_ty = tcx. mk_fn_ptr ( self . ty_of_fn (
2872
2887
ast_ty. hir_id ,
2873
2888
bf. unsafety ,
2874
2889
bf. abi ,
2875
2890
bf. decl ,
2876
2891
None ,
2877
2892
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
+ }
2879
2902
}
2880
2903
hir:: TyKind :: TraitObject ( bounds, lifetime, repr) => {
2881
2904
self . maybe_lint_bare_trait ( ast_ty, in_path) ;
2882
2905
let repr = match repr {
2883
2906
TraitObjectSyntax :: Dyn | TraitObjectSyntax :: None => ty:: Dyn ,
2884
2907
TraitObjectSyntax :: DynStar => ty:: DynStar ,
2885
2908
} ;
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
+ }
2887
2925
}
2888
2926
hir:: TyKind :: Path ( hir:: QPath :: Resolved ( maybe_qself, path) ) => {
2889
2927
debug ! ( ?maybe_qself, ?path) ;
2890
2928
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 )
2892
2930
}
2893
2931
& hir:: TyKind :: OpaqueDef ( item_id, lifetimes, in_trait) => {
2894
2932
let opaque_ty = tcx. hir ( ) . item ( item_id) ;
@@ -3344,3 +3382,24 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
3344
3382
}
3345
3383
}
3346
3384
}
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