Point at invalid utf-8 span on user's source code
``` error: couldn't read `$DIR/not-utf8-bin-file.rs`: stream did not contain valid UTF-8 --> $DIR/not-utf8-2.rs:6:5 | LL | include!("not-utf8-bin-file.rs"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: `[193]` is not valid utf-8 --> $DIR/not-utf8-bin-file.rs:2:14 | LL | let _ = "�|�␂!5�cc␕␂��"; | ^ = note: this error originates in the macro `include` (in Nightly builds, run with -Z macro-backtrace for more info) ``` When we attempt to load a Rust source code file, if there is a OS file failure we try reading the file as bytes. If that succeeds we try to turn it into UTF-8. If *that* fails, we provide additional context about *where* the file has the first invalid UTF-8 character. Fix #76869.
This commit is contained in:
parent
6a64e3b897
commit
57dd42d613
14 changed files with 88 additions and 20 deletions
|
@ -13,7 +13,7 @@ use rustc_expand::base::{
|
|||
use rustc_expand::module::DirOwnership;
|
||||
use rustc_lint_defs::BuiltinLintDiag;
|
||||
use rustc_parse::parser::{ForceCollect, Parser};
|
||||
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal};
|
||||
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal, utf8_error};
|
||||
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
|
||||
use rustc_span::source_map::SourceMap;
|
||||
use rustc_span::{Pos, Span, Symbol};
|
||||
|
@ -209,9 +209,10 @@ pub(crate) fn expand_include_str(
|
|||
let interned_src = Symbol::intern(src);
|
||||
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
|
||||
}
|
||||
Err(_) => {
|
||||
let guar = cx.dcx().span_err(sp, format!("`{path}` wasn't a utf-8 file"));
|
||||
DummyResult::any(sp, guar)
|
||||
Err(utf8err) => {
|
||||
let mut err = cx.dcx().struct_span_err(sp, format!("`{path}` wasn't a utf-8 file"));
|
||||
utf8_error(cx.source_map(), path.as_str(), None, &mut err, utf8err, &bytes[..]);
|
||||
DummyResult::any(sp, err.emit())
|
||||
}
|
||||
},
|
||||
Err(dummy) => dummy,
|
||||
|
@ -273,7 +274,7 @@ fn load_binary_file(
|
|||
.and_then(|path| path.into_os_string().into_string().ok());
|
||||
|
||||
if let Some(new_path) = new_path {
|
||||
err.span_suggestion(
|
||||
err.span_suggestion_verbose(
|
||||
path_span,
|
||||
"there is a file with the same name in a different directory",
|
||||
format!("\"{}\"", new_path.replace('\\', "/").escape_debug()),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue