Skip to content

Commit

Permalink
Tweak suggestion for this -> self
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Aug 14, 2020
1 parent 3df25ae commit 643258f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
30 changes: 27 additions & 3 deletions src/librustc_resolve/late/diagnostics.rs
Expand Up @@ -7,6 +7,7 @@ use crate::{PathResult, PathSource, Segment};

use rustc_ast::ast::{self, Expr, ExprKind, Item, ItemKind, NodeId, Path, Ty, TyKind};
use rustc_ast::util::lev_distance::find_best_match_for_name;
use rustc_ast::visit::FnKind;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir;
Expand Down Expand Up @@ -175,15 +176,38 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
let mut err = self.r.session.struct_span_err_with_code(base_span, &base_msg, code);

// Emit help message for fake-self from other languages (e.g., `this` in Javascript).
if ["this", "my"].contains(&&*item_str.as_str())
&& self.self_value_is_available(path[0].ident.span, span)
{
if ["this", "my"].contains(&&*item_str.as_str()) && self.self_type_is_available(span) {
err.span_suggestion_short(
span,
"you might have meant to use `self` here instead",
"self".to_string(),
Applicability::MaybeIncorrect,
);
if !self.self_value_is_available(path[0].ident.span, span) {
if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) =
&self.diagnostic_metadata.current_function
{
if let Some(param) = sig.decl.inputs.get(0) {
err.span_suggestion_verbose(
param.span.shrink_to_lo(),
"you are also missing a `self` receiver argument",
"&self, ".to_string(),
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion_verbose(
self.r
.session
.source_map()
.span_through_char(*fn_span, '(')
.shrink_to_hi(),
"you are also missing a `self` receiver argument",
"&self".to_string(),
Applicability::MaybeIncorrect,
);
}
}
}
}

// Emit special messages for unresolved `Self` and `self`.
Expand Down
9 changes: 8 additions & 1 deletion src/test/ui/issues/issue-5099.rs
@@ -1,3 +1,10 @@
trait B < A > { fn a() -> A { this.a } } //~ ERROR cannot find value `this` in this scope
trait B <A> {
fn a() -> A {
this.a //~ ERROR cannot find value `this` in this scope
}
fn b(x: i32) {
this.b(x); //~ ERROR cannot find value `this` in this scope
}
}

fn main() {}
32 changes: 28 additions & 4 deletions src/test/ui/issues/issue-5099.stderr
@@ -1,9 +1,33 @@
error[E0425]: cannot find value `this` in this scope
--> $DIR/issue-5099.rs:1:31
--> $DIR/issue-5099.rs:3:9
|
LL | trait B < A > { fn a() -> A { this.a } }
| ^^^^ not found in this scope
LL | this.a
| ^^^^ not found in this scope
|
help: you might have meant to use `self` here instead
|
LL | self.a
| ^^^^
help: you are also missing a `self` receiver argument
|
LL | fn a(&self) -> A {
| ^^^^^

error[E0425]: cannot find value `this` in this scope
--> $DIR/issue-5099.rs:6:9
|
LL | this.b(x);
| ^^^^ not found in this scope
|
help: you might have meant to use `self` here instead
|
LL | self.b(x);
| ^^^^
help: you are also missing a `self` receiver argument
|
LL | fn b(&self, x: i32) {
| ^^^^^^

error: aborting due to previous error
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.

0 comments on commit 643258f

Please sign in to comment.