Skip to content

Commit 2fe9f76

Browse files
authored
Rollup merge of #92802 - compiler-errors:deduplicate-stack-trace, r=oli-obk
Deduplicate lines in long const-eval stack trace Lemme know if this is kinda overkill, lol. Fixes #92796
2 parents 9298bd8 + 73ad8df commit 2fe9f76

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,37 @@ impl<'tcx> ConstEvalErr<'tcx> {
156156
}
157157
// Add spans for the stacktrace. Don't print a single-line backtrace though.
158158
if self.stacktrace.len() > 1 {
159+
// Helper closure to print duplicated lines.
160+
let mut flush_last_line = |last_frame, times| {
161+
if let Some((line, span)) = last_frame {
162+
err.span_label(span, &line);
163+
// Don't print [... additional calls ...] if the number of lines is small
164+
if times < 3 {
165+
for _ in 0..times {
166+
err.span_label(span, &line);
167+
}
168+
} else {
169+
err.span_label(
170+
span,
171+
format!("[... {} additional calls {} ...]", times, &line),
172+
);
173+
}
174+
}
175+
};
176+
177+
let mut last_frame = None;
178+
let mut times = 0;
159179
for frame_info in &self.stacktrace {
160-
err.span_label(frame_info.span, frame_info.to_string());
180+
let frame = (frame_info.to_string(), frame_info.span);
181+
if last_frame.as_ref() == Some(&frame) {
182+
times += 1;
183+
} else {
184+
flush_last_line(last_frame, times);
185+
last_frame = Some(frame);
186+
times = 0;
187+
}
161188
}
189+
flush_last_line(last_frame, times);
162190
}
163191
// Let the caller finish the job.
164192
emit(err)

src/test/ui/consts/recursive.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![allow(unused)]
2+
3+
const fn f<T>(x: T) { //~ WARN function cannot return without recursing
4+
f(x);
5+
//~^ ERROR any use of this value will cause an error
6+
//~| WARN this was previously accepted by the compiler
7+
}
8+
9+
const X: () = f(1);
10+
11+
fn main() {}

src/test/ui/consts/recursive.stderr

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
warning: function cannot return without recursing
2+
--> $DIR/recursive.rs:3:1
3+
|
4+
LL | const fn f<T>(x: T) {
5+
| ^^^^^^^^^^^^^^^^^^^ cannot return without recursing
6+
LL | f(x);
7+
| ---- recursive call site
8+
|
9+
= note: `#[warn(unconditional_recursion)]` on by default
10+
= help: a `loop` may express intention better if this is on purpose
11+
12+
error: any use of this value will cause an error
13+
--> $DIR/recursive.rs:4:5
14+
|
15+
LL | f(x);
16+
| ^^^^
17+
| |
18+
| reached the configured maximum number of stack frames
19+
| inside `f::<i32>` at $DIR/recursive.rs:4:5
20+
| [... 126 additional calls inside `f::<i32>` at $DIR/recursive.rs:4:5 ...]
21+
| inside `X` at $DIR/recursive.rs:9:15
22+
...
23+
LL | const X: () = f(1);
24+
| -------------------
25+
|
26+
= note: `#[deny(const_err)]` on by default
27+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
28+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
29+
30+
error: aborting due to previous error; 1 warning emitted
31+

0 commit comments

Comments
 (0)