@@ -7,8 +7,6 @@ use rustc_hir as hir;
77use rustc_hir:: def:: { DefKind , PartialRes , Res } ;
88use rustc_hir:: def_id:: DefId ;
99use rustc_hir:: GenericArg ;
10- use rustc_session:: lint:: builtin:: ELIDED_LIFETIMES_IN_PATHS ;
11- use rustc_session:: lint:: BuiltinLintDiagnostics ;
1210use rustc_span:: symbol:: Ident ;
1311use rustc_span:: { BytePos , Span , DUMMY_SP } ;
1412
@@ -270,12 +268,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
270268
271269 let has_lifetimes =
272270 generic_args. args . iter ( ) . any ( |arg| matches ! ( arg, GenericArg :: Lifetime ( _) ) ) ;
273- if !generic_args. parenthesized && !has_lifetimes {
271+ if !generic_args. parenthesized && !has_lifetimes && expected_lifetimes > 0 {
274272 // Note: these spans are used for diagnostics when they can't be inferred.
275273 // See rustc_resolve::late::lifetimes::LifetimeContext::add_missing_lifetime_specifiers_label
276274 let elided_lifetime_span = if generic_args. span . is_empty ( ) {
277275 // If there are no brackets, use the identifier span.
278- segment . ident . span
276+ path_span
279277 } else if generic_args. is_empty ( ) {
280278 // If there are brackets, but not generic arguments, then use the opening bracket
281279 generic_args. span . with_hi ( generic_args. span . lo ( ) + BytePos ( 1 ) )
@@ -284,67 +282,47 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
284282 generic_args. span . with_lo ( generic_args. span . lo ( ) + BytePos ( 1 ) ) . shrink_to_lo ( )
285283 } ;
286284 generic_args. args = self
287- . elided_path_lifetimes ( elided_lifetime_span, expected_lifetimes)
285+ . elided_path_lifetimes ( elided_lifetime_span, expected_lifetimes, param_mode )
288286 . map ( GenericArg :: Lifetime )
289287 . chain ( generic_args. args . into_iter ( ) )
290288 . collect ( ) ;
291- if expected_lifetimes > 0 && param_mode == ParamMode :: Explicit {
289+ // In create-parameter mode we error here because we don't want to support
290+ // deprecated impl elision in new features like impl elision and `async fn`,
291+ // both of which work using the `CreateParameter` mode:
292+ //
293+ // impl Foo for std::cell::Ref<u32> // note lack of '_
294+ // async fn foo(_: std::cell::Ref<u32>) { ... }
295+ if let ( ParamMode :: Explicit , AnonymousLifetimeMode :: CreateParameter ) =
296+ ( param_mode, self . anonymous_lifetime_mode )
297+ {
292298 let anon_lt_suggestion = vec ! [ "'_" ; expected_lifetimes] . join ( ", " ) ;
293299 let no_non_lt_args = generic_args. args . len ( ) == expected_lifetimes;
294300 let no_bindings = generic_args. bindings . is_empty ( ) ;
295- let ( incl_angl_brckt, insertion_sp , suggestion) = if no_non_lt_args && no_bindings {
301+ let ( incl_angl_brckt, suggestion) = if no_non_lt_args && no_bindings {
296302 // If there are no generic args, our suggestion can include the angle brackets.
297- ( true , path_span . shrink_to_hi ( ) , format ! ( "<{}>" , anon_lt_suggestion) )
303+ ( true , format ! ( "<{}>" , anon_lt_suggestion) )
298304 } else {
299305 // Otherwise we'll insert a `'_, ` right after the opening bracket.
300- let span = generic_args
301- . span
302- . with_lo ( generic_args. span . lo ( ) + BytePos ( 1 ) )
303- . shrink_to_lo ( ) ;
304- ( false , span, format ! ( "{}, " , anon_lt_suggestion) )
306+ ( false , format ! ( "{}, " , anon_lt_suggestion) )
305307 } ;
306- match self . anonymous_lifetime_mode {
307- // In create-parameter mode we error here because we don't want to support
308- // deprecated impl elision in new features like impl elision and `async fn`,
309- // both of which work using the `CreateParameter` mode:
310- //
311- // impl Foo for std::cell::Ref<u32> // note lack of '_
312- // async fn foo(_: std::cell::Ref<u32>) { ... }
313- AnonymousLifetimeMode :: CreateParameter => {
314- let mut err = struct_span_err ! (
315- self . sess,
316- path_span,
317- E0726 ,
318- "implicit elided lifetime not allowed here"
319- ) ;
320- rustc_errors:: add_elided_lifetime_in_path_suggestion (
321- & self . sess . source_map ( ) ,
322- & mut err,
323- expected_lifetimes,
324- path_span,
325- incl_angl_brckt,
326- insertion_sp,
327- suggestion,
328- ) ;
329- err. note ( "assuming a `'static` lifetime..." ) ;
330- err. emit ( ) ;
331- }
332- AnonymousLifetimeMode :: PassThrough | AnonymousLifetimeMode :: ReportError => {
333- self . resolver . lint_buffer ( ) . buffer_lint_with_diagnostic (
334- ELIDED_LIFETIMES_IN_PATHS ,
335- CRATE_NODE_ID ,
336- path_span,
337- "hidden lifetime parameters in types are deprecated" ,
338- BuiltinLintDiagnostics :: ElidedLifetimesInPaths (
339- expected_lifetimes,
340- path_span,
341- incl_angl_brckt,
342- insertion_sp,
343- suggestion,
344- ) ,
345- ) ;
346- }
347- }
308+ let insertion_sp = elided_lifetime_span. shrink_to_hi ( ) ;
309+ let mut err = struct_span_err ! (
310+ self . sess,
311+ path_span,
312+ E0726 ,
313+ "implicit elided lifetime not allowed here"
314+ ) ;
315+ rustc_errors:: add_elided_lifetime_in_path_suggestion (
316+ & self . sess . source_map ( ) ,
317+ & mut err,
318+ expected_lifetimes,
319+ path_span,
320+ incl_angl_brckt,
321+ insertion_sp,
322+ suggestion,
323+ ) ;
324+ err. note ( "assuming a `'static` lifetime..." ) ;
325+ err. emit ( ) ;
348326 }
349327 }
350328
0 commit comments