Fix verbatim paths used with include! · rust-lang/rust@edc97a0 (original) (raw)

File tree

4 files changed

lines changed

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1 1 use std::default::Default;
2 2 use std::iter;
3 +use std::path::Component::Prefix;
3 4 use std::path::{Path, PathBuf};
4 5 use std::rc::Rc;
5 6
@@ -1293,7 +1294,12 @@ pub fn resolve_path(sess: &Session, path: impl Into, span: Span) -> PRe
1293 1294 base_path.push(path);
1294 1295 Ok(base_path)
1295 1296 } else {
1296 -Ok(path)
1297 +// This ensures that Windows verbatim paths are fixed if mixed path separators are used,
1298 +// which can happen when `concat!` is used to join paths.
1299 +match path.components().next() {
1300 +Some(Prefix(prefix)) if prefix.kind().is_verbatim() => Ok(path.components().collect()),
1301 + _ => Ok(path),
1302 +}
1297 1303 }
1298 1304 }
1299 1305
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +static TEST: &str = "Hello World!";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1 +//@ only-windows other platforms do not have Windows verbatim paths
2 +use run_make_support::rustc;
3 +fn main() {
4 +// Canonicalizing the path ensures that it's verbatim (i.e. starts with `\\?\`)
5 +let mut path = std::fs::canonicalize(file!()).unwrap();
6 + path.pop();
7 +rustc().input("verbatim.rs").env("VERBATIM_DIR", path).run();
8 +}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1 +//! Include a file by concating the verbatim path using `/` instead of `\`
2 +
3 +include!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
4 +fn main() {
5 +assert_eq!(TEST, "Hello World!");
6 +
7 +let s = include_str!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
8 +assert_eq!(s, "static TEST: &str = \"Hello World!\";\n");
9 +
10 +let b = include_bytes!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
11 +assert_eq!(b, b"static TEST: &str = \"Hello World!\";\n");
12 +}