diff --git a/compiler/rustc_parse/src/lib.rs b/compiler/rustc_parse/src/lib.rs index 598adbe798595..3998b547f1f47 100644 --- a/compiler/rustc_parse/src/lib.rs +++ b/compiler/rustc_parse/src/lib.rs @@ -152,14 +152,28 @@ fn try_file_to_source_file( path: &Path, spanopt: Option, ) -> Result, Diagnostic> { - sess.source_map().load_file(path).map_err(|e| { - let msg = format!("couldn't read {}: {}", path.display(), e); + let mut res = if path.to_string_lossy().ends_with('>') { + // The file name can't end with '>' because internally in the compiler + // we use the file name "<...>" to represent files that are not actually + // real files. To avoid having to split up paths into their components, + // we do not bother to check for '<' however. This means that file names + // with just a leading '<' will be loadable. + let msg = format!("can't load '{}' because its file name ends with '>'", path.display()); let mut diag = Diagnostic::new(Level::Fatal, msg); - if let Some(sp) = spanopt { - diag.set_span(sp); - } - diag - }) + diag.help("remove the trailing '>' from the file name"); + Err(diag) + } else { + sess.source_map().load_file(path).map_err(|e| { + let msg = format!("couldn't read {}: {}", path.display(), e); + Diagnostic::new(Level::Fatal, msg) + }) + }; + + if let Err(diag) = &mut res && let Some(sp) = spanopt { + diag.set_span(sp); + } + + res } /// Given a session and a path and an optional span (for error reporting), diff --git a/tests/ui/include-macros/leading-lt.rs b/tests/ui/include-macros/leading-lt.rs new file mode 100644 index 0000000000000..6f7fbaeb58677 --- /dev/null +++ b/tests/ui/include-macros/leading-lt.rs @@ -0,0 +1,6 @@ +// run-pass +// check-run-results + +fn main() { + println!(include!("silly-file-names/ b/tests/ui/include-macros/silly-file-names/trailing-gt> new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tests/ui/include-macros/trailing-gt.rs b/tests/ui/include-macros/trailing-gt.rs new file mode 100644 index 0000000000000..d700034b88a45 --- /dev/null +++ b/tests/ui/include-macros/trailing-gt.rs @@ -0,0 +1,3 @@ +fn main() { + include!("silly-file-names/trailing-gt>"); //~ ERROR can't load '$DIR/silly-file-names/trailing-gt>' because its file name ends with '>' +} diff --git a/tests/ui/include-macros/trailing-gt.stderr b/tests/ui/include-macros/trailing-gt.stderr new file mode 100644 index 0000000000000..7275d10a5e819 --- /dev/null +++ b/tests/ui/include-macros/trailing-gt.stderr @@ -0,0 +1,11 @@ +error: can't load '$DIR/silly-file-names/trailing-gt>' because its file name ends with '>' + --> $DIR/trailing-gt.rs:2:5 + | +LL | include!("silly-file-names/trailing-gt>"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: remove the trailing '>' from the file name + = note: this error originates in the macro `include` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error +