Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't provide hint to add lifetime on impl items #37481

Merged
merged 1 commit into from
Nov 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,21 +1052,28 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
match item.node {
hir::ItemFn(ref fn_decl, unsafety, constness, _, ref gen, _) => {
Some((fn_decl, gen, unsafety, constness, item.name, item.span))
},
_ => None
}
_ => None,
}
}
ast_map::NodeImplItem(item) => {
match item.node {
hir::ImplItemKind::Method(ref sig, _) => {
Some((&sig.decl,
&sig.generics,
sig.unsafety,
sig.constness,
item.name,
item.span))
let id = self.tcx.map.get_parent(item.id);
if let Some(ast_map::NodeItem(parent_scope)) = self.tcx.map.find(id) {
if let hir::ItemImpl(_, _, _, None, _, _) = parent_scope.node {
// this impl scope implements a trait, do not recomend
// using explicit lifetimes (#37363)
return;
}
_ => None,
}
if let hir::ImplItemKind::Method(ref sig, _) = item.node {
Some((&sig.decl,
&sig.generics,
sig.unsafety,
sig.constness,
item.name,
item.span))
} else {
None
}
},
ast_map::NodeTraitItem(item) => {
Expand All @@ -1079,12 +1086,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
item.name,
item.span))
}
_ => None
_ => None,
}
}
_ => None
_ => None,
},
None => None
None => None,
};
let (fn_decl, generics, unsafety, constness, name, span)
= node_inner.expect("expect item fn");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ struct Baz<'x> {

impl<'a> Baz<'a> {
fn baz2<'b>(&self, x: &isize) -> (&'b isize, &'b isize) {
//~^ HELP consider using an explicit lifetime parameter as shown: fn baz2<'b>(&self, x: &'
// FIXME #35038: The above suggestion is different on Linux and Mac.
(self.bar, x) //~ ERROR E0312
//~^ ERROR E0312
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/ui/lifetimes/consider-using-explicit-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::str::FromStr;

pub struct Foo<'a> {
field: &'a str,
}

impl<'a> Foo<'a> {
fn bar(path: &str) -> Result<Self, ()> {
Ok(Foo { field: path })
}
}

impl<'a> FromStr for Foo<'a> {
type Err = ();
fn from_str(path: &str) -> Result<Self, ()> {
Ok(Foo { field: path })
}
}
22 changes: 22 additions & 0 deletions src/test/ui/lifetimes/consider-using-explicit-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: main function not found

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> $DIR/consider-using-explicit-lifetime.rs:19:12
|
19 | Ok(Foo { field: path })
| ^^^

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> $DIR/consider-using-explicit-lifetime.rs:26:12
|
26 | Ok(Foo { field: path })
| ^^^
|
help: consider using an explicit lifetime parameter as shown: fn from_str(path: &'a str) -> Result<Self, ()>
--> $DIR/consider-using-explicit-lifetime.rs:25:5
|
25 | fn from_str(path: &str) -> Result<Self, ()> {
| ^

error: aborting due to 2 previous errors