Skip to content

Commit 8661da2

Browse files
committed
Use DiagnosticBuilder::new more.
By making it generic, instead of only for `EmissionGuarantee = ()`, we can use it everywhere.
1 parent 7d03577 commit 8661da2

File tree

2 files changed

+36
-68
lines changed

2 files changed

+36
-68
lines changed

compiler/rustc_errors/src/diagnostic_builder.rs

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -169,41 +169,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
169169
handler: &Handler,
170170
msg: impl Into<DiagnosticMessage>,
171171
) -> DiagnosticBuilder<'_, Self> {
172-
DiagnosticBuilder {
173-
inner: DiagnosticBuilderInner {
174-
state: DiagnosticBuilderState::Emittable(handler),
175-
diagnostic: Box::new(Diagnostic::new(Level::Error { lint: false }, msg)),
176-
},
177-
_marker: PhantomData,
178-
}
179-
}
180-
}
181-
182-
impl<'a> DiagnosticBuilder<'a, ()> {
183-
/// Convenience function for internal use, clients should use one of the
184-
/// `struct_*` methods on [`Handler`].
185-
#[track_caller]
186-
pub(crate) fn new<M: Into<DiagnosticMessage>>(
187-
handler: &'a Handler,
188-
level: Level,
189-
message: M,
190-
) -> Self {
191-
let diagnostic = Diagnostic::new(level, message);
192-
Self::new_diagnostic(handler, diagnostic)
193-
}
194-
195-
/// Creates a new `DiagnosticBuilder` with an already constructed
196-
/// diagnostic.
197-
#[track_caller]
198-
pub(crate) fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
199-
debug!("Created new diagnostic");
200-
Self {
201-
inner: DiagnosticBuilderInner {
202-
state: DiagnosticBuilderState::Emittable(handler),
203-
diagnostic: Box::new(diagnostic),
204-
},
205-
_marker: PhantomData,
206-
}
172+
DiagnosticBuilder::new(handler, Level::Error { lint: false }, msg)
207173
}
208174
}
209175

@@ -254,13 +220,7 @@ impl EmissionGuarantee for Noted {
254220
handler: &Handler,
255221
msg: impl Into<DiagnosticMessage>,
256222
) -> DiagnosticBuilder<'_, Self> {
257-
DiagnosticBuilder {
258-
inner: DiagnosticBuilderInner {
259-
state: DiagnosticBuilderState::Emittable(handler),
260-
diagnostic: Box::new(Diagnostic::new(Level::Note, msg)),
261-
},
262-
_marker: PhantomData,
263-
}
223+
DiagnosticBuilder::new(handler, Level::Note, msg)
264224
}
265225
}
266226

@@ -289,13 +249,7 @@ impl EmissionGuarantee for Bug {
289249
handler: &Handler,
290250
msg: impl Into<DiagnosticMessage>,
291251
) -> DiagnosticBuilder<'_, Self> {
292-
DiagnosticBuilder {
293-
inner: DiagnosticBuilderInner {
294-
state: DiagnosticBuilderState::Emittable(handler),
295-
diagnostic: Box::new(Diagnostic::new(Level::Bug, msg)),
296-
},
297-
_marker: PhantomData,
298-
}
252+
DiagnosticBuilder::new(handler, Level::Bug, msg)
299253
}
300254
}
301255

@@ -319,13 +273,7 @@ impl EmissionGuarantee for ! {
319273
handler: &Handler,
320274
msg: impl Into<DiagnosticMessage>,
321275
) -> DiagnosticBuilder<'_, Self> {
322-
DiagnosticBuilder {
323-
inner: DiagnosticBuilderInner {
324-
state: DiagnosticBuilderState::Emittable(handler),
325-
diagnostic: Box::new(Diagnostic::new(Level::Fatal, msg)),
326-
},
327-
_marker: PhantomData,
328-
}
276+
DiagnosticBuilder::new(handler, Level::Fatal, msg)
329277
}
330278
}
331279

