Skip to content

file!() macro return absolute path in doc-test compile #46372

@la10736

Description

@la10736

This a minimal example. Standard compilation resolve file!() macro as relative project path but doc-test compilation emit follow error:

error: couldn't read "/home/mdamico/temp/doc_test_file_bug/src/..//home/mdamico/temp/doc_test_file_bug/src/lib.rs.bis.rs": No such file or directory (os error 2)
 --> /home/mdamico/temp/doc_test_file_bug/src/lib.rs:1:1
  |
1 | include!(concat!("../", file!(), ".bis.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This behavior involve just include!() macro because when used in function or assignment works like as expected

///
/// ```
/// assert_eq!("src/lib.rs", file!())
/// ```

/// ```
/// assert_eq!("src/lib.rs", doc_test_file_bug::path())
/// ```
pub fn path() -> &'static str {
    file!()
}

///
/// ```
/// assert_eq!("src/lib.rs", doc_test_file_bug::f)
/// ```
pub static f: &'static str = file!();
   Doc-tests doc_test_file_bug

running 3 tests
test src/lib.rs - path (line 6) ... ok
test src/lib.rs - f (line 14) ... ok
test src/lib.rs - path (line 2) ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Now I should disable doc-test to use file!() for including some auto-generated code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocA-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.T-rustdocRelevant to the rustdoc 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