Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit de303b8

Browse files
committed
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.
1 parent c8c1615 commit de303b8

File tree

3 files changed

+13
-104
lines changed

3 files changed

+13
-104
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -740,37 +740,6 @@ impl DiagCtxt {
740740
result
741741
}
742742

743-
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
744-
/// The `id` is used for lint emissions which should also fulfill a lint expectation.
745-
///
746-
/// Attempting to `.emit()` the builder will only emit if either:
747-
/// * `can_emit_warnings` is `true`
748-
/// * `is_force_warn` was set in `DiagnosticId::Lint`
749-
#[track_caller]
750-
pub fn struct_span_warn_with_expectation(
751-
&self,
752-
span: impl Into<MultiSpan>,
753-
msg: impl Into<DiagnosticMessage>,
754-
id: LintExpectationId,
755-
) -> DiagnosticBuilder<'_, ()> {
756-
let mut result = self.struct_warn_with_expectation(msg, id);
757-
result.set_span(span);
758-
result
759-
}
760-
761-
/// Construct a builder at the `Allow` level at the given `span` and with the `msg`.
762-
#[rustc_lint_diagnostics]
763-
#[track_caller]
764-
pub fn struct_span_allow(
765-
&self,
766-
span: impl Into<MultiSpan>,
767-
msg: impl Into<DiagnosticMessage>,
768-
) -> DiagnosticBuilder<'_, ()> {
769-
let mut result = self.struct_allow(msg);
770-
result.set_span(span);
771-
result
772-
}
773-
774743
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
775744
/// Also include a code.
776745
#[rustc_lint_diagnostics]
@@ -797,21 +766,6 @@ impl DiagCtxt {
797766
DiagnosticBuilder::new(self, Level::Warning(None), msg)
798767
}
799768

800-
/// Construct a builder at the `Warning` level with the `msg`. The `id` is used for
801-
/// lint emissions which should also fulfill a lint expectation.
802-
///
803-
/// Attempting to `.emit()` the builder will only emit if either:
804-
/// * `can_emit_warnings` is `true`
805-
/// * `is_force_warn` was set in `DiagnosticId::Lint`
806-
#[track_caller]
807-
pub fn struct_warn_with_expectation(
808-
&self,
809-
msg: impl Into<DiagnosticMessage>,
810-
id: LintExpectationId,
811-
) -> DiagnosticBuilder<'_, ()> {
812-
DiagnosticBuilder::new(self, Level::Warning(Some(id)), msg)
813-
}
814-
815769
/// Construct a builder at the `Allow` level with the `msg`.
816770
#[rustc_lint_diagnostics]
817771
#[track_caller]
@@ -868,13 +822,6 @@ impl DiagCtxt {
868822
DiagnosticBuilder::new(self, Level::Error { lint: false }, msg)
869823
}
870824

871-
/// This should only be used by `rustc_middle::lint::struct_lint_level`. Do not use it for hard errors.
872-
#[doc(hidden)]
873-
#[track_caller]
874-
pub fn struct_err_lint(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
875-
DiagnosticBuilder::new(self, Level::Error { lint: true }, msg)
876-
}
877-
878825
/// Construct a builder at the `Error` level with the `msg` and the `code`.
879826
#[rustc_lint_diagnostics]
880827
#[track_caller]

compiler/rustc_middle/src/lint.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -293,43 +293,33 @@ pub fn struct_lint_level(
293293
},
294294
);
295295

296-
let mut err = match (level, span) {
297-
(Level::Allow, span) => {
296+
// Convert lint level to error level.
297+
let err_level = match level {
298+
Level::Allow => {
298299
if has_future_breakage {
299-
if let Some(span) = span {
300-
sess.struct_span_allow(span, "")
301-
} else {
302-
sess.struct_allow("")
303-
}
300+
rustc_errors::Level::Allow
304301
} else {
305302
return;
306303
}
307304
}
308-
(Level::Expect(expect_id), _) => {
305+
Level::Expect(expect_id) => {
309306
// This case is special as we actually allow the lint itself in this context, but
310307
// we can't return early like in the case for `Level::Allow` because we still
311308
// need the lint diagnostic to be emitted to `rustc_error::DiagCtxtInner`.
312309
//
313310
// We can also not mark the lint expectation as fulfilled here right away, as it
314311
// can still be cancelled in the decorate function. All of this means that we simply
315312
// create a `DiagnosticBuilder` and continue as we would for warnings.
316-
sess.struct_expect("", expect_id)
313+
rustc_errors::Level::Expect(expect_id)
317314
}
318-
(Level::ForceWarn(Some(expect_id)), Some(span)) => {
319-
sess.struct_span_warn_with_expectation(span, "", expect_id)
320-
}
321-
(Level::ForceWarn(Some(expect_id)), None) => {
322-
sess.struct_warn_with_expectation("", expect_id)
323-
}
324-
(Level::Warn | Level::ForceWarn(None), Some(span)) => sess.struct_span_warn(span, ""),
325-
(Level::Warn | Level::ForceWarn(None), None) => sess.struct_warn(""),
326-
(Level::Deny | Level::Forbid, Some(span)) => {
327-
let mut builder = sess.dcx().struct_err_lint("");
328-
builder.set_span(span);
329-
builder
330-
}
331-
(Level::Deny | Level::Forbid, None) => sess.dcx().struct_err_lint(""),
315+
Level::ForceWarn(Some(expect_id)) => rustc_errors::Level::Warning(Some(expect_id)),
316+
Level::Warn | Level::ForceWarn(None) => rustc_errors::Level::Warning(None),
317+
Level::Deny | Level::Forbid => rustc_errors::Level::Error { lint: true },
332318
};
319+
let mut err = DiagnosticBuilder::new(sess.dcx(), err_level, "");
320+
if let Some(span) = span {
321+
err.set_span(span);
322+
}
333323

334324
err.set_is_lint();
335325

compiler/rustc_session/src/session.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,6 @@ impl Session {
321321
}
322322
#[rustc_lint_diagnostics]
323323
#[track_caller]
324-
pub fn struct_span_warn_with_expectation<S: Into<MultiSpan>>(
325-
&self,
326-
sp: S,
327-
msg: impl Into<DiagnosticMessage>,
328-
id: lint::LintExpectationId,
329-
) -> DiagnosticBuilder<'_, ()> {
330-
self.dcx().struct_span_warn_with_expectation(sp, msg, id)
331-
}
332-
#[rustc_lint_diagnostics]
333-
#[track_caller]
334324
pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
335325
&self,
336326
sp: S,
@@ -346,24 +336,6 @@ impl Session {
346336
}
347337
#[rustc_lint_diagnostics]
348338
#[track_caller]
349-
pub fn struct_warn_with_expectation(
350-
&self,
351-
msg: impl Into<DiagnosticMessage>,
352-
id: lint::LintExpectationId,
353-
) -> DiagnosticBuilder<'_, ()> {
354-
self.dcx().struct_warn_with_expectation(msg, id)
355-
}
356-
#[rustc_lint_diagnostics]
357-
#[track_caller]
358-
pub fn struct_span_allow<S: Into<MultiSpan>>(
359-
&self,
360-
sp: S,
361-
msg: impl Into<DiagnosticMessage>,
362-
) -> DiagnosticBuilder<'_, ()> {
363-
self.dcx().struct_span_allow(sp, msg)
364-
}
365-
#[rustc_lint_diagnostics]
366-
#[track_caller]
367339
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
368340
self.dcx().struct_allow(msg)
369341
}

0 commit comments

Comments
 (0)