From e1e52eb5a0156839a71c0ffc5424e2160271170e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 17 Oct 2018 21:07:31 -0700 Subject: [PATCH 1/3] Suggest appropriate syntax on missing lifetime specifier in return type Suggest using `'static` when a lifetime is missing in the return type with a structured suggestion instead of a note. --- src/librustc/hir/lowering.rs | 2 +- src/librustc/middle/resolve_lifetime.rs | 55 ++++++++++++++++--- ...nd-lifetime-in-binding-only.elision.stderr | 6 +- ...und-lifetime-in-return-only.elision.stderr | 6 +- src/test/ui/foreign-fn-return-lifetime.fixed | 18 ++++++ src/test/ui/foreign-fn-return-lifetime.rs | 6 +- src/test/ui/foreign-fn-return-lifetime.stderr | 10 ++-- src/test/ui/issues/issue-13497.stderr | 6 +- src/test/ui/issues/issue-26638.stderr | 12 ++-- ...urn-type-requires-explicit-lifetime.stderr | 24 +++++--- .../lifetime-elision-return-type-trait.rs | 10 ++++ .../lifetime-elision-return-type-trait.stderr | 19 +++++++ .../underscore-lifetime-binders.stderr | 6 +- 13 files changed, 146 insertions(+), 34 deletions(-) create mode 100644 src/test/ui/foreign-fn-return-lifetime.fixed create mode 100644 src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs create mode 100644 src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index cce872927b147..04a2308cead34 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -1152,7 +1152,7 @@ impl<'a> LoweringContext<'a> { TyKind::Slice(ref ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)), TyKind::Ptr(ref mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)), TyKind::Rptr(ref region, ref mt) => { - let span = t.span.shrink_to_lo(); + let span = self.sess.source_map().next_point(t.span.shrink_to_lo()); let lifetime = match *region { Some(ref lt) => self.lower_lifetime(lt), None => self.elided_ref_lifetime(span), diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 98e80d333c1c8..86db74b78b983 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -2238,7 +2238,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { if let Some(params) = error { if lifetime_refs.len() == 1 { - self.report_elision_failure(&mut err, params); + self.report_elision_failure(&mut err, params, span); } } @@ -2249,6 +2249,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { &mut self, db: &mut DiagnosticBuilder<'_>, params: &[ElisionFailureInfo], + span: Span, ) { let mut m = String::new(); let len = params.len(); @@ -2304,7 +2305,29 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { "this function's return type contains a borrowed value, but \ there is no value for it to be borrowed from" ); - help!(db, "consider giving it a 'static lifetime"); + let msg = "consider giving it a 'static lifetime"; + match self.tcx.sess.source_map().span_to_snippet(span) { + Ok(ref snippet) if snippet == "&" => db.span_suggestion_with_applicability( + span, + msg, + "&'static ".to_owned(), + Applicability::MachineApplicable, + ), + Ok(ref snippet) + if snippet == "'_" => db.span_suggestion_with_applicability( + span, + msg, + "'static".to_owned(), + Applicability::MachineApplicable, + ), + Ok(ref snippet) => db.span_suggestion_with_applicability( + span, + msg, + format!("{} + 'static", snippet), + Applicability::MaybeIncorrect, + ), + Err(_) => db.help(msg), + }; } else if elided_len == 0 { help!( db, @@ -2312,11 +2335,29 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { an elided lifetime, but the lifetime cannot be derived from \ the arguments" ); - help!( - db, - "consider giving it an explicit bounded or 'static \ - lifetime" - ); + let msg = "consider giving it an explicit bounded or 'static lifetime"; + match self.tcx.sess.source_map().span_to_snippet(span) { + Ok(ref snippet) if snippet == "&" => db.span_suggestion_with_applicability( + span, + msg, + "&'static ".to_owned(), + Applicability::MachineApplicable, + ), + Ok(ref snippet) + if snippet == "'_" => db.span_suggestion_with_applicability( + span, + msg, + "'static".to_owned(), + Applicability::MachineApplicable, + ), + Ok(ref snippet) => db.span_suggestion_with_applicability( + span, + msg, + format!("{} + 'static", snippet), + Applicability::MaybeIncorrect, + ), + Err(_) => db.help(msg), + }; } else if elided_len == 1 { help!( db, diff --git a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr index 0a12aa76a785b..4336aaf71ba02 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr @@ -2,10 +2,12 @@ error[E0106]: missing lifetime specifier --> $DIR/bound-lifetime-in-binding-only.rs:62:23 | LL | fn elision &i32>() { - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime error: aborting due to previous error diff --git a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr index 8fefdfd4d19ef..fbc4df54225d5 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr @@ -2,10 +2,12 @@ error[E0106]: missing lifetime specifier --> $DIR/bound-lifetime-in-return-only.rs:44:23 | LL | fn elision(_: fn() -> &i32) { - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime error: aborting due to previous error diff --git a/src/test/ui/foreign-fn-return-lifetime.fixed b/src/test/ui/foreign-fn-return-lifetime.fixed new file mode 100644 index 0000000000000..9fc35eae7052f --- /dev/null +++ b/src/test/ui/foreign-fn-return-lifetime.fixed @@ -0,0 +1,18 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-rustfix + +extern "C" { + pub fn g(_: &u8) -> &u8; // OK + pub fn f() -> &'static u8; //~ ERROR missing lifetime specifier +} + +fn main() {} diff --git a/src/test/ui/foreign-fn-return-lifetime.rs b/src/test/ui/foreign-fn-return-lifetime.rs index da77066150cc7..941e7e05a3635 100644 --- a/src/test/ui/foreign-fn-return-lifetime.rs +++ b/src/test/ui/foreign-fn-return-lifetime.rs @@ -8,9 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// run-rustfix + extern "C" { - fn g(_: &u8) -> &u8; // OK - fn f() -> &u8; //~ ERROR missing lifetime specifier + pub fn g(_: &u8) -> &u8; // OK + pub fn f() -> &u8; //~ ERROR missing lifetime specifier } fn main() {} diff --git a/src/test/ui/foreign-fn-return-lifetime.stderr b/src/test/ui/foreign-fn-return-lifetime.stderr index ea15897b3d694..4e52d6044c4e9 100644 --- a/src/test/ui/foreign-fn-return-lifetime.stderr +++ b/src/test/ui/foreign-fn-return-lifetime.stderr @@ -1,11 +1,13 @@ error[E0106]: missing lifetime specifier - --> $DIR/foreign-fn-return-lifetime.rs:13:15 + --> $DIR/foreign-fn-return-lifetime.rs:15:19 | -LL | fn f() -> &u8; //~ ERROR missing lifetime specifier - | ^ expected lifetime parameter +LL | pub fn f() -> &u8; //~ ERROR missing lifetime specifier + | ^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime error: aborting due to previous error diff --git a/src/test/ui/issues/issue-13497.stderr b/src/test/ui/issues/issue-13497.stderr index ab6d041bd48d7..88b6a831d7e41 100644 --- a/src/test/ui/issues/issue-13497.stderr +++ b/src/test/ui/issues/issue-13497.stderr @@ -2,10 +2,12 @@ error[E0106]: missing lifetime specifier --> $DIR/issue-13497.rs:12:5 | LL | &str //~ ERROR missing lifetime specifier - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime error: aborting due to previous error diff --git a/src/test/ui/issues/issue-26638.stderr b/src/test/ui/issues/issue-26638.stderr index cf6fcd9f01cc8..e4464b2dd313d 100644 --- a/src/test/ui/issues/issue-26638.stderr +++ b/src/test/ui/issues/issue-26638.stderr @@ -10,19 +10,23 @@ error[E0106]: missing lifetime specifier --> $DIR/issue-26638.rs:14:40 | LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() } - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments - = help: consider giving it an explicit bounded or 'static lifetime error[E0106]: missing lifetime specifier --> $DIR/issue-26638.rs:17:22 | LL | fn parse_type_3() -> &str { unimplemented!() } - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime error: aborting due to 3 previous errors diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr b/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr index 30cff86ed1d40..9962dbc9812bd 100644 --- a/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr @@ -2,10 +2,12 @@ error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:12:11 | LL | fn f() -> &isize { //~ ERROR missing lifetime specifier - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:17:33 @@ -27,28 +29,34 @@ error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:31:20 | LL | fn i(_x: isize) -> &isize { //~ ERROR missing lifetime specifier - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments - = help: consider giving it an explicit bounded or 'static lifetime error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:44:24 | LL | fn j(_x: StaticStr) -> &isize { //~ ERROR missing lifetime specifier - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments - = help: consider giving it an explicit bounded or 'static lifetime error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:50:49 | LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize { - | ^ expected lifetime parameter + | ^ + | | + | expected lifetime parameter + | help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments - = help: consider giving it an explicit bounded or 'static lifetime error: aborting due to 6 previous errors diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs new file mode 100644 index 0000000000000..6b6e263b7fd1a --- /dev/null +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs @@ -0,0 +1,10 @@ +trait Future { + type Item; + type Error; +} + +use std::error::Error; + +fn foo() -> impl Future> { + Ok(()) +} diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr new file mode 100644 index 0000000000000..426941b949daa --- /dev/null +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr @@ -0,0 +1,19 @@ +error[E0601]: `main` function not found in crate `lifetime_elision_return_type_trait` + | + = note: consider adding a `main` function to `$DIR/lifetime-elision-return-type-trait.rs` + +error[E0106]: missing lifetime specifier + --> $DIR/lifetime-elision-return-type-trait.rs:8:44 + | +LL | fn foo() -> impl Future> { + | ^^^^^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `Error + 'static` + | + = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from + +error: aborting due to 2 previous errors + +Some errors occurred: E0106, E0601. +For more information about an error, try `rustc --explain E0106`. diff --git a/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr b/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr index fc9f3e642d402..b0da67a7c346e 100644 --- a/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr +++ b/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr @@ -26,10 +26,12 @@ error[E0106]: missing lifetime specifier --> $DIR/underscore-lifetime-binders.rs:24:29 | LL | fn meh() -> Box Meh<'_>> //~ ERROR cannot be used here - | ^^ expected lifetime parameter + | ^^ + | | + | expected lifetime parameter + | help: consider giving it a 'static lifetime: `'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime error[E0106]: missing lifetime specifier --> $DIR/underscore-lifetime-binders.rs:30:35 From d0bd69a2d559177724df95463a79af9c2a2ad63d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 18 Oct 2018 13:05:38 -0700 Subject: [PATCH 2/3] review comments --- src/librustc/middle/resolve_lifetime.rs | 99 ++++++++----------- ...nd-lifetime-in-binding-only.elision.stderr | 5 +- ...und-lifetime-in-return-only.elision.stderr | 5 +- src/test/ui/foreign-fn-return-lifetime.stderr | 5 +- src/test/ui/issues/issue-13497.stderr | 5 +- src/test/ui/issues/issue-26638.stderr | 10 +- ...urn-type-requires-explicit-lifetime.stderr | 20 +--- .../lifetime-elision-return-type-trait.stderr | 5 +- .../underscore-lifetime-binders.stderr | 5 +- 9 files changed, 54 insertions(+), 105 deletions(-) diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 86db74b78b983..7547e8039158e 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -2235,22 +2235,46 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { }; let mut err = report_missing_lifetime_specifiers(self.tcx.sess, span, lifetime_refs.len()); + let mut add_label = true; if let Some(params) = error { if lifetime_refs.len() == 1 { - self.report_elision_failure(&mut err, params, span); + add_label = add_label && self.report_elision_failure(&mut err, params, span); } } + if add_label { + add_missing_lifetime_specifiers_label(&mut err, span, lifetime_refs.len()); + } err.emit(); } + fn suggest_lifetime(&self, db: &mut DiagnosticBuilder<'_>, span: Span, msg: &str) -> bool { + match self.tcx.sess.source_map().span_to_snippet(span) { + Ok(ref snippet) => { + let (sugg, applicability) = if &snippet[..] == "&" { + ("&'static ".to_owned(), Applicability::MachineApplicable) + } else if snippet == "'_" { + ("'static".to_owned(), Applicability::MachineApplicable) + } else { + (format!("{} + 'static", snippet), Applicability::MaybeIncorrect) + }; + db.span_suggestion_with_applicability(span, msg, sugg, applicability); + false + } + Err(_) => { + db.help(msg); + true + } + } + } + fn report_elision_failure( &mut self, db: &mut DiagnosticBuilder<'_>, params: &[ElisionFailureInfo], span: Span, - ) { + ) -> bool { let mut m = String::new(); let len = params.len(); @@ -2305,29 +2329,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { "this function's return type contains a borrowed value, but \ there is no value for it to be borrowed from" ); - let msg = "consider giving it a 'static lifetime"; - match self.tcx.sess.source_map().span_to_snippet(span) { - Ok(ref snippet) if snippet == "&" => db.span_suggestion_with_applicability( - span, - msg, - "&'static ".to_owned(), - Applicability::MachineApplicable, - ), - Ok(ref snippet) - if snippet == "'_" => db.span_suggestion_with_applicability( - span, - msg, - "'static".to_owned(), - Applicability::MachineApplicable, - ), - Ok(ref snippet) => db.span_suggestion_with_applicability( - span, - msg, - format!("{} + 'static", snippet), - Applicability::MaybeIncorrect, - ), - Err(_) => db.help(msg), - }; + self.suggest_lifetime(db, span, "consider giving it a 'static lifetime") } else if elided_len == 0 { help!( db, @@ -2336,28 +2338,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { the arguments" ); let msg = "consider giving it an explicit bounded or 'static lifetime"; - match self.tcx.sess.source_map().span_to_snippet(span) { - Ok(ref snippet) if snippet == "&" => db.span_suggestion_with_applicability( - span, - msg, - "&'static ".to_owned(), - Applicability::MachineApplicable, - ), - Ok(ref snippet) - if snippet == "'_" => db.span_suggestion_with_applicability( - span, - msg, - "'static".to_owned(), - Applicability::MachineApplicable, - ), - Ok(ref snippet) => db.span_suggestion_with_applicability( - span, - msg, - format!("{} + 'static", snippet), - Applicability::MaybeIncorrect, - ), - Err(_) => db.help(msg), - }; + self.suggest_lifetime(db, span, msg) } else if elided_len == 1 { help!( db, @@ -2365,6 +2346,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { the signature does not say which {} it is borrowed from", m ); + true } else { help!( db, @@ -2372,6 +2354,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { the signature does not say whether it is borrowed from {}", m ); + true } } @@ -2785,26 +2768,28 @@ fn insert_late_bound_lifetimes( } } -pub fn report_missing_lifetime_specifiers( +fn report_missing_lifetime_specifiers( sess: &Session, span: Span, count: usize, ) -> DiagnosticBuilder<'_> { - let mut err = struct_span_err!( + struct_span_err!( sess, span, E0106, "missing lifetime specifier{}", if count > 1 { "s" } else { "" } - ); + ) +} - let msg: Cow<'static, str> = if count > 1 { - format!("expected {} lifetime parameters", count).into() +fn add_missing_lifetime_specifiers_label( + err: &mut DiagnosticBuilder<'_>, + span: Span, + count: usize, +) { + if count > 1 { + err.span_label(span, format!("expected {} lifetime parameters", count)); } else { - "expected lifetime parameter".into() + err.span_label(span, "expected lifetime parameter"); }; - - err.span_label(span, msg); - - err } diff --git a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr index 4336aaf71ba02..6b9d4ebb2987d 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr @@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier --> $DIR/bound-lifetime-in-binding-only.rs:62:23 | LL | fn elision &i32>() { - | ^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `&'static` + | ^ help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from diff --git a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr index fbc4df54225d5..7906f0a30e4eb 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr @@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier --> $DIR/bound-lifetime-in-return-only.rs:44:23 | LL | fn elision(_: fn() -> &i32) { - | ^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `&'static` + | ^ help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from diff --git a/src/test/ui/foreign-fn-return-lifetime.stderr b/src/test/ui/foreign-fn-return-lifetime.stderr index 4e52d6044c4e9..583487656f24d 100644 --- a/src/test/ui/foreign-fn-return-lifetime.stderr +++ b/src/test/ui/foreign-fn-return-lifetime.stderr @@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier --> $DIR/foreign-fn-return-lifetime.rs:15:19 | LL | pub fn f() -> &u8; //~ ERROR missing lifetime specifier - | ^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `&'static` + | ^ help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from diff --git a/src/test/ui/issues/issue-13497.stderr b/src/test/ui/issues/issue-13497.stderr index 88b6a831d7e41..e592452b89944 100644 --- a/src/test/ui/issues/issue-13497.stderr +++ b/src/test/ui/issues/issue-13497.stderr @@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier --> $DIR/issue-13497.rs:12:5 | LL | &str //~ ERROR missing lifetime specifier - | ^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `&'static` + | ^ help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from diff --git a/src/test/ui/issues/issue-26638.stderr b/src/test/ui/issues/issue-26638.stderr index e4464b2dd313d..0ac6316f0dcf8 100644 --- a/src/test/ui/issues/issue-26638.stderr +++ b/src/test/ui/issues/issue-26638.stderr @@ -10,10 +10,7 @@ error[E0106]: missing lifetime specifier --> $DIR/issue-26638.rs:14:40 | LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() } - | ^ - | | - | expected lifetime parameter - | help: consider giving it an explicit bounded or 'static lifetime: `&'static` + | ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments @@ -21,10 +18,7 @@ error[E0106]: missing lifetime specifier --> $DIR/issue-26638.rs:17:22 | LL | fn parse_type_3() -> &str { unimplemented!() } - | ^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `&'static` + | ^ help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr b/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr index 9962dbc9812bd..4c7a1b5ea9ff0 100644 --- a/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr @@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:12:11 | LL | fn f() -> &isize { //~ ERROR missing lifetime specifier - | ^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `&'static` + | ^ help: consider giving it a 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from @@ -29,10 +26,7 @@ error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:31:20 | LL | fn i(_x: isize) -> &isize { //~ ERROR missing lifetime specifier - | ^ - | | - | expected lifetime parameter - | help: consider giving it an explicit bounded or 'static lifetime: `&'static` + | ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments @@ -40,10 +34,7 @@ error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:44:24 | LL | fn j(_x: StaticStr) -> &isize { //~ ERROR missing lifetime specifier - | ^ - | | - | expected lifetime parameter - | help: consider giving it an explicit bounded or 'static lifetime: `&'static` + | ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments @@ -51,10 +42,7 @@ error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:50:49 | LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize { - | ^ - | | - | expected lifetime parameter - | help: consider giving it an explicit bounded or 'static lifetime: `&'static` + | ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr index 426941b949daa..ab429b9df06cd 100644 --- a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr @@ -6,10 +6,7 @@ error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-trait.rs:8:44 | LL | fn foo() -> impl Future> { - | ^^^^^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `Error + 'static` + | ^^^^^ help: consider giving it a 'static lifetime: `Error + 'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from diff --git a/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr b/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr index b0da67a7c346e..4319843291a20 100644 --- a/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr +++ b/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr @@ -26,10 +26,7 @@ error[E0106]: missing lifetime specifier --> $DIR/underscore-lifetime-binders.rs:24:29 | LL | fn meh() -> Box Meh<'_>> //~ ERROR cannot be used here - | ^^ - | | - | expected lifetime parameter - | help: consider giving it a 'static lifetime: `'static` + | ^^ help: consider giving it a 'static lifetime: `'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from From dd91c8fc5a7c097c508cdffdd236adc64dae01c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Fri, 19 Oct 2018 10:43:43 -0700 Subject: [PATCH 3/3] [review comments] modify test and clean up code Co-Authored-By: estebank --- src/librustc/middle/resolve_lifetime.rs | 2 +- .../ui/lifetimes/lifetime-elision-return-type-trait.rs | 2 ++ .../lifetimes/lifetime-elision-return-type-trait.stderr | 9 ++------- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 7547e8039158e..361abb1689619 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -2252,7 +2252,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { fn suggest_lifetime(&self, db: &mut DiagnosticBuilder<'_>, span: Span, msg: &str) -> bool { match self.tcx.sess.source_map().span_to_snippet(span) { Ok(ref snippet) => { - let (sugg, applicability) = if &snippet[..] == "&" { + let (sugg, applicability) = if snippet == "&" { ("&'static ".to_owned(), Applicability::MachineApplicable) } else if snippet == "'_" { ("'static".to_owned(), Applicability::MachineApplicable) diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs index 6b6e263b7fd1a..eb959bfbcb533 100644 --- a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.rs @@ -8,3 +8,5 @@ use std::error::Error; fn foo() -> impl Future> { Ok(()) } + +fn main() {} diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr index ab429b9df06cd..b2a3d9a94361f 100644 --- a/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr @@ -1,7 +1,3 @@ -error[E0601]: `main` function not found in crate `lifetime_elision_return_type_trait` - | - = note: consider adding a `main` function to `$DIR/lifetime-elision-return-type-trait.rs` - error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-trait.rs:8:44 | @@ -10,7 +6,6 @@ LL | fn foo() -> impl Future> { | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -error: aborting due to 2 previous errors +error: aborting due to previous error -Some errors occurred: E0106, E0601. -For more information about an error, try `rustc --explain E0106`. +For more information about this error, try `rustc --explain E0106`.