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