Skip to content

Commit

Permalink
Add more detailed suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
VirrageS committed Dec 23, 2019
1 parent 8d189ed commit 8e5b2c8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/doc/rustc-guide
10 changes: 5 additions & 5 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -759,8 +759,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// These items live in both the type and value namespaces.
ItemKind::Struct(ref vdata, _) => {
// Define a name in the type namespace.
let item_def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Struct, item_def_id);
let def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Struct, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

// Record field names for error reporting.
Expand Down Expand Up @@ -798,12 +798,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}

ItemKind::Union(ref vdata, _) => {
let item_def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Union, item_def_id);
let def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Union, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

// Record field names for error reporting.
self.insert_field_names_local(item_def_id, vdata);
self.insert_field_names_local(def_id, vdata);
}

ItemKind::Trait(..) => {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/late/diagnostics.rs
Expand Up @@ -265,14 +265,14 @@ impl<'a> LateResolutionVisitor<'a, '_> {
if let PathSource::Expr(parent) = source {
match &parent.map(|p| &p.kind) {
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
let mut expr_kind = &args.first().unwrap().kind;
let mut expr_kind = &args[0].kind;
loop {
match expr_kind {
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
has_self_arg = arg_name.segments[0].ident.name == kw::SelfLower;
break;
},
ExprKind::AddrOf(_, _, expr) => { expr_kind = &expr.kind; }
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
_ => break,
}
}
Expand All @@ -284,7 +284,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
if has_self_arg {
err.span_suggestion(
span,
&"try calling method instead of passing `self` as parameter",
&format!("try calling `{}` as a method", ident),
format!("self.{}", path_str),
Applicability::MachineApplicable,
);
Expand Down
9 changes: 6 additions & 3 deletions src/test/ui/self/suggest-self-2.rs
Expand Up @@ -4,12 +4,15 @@ impl Foo {
fn foo(&self) {
bar(self);
//~^ ERROR cannot find function `bar` in this scope
//~| HELP try calling method instead of passing `self` as parameter
//~| HELP try calling `bar` as a method

bar(&&self, 102);
//~^ ERROR cannot find function `bar` in this scope
//~| HELP try calling `bar` as a method

bar(&self);
bar(&mut self, 102, &"str");
//~^ ERROR cannot find function `bar` in this scope
//~| HELP try calling method instead of passing `self` as parameter
//~| HELP try calling `bar` as a method

bar();
//~^ ERROR cannot find function `bar` in this scope
Expand Down
20 changes: 13 additions & 7 deletions src/test/ui/self/suggest-self-2.stderr
Expand Up @@ -2,27 +2,33 @@ error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:5:9
|
LL | bar(self);
| ^^^ help: try calling method instead of passing `self` as parameter: `self.bar`
| ^^^ help: try calling `bar` as a method: `self.bar`

error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:10:9
--> $DIR/suggest-self-2.rs:9:9
|
LL | bar(&self);
| ^^^ help: try calling method instead of passing `self` as parameter: `self.bar`
LL | bar(&&self, 102);
| ^^^ help: try calling `bar` as a method: `self.bar`

error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:14:9
--> $DIR/suggest-self-2.rs:13:9
|
LL | bar(&mut self, 102, &"str");
| ^^^ help: try calling `bar` as a method: `self.bar`

error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:17:9
|
LL | bar();
| ^^^ not found in this scope

error[E0599]: no method named `bar` found for type `&Foo` in the current scope
--> $DIR/suggest-self-2.rs:17:14
--> $DIR/suggest-self-2.rs:20:14
|
LL | self.bar();
| ^^^ method not found in `&Foo`

error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0425, E0599.
For more information about an error, try `rustc --explain E0425`.

0 comments on commit 8e5b2c8

Please sign in to comment.