Skip to content

Refactor const_eval diagnostics #113297

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

Closed
Show file tree
Hide file tree
Changes from 3 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
9 changes: 5 additions & 4 deletions compiler/rustc_const_eval/src/const_eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use rustc_span::source_map::Spanned;
use rustc_span::{ErrorGuaranteed, Span, Symbol};

use super::InterpCx;
use crate::errors::{self, FrameNote, ReportErrorExt};
use crate::errors::{self, FrameNote};
use crate::interpret::{ErrorHandled, InterpError, InterpErrorInfo, Machine, MachineStopType};
use crate::InterpErrorExt2;

/// The CTFE machine has some custom error kinds.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -166,13 +167,13 @@ where
let (our_span, frames) = get_span_and_frames();
let span = span.unwrap_or(our_span);
let err = mk(span, frames);
let interp_err = InterpErrorExt2 { err: error, span, tcx };

let mut err = tcx.sess.create_err(err);

let msg = error.diagnostic_message();
error.add_args(&tcx.sess.parse_sess.span_diagnostic, &mut err);
err.subdiagnostic(interp_err);

// Use *our* span to label the interp error
err.span_label(our_span, msg);
Copy link
Member

Choose a reason for hiding this comment

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

So this part is probably why the ui tests are now different. To fix this you probably need to eagerly translate the diagnostic message to string and add a label.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah that fixed the majority of the ui tests. Although now some are failing because the help messages are missing.

Now this makes me wonder. If we need to add this to a span message, shouldn't the newtypes better implement the rustc_errors::AddToDiagnostic?

impl AddToDiagnostic for InterpErrorExt {
    fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
    where
        F: Fn(
            &mut rustc_errors::Diagnostic,
            rustc_errors::SubdiagnosticMessage,
        ) -> rustc_errors::SubdiagnosticMessage,
    {
        diag.set_arg("named", self.named);
        let msg = f(diag, crate::fluent_generated::builtin_macros_format_unused_arg.into());

        // here we could add our span labels with our message
        diag.span_label(self.span, msg);

        // here we can add our help messages
        diag.help("some help message");
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

I don't think that is possible. Helps and labels have their own variables and they should be eagerly substituted to prevent any pollution with named arguments. Having the IntoDiagnostic impl and doing the rest by hand might be the best we can do. But you should feel free to experiment whether AddToDiagnostic works.

Copy link
Member

Choose a reason for hiding this comment

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

Actually though, it might be possible. It's just that we have to set the primary message of the diagnostic and the label by eagarly substituting.

Copy link
Author

Choose a reason for hiding this comment

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

I'll try to experiment implementing AddToDiagnostic.

ErrorHandled::Reported(err.emit().into())
}
}
Expand Down
Loading