Skip to content

Better suggestion for unknown method #42391

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

Merged
merged 1 commit into from
Jun 4, 2017
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
13 changes: 12 additions & 1 deletion src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if mode == Mode::MethodCall {
"method"
} else {
"associated item"
match item_name.as_str().chars().next() {
Some(name) => {
if name.is_lowercase() {
"function or associated item"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have said "associated function or item", personally

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the rust nomenclature for associated function "method"? So "method or associated type" would be more precise, since it can't possibly be an associated const.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't it be a const?

I wasn't sure if we call these methods, opinions seem to vary.

I put "function" first because newbies don't know what "associated blah" means.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I it were a const, you'd need to wrap it in parens to be able to do a function call, and the const must be a Fn* or fn type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know if it's a function call at this stage. This fix does not introduce that checking, though @photoszzt may work on that next if he has time.

} else {
"associated item"
}
},
None => {
""
},
}
},
item_name,
self.ty_to_string(actual))
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/bogus-tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fn main() {
let red: color = color::rgb(255, 0, 0);
match red {
color::rgb(r, g, b) => { println!("rgb"); }
color::hsl(h, s, l) => { println!("hsl"); } //~ ERROR no associated
color::hsl(h, s, l) => { println!("hsl"); } //~ ERROR no function
}
}
6 changes: 3 additions & 3 deletions src/test/compile-fail/issue-28344.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use std::ops::BitXor;
fn main() {
let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
//~^ ERROR must be specified
//~| no associated item named
//~| no function or associated item named

let g = BitXor::bitor;
//~^ ERROR must be specified
//~| no associated item named
}
//~| no function or associated item named
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-30123.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ use issue_30123_aux::*;

fn main() {
let ug = Graph::<i32, i32>::new_undirected();
//~^ ERROR no associated item named `new_undirected` found for type
//~^ ERROR no function or associated item named `new_undirected` found for type
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-39559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Dim for Dim3 {

pub struct Vector<T, D: Dim> {
entries: [T; D::dim()]
//~^ ERROR no associated item named `dim` found for type `D` in the current scope
//~^ ERROR no function or associated item named `dim` found for type `D` in the current scope
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-3973.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ impl ToString_ for Point {

fn main() {
let p = Point::new(0.0, 0.0);
//~^ ERROR no associated item named `new` found for type `Point` in the current scope
//~^ ERROR no function or associated item named `new` found for type `Point`
println!("{}", p.to_string());
}
3 changes: 2 additions & 1 deletion src/test/compile-fail/issue-7950.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
struct Foo;

fn main() {
Foo::bar(); //~ ERROR no associated item named `bar` found for type `Foo` in the current scope
Foo::bar();
//~^ ERROR no function or associated item named `bar` found for type `Foo`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/lexical-scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod Foo {
pub fn f() {}
}
fn g<Foo>() {
Foo::f(); //~ ERROR no associated item named `f`
Foo::f(); //~ ERROR no function or associated item named `f`
}

fn main() {}
6 changes: 4 additions & 2 deletions src/test/compile-fail/trait-item-privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ fn check_method() {

// Methods, UFCS
// a, b, c are resolved as trait items, their traits need to be in scope
S::a(&S); //~ ERROR no associated item named `a` found for type `S` in the current scope
S::b(&S); //~ ERROR no associated item named `b` found for type `S` in the current scope
S::a(&S);
//~^ ERROR no function or associated item named `a` found for type `S`
S::b(&S);
//~^ ERROR no function or associated item named `b` found for type `S`
S::c(&S); // OK
// a, b, c are resolved as inherent items, their traits don't need to be in scope
C::a(&S); //~ ERROR method `a` is private
Expand Down
8 changes: 4 additions & 4 deletions src/test/compile-fail/unspecified-self-in-trait-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub trait Bar<X=usize, A=Self> {

fn main() {
let a = Foo::lol();
//~^ ERROR no associated item named
//~^ ERROR no function or associated item named
let b = Foo::<_>::lol();
//~^ ERROR no associated item named
//~^ ERROR no function or associated item named
let c = Bar::lol();
//~^ ERROR no associated item named
//~^ ERROR no function or associated item named
let d = Bar::<usize, _>::lol();
//~^ ERROR no associated item named
//~^ ERROR no function or associated item named
let e = Bar::<usize>::lol();
//~^ ERROR must be explicitly specified
}