From 4b6305cfa49a7d9e764baacc640d519383cac8c2 Mon Sep 17 00:00:00 2001 From: Janusz Marcinkiewicz Date: Mon, 9 Dec 2019 16:05:45 +0100 Subject: [PATCH] Add more detailed suggestion --- src/doc/rustc-guide | 2 +- src/librustc_resolve/build_reduced_graph.rs | 10 +++++----- src/librustc_resolve/late/diagnostics.rs | 6 +++--- src/test/ui/self/suggest-self-2.rs | 9 ++++++--- src/test/ui/self/suggest-self-2.stderr | 20 +++++++++++++------- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/doc/rustc-guide b/src/doc/rustc-guide index 934380b7cfce..7c56708aab79 160000 --- a/src/doc/rustc-guide +++ b/src/doc/rustc-guide @@ -1 +1 @@ -Subproject commit 934380b7cfceaaa4e1b9bb0de4a372f32725520b +Subproject commit 7c56708aab7986ca390221e8e8902f7de7f9b076 diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 55b20b31ad8d..566ba1290743 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -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. @@ -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) => { diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index fd0f6b01d4ce..1df8ff02fceb 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -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, } } @@ -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, ); diff --git a/src/test/ui/self/suggest-self-2.rs b/src/test/ui/self/suggest-self-2.rs index 1926ebe4b831..d6bf54335270 100644 --- a/src/test/ui/self/suggest-self-2.rs +++ b/src/test/ui/self/suggest-self-2.rs @@ -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 diff --git a/src/test/ui/self/suggest-self-2.stderr b/src/test/ui/self/suggest-self-2.stderr index 84dbaa963789..ba71498fae65 100644 --- a/src/test/ui/self/suggest-self-2.stderr +++ b/src/test/ui/self/suggest-self-2.stderr @@ -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`.