Skip to content

Skip redundant frames in const recursion errors #136649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const_eval_fn_ptr_call =
function pointers need an RFC before allowed to be called in {const_eval_const_context}s
const_eval_frame_note = {$times ->
[0] {const_eval_frame_note_inner}
*[other] [... {$times} additional calls {const_eval_frame_note_inner} ...]
*[other] [... {$times} additional calls ...] {const_eval_frame_note_inner}
}

const_eval_frame_note_inner = inside {$where_ ->
Expand Down
21 changes: 10 additions & 11 deletions compiler/rustc_const_eval/src/const_eval/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::mem;

use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, Diagnostic, IntoDiagArg};
use rustc_middle::mir::AssertKind;
use rustc_middle::mir::interpret::{Provenance, ReportedErrorInfo};
Expand All @@ -9,7 +10,7 @@ use rustc_middle::ty::{ConstInt, TyCtxt};
use rustc_span::{Span, Symbol};

use super::CompileTimeMachine;
use crate::errors::{self, FrameNote, ReportErrorExt};
use crate::errors::{FrameNote, ReportErrorExt};
use crate::interpret::{
ErrorHandled, Frame, InterpErrorInfo, InterpErrorKind, MachineStopType, err_inval,
err_machine_stop,
Expand Down Expand Up @@ -68,7 +69,7 @@ impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind {
pub fn get_span_and_frames<'tcx>(
tcx: TyCtxtAt<'tcx>,
stack: &[Frame<'tcx, impl Provenance, impl Sized>],
) -> (Span, Vec<errors::FrameNote>) {
) -> (Span, Vec<FrameNote>) {
let mut stacktrace = Frame::generate_stacktrace_from_stack(stack);
// Filter out `requires_caller_location` frames.
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*tcx));
Expand All @@ -79,8 +80,8 @@ pub fn get_span_and_frames<'tcx>(
// Add notes to the backtrace. Don't print a single-line backtrace though.
if stacktrace.len() > 1 {
// Helper closure to print duplicated lines.
let mut add_frame = |mut frame: errors::FrameNote| {
frames.push(errors::FrameNote { times: 0, ..frame.clone() });
let mut add_frame = |mut frame: FrameNote| {
frames.push(FrameNote { times: 0, ..frame.clone() });
// Don't print [... additional calls ...] if the number of lines is small
if frame.times < 3 {
let times = frame.times;
Comment on lines 85 to 87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this logic still correct? It seems to be we could now have a situation where we see 2 frames that both have been seen already, so last_frame.times is 2, but these were actually 2 different frames and then this here will print the wrong thing.

Expand All @@ -91,21 +92,19 @@ pub fn get_span_and_frames<'tcx>(
}
};

let mut last_frame: Option<errors::FrameNote> = None;
let mut last_frame: Option<FrameNote> = None;
let mut seen = FxHashSet::default();
for frame_info in &stacktrace {
let frame = frame_info.as_note(*tcx);
match last_frame.as_mut() {
Some(last_frame)
if last_frame.span == frame.span
&& last_frame.where_ == frame.where_
&& last_frame.instance == frame.instance =>
{
Some(last_frame) if !seen.insert(frame.clone()) => {
Copy link
Member

@RalfJung RalfJung Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments explaining what is done and why.

I am not sure I agree with removing duplicates from a backtrace -- that seems like it could be quite confusing. I can't quite follow the logic for printing the "N frames skipped" here, is that always printed when seeing a duplicate?

This new logic abuses the times field, where it no longer means "we saw this function N+1 times" -- I think that's too hacky and confusing.

last_frame.times += 1;
}
Some(last_frame) => {
add_frame(mem::replace(last_frame, frame));
}
None => {
seen.insert(frame.clone());
last_frame = Some(frame);
}
}
Expand Down Expand Up @@ -184,7 +183,7 @@ pub(super) fn lint<'tcx, L>(
tcx: TyCtxtAt<'tcx>,
machine: &CompileTimeMachine<'tcx>,
lint: &'static rustc_session::lint::Lint,
decorator: impl FnOnce(Vec<errors::FrameNote>) -> L,
decorator: impl FnOnce(Vec<FrameNote>) -> L,
) where
L: for<'a> rustc_errors::LintDiagnostic<'a, ()>,
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ pub(crate) struct NonConstImplNote {
pub span: Span,
}

#[derive(Subdiagnostic, Clone)]
#[derive(Subdiagnostic, Clone, PartialEq, Eq, Hash)]
#[note(const_eval_frame_note)]
pub struct FrameNote {
#[primary_span]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/recursive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ note: inside `f::<i32>`
|
LL | f(x);
| ^^^^
note: [... 126 additional calls inside `f::<i32>` ...]
note: [... 126 additional calls ...] inside `f::<i32>`
--> $DIR/recursive.rs:4:5
|
LL | f(x);
Expand Down
Loading
Loading