Streamline struct_lint_level.

We can just get the error level in the `match` and then use
`DiagnosticBuilder::new`. This then means a number of `DiagCtxt`
functions are no longer needed, because this was the one place that used
them.

Note: the commit changes the treatment of spans for `Expect`, which was
different to all the other cases, but this has no apparent effect.
This commit is contained in:
Nicholas Nethercote 2023-12-19 13:47:09 +11:00
parent c8c1615df0
commit de303b87c9
3 changed files with 13 additions and 104 deletions

View file

@ -740,37 +740,6 @@ impl DiagCtxt {
result result
} }
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
/// The `id` is used for lint emissions which should also fulfill a lint expectation.
///
/// Attempting to `.emit()` the builder will only emit if either:
/// * `can_emit_warnings` is `true`
/// * `is_force_warn` was set in `DiagnosticId::Lint`
#[track_caller]
pub fn struct_span_warn_with_expectation(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
id: LintExpectationId,
) -> DiagnosticBuilder<'_, ()> {
let mut result = self.struct_warn_with_expectation(msg, id);
result.set_span(span);
result
}
/// Construct a builder at the `Allow` level at the given `span` and with the `msg`.
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_span_allow(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, ()> {
let mut result = self.struct_allow(msg);
result.set_span(span);
result
}
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`. /// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
/// Also include a code. /// Also include a code.
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
@ -797,21 +766,6 @@ impl DiagCtxt {
DiagnosticBuilder::new(self, Level::Warning(None), msg) DiagnosticBuilder::new(self, Level::Warning(None), msg)
} }
/// Construct a builder at the `Warning` level with the `msg`. The `id` is used for
/// lint emissions which should also fulfill a lint expectation.
///
/// Attempting to `.emit()` the builder will only emit if either:
/// * `can_emit_warnings` is `true`
/// * `is_force_warn` was set in `DiagnosticId::Lint`
#[track_caller]
pub fn struct_warn_with_expectation(
&self,
msg: impl Into<DiagnosticMessage>,
id: LintExpectationId,
) -> DiagnosticBuilder<'_, ()> {
DiagnosticBuilder::new(self, Level::Warning(Some(id)), msg)
}
/// Construct a builder at the `Allow` level with the `msg`. /// Construct a builder at the `Allow` level with the `msg`.
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
@ -868,13 +822,6 @@ impl DiagCtxt {
DiagnosticBuilder::new(self, Level::Error { lint: false }, 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.
#[doc(hidden)]
#[track_caller]
pub fn struct_err_lint(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
DiagnosticBuilder::new(self, Level::Error { lint: true }, msg)
}
/// Construct a builder at the `Error` level with the `msg` and the `code`. /// Construct a builder at the `Error` level with the `msg` and the `code`.
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]

View file

@ -293,19 +293,16 @@ pub fn struct_lint_level(
}, },
); );
let mut err = match (level, span) { // Convert lint level to error level.
(Level::Allow, span) => { let err_level = match level {
Level::Allow => {
if has_future_breakage { if has_future_breakage {
if let Some(span) = span { rustc_errors::Level::Allow
sess.struct_span_allow(span, "")
} else {
sess.struct_allow("")
}
} else { } else {
return; return;
} }
} }
(Level::Expect(expect_id), _) => { Level::Expect(expect_id) => {
// This case is special as we actually allow the lint itself in this context, but // This case is special as we actually allow the lint itself in this context, but
// we can't return early like in the case for `Level::Allow` because we still // we can't return early like in the case for `Level::Allow` because we still
// need the lint diagnostic to be emitted to `rustc_error::DiagCtxtInner`. // need the lint diagnostic to be emitted to `rustc_error::DiagCtxtInner`.
@ -313,23 +310,16 @@ pub fn struct_lint_level(
// We can also not mark the lint expectation as fulfilled here right away, as it // We can also not mark the lint expectation as fulfilled here right away, as it
// can still be cancelled in the decorate function. All of this means that we simply // can still be cancelled in the decorate function. All of this means that we simply
// create a `DiagnosticBuilder` and continue as we would for warnings. // create a `DiagnosticBuilder` and continue as we would for warnings.
sess.struct_expect("", expect_id) rustc_errors::Level::Expect(expect_id)
} }
(Level::ForceWarn(Some(expect_id)), Some(span)) => { Level::ForceWarn(Some(expect_id)) => rustc_errors::Level::Warning(Some(expect_id)),
sess.struct_span_warn_with_expectation(span, "", expect_id) Level::Warn | Level::ForceWarn(None) => rustc_errors::Level::Warning(None),
} Level::Deny | Level::Forbid => rustc_errors::Level::Error { lint: true },
(Level::ForceWarn(Some(expect_id)), None) => {
sess.struct_warn_with_expectation("", expect_id)
}
(Level::Warn | Level::ForceWarn(None), Some(span)) => sess.struct_span_warn(span, ""),
(Level::Warn | Level::ForceWarn(None), None) => sess.struct_warn(""),
(Level::Deny | Level::Forbid, Some(span)) => {
let mut builder = sess.dcx().struct_err_lint("");
builder.set_span(span);
builder
}
(Level::Deny | Level::Forbid, None) => sess.dcx().struct_err_lint(""),
}; };
let mut err = DiagnosticBuilder::new(sess.dcx(), err_level, "");
if let Some(span) = span {
err.set_span(span);
}
err.set_is_lint(); err.set_is_lint();

View file

@ -321,16 +321,6 @@ impl Session {
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_span_warn_with_expectation<S: Into<MultiSpan>>(
&self,
sp: S,
msg: impl Into<DiagnosticMessage>,
id: lint::LintExpectationId,
) -> DiagnosticBuilder<'_, ()> {
self.dcx().struct_span_warn_with_expectation(sp, msg, id)
}
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_span_warn_with_code<S: Into<MultiSpan>>( pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -346,24 +336,6 @@ impl Session {
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_warn_with_expectation(
&self,
msg: impl Into<DiagnosticMessage>,
id: lint::LintExpectationId,
) -> DiagnosticBuilder<'_, ()> {
self.dcx().struct_warn_with_expectation(msg, id)
}
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_span_allow<S: Into<MultiSpan>>(
&self,
sp: S,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, ()> {
self.dcx().struct_span_allow(sp, msg)
}
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> { pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
self.dcx().struct_allow(msg) self.dcx().struct_allow(msg)
} }