@@ -646,7 +646,24 @@ struct DiagnosticMetadata<'ast> {
646
646
}
647
647
648
648
#[ derive( Debug ) ]
649
- struct ResolvedNestedElisionTarget {
649
+ enum ResolvedElisionTarget {
650
+ /// Elision in `&u8` -> `&'_ u8`
651
+ TopLevel ( NodeId ) ,
652
+ /// Elision in `Foo` -> `Foo<'_>`
653
+ Nested ( NestedResolvedElisionTarget ) ,
654
+ }
655
+
656
+ impl ResolvedElisionTarget {
657
+ fn node_id ( & self ) -> NodeId {
658
+ match * self {
659
+ Self :: TopLevel ( n) => n,
660
+ Self :: Nested ( NestedResolvedElisionTarget { segment_id, .. } ) => segment_id,
661
+ }
662
+ }
663
+ }
664
+
665
+ #[ derive( Debug ) ]
666
+ struct NestedResolvedElisionTarget {
650
667
segment_id : NodeId ,
651
668
elided_lifetime_span : Span ,
652
669
diagnostic : lint:: BuiltinLintDiagnostics ,
@@ -694,7 +711,7 @@ struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
694
711
lifetime_uses : FxHashMap < LocalDefId , LifetimeUseSet > ,
695
712
696
713
/// Track which types participated in lifetime elision
697
- resolved_lifetime_elisions : Vec < ResolvedNestedElisionTarget > ,
714
+ resolved_lifetime_elisions : Vec < ResolvedElisionTarget > ,
698
715
}
699
716
700
717
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
@@ -1745,6 +1762,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1745
1762
LifetimeElisionCandidate :: Ignore ,
1746
1763
) ;
1747
1764
self . resolve_anonymous_lifetime ( & lt, true ) ;
1765
+
1766
+ self . resolved_lifetime_elisions . push ( ResolvedElisionTarget :: TopLevel ( anchor_id) ) ;
1748
1767
}
1749
1768
1750
1769
#[ instrument( level = "debug" , skip( self ) ) ]
@@ -1957,16 +1976,18 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1957
1976
}
1958
1977
1959
1978
if should_lint {
1960
- self . resolved_lifetime_elisions . push ( ResolvedNestedElisionTarget {
1961
- segment_id,
1962
- elided_lifetime_span,
1963
- diagnostic : lint:: BuiltinLintDiagnostics :: ElidedLifetimesInPaths (
1964
- expected_lifetimes,
1965
- path_span,
1966
- !segment. has_generic_args ,
1979
+ self . resolved_lifetime_elisions . push ( ResolvedElisionTarget :: Nested (
1980
+ NestedResolvedElisionTarget {
1981
+ segment_id,
1967
1982
elided_lifetime_span,
1968
- ) ,
1969
- } ) ;
1983
+ diagnostic : lint:: BuiltinLintDiagnostics :: ElidedLifetimesInPaths (
1984
+ expected_lifetimes,
1985
+ path_span,
1986
+ !segment. has_generic_args ,
1987
+ elided_lifetime_span,
1988
+ ) ,
1989
+ } ,
1990
+ ) ) ;
1970
1991
}
1971
1992
}
1972
1993
}
@@ -2028,22 +2049,80 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
2028
2049
2029
2050
let elision_failures =
2030
2051
replace ( & mut self . diagnostic_metadata . current_elision_failures , outer_failures) ;
2031
- if !elision_failures. is_empty ( ) {
2032
- let Err ( failure_info) = elision_lifetime else { bug ! ( ) } ;
2033
- self . report_missing_lifetime_specifiers ( elision_failures, Some ( failure_info) ) ;
2034
- }
2052
+
2053
+ let elision_lifetime = match ( elision_failures. is_empty ( ) , elision_lifetime) {
2054
+ ( true , Ok ( lifetime) ) => Some ( lifetime) ,
2055
+
2056
+ ( true , Err ( _) ) => None ,
2057
+
2058
+ ( false , Ok ( _) ) => bug ! ( ) ,
2059
+
2060
+ ( false , Err ( failure_info) ) => {
2061
+ self . report_missing_lifetime_specifiers ( elision_failures, Some ( failure_info) ) ;
2062
+ None
2063
+ }
2064
+ } ;
2065
+
2066
+ // We've recorded all elisions that occurred in the params and
2067
+ // outputs, categorized by top-level or nested.
2068
+ //
2069
+ // Our primary lint case is when an output lifetime is tied to
2070
+ // an input lifetime. In that case, we want to warn about any
2071
+ // parameters that had a nested elision.
2072
+ //
2073
+ // The secondary case is for nested elisions that are not part
2074
+ // of the tied lifetime relationship.
2035
2075
2036
2076
let output_resolved_lifetime_elisions =
2037
2077
replace ( & mut self . resolved_lifetime_elisions , outer_resolved_lifetime_elisions) ;
2038
2078
2039
- let resolved_lifetime_elisions =
2040
- param_resolved_lifetime_elisions. into_iter ( ) . chain ( output_resolved_lifetime_elisions) ;
2079
+ match ( output_resolved_lifetime_elisions. is_empty ( ) , elision_lifetime) {
2080
+ ( true , _) | ( _, None ) => {
2081
+ // Treat all parameters as untied
2082
+ self . report_elided_lifetimes_in_paths (
2083
+ param_resolved_lifetime_elisions,
2084
+ lint:: builtin:: ELIDED_LIFETIMES_IN_PATHS_UNTIED ,
2085
+ ) ;
2086
+ }
2087
+ ( false , Some ( elision_lifetime) ) => {
2088
+ let ( primary, secondary) : ( Vec < _ > , Vec < _ > ) =
2089
+ param_resolved_lifetime_elisions. into_iter ( ) . partition ( |re| {
2090
+ // Recover the `NodeId` of an elided lifetime
2091
+ let lvl1 = & self . r . lifetimes_res_map [ & re. node_id ( ) ] ;
2092
+ let lvl2 = match lvl1 {
2093
+ LifetimeRes :: ElidedAnchor { start, .. } => {
2094
+ & self . r . lifetimes_res_map [ & start]
2095
+ }
2096
+ o => o,
2097
+ } ;
2098
+
2099
+ lvl2 == & elision_lifetime
2100
+ } ) ;
2101
+
2102
+ self . report_elided_lifetimes_in_paths (
2103
+ primary. into_iter ( ) . chain ( output_resolved_lifetime_elisions) ,
2104
+ lint:: builtin:: ELIDED_LIFETIMES_IN_PATHS_TIED ,
2105
+ ) ;
2106
+ self . report_elided_lifetimes_in_paths (
2107
+ secondary,
2108
+ lint:: builtin:: ELIDED_LIFETIMES_IN_PATHS_UNTIED ,
2109
+ ) ;
2110
+ }
2111
+ }
2112
+ }
2113
+
2114
+ fn report_elided_lifetimes_in_paths (
2115
+ & mut self ,
2116
+ resolved_elisions : impl IntoIterator < Item = ResolvedElisionTarget > ,
2117
+ lint : & ' static lint:: Lint ,
2118
+ ) {
2119
+ for re in resolved_elisions {
2120
+ let ResolvedElisionTarget :: Nested ( d) = re else { continue } ;
2041
2121
2042
- for re in resolved_lifetime_elisions {
2043
- let ResolvedNestedElisionTarget { segment_id, elided_lifetime_span, diagnostic } = re;
2122
+ let NestedResolvedElisionTarget { segment_id, elided_lifetime_span, diagnostic } = d;
2044
2123
2045
2124
self . r . lint_buffer . buffer_lint_with_diagnostic (
2046
- lint:: builtin :: ELIDED_LIFETIMES_IN_PATHS ,
2125
+ lint,
2047
2126
segment_id,
2048
2127
elided_lifetime_span,
2049
2128
"hidden lifetime parameters in types are deprecated" ,
0 commit comments