Skip to content

Commit 9be0bd8

Browse files
committed
Avoid ICE when include! is used by stdin crate
This should also eliminate the ICE when using `include_bytes!`, `include_str!` and `#[doc(include = "...")]`. Fixes #63900
1 parent e5b8c11 commit 9be0bd8

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/libsyntax_expand/base.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,11 @@ impl<'a> ExtCtxt<'a> {
10721072
/// This unifies the logic used for resolving `include_X!`, and `#[doc(include)]` file paths.
10731073
///
10741074
/// Returns an absolute path to the file that `path` refers to.
1075-
pub fn resolve_path(&self, path: impl Into<PathBuf>, span: Span) -> PathBuf {
1075+
pub fn resolve_path(
1076+
&self,
1077+
path: impl Into<PathBuf>,
1078+
span: Span,
1079+
) -> Result<PathBuf, DiagnosticBuilder<'a>> {
10761080
let path = path.into();
10771081

10781082
// Relative paths are resolved relative to the file in which they are found
@@ -1082,13 +1086,16 @@ impl<'a> ExtCtxt<'a> {
10821086
let mut result = match self.source_map().span_to_unmapped_path(callsite) {
10831087
FileName::Real(path) => path,
10841088
FileName::DocTest(path, _) => path,
1085-
other => panic!("cannot resolve relative path in non-file source `{}`", other),
1089+
other => return Err(self.struct_span_err(
1090+
span,
1091+
&format!("cannot resolve relative path in non-file source `{}`", other),
1092+
)),
10861093
};
10871094
result.pop();
10881095
result.push(path);
1089-
result
1096+
Ok(result)
10901097
} else {
1091-
path
1098+
Ok(path)
10921099
}
10931100
}
10941101
}

src/libsyntax_expand/expand.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,14 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14181418
return noop_visit_attribute(at, self);
14191419
}
14201420

1421-
let filename = self.cx.resolve_path(&*file.as_str(), it.span());
1421+
let filename = match self.cx.resolve_path(&*file.as_str(), it.span()) {
1422+
Ok(filename) => filename,
1423+
Err(mut err) => {
1424+
err.emit();
1425+
continue;
1426+
}
1427+
};
1428+
14221429
match self.cx.source_map().load_file(&filename) {
14231430
Ok(source_file) => {
14241431
let src = source_file.src.as_ref()

src/libsyntax_ext/source_util.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
7676
None => return DummyResult::any(sp),
7777
};
7878
// The file will be added to the code map by the parser
79-
let file = cx.resolve_path(file, sp);
79+
let file = match cx.resolve_path(file, sp) {
80+
Ok(f) => f,
81+
Err(mut err) => {
82+
err.emit();
83+
return DummyResult::any(sp);
84+
},
85+
};
8086
let directory_ownership = DirectoryOwnership::Owned { relative: None };
8187
let p = parse::new_sub_parser_from_file(cx.parse_sess(), &file, directory_ownership, None, sp);
8288

@@ -122,7 +128,13 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
122128
Some(f) => f,
123129
None => return DummyResult::any(sp)
124130
};
125-
let file = cx.resolve_path(file, sp);
131+
let file = match cx.resolve_path(file, sp) {
132+
Ok(f) => f,
133+
Err(mut err) => {
134+
err.emit();
135+
return DummyResult::any(sp);
136+
},
137+
};
126138
match cx.source_map().load_binary_file(&file) {
127139
Ok(bytes) => match std::str::from_utf8(&bytes) {
128140
Ok(src) => {
@@ -147,7 +159,13 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
147159
Some(f) => f,
148160
None => return DummyResult::any(sp)
149161
};
150-
let file = cx.resolve_path(file, sp);
162+
let file = match cx.resolve_path(file, sp) {
163+
Ok(f) => f,
164+
Err(mut err) => {
165+
err.emit();
166+
return DummyResult::any(sp);
167+
},
168+
};
151169
match cx.source_map().load_binary_file(&file) {
152170
Ok(bytes) => {
153171
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes))))

0 commit comments

Comments
 (0)