@@ -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_re_late_bound ( 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_re_early_bound ( 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_re_free ( scope, ty:: BrNamed ( id, name) )
251
251
@@ -1607,7 +1607,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1607
1607
self . ast_region_to_region ( lifetime, None )
1608
1608
} else {
1609
1609
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 ( ) {
1611
1611
self . ast_region_to_region ( lifetime, None )
1612
1612
} else {
1613
1613
self . re_infer ( None , span) . unwrap_or_else ( || {
@@ -2600,6 +2600,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2600
2600
& self ,
2601
2601
opt_self_ty : Option < Ty < ' tcx > > ,
2602
2602
path : & hir:: Path < ' _ > ,
2603
+ hir_id : hir:: HirId ,
2603
2604
permit_variants : bool ,
2604
2605
) -> Ty < ' tcx > {
2605
2606
let tcx = self . tcx ( ) ;
@@ -2663,11 +2664,25 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2663
2664
}
2664
2665
} ) ;
2665
2666
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
+ }
2671
2686
}
2672
2687
Res :: SelfTyParam { .. } => {
2673
2688
// `Self` in trait or type alias.
@@ -2870,27 +2885,50 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2870
2885
hir:: TyKind :: BareFn ( bf) => {
2871
2886
require_c_abi_if_c_variadic ( tcx, bf. decl , bf. abi , ast_ty. span ) ;
2872
2887
2873
- tcx. mk_fn_ptr ( self . ty_of_fn (
2888
+ let fn_ptr_ty = tcx. mk_fn_ptr ( self . ty_of_fn (
2874
2889
ast_ty. hir_id ,
2875
2890
bf. unsafety ,
2876
2891
bf. abi ,
2877
2892
bf. decl ,
2878
2893
None ,
2879
2894
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
+ }
2881
2904
}
2882
2905
hir:: TyKind :: TraitObject ( bounds, lifetime, repr) => {
2883
2906
self . maybe_lint_bare_trait ( ast_ty, in_path) ;
2884
2907
let repr = match repr {
2885
2908
TraitObjectSyntax :: Dyn | TraitObjectSyntax :: None => ty:: Dyn ,
2886
2909
TraitObjectSyntax :: DynStar => ty:: DynStar ,
2887
2910
} ;
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
+ }
2889
2927
}
2890
2928
hir:: TyKind :: Path ( hir:: QPath :: Resolved ( maybe_qself, path) ) => {
2891
2929
debug ! ( ?maybe_qself, ?path) ;
2892
2930
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 )
2894
2932
}
2895
2933
& hir:: TyKind :: OpaqueDef ( item_id, lifetimes, in_trait) => {
2896
2934
let opaque_ty = tcx. hir ( ) . item ( item_id) ;
@@ -3346,3 +3384,24 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
3346
3384
}
3347
3385
}
3348
3386
}
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