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 10, 2019
1 parent f13b8fb commit 4b6305c
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
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,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 @@ -757,12 +757,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::Impl(.., ref impl_items) => {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,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 @@ -278,7 +278,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 4b6305c

Please sign in to comment.