@@ -64,20 +64,18 @@ use require_c_abi_if_variadic;
64
64
use rscope:: { self , UnelidableRscope , RegionScope , ElidableRscope ,
65
65
ObjectLifetimeDefaultRscope , ShiftedRscope , BindingRscope ,
66
66
ElisionFailureInfo , ElidedLifetime } ;
67
- use util:: common:: { ErrorReported , FN_OUTPUT_NAME } ;
68
- use util:: nodemap:: FnvHashSet ;
69
-
67
+ use rustc:: lint;
68
+ use rustc_back:: slice;
70
69
use rustc_const_math:: ConstInt ;
71
-
70
+ use rustc_front:: print:: pprust;
71
+ use rustc_front:: hir;
72
72
use syntax:: { abi, ast} ;
73
73
use syntax:: codemap:: { Span , Pos } ;
74
74
use syntax:: errors:: DiagnosticBuilder ;
75
75
use syntax:: feature_gate:: { GateIssue , emit_feature_err} ;
76
76
use syntax:: parse:: token;
77
-
78
- use rustc:: hir:: print as pprust;
79
- use rustc:: hir;
80
- use rustc_back:: slice;
77
+ use util:: common:: { ErrorReported , FN_OUTPUT_NAME } ;
78
+ use util:: nodemap:: FnvHashSet ;
81
79
82
80
pub trait AstConv < ' tcx > {
83
81
fn tcx < ' a > ( & ' a self ) -> & ' a TyCtxt < ' tcx > ;
@@ -691,6 +689,7 @@ pub fn instantiate_poly_trait_ref<'tcx>(
691
689
PathParamMode :: Explicit ,
692
690
trait_def_id,
693
691
self_ty,
692
+ trait_ref. ref_id ,
694
693
trait_ref. path . segments . last ( ) . unwrap ( ) ,
695
694
poly_projections)
696
695
}
@@ -738,6 +737,7 @@ fn object_path_to_poly_trait_ref<'a,'tcx>(
738
737
span : Span ,
739
738
param_mode : PathParamMode ,
740
739
trait_def_id : DefId ,
740
+ trait_path_ref_id : ast:: NodeId ,
741
741
trait_segment : & hir:: PathSegment ,
742
742
mut projections : & mut Vec < ty:: PolyProjectionPredicate < ' tcx > > )
743
743
-> ty:: PolyTraitRef < ' tcx >
@@ -748,6 +748,7 @@ fn object_path_to_poly_trait_ref<'a,'tcx>(
748
748
param_mode,
749
749
trait_def_id,
750
750
None ,
751
+ trait_path_ref_id,
751
752
trait_segment,
752
753
projections)
753
754
}
@@ -759,6 +760,7 @@ fn ast_path_to_poly_trait_ref<'a,'tcx>(
759
760
param_mode : PathParamMode ,
760
761
trait_def_id : DefId ,
761
762
self_ty : Option < Ty < ' tcx > > ,
763
+ path_id : ast:: NodeId ,
762
764
trait_segment : & hir:: PathSegment ,
763
765
poly_projections : & mut Vec < ty:: PolyProjectionPredicate < ' tcx > > )
764
766
-> ty:: PolyTraitRef < ' tcx >
@@ -789,6 +791,7 @@ fn ast_path_to_poly_trait_ref<'a,'tcx>(
789
791
// specify type to assert that error was already reported in Err case:
790
792
let predicate: Result < _ , ErrorReported > =
791
793
ast_type_binding_to_poly_projection_predicate ( this,
794
+ path_id,
792
795
poly_trait_ref. clone ( ) ,
793
796
self_ty,
794
797
binding) ;
@@ -885,6 +888,7 @@ fn create_substs_for_ast_trait_ref<'a,'tcx>(this: &AstConv<'tcx>,
885
888
886
889
fn ast_type_binding_to_poly_projection_predicate < ' tcx > (
887
890
this : & AstConv < ' tcx > ,
891
+ path_id : ast:: NodeId ,
888
892
mut trait_ref : ty:: PolyTraitRef < ' tcx > ,
889
893
self_ty : Option < Ty < ' tcx > > ,
890
894
binding : & ConvertedBinding < ' tcx > )
@@ -933,11 +937,13 @@ fn ast_type_binding_to_poly_projection_predicate<'tcx>(
933
937
br) ) ;
934
938
}
935
939
} ;
936
- this. tcx ( ) . sess . span_err (
940
+ this. tcx ( ) . sess . add_lint (
941
+ lint:: builtin:: HR_LIFETIME_IN_ASSOC_TYPE ,
942
+ path_id,
937
943
binding. span ,
938
- & format ! ( "binding for associated type `{}` references lifetime `{}`, \
939
- which does not appear in the trait input types",
940
- binding. item_name, br_name) ) ;
944
+ format ! ( "binding for associated type `{}` references lifetime `{}`, \
945
+ which does not appear in the trait input types",
946
+ binding. item_name, br_name) ) ;
941
947
}
942
948
943
949
// Simple case: X is defined in the current trait.
@@ -1070,6 +1076,7 @@ fn ast_ty_to_trait_ref<'tcx>(this: &AstConv<'tcx>,
1070
1076
path. span ,
1071
1077
PathParamMode :: Explicit ,
1072
1078
trait_def_id,
1079
+ ty. id ,
1073
1080
path. segments . last ( ) . unwrap ( ) ,
1074
1081
& mut projection_bounds) ;
1075
1082
Ok ( ( trait_ref, projection_bounds) )
@@ -1480,6 +1487,7 @@ fn base_def_to_ty<'tcx>(this: &AstConv<'tcx>,
1480
1487
param_mode : PathParamMode ,
1481
1488
def : & Def ,
1482
1489
opt_self_ty : Option < Ty < ' tcx > > ,
1490
+ base_path_ref_id : ast:: NodeId ,
1483
1491
base_segments : & [ hir:: PathSegment ] )
1484
1492
-> Ty < ' tcx > {
1485
1493
let tcx = this. tcx ( ) ;
@@ -1495,6 +1503,7 @@ fn base_def_to_ty<'tcx>(this: &AstConv<'tcx>,
1495
1503
span,
1496
1504
param_mode,
1497
1505
trait_def_id,
1506
+ base_path_ref_id,
1498
1507
base_segments. last ( ) . unwrap ( ) ,
1499
1508
& mut projection_bounds) ;
1500
1509
@@ -1584,6 +1593,7 @@ pub fn finish_resolving_def_to_ty<'tcx>(this: &AstConv<'tcx>,
1584
1593
param_mode : PathParamMode ,
1585
1594
def : & Def ,
1586
1595
opt_self_ty : Option < Ty < ' tcx > > ,
1596
+ base_path_ref_id : ast:: NodeId ,
1587
1597
base_segments : & [ hir:: PathSegment ] ,
1588
1598
assoc_segments : & [ hir:: PathSegment ] )
1589
1599
-> Ty < ' tcx > {
@@ -1593,6 +1603,7 @@ pub fn finish_resolving_def_to_ty<'tcx>(this: &AstConv<'tcx>,
1593
1603
param_mode,
1594
1604
def,
1595
1605
opt_self_ty,
1606
+ base_path_ref_id,
1596
1607
base_segments) ;
1597
1608
let mut def = * def;
1598
1609
// If any associated type segments remain, attempt to resolve them.
@@ -1672,7 +1683,50 @@ pub fn ast_ty_to_ty<'tcx>(this: &AstConv<'tcx>,
1672
1683
}
1673
1684
hir:: TyBareFn ( ref bf) => {
1674
1685
require_c_abi_if_variadic ( tcx, & bf. decl , bf. abi , ast_ty. span ) ;
1675
- tcx. mk_fn_ptr ( ty_of_bare_fn ( this, bf. unsafety , bf. abi , & bf. decl ) )
1686
+ let bare_fn_ty = ty_of_bare_fn ( this,
1687
+ bf. unsafety ,
1688
+ bf. abi ,
1689
+ & bf. decl ) ;
1690
+
1691
+ // Find any late-bound regions declared in return type that do
1692
+ // not appear in the arguments. These are not wellformed.
1693
+ //
1694
+ // Example:
1695
+ //
1696
+ // for<'a> fn() -> &'a str <-- 'a is bad
1697
+ // for<'a> fn(&'a String) -> &'a str <-- 'a is ok
1698
+ //
1699
+ // Note that we do this check **here** and not in
1700
+ // `ty_of_bare_fn` because the latter is also used to make
1701
+ // the types for fn items, and we do not want to issue a
1702
+ // warning then. (Once we fix #32330, the regions we are
1703
+ // checking for here would be considered early bound
1704
+ // anyway.)
1705
+ let inputs = bare_fn_ty. sig . inputs ( ) ;
1706
+ let late_bound_in_args = this. tcx ( ) . collect_late_bound_regions ( & inputs) ;
1707
+ let output = bare_fn_ty. sig . output ( ) ;
1708
+ let late_bound_in_ret = this. tcx ( ) . collect_late_bound_regions ( & output) ;
1709
+ for br in late_bound_in_ret. difference ( & late_bound_in_args) {
1710
+ let br_name = match * br {
1711
+ ty:: BrNamed ( _, name) => name,
1712
+ _ => {
1713
+ this. tcx ( ) . sess . span_bug (
1714
+ bf. decl . output . span ( ) ,
1715
+ & format ! ( "anonymous bound region {:?} in \
1716
+ return but not args",
1717
+ br) ) ;
1718
+ }
1719
+ } ;
1720
+ this. tcx ( ) . sess . add_lint (
1721
+ lint:: builtin:: HR_LIFETIME_IN_ASSOC_TYPE ,
1722
+ ast_ty. id ,
1723
+ bf. decl . output . span ( ) ,
1724
+ format ! ( "return type references lifetime `{}`, \
1725
+ which does not appear in the argument types",
1726
+ br_name) ) ;
1727
+ }
1728
+
1729
+ tcx. mk_fn_ptr ( bare_fn_ty)
1676
1730
}
1677
1731
hir:: TyPolyTraitRef ( ref bounds) => {
1678
1732
conv_ty_poly_trait_ref ( this, rscope, ast_ty. span , bounds)
@@ -1700,6 +1754,7 @@ pub fn ast_ty_to_ty<'tcx>(this: &AstConv<'tcx>,
1700
1754
PathParamMode :: Explicit ,
1701
1755
& def,
1702
1756
opt_self_ty,
1757
+ ast_ty. id ,
1703
1758
& path. segments [ ..base_ty_end] ,
1704
1759
& path. segments [ base_ty_end..] ) ;
1705
1760
@@ -1795,8 +1850,11 @@ pub fn ty_of_method<'tcx>(this: &AstConv<'tcx>,
1795
1850
( bare_fn_ty, optional_explicit_self_category. unwrap ( ) )
1796
1851
}
1797
1852
1798
- pub fn ty_of_bare_fn < ' tcx > ( this : & AstConv < ' tcx > , unsafety : hir:: Unsafety , abi : abi:: Abi ,
1799
- decl : & hir:: FnDecl ) -> ty:: BareFnTy < ' tcx > {
1853
+ pub fn ty_of_bare_fn < ' tcx > ( this : & AstConv < ' tcx > ,
1854
+ unsafety : hir:: Unsafety ,
1855
+ abi : abi:: Abi ,
1856
+ decl : & hir:: FnDecl )
1857
+ -> ty:: BareFnTy < ' tcx > {
1800
1858
let ( bare_fn_ty, _) = ty_of_method_or_bare_fn ( this, unsafety, abi, None , decl) ;
1801
1859
bare_fn_ty
1802
1860
}
0 commit comments