Skip to content

Commit 2774446

Browse files
committed
ADD - create and emit Bug support for Diagnostics
UPDATE - migrate constant span_bug to translatable diagnostic.
1 parent d1030fa commit 2774446

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,13 @@ pub struct ErroneousConstant {
585585
pub span: Span,
586586
}
587587

588+
#[derive(Diagnostic)]
589+
#[diag(codegen_ssa_polymorphic_constant_too_generic)]
590+
pub struct PolymorphicConstantTooGeneric {
591+
#[primary_span]
592+
pub span: Span,
593+
}
594+
588595
#[derive(Diagnostic)]
589596
#[diag(codegen_ssa_shuffle_indices_evaluation)]
590597
pub struct ShuffleIndicesEvaluation {

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
4848
self.cx.tcx().sess.emit_err(errors::ErroneousConstant { span: constant.span });
4949
}
5050
ErrorHandled::TooGeneric => {
51-
span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
51+
self.cx
52+
.tcx()
53+
.sess
54+
.diagnostic()
55+
.emit_bug(errors::PolymorphicConstantTooGeneric { span: constant.span });
5256
}
5357
}
5458
err

compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,5 @@ codegen_ssa_invalid_monomorphization_floating_point_vector = invalid monomorphiz
225225
codegen_ssa_invalid_monomorphization_floating_point_type = invalid monomorphization of `{$name}` intrinsic: `{$in_ty}` is not a floating-point type
226226
227227
codegen_ssa_invalid_monomorphization_unrecognized_intrinsic = invalid monomorphization of `{$name}` intrinsic: unrecognized intrinsic `{$name}`
228+
229+
codegen_ssa_polymorphic_constant_too_generic = codegen encountered polymorphic constant: TooGeneric

compiler/rustc_errors/src/diagnostic_builder.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::diagnostic::IntoDiagnosticArg;
22
use crate::{
33
Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed,
4-
SubdiagnosticMessage,
4+
ExplicitBug, SubdiagnosticMessage,
55
};
66
use crate::{Handler, Level, MultiSpan, StashKey};
77
use rustc_lint_defs::Applicability;
@@ -12,6 +12,7 @@ use std::borrow::Cow;
1212
use std::fmt::{self, Debug};
1313
use std::marker::PhantomData;
1414
use std::ops::{Deref, DerefMut};
15+
use std::panic;
1516
use std::thread::panicking;
1617

1718
/// Trait implemented by error types. This should not be implemented manually. Instead, use
@@ -308,6 +309,58 @@ impl EmissionGuarantee for Noted {
308309
}
309310
}
310311

312+
/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
313+
/// bug struct diagnostics.
314+
#[derive(Copy, Clone)]
315+
pub struct Bug;
316+
317+
impl<'a> DiagnosticBuilder<'a, Bug> {
318+
/// Convenience function for internal use, clients should use one of the
319+
/// `struct_*` methods on [`Handler`].
320+
#[track_caller]
321+
pub(crate) fn new_bug(handler: &'a Handler, message: impl Into<DiagnosticMessage>) -> Self {
322+
let diagnostic = Diagnostic::new_with_code(Level::Bug, None, message);
323+
Self::new_diagnostic_bug(handler, diagnostic)
324+
}
325+
326+
/// Creates a new `DiagnosticBuilder` with an already constructed
327+
/// diagnostic.
328+
pub(crate) fn new_diagnostic_bug(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
329+
debug!("Created new diagnostic bug");
330+
Self {
331+
inner: DiagnosticBuilderInner {
332+
state: DiagnosticBuilderState::Emittable(handler),
333+
diagnostic: Box::new(diagnostic),
334+
},
335+
_marker: PhantomData,
336+
}
337+
}
338+
}
339+
340+
impl EmissionGuarantee for Bug {
341+
fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self {
342+
match db.inner.state {
343+
// First `.emit()` call, the `&Handler` is still available.
344+
DiagnosticBuilderState::Emittable(handler) => {
345+
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
346+
347+
handler.emit_diagnostic(&mut db.inner.diagnostic);
348+
}
349+
// `.emit()` was previously called, disallowed from repeating it.
350+
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
351+
}
352+
// Then panic. No need to return the marker type.
353+
panic::panic_any(ExplicitBug);
354+
}
355+
356+
fn make_diagnostic_builder(
357+
handler: &Handler,
358+
msg: impl Into<DiagnosticMessage>,
359+
) -> DiagnosticBuilder<'_, Self> {
360+
DiagnosticBuilder::new_bug(handler, msg)
361+
}
362+
}
363+
311364
impl<'a> DiagnosticBuilder<'a, !> {
312365
/// Convenience function for internal use, clients should use one of the
313366
/// `struct_*` methods on [`Handler`].

compiler/rustc_errors/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,20 @@ impl Handler {
11271127
self.create_fatal(fatal).emit()
11281128
}
11291129

1130+
pub fn create_bug<'a>(
1131+
&'a self,
1132+
bug: impl IntoDiagnostic<'a, diagnostic_builder::Bug>,
1133+
) -> DiagnosticBuilder<'a, diagnostic_builder::Bug> {
1134+
bug.into_diagnostic(self)
1135+
}
1136+
1137+
pub fn emit_bug<'a>(
1138+
&'a self,
1139+
bug: impl IntoDiagnostic<'a, diagnostic_builder::Bug>,
1140+
) -> diagnostic_builder::Bug {
1141+
self.create_bug(bug).emit()
1142+
}
1143+
11301144
fn emit_diag_at_span(
11311145
&self,
11321146
mut diag: Diagnostic,

0 commit comments

Comments
 (0)