@@ -349,13 +297,7 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
349297
handler: &Handler,
350298
msg: impl Into<DiagnosticMessage>,
351299
) -> DiagnosticBuilder<'_, Self> {
352-
DiagnosticBuilder {
353-
inner: DiagnosticBuilderInner {
354-
state: DiagnosticBuilderState::Emittable(handler),
355-
diagnostic: Box::new(Diagnostic::new(Level::Fatal, msg)),
356-
},
357-
_marker: PhantomData,
358-
}
300+
DiagnosticBuilder::new(handler, Level::Fatal, msg)
359301
}
360302
}
361303

@@ -397,6 +339,32 @@ impl<G: EmissionGuarantee> DerefMut for DiagnosticBuilder<'_, G> {
397339
}
398340

399341
impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
342+
/// Convenience function for internal use, clients should use one of the
343+
/// `struct_*` methods on [`Handler`].
344+
#[track_caller]
345+
pub(crate) fn new<M: Into<DiagnosticMessage>>(
346+
handler: &'a Handler,
347+
level: Level,
348+
message: M,
349+
) -> Self {
350+
let diagnostic = Diagnostic::new(level, message);
351+
Self::new_diagnostic(handler, diagnostic)
352+
}
353+
354+
/// Creates a new `DiagnosticBuilder` with an already constructed
355+
/// diagnostic.
356+
#[track_caller]
357+
pub(crate) fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
358+
debug!("Created new diagnostic");
359+
Self {
360+
inner: DiagnosticBuilderInner {
361+
state: DiagnosticBuilderState::Emittable(handler),
362+
diagnostic: Box::new(diagnostic),
363+
},
364+
_marker: PhantomData,
365+
}
366+
}
367+
400368
/// Emit the diagnostic.
401369
#[track_caller]
402370
pub fn emit(&mut self) -> G {

compiler/rustc_errors/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ impl Handler {
776776
#[rustc_lint_diagnostics]
777777
#[track_caller]
778778
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
779-
<()>::make_diagnostic_builder(self, msg)
779+
DiagnosticBuilder::new(self, Level::Warning(None), msg)
780780
}
781781

782782
/// Construct a builder at the `Warning` level with the `msg`. The `id` is used for
@@ -847,7 +847,7 @@ impl Handler {
847847
&self,
848848
msg: impl Into<DiagnosticMessage>,
849849
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
850-
ErrorGuaranteed::make_diagnostic_builder(self, msg)
850+
DiagnosticBuilder::new(self, Level::Error { lint: false }, msg)
851851
}
852852

853853
/// This should only be used by `rustc_middle::lint::struct_lint_level`. Do not use it for hard errors.
@@ -914,7 +914,7 @@ impl Handler {
914914
#[rustc_lint_diagnostics]
915915
#[track_caller]
916916
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
917-
<!>::make_diagnostic_builder(self, msg)
917+
DiagnosticBuilder::new(self, Level::Fatal, msg)
918918
}
919919

920920
/// Construct a builder at the `Help` level with the `msg`.
@@ -1046,12 +1046,12 @@ impl Handler {
10461046

10471047
#[rustc_lint_diagnostics]
10481048
pub fn warn(&self, msg: impl Into<DiagnosticMessage>) {
1049-
DiagnosticBuilder::new(self, Warning(None), msg).emit();
1049+
DiagnosticBuilder::<()>::new(self, Warning(None), msg).emit();
10501050
}
10511051

10521052
#[rustc_lint_diagnostics]
10531053
pub fn note(&self, msg: impl Into<DiagnosticMessage>) {
1054-
DiagnosticBuilder::new(self, Note, msg).emit();
1054+
DiagnosticBuilder::<()>::new(self, Note, msg).emit();
10551055
}
10561056

10571057
pub fn bug(&self, msg: impl Into<DiagnosticMessage>) -> ! {

0 commit comments

Comments
 (0)