Skip to content

Commit ab640ca

Browse files
committed
Inline and remove more DiagnosticBuilder::new_diagnostic_* functions.
They each have a single call site. Note: the `make_diagnostic_builder` calls in `lib.rs` will be replaced in a subsequent commit.
1 parent d4933aa commit ab640ca

File tree

2 files changed

+28
-83
lines changed

2 files changed

+28
-83
lines changed

compiler/rustc_errors/src/diagnostic_builder.rs

Lines changed: 25 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,6 @@ pub trait EmissionGuarantee: Sized {
116116
}
117117

118118
impl<'a> DiagnosticBuilder<'a, ErrorGuaranteed> {
119-
/// Convenience function for internal use, clients should use one of the
120-
/// `struct_*` methods on [`Handler`].
121-
#[track_caller]
122-
pub(crate) fn new_guaranteeing_error<M: Into<DiagnosticMessage>>(
123-
handler: &'a Handler,
124-
message: M,
125-
) -> Self {
126-
Self {
127-
inner: DiagnosticBuilderInner {
128-
state: DiagnosticBuilderState::Emittable(handler),
129-
diagnostic: Box::new(Diagnostic::new_with_code(
130-
Level::Error { lint: false },
131-
None,
132-
message,
133-
)),
134-
},
135-
_marker: PhantomData,
136-
}
137-
}
138-
139119
/// Discard the guarantee `.emit()` would return, in favor of having the
140120
/// type `DiagnosticBuilder<'a, ()>`. This may be necessary whenever there
141121
/// is a common codepath handling both errors and warnings.
@@ -189,7 +169,13 @@ impl EmissionGuarantee for ErrorGuaranteed {
189169
handler: &Handler,
190170
msg: impl Into<DiagnosticMessage>,
191171
) -> DiagnosticBuilder<'_, Self> {
192-
DiagnosticBuilder::new_guaranteeing_error(handler, msg)
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+
}
193179
}
194180
}
195181

