about summary refs log tree commit diff
path: root/compiler/rustc_errors/src/emitter.rs
diff options
context:
space:
mode:
authorHarrison Kaiser <uwaces@gmail.com>2024-12-15 20:06:02 -0500
committerHarrison Kaiser <uwaces@gmail.com>2024-12-16 23:34:46 -0500
commit1e33dd17115ca948c6e5ebf319695ed64490b2bf (patch)
tree4913499178aa13e7f003fbbd22571585bb8a91d8 /compiler/rustc_errors/src/emitter.rs
parent13b77c687c0f5ad06a579af6d0787fe562747501 (diff)
downloadrust-1e33dd17115ca948c6e5ebf319695ed64490b2bf.tar.gz
rust-1e33dd17115ca948c6e5ebf319695ed64490b2bf.zip
Fix logical error with what text is considered whitespace.
There is a logical issue around what counts as leading white-space.
There is code which does a subtraction assuming that no errors will be reported
inside the leading whitespace. However we compute the length of
that whitespace with std::char::is_whitespace and not
rustc_lexer::is_whitespace. The former will include a no-break space while
later will excluded it. We can only safely make the assumption that no errors
will be reported  in whitespace if it is all "Rust Standard" whitespace.
Indeed an error does occur in unicode whitespace if it contains a no-break
space.
Diffstat (limited to 'compiler/rustc_errors/src/emitter.rs')
-rw-r--r--compiler/rustc_errors/src/emitter.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index ac2f91cdeb3..17908bca26a 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -19,6 +19,7 @@ use derive_setters::Setters;
 use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
 use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
 use rustc_error_messages::{FluentArgs, SpanLabel};
+use rustc_lexer;
 use rustc_lint_defs::pluralize;
 use rustc_span::hygiene::{ExpnKind, MacroKind};
 use rustc_span::source_map::SourceMap;
@@ -1698,9 +1699,14 @@ impl HumanEmitter {
                     if let Some(source_string) =
                         line.line_index.checked_sub(1).and_then(|l| file.get_line(l))
                     {
+                        // Whitespace can only be removed (aka considered leading)
+                        // if the lexer considers it whitespace.
+                        // non-rustc_lexer::is_whitespace() chars are reported as an
+                        // error (ex. no-break-spaces \u{a0}), and thus can't be considered
+                        // for removal during error reporting.
                         let leading_whitespace = source_string
                             .chars()
-                            .take_while(|c| c.is_whitespace())
+                            .take_while(|c| rustc_lexer::is_whitespace(*c))
                             .map(|c| {
                                 match c {
                                     // Tabs are displayed as 4 spaces
@@ -1709,7 +1715,7 @@ impl HumanEmitter {
                                 }
                             })
                             .sum();
-                        if source_string.chars().any(|c| !c.is_whitespace()) {
+                        if source_string.chars().any(|c| !rustc_lexer::is_whitespace(c)) {
                             whitespace_margin = min(whitespace_margin, leading_whitespace);
                         }
                     }