Skip to content

Commit 2677eca

Browse files
author
David Koloski
committed
Switch the primary diagnostic to unknown_lints
This also affects the `non_exhaustive_omitted_patterns` and `must_not_suspend` lints as they are not stable. This also changes the diagnostic level to pull from `unknown_lints` instead of always being allow or deny.
1 parent 8852752 commit 2677eca

21 files changed

+310
-142
lines changed

compiler/rustc_lint/src/levels.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_session::lint::{
1717
builtin::{self, FORBIDDEN_LINT_GROUPS},
1818
Level, Lint, LintExpectationId, LintId,
1919
};
20-
use rustc_session::parse::feature_err;
20+
use rustc_session::parse::{add_feature_diagnostics, feature_err};
2121
use rustc_session::Session;
2222
use rustc_span::symbol::{sym, Symbol};
2323
use rustc_span::{source_map::MultiSpan, Span, DUMMY_SP};
@@ -104,10 +104,8 @@ impl<'s> LintLevelsBuilder<'s> {
104104
fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
105105
self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
106106

107-
self.cur = self.sets.list.push(LintSet {
108-
specs: FxHashMap::default(),
109-
parent: COMMAND_LINE,
110-
});
107+
self.cur =
108+
self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: COMMAND_LINE });
111109
for &(ref lint_name, level) in &sess.opts.lint_opts {
112110
store.check_lint_name_cmdline(sess, &lint_name, level, self.registered_tools);
113111
let orig_level = level;
@@ -134,11 +132,7 @@ impl<'s> LintLevelsBuilder<'s> {
134132
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
135133
/// (e.g. if a forbid was already inserted on the same scope), then emits a
136134
/// diagnostic with no change to `specs`.
137-
fn insert_spec(
138-
&mut self,
139-
id: LintId,
140-
(level, src): LevelAndSource,
141-
) {
135+
fn insert_spec(&mut self, id: LintId, (level, src): LevelAndSource) {
142136
let (old_level, old_src) =
143137
self.sets.get_lint_level(id.lint, self.cur, Some(self.current_specs()), &self.sess);
144138
// Setting to a non-forbid level is an error if the lint previously had
@@ -163,7 +157,10 @@ impl<'s> LintLevelsBuilder<'s> {
163157
};
164158
debug!(
165159
"fcw_warning={:?}, specs.get(&id) = {:?}, old_src={:?}, id_name={:?}",
166-
fcw_warning, self.current_specs(), old_src, id_name
160+
fcw_warning,
161+
self.current_specs(),
162+
old_src,
163+
id_name
167164
);
168165

169166
let decorate_diag = |diag: &mut Diagnostic| {
@@ -249,10 +246,8 @@ impl<'s> LintLevelsBuilder<'s> {
249246
source_hir_id: Option<HirId>,
250247
) -> BuilderPush {
251248
let prev = self.cur;
252-
self.cur = self.sets.list.push(LintSet {
253-
specs: FxHashMap::default(),
254-
parent: prev,
255-
});
249+
self.cur = self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev });
250+
256251
let sess = self.sess;
257252
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
258253
for (attr_index, attr) in attrs.iter().enumerate() {
@@ -391,8 +386,12 @@ impl<'s> LintLevelsBuilder<'s> {
391386
}
392387
Err((Some(ids), ref new_lint_name)) => {
393388
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
394-
let (lvl, src) =
395-
self.sets.get_lint_level(lint, self.cur, Some(self.current_specs()), &sess);
389+
let (lvl, src) = self.sets.get_lint_level(
390+
lint,
391+
self.cur,
392+
Some(self.current_specs()),
393+
&sess,
394+
);
396395
struct_lint_level(
397396
self.sess,
398397
lint,
@@ -462,8 +461,12 @@ impl<'s> LintLevelsBuilder<'s> {
462461

463462
CheckLintNameResult::Warning(msg, renamed) => {
464463
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
465-
let (renamed_lint_level, src) =
466-
self.sets.get_lint_level(lint, self.cur, Some(self.current_specs()), &sess);
464+
let (renamed_lint_level, src) = self.sets.get_lint_level(
465+
lint,
466+
self.cur,
467+
Some(self.current_specs()),
468+
&sess,
469+
);
467470
struct_lint_level(
468471
self.sess,
469472
lint,
@@ -486,8 +489,12 @@ impl<'s> LintLevelsBuilder<'s> {
486489
}
487490
CheckLintNameResult::NoLint(suggestion) => {
488491
let lint = builtin::UNKNOWN_LINTS;
489-
let (level, src) =
490-
self.sets.get_lint_level(lint, self.cur, Some(self.current_specs()), self.sess);
492+
let (level, src) = self.sets.get_lint_level(
493+
lint,
494+
self.cur,
495+
Some(self.current_specs()),
496+
self.sess,
497+
);
491498
struct_lint_level(self.sess, lint, level, src, Some(sp.into()), |lint| {
492499
let name = if let Some(tool_ident) = tool_ident {
493500
format!("{}::{}", tool_ident.name, name)
@@ -594,16 +601,14 @@ impl<'s> LintLevelsBuilder<'s> {
594601
fn check_gated_lint(&self, lint_id: LintId, span: Span) -> bool {
595602
if let Some(feature) = lint_id.lint.feature_gate {
596603
if !self.sess.features_untracked().enabled(feature) {
597-
let (unknown_lints_level, _) = self.lint_level(builtin::UNKNOWN_LINTS);
598-
if unknown_lints_level != Level::Allow {
599-
feature_err(
600-
&self.sess.parse_sess,
601-
feature,
602-
span,
603-
&format!("the `{}` lint is unstable", lint_id.lint.name_lower()),
604-
)
605-
.emit();
606-
}
604+
let (level, src) = self.lint_level(builtin::UNKNOWN_LINTS);
605+
struct_lint_level(self.sess, lint_id.lint, level, src, Some(span.into()), |lint| {
606+
let mut db =
607+
lint.build(&format!("unknown lint: `{}`", lint_id.lint.name_lower()));
608+
db.note(&format!("the `{}` lint is unstable", lint_id.lint.name_lower(),));
609+
add_feature_diagnostics(&mut db, &self.sess.parse_sess, feature);
610+
db.emit();
611+
});
607612
return false;
608613
}
609614
}

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,6 +3775,7 @@ declare_lint! {
37753775

37763776
declare_lint! {
37773777
#[doc(hidden)]
3778+
/// Added for testing unsable lints; perma-unstable.
37783779
pub TEST_UNSTABLE_LINT,
37793780
Deny,
37803781
"this unstable lint is only for testing",

compiler/rustc_session/src/parse.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,26 @@ pub fn feature_err_issue<'a>(
9898
explain: &str,
9999
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
100100
let mut err = sess.span_diagnostic.struct_span_err_with_code(span, explain, error_code!(E0658));
101+
add_feature_diagnostics_for_issue(&mut err, sess, feature, issue);
102+
err
103+
}
104+
105+
/// Adds the diagnostics for a feature to an existing error.
106+
pub fn add_feature_diagnostics<'a>(err: &mut Diagnostic, sess: &'a ParseSess, feature: Symbol) {
107+
add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language);
108+
}
101109

110+
/// Adds the diagnostics for a feature to an existing error.
111+
///
112+
/// This variant allows you to control whether it is a library or language feature.
113+
/// Almost always, you want to use this for a language feature. If so, prefer
114+
/// `add_feature_diagnostics`.
115+
pub fn add_feature_diagnostics_for_issue<'a>(
116+
err: &mut Diagnostic,
117+
sess: &'a ParseSess,
118+
feature: Symbol,
119+
issue: GateIssue,
120+
) {
102121
if let Some(n) = find_feature_issue(feature, issue) {
103122
err.note(&format!(
104123
"see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information",
@@ -110,8 +129,6 @@ pub fn feature_err_issue<'a>(
110129
if sess.unstable_features.is_nightly_build() {
111130
err.help(&format!("add `#![feature({})]` to the crate attributes to enable", feature));
112131
}
113-
114-
err
115132
}
116133

117134
/// Info about a parsing session.
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1+
// check-fail
2+
13
#![deny(non_exhaustive_omitted_patterns)]
2-
//~^ ERROR the `non_exhaustive_omitted_patterns` lint is unstable
3-
//~| ERROR the `non_exhaustive_omitted_patterns` lint is unstable
4+
//~^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
5+
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
46
#![allow(non_exhaustive_omitted_patterns)]
5-
//~^ ERROR the `non_exhaustive_omitted_patterns` lint is unstable
6-
//~| ERROR the `non_exhaustive_omitted_patterns` lint is unstable
7+
//~^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
8+
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
79

810
fn main() {
911
enum Foo {
1012
A, B, C,
1113
}
1214

1315
#[allow(non_exhaustive_omitted_patterns)]
16+
//~^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
17+
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
18+
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
19+
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
1420
match Foo::A {
1521
Foo::A => {}
1622
Foo::B => {}
1723
}
18-
//~^^^^^ ERROR the `non_exhaustive_omitted_patterns` lint is unstable
19-
//~| ERROR the `non_exhaustive_omitted_patterns` lint is unstable
20-
//~| ERROR the `non_exhaustive_omitted_patterns` lint is unstable
21-
//~| ERROR the `non_exhaustive_omitted_patterns` lint is unstable
24+
//~^^^^ ERROR non-exhaustive patterns: `C` not covered
2225

2326
match Foo::A {
2427
Foo::A => {}
2528
Foo::B => {}
2629
#[warn(non_exhaustive_omitted_patterns)]
2730
_ => {}
2831
}
29-
//~^^^ ERROR the `non_exhaustive_omitted_patterns` lint is unstable
30-
//~| ERROR the `non_exhaustive_omitted_patterns` lint is unstable
32+
//~^^^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
33+
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
3134
}
Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,119 @@
1-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
2-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:1:1
1+
warning: unknown lint: `non_exhaustive_omitted_patterns`
2+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:3:1
33
|
44
LL | #![deny(non_exhaustive_omitted_patterns)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: `#[warn(non_exhaustive_omitted_patterns)]` on by default
8+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
79
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
810
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
911

10-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
11-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:4:1
12+
warning: unknown lint: `non_exhaustive_omitted_patterns`
13+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:6:1
1214
|
1315
LL | #![allow(non_exhaustive_omitted_patterns)]
1416
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1517
|
18+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
1619
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
1720
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
1821

19-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
20-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:13:5
22+
warning: unknown lint: `non_exhaustive_omitted_patterns`
23+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
2124
|
2225
LL | #[allow(non_exhaustive_omitted_patterns)]
2326
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2427
|
28+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
2529
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
2630
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
2731

28-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
29-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:13:5
32+
warning: unknown lint: `non_exhaustive_omitted_patterns`
33+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
3034
|
3135
LL | #[allow(non_exhaustive_omitted_patterns)]
3236
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3337
|
38+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
3439
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
3540
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
3641

37-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
38-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:26:9
42+
warning: unknown lint: `non_exhaustive_omitted_patterns`
43+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:29:9
3944
|
4045
LL | #[warn(non_exhaustive_omitted_patterns)]
4146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4247
|
48+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
4349
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
4450
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
4551

46-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
47-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:1:1
52+
warning: unknown lint: `non_exhaustive_omitted_patterns`
53+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:3:1
4854
|
4955
LL | #![deny(non_exhaustive_omitted_patterns)]
5056
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5157
|
58+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
5259
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
5360
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
5461

55-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
56-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:4:1
62+
warning: unknown lint: `non_exhaustive_omitted_patterns`
63+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:6:1
5764
|
5865
LL | #![allow(non_exhaustive_omitted_patterns)]
5966
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6067
|
68+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
6169
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
6270
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
6371

64-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
65-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:13:5
72+
warning: unknown lint: `non_exhaustive_omitted_patterns`
73+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
6674
|
6775
LL | #[allow(non_exhaustive_omitted_patterns)]
6876
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6977
|
78+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
7079
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
7180
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
7281

73-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
74-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:13:5
82+
warning: unknown lint: `non_exhaustive_omitted_patterns`
83+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
7584
|
7685
LL | #[allow(non_exhaustive_omitted_patterns)]
7786
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7887
|
88+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
7989
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
8090
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
8191

82-
error[E0658]: the `non_exhaustive_omitted_patterns` lint is unstable
83-
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:26:9
92+
warning: unknown lint: `non_exhaustive_omitted_patterns`
93+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:29:9
8494
|
8595
LL | #[warn(non_exhaustive_omitted_patterns)]
8696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8797
|
98+
= note: the `non_exhaustive_omitted_patterns` lint is unstable
8899
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
89100
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
90101

91-
error: aborting due to 10 previous errors
102+
error[E0004]: non-exhaustive patterns: `C` not covered
103+
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:20:11
104+
|
105+
LL | / enum Foo {
106+
LL | | A, B, C,
107+
| | - not covered
108+
LL | | }
109+
| |_____- `Foo` defined here
110+
...
111+
LL | match Foo::A {
112+
| ^^^^^^ pattern `C` not covered
113+
|
114+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
115+
= note: the matched value is of type `Foo`
92116

93-
For more information about this error, try `rustc --explain E0658`.
117+
error: aborting due to previous error; 10 warnings emitted
118+
119+
For more information about this error, try `rustc --explain E0004`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
3+
// `test_unstable_lint` is for testing and should never be stabilized.
4+
#![allow(test_unstable_lint)]
5+
//~^ WARNING unknown lint: `test_unstable_lint`
6+
//~| WARNING unknown lint: `test_unstable_lint`
7+
//~| WARNING unknown lint: `test_unstable_lint`
8+
9+
fn main() {}

0 commit comments

Comments
 (0)