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

Wrong error if a crate exports a macro with the same name as itself #47361

Closed
dtolnay opened this issue Jan 11, 2018 · 4 comments
Closed

Wrong error if a crate exports a macro with the same name as itself #47361

dtolnay opened this issue Jan 11, 2018 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Jan 11, 2018

If a crate or module defines a macro with the same name as the crate or module, any type error suggests that you invoke a non-existent macro instead of what you meant to write.

mod syn {
    #[allow(unused_macros)]
    macro_rules! syn {
        () => {}
    }
}

fn main() {
    let _: syn::Foo;
}
error[E0573]: expected type, found macro `syn::Foo`
  --> src/main.rs:22:9
   |
22 |     let _: syn::Foo;
   |            ^^^^^^^^ did you mean `syn::Foo!(...)`?

If the macro name is different from the module name then the error is reasonable.

error[E0412]: cannot find type `Foo` in module `syn`
  --> src/main.rs:22:14
   |
22 |     let _: syn::Foo;
   |                 ^^^ not found in `syn`

This currently affects the Syn crate. @Arnavion

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Jan 11, 2018
@dtolnay
Copy link
Member Author

dtolnay commented Jan 11, 2018

Mentioning @petrochenkov because the most relevant sounding change I could find is #38154.

@petrochenkov
Copy link
Contributor

#38154 was written when macro paths with multiple segments weren't legal, so it made... assumptions.
The mistake is here:

self.macro_names.contains(&path[0].node.modern())) {
.

@petrochenkov petrochenkov added the A-resolve Area: Name resolution label Jan 12, 2018
@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Apr 23, 2018
@estebank
Copy link
Contributor

estebank commented May 7, 2019

Repro case and current output for case from #57588:

use std::env;

fn main() {
    env::vav_os("PATH");
}
error[E0423]: expected function, found macro `env::vav_os`
 --> src/main.rs:4:5
  |
4 |     env::vav_os("PATH");
  |     ^^^^^^^^^^^
help: a function with a similar name exists
  |
4 |     env::var_os("PATH");
  |          ^^^^^^
help: use `!` to invoke the macro
  |
4 |     env::vav_os!("PATH");
  |     ^^^^^^^^^^^^

Current output for the case in the original report:

error[E0573]: expected type, found macro `syn::Foo`
  --> src/main.rs:10:12
   |
10 |     let _: syn::Foo;
   |            ^^^^^^^^ help: use `!` to invoke the macro: `syn::Foo!`

@estebank
Copy link
Contributor

estebank commented Feb 3, 2023

Current output:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d64537ec9c6390f24998d9f5aeabb0f1

error[E0425]: cannot find function `vav_os` in module `env`
 --> src/main.rs:4:10
  |
4 |     env::vav_os("PATH");
  |          ^^^^^^ help: a function with a similar name exists: `var_os`
 --> /rustc/f3126500f25114ba4e0ac3e76694dd45a22de56d/library/std/src/env.rs:268:1
  |
  = note: similarly named function `var_os` defined here

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a1153b2234ab364b7fc70b182128e153

error[E0412]: cannot find type `Foo` in module `syn`
 --> src/main.rs:9:17
  |
9 |     let _: syn::Foo;
  |                 ^^^ not found in `syn`

@estebank estebank closed this as completed Feb 3, 2023
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-resolve Area: Name resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants