The diagnostic emitter doesn't deal with non-ASCII characters correctly when eliding parts of overly long source lines #132860
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-Unicode
Area: Unicode
C-bug
Category: This is a bug.
D-diagnostic-infra
Diagnostics: Issues that affect all diagnostics, or relate to the diagnostic machinery itself.
P-low
Low priority
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Found while reviewing #126597 for the nth and final time. Many parts of the emitter look fishy to me wrt. Unicode handling and to be honest it was quite frustrating to review code related to it because the emitter doesn't make any attempt to newtype the different units used, namely byte lengths/offsets, char / Unicode scalar lengths/offsets and Unicode widths (I hope rust-lang/annotate-snippets-rs will remedy that). That's just an aside.
The string offset acrobatics performed in
HumanEmitter::render_source_line
andHumanEmitter::draw_line
look incredibly suspicious to me. Let me just link some parts where we likely incorrectly reinterpret different units (byte len, char count, Unicode width):rust/compiler/rustc_errors/src/emitter.rs
Lines 733 to 736 in c22887b
rust/compiler/rustc_errors/src/emitter.rs
Lines 663 to 681 in c22887b
I gave up trying to make sense of this – having to look at all the weakly typed variables and fields of type
usize
.However, based on these functions I crafted a pathological input file where it's clear something is amiss.
Example Reproducer
/*这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
Compiler Output
Clearly butchered:
Counterexample
Compare this to ASCII-only input:
/*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/?
Compiler Output
Perfectly sensible:
The text was updated successfully, but these errors were encountered: