Skip to content

Commit 91ad4e3

Browse files
committed
Add Handler::struct_diagnostic()
This unifies the struct_{warn,error,fatal}() methods in one generic method.
1 parent aa8e761 commit 91ad4e3

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

compiler/rustc_errors/src/diagnostic_builder.rs

+30
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ pub trait EmissionGuarantee: Sized {
8484
/// of `Self` without actually performing the emission.
8585
#[track_caller]
8686
fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self;
87+
88+
/// Creates a new `DiagnosticBuilder` that will return this type of guarantee.
89+
#[track_caller]
90+
fn make_diagnostic_builder(
91+
handler: &Handler,
92+
msg: impl Into<DiagnosticMessage>,
93+
) -> DiagnosticBuilder<'_, Self>;
8794
}
8895

8996
/// Private module for sealing the `IsError` helper trait.
@@ -166,6 +173,15 @@ impl EmissionGuarantee for ErrorGuaranteed {
166173
}
167174
}
168175
}
176+
177+
fn make_diagnostic_builder(
178+
handler: &Handler,
179+
msg: impl Into<DiagnosticMessage>,
180+
) -> DiagnosticBuilder<'_, Self> {
181+
DiagnosticBuilder::new_guaranteeing_error::<_, { Level::Error { lint: false } }>(
182+
handler, msg,
183+
)
184+
}
169185
}
170186

171187
impl<'a> DiagnosticBuilder<'a, ()> {
@@ -208,6 +224,13 @@ impl EmissionGuarantee for () {
208224
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
209225
}
210226
}
227+
228+
fn make_diagnostic_builder(
229+
handler: &Handler,
230+
msg: impl Into<DiagnosticMessage>,
231+
) -> DiagnosticBuilder<'_, Self> {
232+
DiagnosticBuilder::new(handler, Level::Warning(None), msg)
233+
}
211234
}
212235

213236
impl<'a> DiagnosticBuilder<'a, !> {
@@ -247,6 +270,13 @@ impl EmissionGuarantee for ! {
247270
// Then fatally error, returning `!`
248271
crate::FatalError.raise()
249272
}
273+
274+
fn make_diagnostic_builder(
275+
handler: &Handler,
276+
msg: impl Into<DiagnosticMessage>,
277+
) -> DiagnosticBuilder<'_, Self> {
278+
DiagnosticBuilder::new_fatal(handler, msg)
279+
}
250280
}
251281

252282
/// In general, the `DiagnosticBuilder` uses deref to allow access to

compiler/rustc_errors/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,15 @@ impl Handler {
641641
self.inner.borrow_mut().emit_stashed_diagnostics()
642642
}
643643

644+
/// Construct a builder with the `msg` at the level appropriate for the specific `EmissionGuarantee`.
645+
#[rustc_lint_diagnostics]
646+
pub fn struct_diagnostic<G: EmissionGuarantee>(
647+
&self,
648+
msg: impl Into<DiagnosticMessage>,
649+
) -> DiagnosticBuilder<'_, G> {
650+
G::make_diagnostic_builder(self, msg)
651+
}
652+
644653
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
645654
///
646655
/// Attempting to `.emit()` the builder will only emit if either:

compiler/rustc_session/src/parse.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::sync::{Lock, Lrc};
1212
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
1313
use rustc_errors::{
1414
error_code, fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId,
15-
DiagnosticMessage, ErrorGuaranteed, MultiSpan, StashKey,
15+
DiagnosticMessage, EmissionGuarantee, ErrorGuaranteed, MultiSpan, StashKey,
1616
};
1717
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
1818
use rustc_span::edition::Edition;
@@ -372,4 +372,12 @@ impl ParseSess {
372372
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
373373
self.span_diagnostic.struct_warn(msg)
374374
}
375+
376+
#[rustc_lint_diagnostics]
377+
pub fn struct_diagnostic<G: EmissionGuarantee>(
378+
&self,
379+
msg: impl Into<DiagnosticMessage>,
380+
) -> DiagnosticBuilder<'_, G> {
381+
self.span_diagnostic.struct_diagnostic(msg)
382+
}
375383
}

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ LL | #[derive(SessionDiagnostic)]
411411
|
412412
= help: normalized in stderr
413413
note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg`
414-
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:539:19
414+
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:569:19
415415
|
416416
LL | arg: impl IntoDiagnosticArg,
417417
| ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg`

0 commit comments

Comments
 (0)