Change message type in bug functions.

From `impl Into<DiagnosticMessage>` to `impl Into<Cow<'static, str>>`.

Because these functions don't produce user-facing output and we don't
want their strings to be translated.
This commit is contained in:
Nicholas Nethercote 2024-02-21 14:51:59 +11:00
parent 1547c076bf
commit d602394827
5 changed files with 36 additions and 23 deletions

View File

@ -1,6 +1,9 @@
use std::borrow::{Borrow, Cow};
use std::cmp;
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use std::iter;
use std::ops::Bound;
use std::ops::Deref; use std::ops::Deref;
use std::{borrow::Borrow, cmp, iter, ops::Bound};
use rustc_index::Idx; use rustc_index::Idx;
use tracing::debug; use tracing::debug;
@ -32,7 +35,7 @@ where
pub trait LayoutCalculator { pub trait LayoutCalculator {
type TargetDataLayoutRef: Borrow<TargetDataLayout>; type TargetDataLayoutRef: Borrow<TargetDataLayout>;
fn delayed_bug(&self, txt: String); fn delayed_bug(&self, txt: impl Into<Cow<'static, str>>);
fn current_data_layout(&self) -> Self::TargetDataLayoutRef; fn current_data_layout(&self) -> Self::TargetDataLayoutRef;
fn scalar_pair<FieldIdx: Idx, VariantIdx: Idx>( fn scalar_pair<FieldIdx: Idx, VariantIdx: Idx>(

View File

@ -1076,32 +1076,36 @@ impl DiagCtxt {
// Functions beginning with `struct_`/`create_` create a diagnostic. Other // Functions beginning with `struct_`/`create_` create a diagnostic. Other
// functions create and emit a diagnostic all in one go. // functions create and emit a diagnostic all in one go.
impl DiagCtxt { impl DiagCtxt {
// No `#[rustc_lint_diagnostics]` because bug messages aren't user-facing. // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing.
#[track_caller] #[track_caller]
pub fn struct_bug(&self, msg: impl Into<DiagMessage>) -> Diag<'_, BugAbort> { pub fn struct_bug(&self, msg: impl Into<Cow<'static, str>>) -> Diag<'_, BugAbort> {
Diag::new(self, Bug, msg) Diag::new(self, Bug, msg.into())
} }
// No `#[rustc_lint_diagnostics]` because bug messages aren't user-facing. // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing.
#[track_caller] #[track_caller]
pub fn bug(&self, msg: impl Into<DiagMessage>) -> ! { pub fn bug(&self, msg: impl Into<Cow<'static, str>>) -> ! {
self.struct_bug(msg).emit() self.struct_bug(msg).emit()
} }
// No `#[rustc_lint_diagnostics]` because bug messages aren't user-facing. // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing.
#[track_caller] #[track_caller]
pub fn struct_span_bug( pub fn struct_span_bug(
&self, &self,
span: impl Into<MultiSpan>, span: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>, msg: impl Into<Cow<'static, str>>,
) -> Diag<'_, BugAbort> { ) -> Diag<'_, BugAbort> {
self.struct_bug(msg).with_span(span) self.struct_bug(msg).with_span(span)
} }
// No `#[rustc_lint_diagnostics]` because bug messages aren't user-facing. // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing.
#[track_caller] #[track_caller]
pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) -> ! { pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<Cow<'static, str>>) -> ! {
self.struct_span_bug(span, msg).emit() self.struct_span_bug(span, msg.into()).emit()
} }
#[track_caller] #[track_caller]
@ -1215,24 +1219,28 @@ impl DiagCtxt {
} }
/// Ensures that an error is printed. See `Level::DelayedBug`. /// Ensures that an error is printed. See `Level::DelayedBug`.
// No `#[rustc_lint_diagnostics]` because bug messages aren't user-facing. //
// No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing.
#[track_caller] #[track_caller]
pub fn delayed_bug(&self, msg: impl Into<DiagMessage>) -> ErrorGuaranteed { pub fn delayed_bug(&self, msg: impl Into<Cow<'static, str>>) -> ErrorGuaranteed {
Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg).emit() Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg.into()).emit()
} }
/// Ensures that an error is printed. See `Level::DelayedBug`. /// Ensures that an error is printed. See `Level::DelayedBug`.
/// ///
/// Note: this function used to be called `delay_span_bug`. It was renamed /// Note: this function used to be called `delay_span_bug`. It was renamed
/// to match similar functions like `span_err`, `span_warn`, etc. /// to match similar functions like `span_err`, `span_warn`, etc.
// No `#[rustc_lint_diagnostics]` because bug messages aren't user-facing. //
// No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing.
#[track_caller] #[track_caller]
pub fn span_delayed_bug( pub fn span_delayed_bug(
&self, &self,
sp: impl Into<MultiSpan>, sp: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>, msg: impl Into<Cow<'static, str>>,
) -> ErrorGuaranteed { ) -> ErrorGuaranteed {
Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg).with_span(sp).emit() Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg.into()).with_span(sp).emit()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]

View File

@ -17,6 +17,7 @@ use rustc_target::abi::call::FnAbi;
use rustc_target::abi::*; use rustc_target::abi::*;
use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target}; use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target};
use std::borrow::Cow;
use std::cmp; use std::cmp;
use std::fmt; use std::fmt;
use std::num::NonZero; use std::num::NonZero;
@ -268,7 +269,7 @@ pub struct LayoutCx<'tcx, C> {
impl<'tcx> LayoutCalculator for LayoutCx<'tcx, TyCtxt<'tcx>> { impl<'tcx> LayoutCalculator for LayoutCx<'tcx, TyCtxt<'tcx>> {
type TargetDataLayoutRef = &'tcx TargetDataLayout; type TargetDataLayoutRef = &'tcx TargetDataLayout;
fn delayed_bug(&self, txt: String) { fn delayed_bug(&self, txt: impl Into<Cow<'static, str>>) {
self.tcx.dcx().delayed_bug(txt); self.tcx.dcx().delayed_bug(txt);
} }

View File

@ -13,7 +13,7 @@ use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
use crate::ty::{List, ParamEnv}; use crate::ty::{List, ParamEnv};
use hir::def::DefKind; use hir::def::DefKind;
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
use rustc_errors::{DiagArgValue, DiagMessage, ErrorGuaranteed, IntoDiagnosticArg, MultiSpan}; use rustc_errors::{DiagArgValue, ErrorGuaranteed, IntoDiagnosticArg, MultiSpan};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::LangItem; use rustc_hir::LangItem;
@ -1543,7 +1543,7 @@ impl<'tcx> Ty<'tcx> {
pub fn new_error_with_message<S: Into<MultiSpan>>( pub fn new_error_with_message<S: Into<MultiSpan>>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
span: S, span: S,
msg: impl Into<DiagMessage>, msg: impl Into<Cow<'static, str>>,
) -> Ty<'tcx> { ) -> Ty<'tcx> {
let reported = tcx.dcx().span_delayed_bug(span, msg); let reported = tcx.dcx().span_delayed_bug(span, msg);
Ty::new(tcx, Error(reported)) Ty::new(tcx, Error(reported))

View File

@ -1,5 +1,6 @@
//! Compute the binary representation of a type //! Compute the binary representation of a type
use std::borrow::Cow;
use std::fmt; use std::fmt;
use base_db::salsa::Cycle; use base_db::salsa::Cycle;
@ -114,8 +115,8 @@ struct LayoutCx<'a> {
impl<'a> LayoutCalculator for LayoutCx<'a> { impl<'a> LayoutCalculator for LayoutCx<'a> {
type TargetDataLayoutRef = &'a TargetDataLayout; type TargetDataLayoutRef = &'a TargetDataLayout;
fn delayed_bug(&self, txt: String) { fn delayed_bug(&self, txt: impl Into<Cow<'static, str>>) {
never!("{}", txt); never!("{}", txt.into());
} }
fn current_data_layout(&self) -> &'a TargetDataLayout { fn current_data_layout(&self) -> &'a TargetDataLayout {