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

Doc comments break proc_macro::SourceFile #62892

Open
unneon opened this issue Jul 23, 2019 · 1 comment
Open

Doc comments break proc_macro::SourceFile #62892

unneon opened this issue Jul 23, 2019 · 1 comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@unneon
Copy link

unneon commented Jul 23, 2019

Adding a doc comment before a procedural macro invocation causes proc_macro::SourceFile::path to return the path to lib.rs or main.rs instead of the actual path. The documentation states that if proc_macro::SourceFile::is_real returns false, the returned path may not be an actual path on the filesystem, but this is not the case(it returns true and the path is a real path). I originally found the issue in unneon/icie@53a9913.

I tried this code:

// src/lib.rs

pub mod sub;
// src/sub.rs

#[ohnodoc_codegen::sparklify]
pub fn without_doc_comment() -> i32 {
    42
}

/// I'm a doc comment!
#[ohnodoc_codegen::sparklify]
pub fn with_doc_comment() -> i32 {
    42
}
// ohnodoc-codegen/src/lib.rs

#![feature(proc_macro_span)]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn sparklify(_params: TokenStream, item: TokenStream) -> TokenStream {
    let source = item.clone().into_iter().next().unwrap().span().source_file();
    eprintln!("path = {:?}, is_real = {:?}", source.path(), source.is_real());
    item
}

I expected to see this happen: during compilation, these two lines are printed to stderr:

path = "src/sub.rs", is_real = true
path = "src/sub.rs", is_real = true

Instead, this happened: these two lines were printed:

path = "src/sub.rs", is_real = true
path = "src/lib.rs", is_real = true

Meta

rustc 1.38.0-nightly (07e0c36 2019-07-16)
binary: rustc
commit-hash: 07e0c36
commit-date: 2019-07-16
host: x86_64-unknown-linux-gnu
release: 1.38.0-nightly
LLVM version: 8.0

@jonas-schievink jonas-schievink added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 23, 2019
@jonas-schievink
Copy link
Contributor

cc #54725

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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants