Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup Windows verbatim paths when used with the include! macro #125205

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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),
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/run-make/import-macro-verbatim/include/include.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static TEST: &str = "Hello World!";
8 changes: 8 additions & 0 deletions tests/run-make/import-macro-verbatim/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ only-windows other platforms do not have Windows verbatim paths
use run_make_support::rustc;
fn main() {
// Canonicalizing the path ensures that it's verbatim (i.e. starts with `\\?\`)
let mut path = std::fs::canonicalize(file!()).unwrap();
path.pop();
rustc().input("verbatim.rs").env("VERBATIM_DIR", path).run();
}
12 changes: 12 additions & 0 deletions tests/run-make/import-macro-verbatim/verbatim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Include a file by concating the verbatim path using `/` instead of `\`

include!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
fn main() {
assert_eq!(TEST, "Hello World!");

let s = include_str!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
assert_eq!(s, "static TEST: &str = \"Hello World!\";\n");

let b = include_bytes!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
assert_eq!(b, b"static TEST: &str = \"Hello World!\";\n");
}
Loading