Skip to content

Commit badd6a5

Browse files
committed
Auto merge of rust-lang#104449 - oli-obk:unhide_unknown_spans, r=estebank,RalfJung
Start emitting labels even if their pointed to file is not available locally r? `@estebank` cc `@RalfJung` fixes rust-lang#97699
2 parents 7701a7e + 717fdb5 commit badd6a5

File tree

103 files changed

+2196
-870
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+2196
-870
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ impl<'tcx> ConstEvalErr<'tcx> {
123123
// Helper closure to print duplicated lines.
124124
let mut flush_last_line = |last_frame, times| {
125125
if let Some((line, span)) = last_frame {
126-
err.span_label(span, &line);
126+
err.span_note(span, &line);
127127
// Don't print [... additional calls ...] if the number of lines is small
128128
if times < 3 {
129129
for _ in 0..times {
130-
err.span_label(span, &line);
130+
err.span_note(span, &line);
131131
}
132132
} else {
133-
err.span_label(
133+
err.span_note(
134134
span,
135135
format!("[... {} additional calls {} ...]", times, &line),
136136
);

compiler/rustc_const_eval/src/interpret/eval_context.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::{
1717
};
1818
use rustc_mir_dataflow::storage::always_storage_live_locals;
1919
use rustc_session::Limit;
20-
use rustc_span::{Pos, Span};
20+
use rustc_span::Span;
2121
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};
2222

2323
use super::{
@@ -256,25 +256,13 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
256256
if tcx.def_key(self.instance.def_id()).disambiguated_data.data
257257
== DefPathData::ClosureExpr
258258
{
259-
write!(f, "inside closure")?;
259+
write!(f, "inside closure")
260260
} else {
261261
// Note: this triggers a `good_path_bug` state, which means that if we ever get here
262262
// we must emit a diagnostic. We should never display a `FrameInfo` unless we
263263
// actually want to emit a warning or error to the user.
264-
write!(f, "inside `{}`", self.instance)?;
264+
write!(f, "inside `{}`", self.instance)
265265
}
266-
if !self.span.is_dummy() {
267-
let sm = tcx.sess.source_map();
268-
let lo = sm.lookup_char_pos(self.span.lo());
269-
write!(
270-
f,
271-
" at {}:{}:{}",
272-
sm.filename_for_diagnostics(&lo.file.name),
273-
lo.line,
274-
lo.col.to_usize() + 1
275-
)?;
276-
}
277-
Ok(())
278266
})
279267
}
280268
}

compiler/rustc_errors/src/emitter.rs

+81-59
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_lint_defs::pluralize;
2424

2525
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
2626
use rustc_data_structures::sync::Lrc;
27-
use rustc_error_messages::FluentArgs;
27+
use rustc_error_messages::{FluentArgs, SpanLabel};
2828
use rustc_span::hygiene::{ExpnKind, MacroKind};
2929
use std::borrow::Cow;
3030
use std::cmp::{max, min, Reverse};
@@ -773,6 +773,7 @@ impl EmitterWriter {
773773
draw_col_separator_no_space(buffer, line_offset, width_offset - 2);
774774
}
775775

776+
#[instrument(level = "trace", skip(self), ret)]
776777
fn render_source_line(
777778
&self,
778779
buffer: &mut StyledBuffer,
@@ -804,6 +805,7 @@ impl EmitterWriter {
804805
Some(s) => normalize_whitespace(&s),
805806
None => return Vec::new(),
806807
};
808+
trace!(?source_string);
807809

808810
let line_offset = buffer.num_lines();
809811

@@ -1323,6 +1325,7 @@ impl EmitterWriter {
13231325
}
13241326
}
13251327

1328+
#[instrument(level = "trace", skip(self, args), ret)]
13261329
fn emit_message_default(
13271330
&mut self,
13281331
msp: &MultiSpan,
@@ -1384,22 +1387,15 @@ impl EmitterWriter {
13841387
}
13851388
}
13861389
let mut annotated_files = FileWithAnnotatedLines::collect_annotations(self, args, msp);
1390+
trace!("{annotated_files:#?}");
13871391

13881392
// Make sure our primary file comes first
1389-
let (primary_lo, sm) = if let (Some(sm), Some(ref primary_span)) =
1390-
(self.sm.as_ref(), msp.primary_span().as_ref())
1391-
{
1392-
if !primary_span.is_dummy() {
1393-
(sm.lookup_char_pos(primary_span.lo()), sm)
1394-
} else {
1395-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1396-
return Ok(());
1397-
}
1398-
} else {
1393+
let primary_span = msp.primary_span().unwrap_or_default();
1394+
let (Some(sm), false) = (self.sm.as_ref(), primary_span.is_dummy()) else {
13991395
// If we don't have span information, emit and exit
1400-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1401-
return Ok(());
1396+
return emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message);
14021397
};
1398+
let primary_lo = sm.lookup_char_pos(primary_span.lo());
14031399
if let Ok(pos) =
14041400
annotated_files.binary_search_by(|x| x.file.name.cmp(&primary_lo.file.name))
14051401
{
@@ -1410,6 +1406,54 @@ impl EmitterWriter {
14101406
for annotated_file in annotated_files {
14111407
// we can't annotate anything if the source is unavailable.
14121408
if !sm.ensure_source_file_source_present(annotated_file.file.clone()) {
1409+
if !self.short_message {
1410+
// We'll just print an unannotated message.
1411+
for (annotation_id, line) in annotated_file.lines.into_iter().enumerate() {
1412+
let mut annotations = line.annotations.clone();
1413+
annotations.sort_by_key(|a| Reverse(a.start_col));
1414+
let mut line_idx = buffer.num_lines();
1415+
buffer.append(
1416+
line_idx,
1417+
&format!(
1418+
"{}:{}:{}",
1419+
sm.filename_for_diagnostics(&annotated_file.file.name),
1420+
sm.doctest_offset_line(&annotated_file.file.name, line.line_index),
1421+
annotations[0].start_col + 1,
1422+
),
1423+
Style::LineAndColumn,
1424+
);
1425+
if annotation_id == 0 {
1426+
buffer.prepend(line_idx, "--> ", Style::LineNumber);
1427+
for _ in 0..max_line_num_len {
1428+
buffer.prepend(line_idx, " ", Style::NoStyle);
1429+
}
1430+
line_idx += 1;
1431+
};
1432+
for (i, annotation) in annotations.into_iter().enumerate() {
1433+
if let Some(label) = &annotation.label {
1434+
let style = if annotation.is_primary {
1435+
Style::LabelPrimary
1436+
} else {
1437+
Style::LabelSecondary
1438+
};
1439+
if annotation_id == 0 {
1440+
buffer.prepend(line_idx, " |", Style::LineNumber);
1441+
for _ in 0..max_line_num_len {
1442+
buffer.prepend(line_idx, " ", Style::NoStyle);
1443+
}
1444+
line_idx += 1;
1445+
buffer.append(line_idx + i, " = note: ", style);
1446+
for _ in 0..max_line_num_len {
1447+
buffer.prepend(line_idx, " ", Style::NoStyle);
1448+
}
1449+
} else {
1450+
buffer.append(line_idx + i, ": ", style);
1451+
}
1452+
buffer.append(line_idx + i, label, style);
1453+
}
1454+
}
1455+
}
1456+
}
14131457
continue;
14141458
}
14151459

@@ -1656,6 +1700,7 @@ impl EmitterWriter {
16561700
multilines.extend(&to_add);
16571701
}
16581702
}
1703+
trace!("buffer: {:#?}", buffer.render());
16591704
}
16601705

16611706
if let Some(tracked) = emitted_at {
@@ -1979,6 +2024,7 @@ impl EmitterWriter {
19792024
Ok(())
19802025
}
19812026

2027+
#[instrument(level = "trace", skip(self, args, code, children, suggestions))]
19822028
fn emit_messages_default(
19832029
&mut self,
19842030
level: &Level,
@@ -2209,46 +2255,28 @@ impl FileWithAnnotatedLines {
22092255
let mut multiline_annotations = vec![];
22102256

22112257
if let Some(ref sm) = emitter.source_map() {
2212-
for span_label in msp.span_labels() {
2213-
let fixup_lo_hi = |span: Span| {
2214-
let lo = sm.lookup_char_pos(span.lo());
2215-
let mut hi = sm.lookup_char_pos(span.hi());
2216-
2217-
// Watch out for "empty spans". If we get a span like 6..6, we
2218-
// want to just display a `^` at 6, so convert that to
2219-
// 6..7. This is degenerate input, but it's best to degrade
2220-
// gracefully -- and the parser likes to supply a span like
2221-
// that for EOF, in particular.
2222-
2223-
if lo.col_display == hi.col_display && lo.line == hi.line {
2224-
hi.col_display += 1;
2225-
}
2226-
(lo, hi)
2258+
for SpanLabel { span, is_primary, label } in msp.span_labels() {
2259+
// If we don't have a useful span, pick the primary span if that exists.
2260+
// Worst case we'll just print an error at the top of the main file.
2261+
let span = match (span.is_dummy(), msp.primary_span()) {
2262+
(_, None) | (false, _) => span,
2263+
(true, Some(span)) => span,
22272264
};
22282265

2229-
if span_label.span.is_dummy() {
2230-
if let Some(span) = msp.primary_span() {
2231-
// if we don't know where to render the annotation, emit it as a note
2232-
// on the primary span.
2233-
2234-
let (lo, hi) = fixup_lo_hi(span);
2235-
2236-
let ann = Annotation {
2237-
start_col: lo.col_display,
2238-
end_col: hi.col_display,
2239-
is_primary: span_label.is_primary,
2240-
label: span_label
2241-
.label
2242-
.as_ref()
2243-
.map(|m| emitter.translate_message(m, args).to_string()),
2244-
annotation_type: AnnotationType::Singleline,
2245-
};
2246-
add_annotation_to_file(&mut output, lo.file, lo.line, ann);
2247-
}
2248-
continue;
2266+
let lo = sm.lookup_char_pos(span.lo());
2267+
let mut hi = sm.lookup_char_pos(span.hi());
2268+
2269+
// Watch out for "empty spans". If we get a span like 6..6, we
2270+
// want to just display a `^` at 6, so convert that to
2271+
// 6..7. This is degenerate input, but it's best to degrade
2272+
// gracefully -- and the parser likes to supply a span like
2273+
// that for EOF, in particular.
2274+
2275+
if lo.col_display == hi.col_display && lo.line == hi.line {
2276+
hi.col_display += 1;
22492277
}
22502278

2251-
let (lo, hi) = fixup_lo_hi(span_label.span);
2279+
let label = label.as_ref().map(|m| emitter.translate_message(m, args).to_string());
22522280

22532281
if lo.line != hi.line {
22542282
let ml = MultilineAnnotation {
@@ -2257,23 +2285,17 @@ impl FileWithAnnotatedLines {
22572285
line_end: hi.line,
22582286
start_col: lo.col_display,
22592287
end_col: hi.col_display,
2260-
is_primary: span_label.is_primary,
2261-
label: span_label
2262-
.label
2263-
.as_ref()
2264-
.map(|m| emitter.translate_message(m, args).to_string()),
2288+
is_primary,
2289+
label,
22652290
overlaps_exactly: false,
22662291
};
22672292
multiline_annotations.push((lo.file, ml));
22682293
} else {
22692294
let ann = Annotation {
22702295
start_col: lo.col_display,
22712296
end_col: hi.col_display,
2272-
is_primary: span_label.is_primary,
2273-
label: span_label
2274-
.label
2275-
.as_ref()
2276-
.map(|m| emitter.translate_message(m, args).to_string()),
2297+
is_primary,
2298+
label,
22772299
annotation_type: AnnotationType::Singleline,
22782300
};
22792301
add_annotation_to_file(&mut output, lo.file, lo.line, ann);

src/test/ui/borrowck/issue-81899.stderr

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
error[E0080]: evaluation of constant value failed
22
--> $DIR/issue-81899.rs:11:5
33
|
4-
LL | const _CONST: &[u8] = &f(&[], |_| {});
5-
| -------------- inside `_CONST` at $DIR/issue-81899.rs:4:24
6-
...
4+
LL | panic!()
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/issue-81899.rs:11:5
6+
|
7+
note: inside `f::<[closure@$DIR/issue-81899.rs:4:31: 4:34]>`
8+
--> $DIR/issue-81899.rs:11:5
9+
|
710
LL | panic!()
811
| ^^^^^^^^
9-
| |
10-
| the evaluated program panicked at 'explicit panic', $DIR/issue-81899.rs:11:5
11-
| inside `f::<[closure@$DIR/issue-81899.rs:4:31: 4:34]>` at $SRC_DIR/std/src/panic.rs:LL:COL
12+
note: inside `_CONST`
13+
--> $DIR/issue-81899.rs:4:24
1214
|
15+
LL | const _CONST: &[u8] = &f(&[], |_| {});
16+
| ^^^^^^^^^^^^^^
1317
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1418

1519
note: erroneous constant used

src/test/ui/borrowck/issue-88434-minimal-example.stderr

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
error[E0080]: evaluation of constant value failed
22
--> $DIR/issue-88434-minimal-example.rs:10:5
33
|
4-
LL | const _CONST: &() = &f(&|_| {});
5-
| ---------- inside `_CONST` at $DIR/issue-88434-minimal-example.rs:3:22
6-
...
4+
LL | panic!()
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/issue-88434-minimal-example.rs:10:5
6+
|
7+
note: inside `f::<[closure@$DIR/issue-88434-minimal-example.rs:3:25: 3:28]>`
8+
--> $DIR/issue-88434-minimal-example.rs:10:5
9+
|
710
LL | panic!()
811
| ^^^^^^^^
9-
| |
10-
| the evaluated program panicked at 'explicit panic', $DIR/issue-88434-minimal-example.rs:10:5
11-
| inside `f::<[closure@$DIR/issue-88434-minimal-example.rs:3:25: 3:28]>` at $SRC_DIR/std/src/panic.rs:LL:COL
12+
note: inside `_CONST`
13+
--> $DIR/issue-88434-minimal-example.rs:3:22
1214
|
15+
LL | const _CONST: &() = &f(&|_| {});
16+
| ^^^^^^^^^^
1317
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1418

1519
note: erroneous constant used

src/test/ui/borrowck/issue-88434-removal-index-should-be-less.stderr

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
error[E0080]: evaluation of constant value failed
22
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
33
|
4-
LL | const _CONST: &[u8] = &f(&[], |_| {});
5-
| -------------- inside `_CONST` at $DIR/issue-88434-removal-index-should-be-less.rs:3:24
6-
...
4+
LL | panic!()
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/issue-88434-removal-index-should-be-less.rs:10:5
6+
|
7+
note: inside `f::<[closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34]>`
8+
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
9+
|
710
LL | panic!()
811
| ^^^^^^^^
9-
| |
10-
| the evaluated program panicked at 'explicit panic', $DIR/issue-88434-removal-index-should-be-less.rs:10:5
11-
| inside `f::<[closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34]>` at $SRC_DIR/std/src/panic.rs:LL:COL
12+
note: inside `_CONST`
13+
--> $DIR/issue-88434-removal-index-should-be-less.rs:3:24
1214
|
15+
LL | const _CONST: &[u8] = &f(&[], |_| {});
16+
| ^^^^^^^^^^^^^^
1317
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1418

1519
note: erroneous constant used

0 commit comments

Comments
 (0)