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

Macro declarations do not work in impl blocks #37205

Closed
KiChjang opened this issue Oct 16, 2016 · 6 comments · Fixed by #70428
Closed

Macro declarations do not work in impl blocks #37205

KiChjang opened this issue Oct 16, 2016 · 6 comments · Fixed by #70428
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-parser Area: The parsing of Rust source code to an AST. C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@KiChjang
Copy link
Member

Minimal test case:

struct Foo;

impl Foo {
    macro_rules! bar {
        () => (
            println!("Hi");
        );
    }

    pub fn say() {
        bar!();
    }
}

fn main() {
    Foo::say();
}

On stable it emits the following error:

rustc 1.12.0 (3191fbae9 2016-09-23)
error: expected one of `const`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `macro_rules`
 --> <anon>:4:5
  |
4 |     macro_rules! bar {
  |     ^^^^^^^^^^^

error: aborting due to previous error

And on beta or nightly, it emits the following instead:

rustc 1.14.0-nightly (6e8f92f11 2016-10-07)
error: expected open delimiter
 --> <anon>:4:18
  |
4 |     macro_rules! bar {
  |                  ^^^

error: aborting due to previous error
@Mark-Simulacrum Mark-Simulacrum added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-parser Area: The parsing of Rust source code to an AST. labels May 14, 2017
@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 22, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 26, 2017
@steveklabnik
Copy link
Member

Triage: no change

@Centril
Copy link
Contributor

Centril commented Mar 26, 2020

Triage: we now emit:

error: macro definition is not supported in `trait`s or `impl`s
 --> src/main.rs:4:5
  |
4 |     macro_rules! bar {
  |     ^^^^^^^^^^^^^^^^

error: cannot find macro `bar` in this scope
  --> src/main.rs:11:9
   |
11 |         bar!();
   |         ^^^

error: aborting due to 2 previous errors

@KiChjang do you expect the example to compile successfully? If not, I think we can close.

@KiChjang
Copy link
Member Author

It should compile, yes. I haven't heard a specific reason why we shouldn't allow macro declarations in impl blocks.

@Centril
Copy link
Contributor

Centril commented Mar 26, 2020

I'm not sure exactly how such a thing would interact with resolve, if e.g., the impl has type parameters... is this macro only local to the impl, and what about if it is exported?

cc @petrochenkov

@petrochenkov
Copy link
Contributor

I haven't heard a specific reason why we shouldn't allow macro declarations in impl blocks.

Why should we?
Impls are entirely about type-relative stuff.
We don't have associated macros, we don't want to resolve macros during type-checking, so we don't support macros in impls.

We could implement this only for macro_rules with their let-like scoping and not for macro items, but it would be strange to support a new exclusive feature for something we eventually want to replace.

@KiChjang
Copy link
Member Author

This issue was created before macro items were introduced, so if that can introduce proper scoping so that it's able to be used in methods, then there's no need to have macro declarations in impl blocks.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 26, 2020
`error_bad_item_kind`: add help text

For example, this adds:
```
    = help: consider moving the `use` import out to a nearby module scope
```
r? @petrochenkov @estebank

Fixes rust-lang#37205.
@bors bors closed this as completed in ef43cde Mar 27, 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, ..) A-parser Area: The parsing of Rust source code to an AST. C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants