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

Mod item in procedural macro output looks for the wrong source file #58818

Closed
dtolnay opened this issue Feb 28, 2019 · 4 comments · Fixed by #70238
Closed

Mod item in procedural macro output looks for the wrong source file #58818

dtolnay opened this issue Feb 28, 2019 · 4 comments · Fixed by #70238
Assignees
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Feb 28, 2019

I have this crate layout:

  • Cargo.toml
  • src/
    • main.rs
    • parent/
      • child.rs

where child.rs is an empty file and main.rs is:

mod parent {
    mod child;
}

fn main() {}

This compiles successfully and we end up with a module at crate::parent::child backed by the file src/parent/child.rs.

But if mod child; goes anywhere near a proc macro, it no longer works.

mod parent {
    repro::noop! { mod child; }
}

fn main() {}

where noop! is this macro:

#[proc_macro]
pub fn noop(input: TokenStream) -> TokenStream {
    input
}

The error is:

error[E0583]: file not found for module `child`
 --> src/main.rs:2:5
  |
2 |     repro::noop! { mod child; }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: name the file either child.rs or child/mod.rs inside the directory "src"

so it is looking for a file corresponding to mod child as though the mod child were not contained inside of mod parent. I hit this while working on https://github.com/dtolnay/automod.

The same problem does not occur if noop! is defined as a macro_rules macro.

Repro script

#!/bin/bash

cargo new repro

echo >>repro/Cargo.toml '
[lib]
proc-macro = true
'

echo >repro/src/lib.rs '
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn noop(input: TokenStream) -> TokenStream {
    input
}
'

echo >repro/src/main.rs '
mod parent {
    repro::noop! { mod child; }
}

fn main() {}
'

mkdir repro/src/parent
touch repro/src/parent/child.rs

cargo check --manifest-path repro/Cargo.toml

Mentioning @petrochenkov who may know what is going wrong or whether there is a simple rustc fix.

@dtolnay dtolnay added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Feb 28, 2019
@petrochenkov petrochenkov self-assigned this Feb 28, 2019
@petrochenkov
Copy link
Contributor

I never audited the module search code and don't know how it fits into the larger macro story.
Even without macros it's pretty inconsistent, so I'd expect issues in macros as well.
I'll investigate.

@dtolnay
Copy link
Member Author

dtolnay commented Oct 2, 2019

Still the same behavior as of rustc 1.40.0-nightly (702b45e 2019-10-01).

@petrochenkov
Copy link
Contributor

It's still in my queue, just not with a high enough priority :(

@petrochenkov
Copy link
Contributor

This was fixed by #69838, I'll add a test.

@petrochenkov petrochenkov added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Mar 21, 2020
@jonas-schievink jonas-schievink added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Mar 21, 2020
@bors bors closed this as completed in 17e6ed1 Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. 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.

3 participants