Skip to content

Commit e51d2a0

Browse files
committed
Emit error instead of ICE when loading files with trailing '>'
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
1 parent e877e2a commit e51d2a0

File tree

7 files changed

+43
-7
lines changed

7 files changed

+43
-7
lines changed

compiler/rustc_parse/src/lib.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,28 @@ fn try_file_to_source_file(
152152
path: &Path,
153153
spanopt: Option<Span>,
154154
) -> Result<Lrc<SourceFile>, Diagnostic> {
155-
sess.source_map().load_file(path).map_err(|e| {
156-
let msg = format!("couldn't read {}: {}", path.display(), e);
155+
let mut res = if path.to_string_lossy().ends_with('>') {
156+
// The file name can't end with '>' because internally in the compiler
157+
// we use the file name "<...>" to represent files that are not actually
158+
// real files. To avoid having to split up paths into their components,
159+
// we do not bother to check for '<' however. This means that file names
160+
// with just a leading '<' will be loadable.
161+
let msg = format!("can't load '{}' because its file name ends with '>'", path.display());
157162
let mut diag = Diagnostic::new(Level::Fatal, msg);
158-
if let Some(sp) = spanopt {
159-
diag.set_span(sp);
160-
}
161-
diag
162-
})
163+
diag.help("remove the trailing '>' from the file name");
164+
Err(diag)
165+
} else {
166+
sess.source_map().load_file(path).map_err(|e| {
167+
let msg = format!("couldn't read {}: {}", path.display(), e);
168+
Diagnostic::new(Level::Fatal, msg)
169+
})
170+
};
171+
172+
if let Err(diag) = &mut res && let Some(sp) = spanopt {
173+
diag.set_span(sp);
174+
}
175+
176+
res
163177
}
164178

165179
/// Given a session and a path and an optional span (for error reporting),

tests/ui/include-macros/leading-lt.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-pass
2+
// check-run-results
3+
4+
fn main() {
5+
println!(include!("silly-file-names/<leading-lt"));
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
supported for backwards compatibility
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"supported for backwards compatibility"

tests/ui/include-macros/silly-file-names/trailing-gt>

Whitespace-only changes.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
include!("silly-file-names/trailing-gt>"); //~ ERROR can't load '$DIR/silly-file-names/trailing-gt>' because its file name ends with '>'
3+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: can't load '$DIR/silly-file-names/trailing-gt>' because its file name ends with '>'
2+
--> $DIR/trailing-gt.rs:2:5
3+
|
4+
LL | include!("silly-file-names/trailing-gt>");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: remove the trailing '>' from the file name
8+
= note: this error originates in the macro `include` (in Nightly builds, run with -Z macro-backtrace for more info)
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)