From 78f62c619006500485e96056f733d74be02cab8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 30 Aug 2019 17:45:34 -0700 Subject: [PATCH 1/3] Account for rounding errors when deciding the diagnostic boundaries --- src/librustc_errors/emitter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 77d373e7a8ca8..fddb6c5c259c9 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -146,12 +146,12 @@ impl Margin { } else if self.label_right - self.span_left <= self.column_width { // Attempt to fit the code window considering only the spans and labels. let padding_left = (self.column_width - (self.label_right - self.span_left)) / 2; - self.computed_left = self.span_left - padding_left; + self.computed_left = max(self.span_left, padding_left) - padding_left; self.computed_right = self.computed_left + self.column_width; } else if self.span_right - self.span_left <= self.column_width { // Attempt to fit the code window considering the spans and labels plus padding. let padding_left = (self.column_width - (self.span_right - self.span_left)) / 5 * 2; - self.computed_left = self.span_left - padding_left; + self.computed_left = max(self.span_left, padding_left) - padding_left; self.computed_right = self.computed_left + self.column_width; } else { // Mostly give up but still don't show the full line. self.computed_left = self.span_left; From bf90154410b372ad0c8731a6d470acd9bf820f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 30 Aug 2019 19:47:21 -0700 Subject: [PATCH 2/3] Tweak terminal width trimming Properly account for left margin when setting terminal width through CLI flag and don't trim code by default if we can't get the terminal's dimensions. --- src/librustc_errors/emitter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index fddb6c5c259c9..02473cc86bdff 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -1304,11 +1304,11 @@ impl EmitterWriter { }; let column_width = if let Some(width) = self.terminal_width { - width + max(width, code_offset) - code_offset } else if self.ui_testing { 140 } else { - term_size::dimensions().map(|(w, _)| w - code_offset).unwrap_or(140) + term_size::dimensions().map(|(w, _)| w - code_offset).unwrap_or(std::usize::MAX) }; let margin = Margin::new( From 84567190e0d36f9a61f9bc833bd9fa559aeb0089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 31 Aug 2019 13:42:53 -0700 Subject: [PATCH 3/3] Use saturating_sub --- src/librustc_errors/emitter.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 02473cc86bdff..a0ce761cfa277 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -146,12 +146,12 @@ impl Margin { } else if self.label_right - self.span_left <= self.column_width { // Attempt to fit the code window considering only the spans and labels. let padding_left = (self.column_width - (self.label_right - self.span_left)) / 2; - self.computed_left = max(self.span_left, padding_left) - padding_left; + self.computed_left = self.span_left.saturating_sub(padding_left); self.computed_right = self.computed_left + self.column_width; } else if self.span_right - self.span_left <= self.column_width { // Attempt to fit the code window considering the spans and labels plus padding. let padding_left = (self.column_width - (self.span_right - self.span_left)) / 5 * 2; - self.computed_left = max(self.span_left, padding_left) - padding_left; + self.computed_left = self.span_left.saturating_sub(padding_left); self.computed_right = self.computed_left + self.column_width; } else { // Mostly give up but still don't show the full line. self.computed_left = self.span_left; @@ -1304,11 +1304,13 @@ impl EmitterWriter { }; let column_width = if let Some(width) = self.terminal_width { - max(width, code_offset) - code_offset + width.saturating_sub(code_offset) } else if self.ui_testing { 140 } else { - term_size::dimensions().map(|(w, _)| w - code_offset).unwrap_or(std::usize::MAX) + term_size::dimensions() + .map(|(w, _)| w.saturating_sub(code_offset)) + .unwrap_or(std::usize::MAX) }; let margin = Margin::new(