From 8e5b2c80d3f30c9d83a8e921e78e6c10e54c8319 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 | 11 +++++++---- src/test/ui/self/suggest-self-2.stderr | 20 +++++++++++++------- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/doc/rustc-guide b/src/doc/rustc-guide index 934380b7cfc..7c56708aab7 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 c356a7e31b1..fed4202d961 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -759,8 +759,8 @@ fn build_reduced_graph_for_item(&mut self, item: &'b Item) { // 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. @@ -798,12 +798,12 @@ fn build_reduced_graph_for_item(&mut self, item: &'b Item) { } 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(..) => { diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index e40c6fa7088..cc6fbf840a4 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -265,14 +265,14 @@ pub(crate) fn smart_resolve_report_errors( 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, } } @@ -284,7 +284,7 @@ pub(crate) fn smart_resolve_report_errors( 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 1926ebe4b83..d6bf5433527 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); + bar(&&self, 102); //~^ 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(&mut self, 102, &"str"); + //~^ ERROR cannot find function `bar` in this scope + //~| 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 84dbaa96378..ba71498fae6 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`.