From de959afab5042c44d78703050353cb5b20d442d2 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Fri, 3 Nov 2017 17:30:14 -0700 Subject: Handle anon lifetime arg being returned with named lifetime return type When there's a lifetime mismatch between an argument with an anonymous lifetime being returned in a method with a return type that has a named lifetime, show specialized lifetime error pointing at argument with a hint to give it an explicit lifetime matching the return type. ``` error[E0621]: explicit lifetime required in the type of `other` --> file2.rs:21:21 | 17 | fn bar(&self, other: Foo) -> Foo<'a> { | ----- consider changing the type of `other` to `Foo<'a>` ... 21 | other | ^^^^^ lifetime `'a` required ``` Follow up to #44124 and #42669. --- ...ex3-both-anon-regions-earlybound-regions.stderr | 6 ++--- .../ui/lifetime-errors/ex4-anon-named-regions.rs | 30 ++++++++++++++++++++++ .../lifetime-errors/ex4-anon-named-regions.stderr | 11 ++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/lifetime-errors/ex4-anon-named-regions.rs create mode 100644 src/test/ui/lifetime-errors/ex4-anon-named-regions.stderr (limited to 'src/test/ui/lifetime-errors') diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-earlybound-regions.stderr b/src/test/ui/lifetime-errors/ex3-both-anon-regions-earlybound-regions.stderr index 58f2cb94cec..6c5ed672604 100644 --- a/src/test/ui/lifetime-errors/ex3-both-anon-regions-earlybound-regions.stderr +++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-earlybound-regions.stderr @@ -1,11 +1,11 @@ -error[E0623]: lifetime mismatch +error[E0621]: explicit lifetime required in the type of `y` --> $DIR/ex3-both-anon-regions-earlybound-regions.rs:17:12 | 13 | fn baz<'a, 'b, T>(x: &mut Vec<&'a T>, y: &T) - | ----- -- these two types are declared with different lifetimes... + | - consider changing the type of `y` to `&'a T` ... 17 | x.push(y); - | ^ ...but data from `y` flows into `x` here + | ^ lifetime `'a` required error: aborting due to previous error diff --git a/src/test/ui/lifetime-errors/ex4-anon-named-regions.rs b/src/test/ui/lifetime-errors/ex4-anon-named-regions.rs new file mode 100644 index 00000000000..55752f753ef --- /dev/null +++ b/src/test/ui/lifetime-errors/ex4-anon-named-regions.rs @@ -0,0 +1,30 @@ +// 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. + +#[derive(Clone)] +enum Foo<'a> { + Bar(&'a str), +} + +impl<'a> Foo<'a> { + fn bar(&self, other: Foo) -> Foo<'a> { + match *self { + Foo::Bar(s) => { + if s == "test" { + other + } else { + self.clone() + } + } + } + } +} + +fn main() { } diff --git a/src/test/ui/lifetime-errors/ex4-anon-named-regions.stderr b/src/test/ui/lifetime-errors/ex4-anon-named-regions.stderr new file mode 100644 index 00000000000..4a127bbc551 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex4-anon-named-regions.stderr @@ -0,0 +1,11 @@ +error[E0621]: explicit lifetime required in the type of `other` + --> $DIR/ex4-anon-named-regions.rs:21:21 + | +17 | fn bar(&self, other: Foo) -> Foo<'a> { + | ----- consider changing the type of `other` to `Foo<'a>` +... +21 | other + | ^^^^^ lifetime `'a` required + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5