@@ -249,21 +235,6 @@ impl EmissionGuarantee for () {
249235
#[derive(Copy, Clone)]
250236
pub struct Noted;
251237

252-
impl<'a> DiagnosticBuilder<'a, Noted> {
253-
/// Convenience function for internal use, clients should use one of the
254-
/// `struct_*` methods on [`Handler`].
255-
pub(crate) fn new_note(handler: &'a Handler, message: impl Into<DiagnosticMessage>) -> Self {
256-
let diagnostic = Diagnostic::new_with_code(Level::Note, None, message);
257-
Self {
258-
inner: DiagnosticBuilderInner {
259-
state: DiagnosticBuilderState::Emittable(handler),
260-
diagnostic: Box::new(diagnostic),
261-
},
262-
_marker: PhantomData,
263-
}
264-
}
265-
}
266-
267238
impl EmissionGuarantee for Noted {
268239
fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self {
269240
match db.inner.state {
@@ -283,31 +254,21 @@ impl EmissionGuarantee for Noted {
283254
handler: &Handler,
284255
msg: impl Into<DiagnosticMessage>,
285256
) -> DiagnosticBuilder<'_, Self> {
286-
DiagnosticBuilder::new_note(handler, msg)
287-
}
288-
}
289-
290-
/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
291-
/// bug struct diagnostics.
292-
#[derive(Copy, Clone)]
293-
pub struct Bug;
294-
295-
impl<'a> DiagnosticBuilder<'a, Bug> {
296-
/// Convenience function for internal use, clients should use one of the
297-
/// `struct_*` methods on [`Handler`].
298-
#[track_caller]
299-
pub(crate) fn new_bug(handler: &'a Handler, message: impl Into<DiagnosticMessage>) -> Self {
300-
let diagnostic = Diagnostic::new_with_code(Level::Bug, None, message);
301-
Self {
257+
DiagnosticBuilder {
302258
inner: DiagnosticBuilderInner {
303259
state: DiagnosticBuilderState::Emittable(handler),
304-
diagnostic: Box::new(diagnostic),
260+
diagnostic: Box::new(Diagnostic::new_with_code(Level::Note, None, msg)),
305261
},
306262
_marker: PhantomData,
307263
}
308264
}
309265
}
310266

267+
/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
268+
/// bug struct diagnostics.
269+
#[derive(Copy, Clone)]
270+
pub struct Bug;
271+
311272
impl EmissionGuarantee for Bug {
312273
fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self {
313274
match db.inner.state {
@@ -328,19 +289,10 @@ impl EmissionGuarantee for Bug {
328289
handler: &Handler,
329290
msg: impl Into<DiagnosticMessage>,
330291
) -> DiagnosticBuilder<'_, Self> {
331-
DiagnosticBuilder::new_bug(handler, msg)
332-
}
333-
}
334-
335-
impl<'a> DiagnosticBuilder<'a, !> {
336-
/// Convenience function for internal use, clients should use one of the
337-
/// `struct_*` methods on [`Handler`].
338-
#[track_caller]
339-
pub(crate) fn new_fatal(handler: &'a Handler, message: impl Into<DiagnosticMessage>) -> Self {
340-
Self {
292+
DiagnosticBuilder {
341293
inner: DiagnosticBuilderInner {
342294
state: DiagnosticBuilderState::Emittable(handler),
343-
diagnostic: Box::new(Diagnostic::new_with_code(Level::Fatal, None, message)),
295+
diagnostic: Box::new(Diagnostic::new_with_code(Level::Bug, None, msg)),
344296
},
345297
_marker: PhantomData,
346298
}
@@ -367,23 +319,10 @@ impl EmissionGuarantee for ! {
367319
handler: &Handler,
368320
msg: impl Into<DiagnosticMessage>,
369321
) -> DiagnosticBuilder<'_, Self> {
370-
DiagnosticBuilder::new_fatal(handler, msg)
371-
}
372-
}
373-
374-
impl<'a> DiagnosticBuilder<'a, rustc_span::fatal_error::FatalError> {
375-
/// Convenience function for internal use, clients should use one of the
376-
/// `struct_*` methods on [`Handler`].
377-
#[track_caller]
378-
pub(crate) fn new_almost_fatal(
379-
handler: &'a Handler,
380-
message: impl Into<DiagnosticMessage>,
381-
) -> Self {
382-
let diagnostic = Diagnostic::new_with_code(Level::Fatal, None, message);
383-
Self {
322+
DiagnosticBuilder {
384323
inner: DiagnosticBuilderInner {
385324
state: DiagnosticBuilderState::Emittable(handler),
386-
diagnostic: Box::new(diagnostic),
325+
diagnostic: Box::new(Diagnostic::new_with_code(Level::Fatal, None, msg)),
387326
},
388327
_marker: PhantomData,
389328
}
@@ -410,7 +349,13 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
410349
handler: &Handler,
411350
msg: impl Into<DiagnosticMessage>,
412351
) -> DiagnosticBuilder<'_, Self> {
413-
DiagnosticBuilder::new_almost_fatal(handler, msg)
352+
DiagnosticBuilder {
353+
inner: DiagnosticBuilderInner {
354+
state: DiagnosticBuilderState::Emittable(handler),
355+
diagnostic: Box::new(Diagnostic::new_with_code(Level::Fatal, None, msg)),
356+
},
357+
_marker: PhantomData,
358+
}
414359
}
415360
}
416361

compiler/rustc_errors/src/lib.rs

Lines changed: 3 additions & 3 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-
DiagnosticBuilder::new(self, Level::Warning(None), msg)
779+
<()>::make_diagnostic_builder(self, 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-
DiagnosticBuilder::new_guaranteeing_error(self, msg)
850+
ErrorGuaranteed::make_diagnostic_builder(self, 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-
DiagnosticBuilder::new_fatal(self, msg)
917+
<!>::make_diagnostic_builder(self, msg)
918918
}
919919

920920
/// Construct a builder at the `Help` level with the `msg`.

0 commit comments

Comments
 (0)