diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2025-02-27 21:11:22 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2025-03-07 17:55:08 +0000 |
| commit | 72326bfe4033fe51c5cb0f31614bbf6e66ec77f9 (patch) | |
| tree | 9b2bf94c15ff4dc1faac0c03b5bfe772357e270c /compiler/rustc_errors/src/emitter.rs | |
| parent | d975bd3a67e4f18c02f57d8d66a9d32daa295a47 (diff) | |
| download | rust-72326bfe4033fe51c5cb0f31614bbf6e66ec77f9.tar.gz rust-72326bfe4033fe51c5cb0f31614bbf6e66ec77f9.zip | |
On long spans, trim the middle of them to make them fit in the terminal width
When encountering a single line span that is wider than the terminal, we keep context at the start and end of the span but otherwise remove the code from the middle. This is somewhat independent from whether the left and right margins of the output have been trimmed as well.
```
error[E0308]: mismatched types
--> $DIR/long-span.rs:6:15
|
LL | ... = [0, 0, 0, 0, ..., 0, 0];
| ^^^^^^^^^^^^^...^^^^^^^ expected `u8`, found `[{integer}; 1681]`
```
Address part of #137680 (missing handling of the long suggestion). Fix #125581.
Diffstat (limited to 'compiler/rustc_errors/src/emitter.rs')
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 18846a6dbe1..9277bae94ec 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1238,6 +1238,33 @@ impl HumanEmitter { ); } } + + // We look for individual *long* spans, and we trim the *middle*, so that we render + // LL | ...= [0, 0, 0, ..., 0, 0]; + // | ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]` + for &(pos, annotation) in &annotations_position { + let AnnotationType::Singleline = annotation.annotation_type else { continue }; + let width = annotation.end_col.display - annotation.start_col.display; + if pos == 0 && width > margin.column_width && width > 10 { + // If the terminal is *too* small, we keep at least a tiny bit of the span for + // display. + let pad = max(margin.column_width / 2, 5); + // Code line + buffer.replace( + line_offset, + annotation.start_col.file + pad, + annotation.end_col.file - pad, + self.margin(), + ); + // Underline line + buffer.replace( + line_offset + 1, + annotation.start_col.file + pad, + annotation.end_col.file - pad, + self.margin(), + ); + } + } annotations_position .iter() .filter_map(|&(_, annotation)| match annotation.annotation_type { |
