Skip to content

Emit a better diagnostic when function actually has a 'self' parameter #72308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ struct DiagnosticMetadata<'ast> {
currently_processing_generics: bool,

/// The current enclosing function (used for better errors).
current_function: Option<Span>,
current_function: Option<(FnKind<'ast>, Span)>,

/// A list of labels as of yet unused. Labels will be removed from this map when
/// they are used (in a `break` or `continue` statement)
Expand Down Expand Up @@ -466,7 +466,8 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
FnKind::Fn(FnCtxt::Free | FnCtxt::Foreign, ..) => FnItemRibKind,
FnKind::Fn(FnCtxt::Assoc(_), ..) | FnKind::Closure(..) => NormalRibKind,
};
let previous_value = replace(&mut self.diagnostic_metadata.current_function, Some(sp));
let previous_value =
replace(&mut self.diagnostic_metadata.current_function, Some((fn_kind, sp)));
debug!("(resolving function) entering function");
let declaration = fn_kind.decl();

Expand Down
11 changes: 9 additions & 2 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,15 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
_ => "`self` value is a keyword only available in methods with a `self` parameter"
.to_string(),
});
if let Some(span) = &self.diagnostic_metadata.current_function {
err.span_label(*span, "this function doesn't have a `self` parameter");
if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function {
// The current function has a `self' parameter, but we were unable to resolve
// a reference to `self`. This can only happen if the `self` identifier we
// are resolving came from a different hygiene context.
if fn_kind.decl().inputs.get(0).map(|p| p.is_self()).unwrap_or(false) {
err.span_label(*span, "this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters");
} else {
err.span_label(*span, "this function doesn't have a `self` parameter");
}
}
return (err, Vec::new());
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/hygiene/missing-self-diag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Regression test for issue #66898
// Tests that we don't emit a nonsensical error message
// when a macro invocation tries to access `self` from a function
// that has a 'self' parameter

pub struct Foo;

macro_rules! call_bar {
() => {
self.bar(); //~ ERROR expected value
}
}

impl Foo {
pub fn foo(&self) {
call_bar!();
}

pub fn bar(&self) {
}
}

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/hygiene/missing-self-diag.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0424]: expected value, found module `self`
--> $DIR/missing-self-diag.rs:10:9
|
LL | self.bar();
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
...
LL | / pub fn foo(&self) {
LL | | call_bar!();
| | ------------ in this macro invocation
LL | | }
| |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

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