Skip to content

Commit 155565f

Browse files
committed
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.
1 parent f2b91cc commit 155565f

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,7 @@ dependencies = [
37033703
"rustc_fluent_macro",
37043704
"rustc_hir",
37053705
"rustc_index",
3706+
"rustc_lexer",
37063707
"rustc_lint_defs",
37073708
"rustc_macros",
37083709
"rustc_serialize",

compiler/rustc_errors/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rustc_error_messages = { path = "../rustc_error_messages" }
1616
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1717
rustc_hir = { path = "../rustc_hir" }
1818
rustc_index = { path = "../rustc_index" }
19+
rustc_lexer = { path = "../rustc_lexer" }
1920
rustc_lint_defs = { path = "../rustc_lint_defs" }
2021
rustc_macros = { path = "../rustc_macros" }
2122
rustc_serialize = { path = "../rustc_serialize" }

compiler/rustc_errors/src/emitter.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use derive_setters::Setters;
1919
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
2020
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
2121
use rustc_error_messages::{FluentArgs, SpanLabel};
22+
use rustc_lexer;
2223
use rustc_lint_defs::pluralize;
2324
use rustc_span::hygiene::{ExpnKind, MacroKind};
2425
use rustc_span::source_map::SourceMap;
@@ -1698,9 +1699,14 @@ impl HumanEmitter {
16981699
if let Some(source_string) =
16991700
line.line_index.checked_sub(1).and_then(|l| file.get_line(l))
17001701
{
1702+
// Whitespace can only be removed (aka considered leading)
1703+
// if the lexer considers it whitespace.
1704+
// non-rustc_lexer::is_whitespace() chars are reported as an
1705+
// error (ex. no-break-spaces \u{a0}), and thus can't be considered
1706+
// for removal during error reporting.
17011707
let leading_whitespace = source_string
17021708
.chars()
1703-
.take_while(|c| c.is_whitespace())
1709+
.take_while(|c| rustc_lexer::is_whitespace(*c))
17041710
.map(|c| {
17051711
match c {
17061712
// Tabs are displayed as 4 spaces
@@ -1709,7 +1715,7 @@ impl HumanEmitter {
17091715
}
17101716
})
17111717
.sum();
1712-
if source_string.chars().any(|c| !c.is_whitespace()) {
1718+
if source_string.chars().any(|c| rustc_lexer::is_whitespace(c)) {
17131719
whitespace_margin = min(whitespace_margin, leading_whitespace);
17141720
}
17151721
}

0 commit comments

Comments
 (0)