Description
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