From de303b87c94ad3a2b6a08ecdf6173e988c25ccf9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 19 Dec 2023 13:47:09 +1100 Subject: [PATCH] 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. --- compiler/rustc_errors/src/lib.rs | 53 --------------------------- compiler/rustc_middle/src/lint.rs | 36 +++++++----------- compiler/rustc_session/src/session.rs | 28 -------------- 3 files changed, 13 insertions(+), 104 deletions(-) diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 6b0b1710d42..12d04764136 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -740,37 +740,6 @@ impl DiagCtxt { 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, - msg: impl Into, - 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, - msg: impl Into, - ) -> 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`. /// Also include a code. #[rustc_lint_diagnostics] @@ -797,21 +766,6 @@ impl DiagCtxt { 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, - id: LintExpectationId, - ) -> DiagnosticBuilder<'_, ()> { - DiagnosticBuilder::new(self, Level::Warning(Some(id)), msg) - } - /// Construct a builder at the `Allow` level with the `msg`. #[rustc_lint_diagnostics] #[track_caller] @@ -868,13 +822,6 @@ impl DiagCtxt { 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) -> DiagnosticBuilder<'_, ()> { - DiagnosticBuilder::new(self, Level::Error { lint: true }, msg) - } - /// Construct a builder at the `Error` level with the `msg` and the `code`. #[rustc_lint_diagnostics] #[track_caller] diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index c49c4ee819c..d34d9160d55 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -293,19 +293,16 @@ pub fn struct_lint_level( }, ); - let mut err = match (level, span) { - (Level::Allow, span) => { + // Convert lint level to error level. + let err_level = match level { + Level::Allow => { if has_future_breakage { - if let Some(span) = span { - sess.struct_span_allow(span, "") - } else { - sess.struct_allow("") - } + rustc_errors::Level::Allow } else { return; } } - (Level::Expect(expect_id), _) => { + Level::Expect(expect_id) => { // 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 // 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 // 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. - sess.struct_expect("", expect_id) + rustc_errors::Level::Expect(expect_id) } - (Level::ForceWarn(Some(expect_id)), Some(span)) => { - sess.struct_span_warn_with_expectation(span, "", expect_id) - } - (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(""), + Level::ForceWarn(Some(expect_id)) => rustc_errors::Level::Warning(Some(expect_id)), + Level::Warn | Level::ForceWarn(None) => rustc_errors::Level::Warning(None), + Level::Deny | Level::Forbid => rustc_errors::Level::Error { lint: true }, }; + let mut err = DiagnosticBuilder::new(sess.dcx(), err_level, ""); + if let Some(span) = span { + err.set_span(span); + } err.set_is_lint(); diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index c9f0f74b0b2..358219c630f 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -321,16 +321,6 @@ impl Session { } #[rustc_lint_diagnostics] #[track_caller] - pub fn struct_span_warn_with_expectation>( - &self, - sp: S, - msg: impl Into, - 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>( &self, sp: S, @@ -346,24 +336,6 @@ impl Session { } #[rustc_lint_diagnostics] #[track_caller] - pub fn struct_warn_with_expectation( - &self, - msg: impl Into, - id: lint::LintExpectationId, - ) -> DiagnosticBuilder<'_, ()> { - self.dcx().struct_warn_with_expectation(msg, id) - } - #[rustc_lint_diagnostics] - #[track_caller] - pub fn struct_span_allow>( - &self, - sp: S, - msg: impl Into, - ) -> DiagnosticBuilder<'_, ()> { - self.dcx().struct_span_allow(sp, msg) - } - #[rustc_lint_diagnostics] - #[track_caller] pub fn struct_allow(&self, msg: impl Into) -> DiagnosticBuilder<'_, ()> { self.dcx().struct_allow(msg) }