Skip to content

Doc comments break proc_macro::SourceFile #62892

Open
@unneon

Description

@unneon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions