Skip to content

Commit

Permalink
Emit error instead of ICE when loading files with trailing '>'
Browse files Browse the repository at this point in the history
Loading files with a leading '<' has been supported since Rust 1.0, so
we must keep support for that:

    touch '<leading-lt'
    echo 'include!("<leading-lt");' > leading-lt.rs
    rustc +1.0 --crate-type lib leading-lt.rs    # No error
  • Loading branch information
Enselic committed Aug 28, 2023
1 parent e877e2a commit e51d2a0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
28 changes: 21 additions & 7 deletions compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,28 @@ fn try_file_to_source_file(
path: &Path,
spanopt: Option<Span>,
) -> Result<Lrc<SourceFile>, 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),
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/include-macros/leading-lt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-pass
// check-run-results

fn main() {
println!(include!("silly-file-names/<leading-lt"));
}
1 change: 1 addition & 0 deletions tests/ui/include-macros/leading-lt.run.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
supported for backwards compatibility
1 change: 1 addition & 0 deletions tests/ui/include-macros/silly-file-names/<leading-lt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"supported for backwards compatibility"
Empty file.
3 changes: 3 additions & 0 deletions tests/ui/include-macros/trailing-gt.rs
Original file line number Diff line number Diff line change
@@ -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 '>'
}
11 changes: 11 additions & 0 deletions tests/ui/include-macros/trailing-gt.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e51d2a0

Please sign in to comment.