Skip to content

Commit 8e5b2c8

Browse files
committed
Add more detailed suggestion
1 parent 8d189ed commit 8e5b2c8

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

src/doc/rustc-guide

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
759759
// These items live in both the type and value namespaces.
760760
ItemKind::Struct(ref vdata, _) => {
761761
// Define a name in the type namespace.
762-
let item_def_id = self.r.definitions.local_def_id(item.id);
763-
let res = Res::Def(DefKind::Struct, item_def_id);
762+
let def_id = self.r.definitions.local_def_id(item.id);
763+
let res = Res::Def(DefKind::Struct, def_id);
764764
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
765765

766766
// Record field names for error reporting.
@@ -798,12 +798,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
798798
}
799799

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

805805
// Record field names for error reporting.
806-
self.insert_field_names_local(item_def_id, vdata);
806+
self.insert_field_names_local(def_id, vdata);
807807
}
808808

809809
ItemKind::Trait(..) => {

src/librustc_resolve/late/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ impl<'a> LateResolutionVisitor<'a, '_> {
265265
if let PathSource::Expr(parent) = source {
266266
match &parent.map(|p| &p.kind) {
267267
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
268-
let mut expr_kind = &args.first().unwrap().kind;
268+
let mut expr_kind = &args[0].kind;
269269
loop {
270270
match expr_kind {
271271
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
272272
has_self_arg = arg_name.segments[0].ident.name == kw::SelfLower;
273273
break;
274274
},
275-
ExprKind::AddrOf(_, _, expr) => { expr_kind = &expr.kind; }
275+
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
276276
_ => break,
277277
}
278278
}
@@ -284,7 +284,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
284284
if has_self_arg {
285285
err.span_suggestion(
286286
span,
287-
&"try calling method instead of passing `self` as parameter",
287+
&format!("try calling `{}` as a method", ident),
288288
format!("self.{}", path_str),
289289
Applicability::MachineApplicable,
290290
);

src/test/ui/self/suggest-self-2.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ impl Foo {
44
fn foo(&self) {
55
bar(self);
66
//~^ ERROR cannot find function `bar` in this scope
7-
//~| HELP try calling method instead of passing `self` as parameter
7+
//~| HELP try calling `bar` as a method
88

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

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

1417
bar();
1518
//~^ ERROR cannot find function `bar` in this scope

src/test/ui/self/suggest-self-2.stderr

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@ error[E0425]: cannot find function `bar` in this scope
22
--> $DIR/suggest-self-2.rs:5:9
33
|
44
LL | bar(self);
5-
| ^^^ help: try calling method instead of passing `self` as parameter: `self.bar`
5+
| ^^^ help: try calling `bar` as a method: `self.bar`
66

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

1313
error[E0425]: cannot find function `bar` in this scope
14-
--> $DIR/suggest-self-2.rs:14:9
14+
--> $DIR/suggest-self-2.rs:13:9
15+
|
16+
LL | bar(&mut self, 102, &"str");
17+
| ^^^ help: try calling `bar` as a method: `self.bar`
18+
19+
error[E0425]: cannot find function `bar` in this scope
20+
--> $DIR/suggest-self-2.rs:17:9
1521
|
1622
LL | bar();
1723
| ^^^ not found in this scope
1824

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

25-
error: aborting due to 4 previous errors
31+
error: aborting due to 5 previous errors
2632

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

0 commit comments

Comments
 (0)