Skip to content

Commit 87b6d38

Browse files
committed
Don't hint to add lifetime on trait impl
Don't provide hint to add lifetime on impl items that implement a trait. ```rust use std::str::FromStr; pub struct Foo<'a> { field: &'a str, } impl<'a> FromStr for Foo<'a> { type Err = (); fn from_str(path: &str) -> Result<Self, ()> { Ok(Foo { field: path }) } } ``` would give the following hint: ```nocode help: consider using an explicit lifetime parameter as shown: fn from_str(path: &'a str) -> Result<Self, ()> --> <anon>:9:5 | 9 | fn from_str(path: &str) -> Result<Self, ()> { | ^ ``` which is never correct, since then there will be a lifetime mismatch between the impl and the trait. Remove this hint for impl items that implement a trait.
1 parent da2ce22 commit 87b6d38

4 files changed

+71
-16
lines changed

src/librustc/infer/error_reporting.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -1052,21 +1052,28 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10521052
match item.node {
10531053
hir::ItemFn(ref fn_decl, unsafety, constness, _, ref gen, _) => {
10541054
Some((fn_decl, gen, unsafety, constness, item.name, item.span))
1055-
},
1056-
_ => None
1055+
}
1056+
_ => None,
10571057
}
10581058
}
10591059
ast_map::NodeImplItem(item) => {
1060-
match item.node {
1061-
hir::ImplItemKind::Method(ref sig, _) => {
1062-
Some((&sig.decl,
1063-
&sig.generics,
1064-
sig.unsafety,
1065-
sig.constness,
1066-
item.name,
1067-
item.span))
1060+
let id = self.tcx.map.get_parent(item.id);
1061+
if let Some(ast_map::NodeItem(parent_scope)) = self.tcx.map.find(id) {
1062+
if let hir::ItemImpl(_, _, _, None, _, _) = parent_scope.node {
1063+
// this impl scope implements a trait, do not recomend
1064+
// using explicit lifetimes (#37363)
1065+
return;
10681066
}
1069-
_ => None,
1067+
}
1068+
if let hir::ImplItemKind::Method(ref sig, _) = item.node {
1069+
Some((&sig.decl,
1070+
&sig.generics,
1071+
sig.unsafety,
1072+
sig.constness,
1073+
item.name,
1074+
item.span))
1075+
} else {
1076+
None
10701077
}
10711078
},
10721079
ast_map::NodeTraitItem(item) => {
@@ -1079,12 +1086,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10791086
item.name,
10801087
item.span))
10811088
}
1082-
_ => None
1089+
_ => None,
10831090
}
10841091
}
1085-
_ => None
1092+
_ => None,
10861093
},
1087-
None => None
1094+
None => None,
10881095
};
10891096
let (fn_decl, generics, unsafety, constness, name, span)
10901097
= node_inner.expect("expect item fn");

src/test/compile-fail/lifetime-inference-give-expl-lifetime-param.rs

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ struct Baz<'x> {
4949

5050
impl<'a> Baz<'a> {
5151
fn baz2<'b>(&self, x: &isize) -> (&'b isize, &'b isize) {
52-
//~^ HELP consider using an explicit lifetime parameter as shown: fn baz2<'b>(&self, x: &'
53-
// FIXME #35038: The above suggestion is different on Linux and Mac.
5452
(self.bar, x) //~ ERROR E0312
5553
//~^ ERROR E0312
5654
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::str::FromStr;
12+
13+
pub struct Foo<'a> {
14+
field: &'a str,
15+
}
16+
17+
impl<'a> Foo<'a> {
18+
fn bar(path: &str) -> Result<Self, ()> {
19+
Ok(Foo { field: path })
20+
}
21+
}
22+
23+
impl<'a> FromStr for Foo<'a> {
24+
type Err = ();
25+
fn from_str(path: &str) -> Result<Self, ()> {
26+
Ok(Foo { field: path })
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: main function not found
2+
3+
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
4+
--> $DIR/consider-using-explicit-lifetime.rs:19:12
5+
|
6+
19 | Ok(Foo { field: path })
7+
| ^^^
8+
9+
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
10+
--> $DIR/consider-using-explicit-lifetime.rs:26:12
11+
|
12+
26 | Ok(Foo { field: path })
13+
| ^^^
14+
|
15+
help: consider using an explicit lifetime parameter as shown: fn from_str(path: &'a str) -> Result<Self, ()>
16+
--> $DIR/consider-using-explicit-lifetime.rs:25:5
17+
|
18+
25 | fn from_str(path: &str) -> Result<Self, ()> {
19+
| ^
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)