From b9bf119c4f98f40eb84b385a6d3239c358b054cb Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 14 Nov 2022 11:12:51 +0000 Subject: Simplify some nested conditions --- compiler/rustc_errors/src/emitter.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index db595df8ec1..98639434d8c 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1386,20 +1386,13 @@ impl EmitterWriter { let mut annotated_files = FileWithAnnotatedLines::collect_annotations(self, args, msp); // Make sure our primary file comes first - let (primary_lo, sm) = if let (Some(sm), Some(ref primary_span)) = - (self.sm.as_ref(), msp.primary_span().as_ref()) - { - if !primary_span.is_dummy() { - (sm.lookup_char_pos(primary_span.lo()), sm) - } else { - emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?; - return Ok(()); - } - } else { + let primary_span = msp.primary_span().unwrap_or_default(); + let (Some(sm), false) = (self.sm.as_ref(), primary_span.is_dummy()) else { // If we don't have span information, emit and exit emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?; return Ok(()); }; + let primary_lo = sm.lookup_char_pos(primary_span.lo()); if let Ok(pos) = annotated_files.binary_search_by(|x| x.file.name.cmp(&primary_lo.file.name)) { -- cgit 1.4.1-3-g733a5 From 9eb9176b08a4a61f7725e7da73558abc508404d9 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 15 Nov 2022 09:43:04 +0000 Subject: Simplify span fallback --- compiler/rustc_errors/src/emitter.rs | 70 ++++++++++++------------------------ 1 file changed, 23 insertions(+), 47 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 98639434d8c..cb5aadce8e3 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -24,7 +24,7 @@ use rustc_lint_defs::pluralize; use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::sync::Lrc; -use rustc_error_messages::FluentArgs; +use rustc_error_messages::{FluentArgs, SpanLabel}; use rustc_span::hygiene::{ExpnKind, MacroKind}; use std::borrow::Cow; use std::cmp::{max, min, Reverse}; @@ -2202,46 +2202,28 @@ impl FileWithAnnotatedLines { let mut multiline_annotations = vec![]; if let Some(ref sm) = emitter.source_map() { - for span_label in msp.span_labels() { - let fixup_lo_hi = |span: Span| { - let lo = sm.lookup_char_pos(span.lo()); - let mut hi = sm.lookup_char_pos(span.hi()); - - // Watch out for "empty spans". If we get a span like 6..6, we - // want to just display a `^` at 6, so convert that to - // 6..7. This is degenerate input, but it's best to degrade - // gracefully -- and the parser likes to supply a span like - // that for EOF, in particular. - - if lo.col_display == hi.col_display && lo.line == hi.line { - hi.col_display += 1; - } - (lo, hi) + for SpanLabel { span, is_primary, label } in msp.span_labels() { + // If we don't have a useful span, pick the primary span if that exists. + // Worst case we'll just print an error at the top of the main file. + let span = match (span.is_dummy(), msp.primary_span()) { + (_, None) | (false, _) => span, + (true, Some(span)) => span, }; - if span_label.span.is_dummy() { - if let Some(span) = msp.primary_span() { - // if we don't know where to render the annotation, emit it as a note - // on the primary span. - - let (lo, hi) = fixup_lo_hi(span); - - let ann = Annotation { - start_col: lo.col_display, - end_col: hi.col_display, - is_primary: span_label.is_primary, - label: span_label - .label - .as_ref() - .map(|m| emitter.translate_message(m, args).to_string()), - annotation_type: AnnotationType::Singleline, - }; - add_annotation_to_file(&mut output, lo.file, lo.line, ann); - } - continue; + let lo = sm.lookup_char_pos(span.lo()); + let mut hi = sm.lookup_char_pos(span.hi()); + + // Watch out for "empty spans". If we get a span like 6..6, we + // want to just display a `^` at 6, so convert that to + // 6..7. This is degenerate input, but it's best to degrade + // gracefully -- and the parser likes to supply a span like + // that for EOF, in particular. + + if lo.col_display == hi.col_display && lo.line == hi.line { + hi.col_display += 1; } - let (lo, hi) = fixup_lo_hi(span_label.span); + let label = label.as_ref().map(|m| emitter.translate_message(m, args).to_string()); if lo.line != hi.line { let ml = MultilineAnnotation { @@ -2250,11 +2232,8 @@ impl FileWithAnnotatedLines { line_end: hi.line, start_col: lo.col_display, end_col: hi.col_display, - is_primary: span_label.is_primary, - label: span_label - .label - .as_ref() - .map(|m| emitter.translate_message(m, args).to_string()), + is_primary, + label, overlaps_exactly: false, }; multiline_annotations.push((lo.file, ml)); @@ -2262,11 +2241,8 @@ impl FileWithAnnotatedLines { let ann = Annotation { start_col: lo.col_display, end_col: hi.col_display, - is_primary: span_label.is_primary, - label: span_label - .label - .as_ref() - .map(|m| emitter.translate_message(m, args).to_string()), + is_primary, + label, annotation_type: AnnotationType::Singleline, }; add_annotation_to_file(&mut output, lo.file, lo.line, ann); -- cgit 1.4.1-3-g733a5 From 19d7dceed302b254f21fd77084ff6b468305058a Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 15 Nov 2022 09:58:43 +0000 Subject: remove an unnecessary `?` --- compiler/rustc_errors/src/emitter.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index cb5aadce8e3..ce5a91ef4a2 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1389,8 +1389,7 @@ impl EmitterWriter { let primary_span = msp.primary_span().unwrap_or_default(); let (Some(sm), false) = (self.sm.as_ref(), primary_span.is_dummy()) else { // If we don't have span information, emit and exit - emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?; - return Ok(()); + return emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message); }; let primary_lo = sm.lookup_char_pos(primary_span.lo()); if let Ok(pos) = -- cgit 1.4.1-3-g733a5 From 10b75cbbb04796cdf5264616493f3a20eb43ed0c Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 15 Nov 2022 15:24:54 +0000 Subject: Start emitting labels even if their pointed to file is not available locally --- compiler/rustc_errors/src/emitter.rs | 42 ++++++++++++++++++++++ src/test/ui/consts/missing_span_in_backtrace.rs | 26 ++++++++++++++ .../ui/consts/missing_span_in_backtrace.stderr | 23 ++++++++++++ src/test/ui/span/issue-71363.stderr | 2 ++ 4 files changed, 93 insertions(+) create mode 100644 src/test/ui/consts/missing_span_in_backtrace.rs create mode 100644 src/test/ui/consts/missing_span_in_backtrace.stderr (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index ce5a91ef4a2..7e1effd8378 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -773,6 +773,7 @@ impl EmitterWriter { draw_col_separator_no_space(buffer, line_offset, width_offset - 2); } + #[instrument(level = "trace", skip(self), ret)] fn render_source_line( &self, buffer: &mut StyledBuffer, @@ -804,6 +805,7 @@ impl EmitterWriter { Some(s) => normalize_whitespace(&s), None => return Vec::new(), }; + trace!(?source_string); let line_offset = buffer.num_lines(); @@ -1323,6 +1325,7 @@ impl EmitterWriter { } } + #[instrument(level = "trace", skip(self, args), ret)] fn emit_message_default( &mut self, msp: &MultiSpan, @@ -1384,6 +1387,7 @@ impl EmitterWriter { } } let mut annotated_files = FileWithAnnotatedLines::collect_annotations(self, args, msp); + trace!("{annotated_files:#?}"); // Make sure our primary file comes first let primary_span = msp.primary_span().unwrap_or_default(); @@ -1402,6 +1406,42 @@ impl EmitterWriter { for annotated_file in annotated_files { // we can't annotate anything if the source is unavailable. if !sm.ensure_source_file_source_present(annotated_file.file.clone()) { + if !self.short_message { + // We'll just print an unannotated message. + for line in annotated_file.lines { + let mut annotations = line.annotations.clone(); + annotations.sort_by_key(|a| Reverse(a.start_col)); + let mut line_idx = buffer.num_lines(); + buffer.append( + line_idx, + &format!( + "{}:{}:{}", + sm.filename_for_diagnostics(&annotated_file.file.name), + sm.doctest_offset_line(&annotated_file.file.name, line.line_index), + annotations[0].start_col + 1, + ), + Style::LineAndColumn, + ); + let prefix = if annotations.len() > 1 { + buffer.prepend(line_idx, "--> ", Style::LineNumber); + line_idx += 1; + "note: " + } else { + ": " + }; + for (i, annotation) in annotations.into_iter().enumerate() { + if let Some(label) = &annotation.label { + let style = if annotation.is_primary { + Style::LabelPrimary + } else { + Style::LabelSecondary + }; + buffer.append(line_idx + i, prefix, style); + buffer.append(line_idx + i, label, style); + } + } + } + } continue; } @@ -1648,6 +1688,7 @@ impl EmitterWriter { multilines.extend(&to_add); } } + trace!("buffer: {:#?}", buffer.render()); } if let Some(tracked) = emitted_at { @@ -1971,6 +2012,7 @@ impl EmitterWriter { Ok(()) } + #[instrument(level = "trace", skip(self, args, code, children, suggestions))] fn emit_messages_default( &mut self, level: &Level, diff --git a/src/test/ui/consts/missing_span_in_backtrace.rs b/src/test/ui/consts/missing_span_in_backtrace.rs new file mode 100644 index 00000000000..dd4ee3bed44 --- /dev/null +++ b/src/test/ui/consts/missing_span_in_backtrace.rs @@ -0,0 +1,26 @@ +// compile-flags: -Z simulate-remapped-rust-src-base=/rustc/xyz -Z translate-remapped-path-to-local-path=no + +#![feature(const_swap)] +#![feature(const_mut_refs)] +use std::{ + mem::{self, MaybeUninit}, + ptr, +}; + +const X: () = { + let mut ptr1 = &1; + let mut ptr2 = &2; + + // Swap them, bytewise. + unsafe { + ptr::swap_nonoverlapping( + &mut ptr1 as *mut _ as *mut MaybeUninit, + &mut ptr2 as *mut _ as *mut MaybeUninit, + mem::size_of::<&i32>(), + ); + } +}; + +fn main() { + X +} diff --git a/src/test/ui/consts/missing_span_in_backtrace.stderr b/src/test/ui/consts/missing_span_in_backtrace.stderr new file mode 100644 index 00000000000..9969d5b63e7 --- /dev/null +++ b/src/test/ui/consts/missing_span_in_backtrace.stderr @@ -0,0 +1,23 @@ +error[E0080]: evaluation of constant value failed +/rustc/xyz/library/core/src/ptr/mod.rs:929:14: inside `swap_nonoverlapping::>` at /rustc/xyz/library/core/src/ptr/mod.rs:929:14 +/rustc/xyz/library/core/src/ptr/mod.rs:948:9: inside `ptr::swap_nonoverlapping_simple_untyped::>` at /rustc/xyz/library/core/src/ptr/mod.rs:948:9 +--> /rustc/xyz/library/core/src/ptr/mod.rs:1139:9 +note: unable to copy parts of a pointer from memory at alloc6+0x1 +note: inside `std::ptr::read::>>` at /rustc/xyz/library/core/src/ptr/mod.rs:1139:9 +/rustc/xyz/library/core/src/mem/mod.rs:776:17: inside `mem::swap_simple::>>` at /rustc/xyz/library/core/src/mem/mod.rs:776:17 + | + ::: $DIR/missing_span_in_backtrace.rs:16:9 + | +LL | / ptr::swap_nonoverlapping( +LL | | &mut ptr1 as *mut _ as *mut MaybeUninit, +LL | | &mut ptr2 as *mut _ as *mut MaybeUninit, +LL | | mem::size_of::<&i32>(), +LL | | ); + | |_________- inside `X` at $DIR/missing_span_in_backtrace.rs:16:9 + | + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/span/issue-71363.stderr b/src/test/ui/span/issue-71363.stderr index 04e2b46c317..c0268ec683f 100644 --- a/src/test/ui/span/issue-71363.stderr +++ b/src/test/ui/span/issue-71363.stderr @@ -7,6 +7,7 @@ error[E0277]: `MyError` doesn't implement `std::fmt::Display` = help: the trait `std::fmt::Display` is not implemented for `MyError` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead note: required by a bound in `std::error::Error` +/rustc/xyz/library/core/src/error.rs:31:26: required by this bound in `std::error::Error` error[E0277]: `MyError` doesn't implement `Debug` --> $DIR/issue-71363.rs:4:6 @@ -17,6 +18,7 @@ error[E0277]: `MyError` doesn't implement `Debug` = help: the trait `Debug` is not implemented for `MyError` = note: add `#[derive(Debug)]` to `MyError` or manually `impl Debug for MyError` note: required by a bound in `std::error::Error` +/rustc/xyz/library/core/src/error.rs:31:18: required by this bound in `std::error::Error` help: consider annotating `MyError` with `#[derive(Debug)]` | 3 | #[derive(Debug)] -- cgit 1.4.1-3-g733a5 From f89d6236aa60ddc955c1f4dd778e9b8abba9ad9a Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 29 Nov 2022 13:35:13 +0000 Subject: Properly indent messages --- compiler/rustc_errors/src/emitter.rs | 24 ++++++++++++++++------ .../ui/consts/missing_span_in_backtrace.stderr | 12 ++++++----- src/test/ui/span/issue-71363.stderr | 8 ++++++-- 3 files changed, 31 insertions(+), 13 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 7e1effd8378..4df2198fb0e 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1408,7 +1408,7 @@ impl EmitterWriter { if !sm.ensure_source_file_source_present(annotated_file.file.clone()) { if !self.short_message { // We'll just print an unannotated message. - for line in annotated_file.lines { + for (annotation_id, line) in annotated_file.lines.into_iter().enumerate() { let mut annotations = line.annotations.clone(); annotations.sort_by_key(|a| Reverse(a.start_col)); let mut line_idx = buffer.num_lines(); @@ -1422,12 +1422,12 @@ impl EmitterWriter { ), Style::LineAndColumn, ); - let prefix = if annotations.len() > 1 { + if annotation_id == 0 { buffer.prepend(line_idx, "--> ", Style::LineNumber); + for _ in 0..max_line_num_len { + buffer.prepend(line_idx, " ", Style::NoStyle); + } line_idx += 1; - "note: " - } else { - ": " }; for (i, annotation) in annotations.into_iter().enumerate() { if let Some(label) = &annotation.label { @@ -1436,7 +1436,19 @@ impl EmitterWriter { } else { Style::LabelSecondary }; - buffer.append(line_idx + i, prefix, style); + if annotation_id == 0 { + buffer.prepend(line_idx, " |", Style::LineNumber); + for _ in 0..max_line_num_len { + buffer.prepend(line_idx, " ", Style::NoStyle); + } + line_idx += 1; + buffer.append(line_idx + i, " = note: ", style); + for _ in 0..max_line_num_len { + buffer.prepend(line_idx, " ", Style::NoStyle); + } + } else { + buffer.append(line_idx + i, ": ", style); + } buffer.append(line_idx + i, label, style); } } diff --git a/src/test/ui/consts/missing_span_in_backtrace.stderr b/src/test/ui/consts/missing_span_in_backtrace.stderr index f2a79a1d3d3..b8c20df8700 100644 --- a/src/test/ui/consts/missing_span_in_backtrace.stderr +++ b/src/test/ui/consts/missing_span_in_backtrace.stderr @@ -1,16 +1,18 @@ error[E0080]: evaluation of constant value failed -/rustc/xyz/library/core/src/ptr/mod.rs:1135:9: unable to copy parts of a pointer from memory at alloc10 + --> /rustc/xyz/library/core/src/ptr/mod.rs:1135:9 + | + = note: unable to copy parts of a pointer from memory at alloc10 | = help: this code performed an operation that depends on the underlying bytes representing a pointer = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported note: inside `std::ptr::read::>>` -/rustc/xyz/library/core/src/ptr/mod.rs:1135:9 + --> /rustc/xyz/library/core/src/ptr/mod.rs:1135:9 note: inside `mem::swap_simple::>>` -/rustc/xyz/library/core/src/mem/mod.rs:773:17 + --> /rustc/xyz/library/core/src/mem/mod.rs:773:17 note: inside `ptr::swap_nonoverlapping_simple_untyped::>` -/rustc/xyz/library/core/src/ptr/mod.rs:944:9 + --> /rustc/xyz/library/core/src/ptr/mod.rs:944:9 note: inside `swap_nonoverlapping::>` -/rustc/xyz/library/core/src/ptr/mod.rs:925:14 + --> /rustc/xyz/library/core/src/ptr/mod.rs:925:14 note: inside `X` --> $DIR/missing_span_in_backtrace.rs:16:9 | diff --git a/src/test/ui/span/issue-71363.stderr b/src/test/ui/span/issue-71363.stderr index c0268ec683f..789a386bf66 100644 --- a/src/test/ui/span/issue-71363.stderr +++ b/src/test/ui/span/issue-71363.stderr @@ -7,7 +7,9 @@ error[E0277]: `MyError` doesn't implement `std::fmt::Display` = help: the trait `std::fmt::Display` is not implemented for `MyError` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead note: required by a bound in `std::error::Error` -/rustc/xyz/library/core/src/error.rs:31:26: required by this bound in `std::error::Error` + --> /rustc/xyz/library/core/src/error.rs:31:26 + | + = note: required by this bound in `std::error::Error` error[E0277]: `MyError` doesn't implement `Debug` --> $DIR/issue-71363.rs:4:6 @@ -18,7 +20,9 @@ error[E0277]: `MyError` doesn't implement `Debug` = help: the trait `Debug` is not implemented for `MyError` = note: add `#[derive(Debug)]` to `MyError` or manually `impl Debug for MyError` note: required by a bound in `std::error::Error` -/rustc/xyz/library/core/src/error.rs:31:18: required by this bound in `std::error::Error` + --> /rustc/xyz/library/core/src/error.rs:31:18 + | + = note: required by this bound in `std::error::Error` help: consider annotating `MyError` with `#[derive(Debug)]` | 3 | #[derive(Debug)] -- cgit 1.4.1-3-g733a5