Use `DiagnosticBuilder::new` more.

By making it generic, instead of only for `EmissionGuarantee = ()`, we
can use it everywhere.
This commit is contained in:
Nicholas Nethercote 2023-12-04 10:44:57 +11:00
parent b7e18cabd2
commit 8c20ad6a08
2 changed files with 36 additions and 68 deletions

View File

@ -169,41 +169,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder {
inner: DiagnosticBuilderInner {
state: DiagnosticBuilderState::Emittable(handler),
diagnostic: Box::new(Diagnostic::new(Level::Error { lint: false }, msg)),
},
_marker: PhantomData,
}
}
}
impl<'a> DiagnosticBuilder<'a, ()> {
/// Convenience function for internal use, clients should use one of the
/// `struct_*` methods on [`Handler`].
#[track_caller]
pub(crate) fn new<M: Into<DiagnosticMessage>>(
handler: &'a Handler,
level: Level,
message: M,
) -> Self {
let diagnostic = Diagnostic::new(level, message);
Self::new_diagnostic(handler, diagnostic)
}
/// Creates a new `DiagnosticBuilder` with an already constructed
/// diagnostic.
#[track_caller]
pub(crate) fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
debug!("Created new diagnostic");
Self {
inner: DiagnosticBuilderInner {
state: DiagnosticBuilderState::Emittable(handler),
diagnostic: Box::new(diagnostic),
},
_marker: PhantomData,
}
DiagnosticBuilder::new(handler, Level::Error { lint: false }, msg)
}
}
@ -254,13 +220,7 @@ impl EmissionGuarantee for Noted {
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder {
inner: DiagnosticBuilderInner {
state: DiagnosticBuilderState::Emittable(handler),
diagnostic: Box::new(Diagnostic::new(Level::Note, msg)),
},
_marker: PhantomData,
}
DiagnosticBuilder::new(handler, Level::Note, msg)
}
}
@ -289,13 +249,7 @@ impl EmissionGuarantee for Bug {
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder {
inner: DiagnosticBuilderInner {
state: DiagnosticBuilderState::Emittable(handler),
diagnostic: Box::new(Diagnostic::new(Level::Bug, msg)),
},
_marker: PhantomData,
}
DiagnosticBuilder::new(handler, Level::Bug, msg)
}
}
@ -319,13 +273,7 @@ impl EmissionGuarantee for ! {
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder {
inner: DiagnosticBuilderInner {
state: DiagnosticBuilderState::Emittable(handler),
diagnostic: Box::new(Diagnostic::new(Level::Fatal, msg)),
},
_marker: PhantomData,
}
DiagnosticBuilder::new(handler, Level::Fatal, msg)
}
}
@ -349,13 +297,7 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder {
inner: DiagnosticBuilderInner {
state: DiagnosticBuilderState::Emittable(handler),
diagnostic: Box::new(Diagnostic::new(Level::Fatal, msg)),
},
_marker: PhantomData,
}
DiagnosticBuilder::new(handler, Level::Fatal, msg)
}
}
@ -397,6 +339,32 @@ impl<G: EmissionGuarantee> DerefMut for DiagnosticBuilder<'_, G> {
}
impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
/// Convenience function for internal use, clients should use one of the
/// `struct_*` methods on [`Handler`].
#[track_caller]
pub(crate) fn new<M: Into<DiagnosticMessage>>(
handler: &'a Handler,
level: Level,
message: M,
) -> Self {
let diagnostic = Diagnostic::new(level, message);
Self::new_diagnostic(handler, diagnostic)
}
/// Creates a new `DiagnosticBuilder` with an already constructed
/// diagnostic.
#[track_caller]
pub(crate) fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> Self {
debug!("Created new diagnostic");
Self {
inner: DiagnosticBuilderInner {
state: DiagnosticBuilderState::Emittable(handler),
diagnostic: Box::new(diagnostic),
},
_marker: PhantomData,
}
}
/// Emit the diagnostic.
#[track_caller]
pub fn emit(&mut self) -> G {

View File

@ -776,7 +776,7 @@ impl Handler {
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
<()>::make_diagnostic_builder(self, msg)
DiagnosticBuilder::new(self, Level::Warning(None), msg)
}
/// Construct a builder at the `Warning` level with the `msg`. The `id` is used for
@ -847,7 +847,7 @@ impl Handler {
&self,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
ErrorGuaranteed::make_diagnostic_builder(self, msg)
DiagnosticBuilder::new(self, Level::Error { lint: false }, msg)
}
/// 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 {
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
<!>::make_diagnostic_builder(self, msg)
DiagnosticBuilder::new(self, Level::Fatal, msg)
}
/// Construct a builder at the `Help` level with the `msg`.
@ -1046,12 +1046,12 @@ impl Handler {
#[rustc_lint_diagnostics]
pub fn warn(&self, msg: impl Into<DiagnosticMessage>) {
DiagnosticBuilder::new(self, Warning(None), msg).emit();
DiagnosticBuilder::<()>::new(self, Warning(None), msg).emit();
}
#[rustc_lint_diagnostics]
pub fn note(&self, msg: impl Into<DiagnosticMessage>) {
DiagnosticBuilder::new(self, Note, msg).emit();
DiagnosticBuilder::<()>::new(self, Note, msg).emit();
}
pub fn bug(&self, msg: impl Into<DiagnosticMessage>) -> ! {