Skip to content
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

Confusing "this function doesn't have a self parameter" hint when using macro #66898

Closed
Patryk27 opened this issue Nov 30, 2019 · 3 comments · Fixed by #72308
Closed

Confusing "this function doesn't have a self parameter" hint when using macro #66898

Patryk27 opened this issue Nov 30, 2019 · 3 comments · Fixed by #72308
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Patryk27
Copy link
Contributor

Patryk27 commented Nov 30, 2019

Hi,

pub struct Foo;

macro_rules! call_bar {
    () => {
        self.bar();
    }
}

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

... returns:

error[E0424]: expected value, found module `self`
  --> src/lib.rs:5:9
   |
5  |           self.bar();
   |           ^^^^ `self` value is a keyword only available in methods with a `self` parameter
...
10 | /     pub fn foo(&self) {
11 | |         call_bar!();
   | |         ------------ in this macro invocation
12 | |     }
   | |_____- this function doesn't have a `self` parameter

(playground)

Message says this function doesn't have a 'self' parameter, even though it does have a self parameter, which is kinda confusing.

This happens only on beta and nightly, since current stable doesn't provide that hint at all.

Thanks,
Patryk.

@Patryk27 Patryk27 changed the title Invalid "this function doesn't have a self parameter" hint when using macro Confusing "this function doesn't have a self parameter" hint when using macro Nov 30, 2019
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 30, 2019
@kepexx
Copy link

kepexx commented Nov 30, 2019

This happened to me on an old nightly (I believe 1.38.0 when it was still nightly, I may be wrong)

@estebank estebank added D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Feb 4, 2020
@ChosunOne
Copy link

ChosunOne commented May 3, 2020

Running into this issue on latest stable as well. What is the actual problem?

@Ryan1729
Copy link
Contributor

I just ran into what appears to be the same issue on 1.43.1, in the context of a trait method.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 25, 2020
…, r=matthewjasper

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

Fixes rust-lang#66898

When we are unable to resolve a reference to `self`, we current assume
that the containing function doesn't have a `self` parameter, and
emit an error message accordingly.

However, if the reference to `self` was created by a macro invocation,
then resolution will correctly fail, due to hygiene. In this case, we
don't want to tell the user that the containing fuction doesn't have a
'self' paramter if it actually has one.

This PR checks for the precense of a 'self' parameter, and adjusts the
error message we emit accordingly.

TODO: The exact error message we emit could probably be improved. Should
we explicitly mention hygiene?
@bors bors closed this as completed in b684448 May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants