Skip to content

Commit ea1d186

Browse files
authored
Rollup merge of #134442 - epage:change, r=workingjubilee
Specify the behavior of `file!` This takes the current behavior of `file!` and documents it so it is safe to make assumptions about. For example, Cargo could provide a `CARGO_RUSTC_CURRENT_DIR` as a base path for `file!`. Example use cases - Being able to look up test assets relative to the current file ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/cargo_add/add_basic/mod.rs#L34)) - Inline snapshotting libraries being able to update Rust source code ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/alt_registry.rs#L36-L45)) See rust-lang/cargo#3946 for more context. T-libs-api discussed two solutions in rust-lang/libs-team#478 - `file_absolute!`: - Has less meaning in other build tools like buck2 - Bakes in the assumption that a full path is available (e.g. with trim-paths) - Specifying `file!`s behavior (this PR): - Leaves it to the user to deal with trim-paths - Even though `file!` is currently unspecified, changing it would likely have too large of an impact on the ecosystem at this time. A future possibility is that rustc could have a flag that controls modifies the base path used for `file!`. That seems purely additive with specifying the behavior and we do not want to block on it. It would also likely be too disruptive for Cargo users (as mentioned). However, we tried to keep this in mind when specifying the behavior.
2 parents 100199c + fea6a22 commit ea1d186

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/core/src/macros/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,19 @@ pub(crate) mod builtin {
12731273
/// first macro invocation leading up to the invocation of the `file!`
12741274
/// macro.
12751275
///
1276+
/// The file name is derived from the crate root's source path passed to the Rust compiler
1277+
/// and the sequence the compiler takes to get from the crate root to the
1278+
/// module containing `file!`, modified by any flags passed to the Rust compiler (e.g.
1279+
/// `--remap-path-prefix`). If the crate's source path is relative, the initial base
1280+
/// directory will be the working directory of the Rust compiler. For example, if the source
1281+
/// path passed to the compiler is `./src/lib.rs` which has a `mod foo;` with a source path of
1282+
/// `src/foo/mod.rs`, then calling `file!` inside `mod foo;` will return `./src/foo/mod.rs`.
1283+
///
1284+
/// Future compiler options might make further changes to the behavior of `file!`,
1285+
/// including potentially making it entirely empty. Code (e.g. test libraries)
1286+
/// relying on `file!` producing an openable file path would be incompatible
1287+
/// with such options, and might wish to recommend not using those options.
1288+
///
12761289
/// # Examples
12771290
///
12781291
/// ```

0 commit comments

Comments
 (0)