diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-10-22 15:28:35 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-22 15:28:35 +0200 |
| commit | 4d378f26a9e9cfa2efa279e0c59c111c47068784 (patch) | |
| tree | dc0443f74c7f41ddd8238923110f4115cee9ed0c /compiler/rustc_expand/src/base.rs | |
| parent | 916e9ced404f276e90171d7852d436b6ca92df56 (diff) | |
| parent | edc97a0df2fbff05dd03483938425d1590440c82 (diff) | |
| download | rust-4d378f26a9e9cfa2efa279e0c59c111c47068784.tar.gz rust-4d378f26a9e9cfa2efa279e0c59c111c47068784.zip | |
Rollup merge of #125205 - ChrisDenton:verbatim-include, r=jieyouxu
Fixup Windows verbatim paths when used with the `include!` macro
On Windows, the following code can fail if the `OUT_DIR` environment variable is a [verbatim path](https://doc.rust-lang.org/std/path/enum.Prefix.html) (i.e. begins with `\\?\`):
```rust
include!(concat!(env!("OUT_DIR"), "/src/repro.rs"));
```
This is because verbatim paths treat `/` literally, as if it were just another character in the file name.
The good news is that the standard library already has code to fix this. We can simply use `components` to normalize the path so it works as intended.
Diffstat (limited to 'compiler/rustc_expand/src/base.rs')
| -rw-r--r-- | compiler/rustc_expand/src/base.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index f0cfe133a49..bed500c3032 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -1,5 +1,6 @@ use std::default::Default; use std::iter; +use std::path::Component::Prefix; use std::path::{Path, PathBuf}; use std::rc::Rc; @@ -1293,7 +1294,12 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe base_path.push(path); Ok(base_path) } else { - Ok(path) + // This ensures that Windows verbatim paths are fixed if mixed path separators are used, + // which can happen when `concat!` is used to join paths. + match path.components().next() { + Some(Prefix(prefix)) if prefix.kind().is_verbatim() => Ok(path.components().collect()), + _ => Ok(path), + } } } |
