Skip to content

Commit d274417

Browse files
committed
unknown unstable lint command line
fix ##113702 fix #113702 unknown unstable lint command lint improve impelementation
1 parent 9530589 commit d274417

File tree

7 files changed

+46
-21
lines changed

7 files changed

+46
-21
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ fn default_body_is_unstable(
289289
&tcx.sess.parse_sess,
290290
feature,
291291
rustc_feature::GateIssue::Library(issue),
292+
false,
292293
);
293294

294295
err.emit();

compiler/rustc_lint/src/levels.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast as ast;
1212
use rustc_ast_pretty::pprust;
1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_errors::{DecorateLint, DiagnosticBuilder, DiagnosticMessage, MultiSpan};
15-
use rustc_feature::Features;
15+
use rustc_feature::{Features, GateIssue};
1616
use rustc_hir as hir;
1717
use rustc_hir::intravisit::{self, Visitor};
1818
use rustc_hir::HirId;
@@ -24,12 +24,14 @@ use rustc_middle::lint::{
2424
};
2525
use rustc_middle::query::Providers;
2626
use rustc_middle::ty::{RegisteredTools, TyCtxt};
27-
use rustc_session::lint::builtin::{RENAMED_AND_REMOVED_LINTS, UNKNOWN_LINTS, UNUSED_ATTRIBUTES};
2827
use rustc_session::lint::{
29-
builtin::{self, FORBIDDEN_LINT_GROUPS, SINGLE_USE_LIFETIMES, UNFULFILLED_LINT_EXPECTATIONS},
28+
builtin::{
29+
self, FORBIDDEN_LINT_GROUPS, RENAMED_AND_REMOVED_LINTS, SINGLE_USE_LIFETIMES,
30+
UNFULFILLED_LINT_EXPECTATIONS, UNKNOWN_LINTS, UNUSED_ATTRIBUTES,
31+
},
3032
Level, Lint, LintExpectationId, LintId,
3133
};
32-
use rustc_session::parse::{add_feature_diagnostics, feature_err};
34+
use rustc_session::parse::feature_err;
3335
use rustc_session::Session;
3436
use rustc_span::symbol::{sym, Symbol};
3537
use rustc_span::{Span, DUMMY_SP};
@@ -566,7 +568,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
566568
continue;
567569
}
568570

569-
if self.check_gated_lint(id, DUMMY_SP) {
571+
if self.check_gated_lint(id, DUMMY_SP, true) {
570572
let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
571573
self.insert(id, (level, src));
572574
}
@@ -837,7 +839,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
837839
reason,
838840
};
839841
for &id in *ids {
840-
if self.check_gated_lint(id, attr.span) {
842+
if self.check_gated_lint(id, attr.span, false) {
841843
self.insert_spec(id, (level, src));
842844
}
843845
}
@@ -854,7 +856,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
854856
reason,
855857
};
856858
for &id in ids {
857-
if self.check_gated_lint(id, attr.span) {
859+
if self.check_gated_lint(id, attr.span, false) {
858860
self.insert_spec(id, (level, src));
859861
}
860862
}
@@ -955,7 +957,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
955957
reason,
956958
};
957959
for &id in ids {
958-
if self.check_gated_lint(id, attr.span) {
960+
if self.check_gated_lint(id, attr.span, false) {
959961
self.insert_spec(id, (level, src));
960962
}
961963
}
@@ -1000,7 +1002,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10001002
// FIXME only emit this once for each attribute, instead of repeating it 4 times for
10011003
// pre-expansion lints, post-expansion lints, `shallow_lint_levels_on` and `lint_expectations`.
10021004
#[track_caller]
1003-
fn check_gated_lint(&self, lint_id: LintId, span: Span) -> bool {
1005+
fn check_gated_lint(&self, lint_id: LintId, span: Span, lint_from_cli: bool) -> bool {
10041006
if let Some(feature) = lint_id.lint.feature_gate {
10051007
if !self.features.enabled(feature) {
10061008
let lint = builtin::UNKNOWN_LINTS;
@@ -1015,7 +1017,13 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10151017
|lint| {
10161018
lint.set_arg("name", lint_id.lint.name_lower());
10171019
lint.note(fluent::lint_note);
1018-
add_feature_diagnostics(lint, &self.sess.parse_sess, feature);
1020+
rustc_session::parse::add_feature_diagnostics_for_issue(
1021+
lint,
1022+
&self.sess.parse_sess,
1023+
feature,
1024+
GateIssue::Language,
1025+
lint_from_cli,
1026+
);
10191027
lint
10201028
},
10211029
);

compiler/rustc_session/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ session_cannot_mix_and_match_sanitizers = `-Zsanitizer={$first}` is incompatible
88
session_cgu_not_recorded =
99
CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded
1010
11+
session_cli_feature_diagnostic_help =
12+
add `-Zcrate-attr="feature({$feature})"` to the command-line options to enable
13+
1114
session_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$s}` != `{$name}`
1215
1316
session_crate_name_empty = crate name must not be empty

compiler/rustc_session/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ pub struct FeatureDiagnosticHelp {
5757
pub feature: Symbol,
5858
}
5959

60+
#[derive(Subdiagnostic)]
61+
#[help(session_cli_feature_diagnostic_help)]
62+
pub struct CliFeatureDiagnosticHelp {
63+
pub feature: Symbol,
64+
}
65+
6066
#[derive(Diagnostic)]
6167
#[diag(session_not_circumvent_feature)]
6268
pub struct NotCircumventFeature;

compiler/rustc_session/src/parse.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
//! It also serves as an input to the parser itself.
33
44
use crate::config::CheckCfg;
5-
use crate::errors::{FeatureDiagnosticForIssue, FeatureDiagnosticHelp, FeatureGateError};
5+
use crate::errors::{
6+
CliFeatureDiagnosticHelp, FeatureDiagnosticForIssue, FeatureDiagnosticHelp, FeatureGateError,
7+
};
68
use crate::lint::{
79
builtin::UNSTABLE_SYNTAX_PRE_EXPANSION, BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId,
810
};
@@ -110,7 +112,7 @@ pub fn feature_err_issue(
110112
}
111113

112114
let mut err = sess.create_err(FeatureGateError { span, explain: explain.into() });
113-
add_feature_diagnostics_for_issue(&mut err, sess, feature, issue);
115+
add_feature_diagnostics_for_issue(&mut err, sess, feature, issue, false);
114116
err
115117
}
116118

@@ -139,7 +141,7 @@ pub fn feature_warn_issue(
139141
explain: &'static str,
140142
) {
141143
let mut err = sess.span_diagnostic.struct_span_warn(span, explain);
142-
add_feature_diagnostics_for_issue(&mut err, sess, feature, issue);
144+
add_feature_diagnostics_for_issue(&mut err, sess, feature, issue, false);
143145

144146
// Decorate this as a future-incompatibility lint as in rustc_middle::lint::struct_lint_level
145147
let lint = UNSTABLE_SYNTAX_PRE_EXPANSION;
@@ -158,7 +160,7 @@ pub fn feature_warn_issue(
158160

159161
/// Adds the diagnostics for a feature to an existing error.
160162
pub fn add_feature_diagnostics(err: &mut Diagnostic, sess: &ParseSess, feature: Symbol) {
161-
add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language);
163+
add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language, false);
162164
}
163165

164166
/// Adds the diagnostics for a feature to an existing error.
@@ -171,14 +173,19 @@ pub fn add_feature_diagnostics_for_issue(
171173
sess: &ParseSess,
172174
feature: Symbol,
173175
issue: GateIssue,
176+
feature_from_cli: bool,
174177
) {
175178
if let Some(n) = find_feature_issue(feature, issue) {
176179
err.subdiagnostic(FeatureDiagnosticForIssue { n });
177180
}
178181

179182
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
180183
if sess.unstable_features.is_nightly_build() {
181-
err.subdiagnostic(FeatureDiagnosticHelp { feature });
184+
if feature_from_cli {
185+
err.subdiagnostic(CliFeatureDiagnosticHelp { feature });
186+
} else {
187+
err.subdiagnostic(FeatureDiagnosticHelp { feature });
188+
}
182189
}
183190
}
184191

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error: unknown lint: `test_unstable_lint`
22
|
33
= note: the `test_unstable_lint` lint is unstable
4-
= help: add `#![feature(test_unstable_lint)]` to the crate attributes to enable
4+
= help: add `-Zcrate-attr="feature(test_unstable_lint)"` to the command-line options to enable
55
= note: requested on the command line with `-D unknown-lints`
66

77
error: unknown lint: `test_unstable_lint`
88
|
99
= note: the `test_unstable_lint` lint is unstable
10-
= help: add `#![feature(test_unstable_lint)]` to the crate attributes to enable
10+
= help: add `-Zcrate-attr="feature(test_unstable_lint)"` to the command-line options to enable
1111

1212
error: unknown lint: `test_unstable_lint`
1313
|
1414
= note: the `test_unstable_lint` lint is unstable
15-
= help: add `#![feature(test_unstable_lint)]` to the crate attributes to enable
15+
= help: add `-Zcrate-attr="feature(test_unstable_lint)"` to the command-line options to enable
1616

1717
error: aborting due to 3 previous errors
1818

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
warning: unknown lint: `test_unstable_lint`
22
|
33
= note: the `test_unstable_lint` lint is unstable
4-
= help: add `#![feature(test_unstable_lint)]` to the crate attributes to enable
4+
= help: add `-Zcrate-attr="feature(test_unstable_lint)"` to the command-line options to enable
55
= note: requested on the command line with `-W unknown-lints`
66

77
warning: unknown lint: `test_unstable_lint`
88
|
99
= note: the `test_unstable_lint` lint is unstable
10-
= help: add `#![feature(test_unstable_lint)]` to the crate attributes to enable
10+
= help: add `-Zcrate-attr="feature(test_unstable_lint)"` to the command-line options to enable
1111

1212
warning: unknown lint: `test_unstable_lint`
1313
|
1414
= note: the `test_unstable_lint` lint is unstable
15-
= help: add `#![feature(test_unstable_lint)]` to the crate attributes to enable
15+
= help: add `-Zcrate-attr="feature(test_unstable_lint)"` to the command-line options to enable
1616

1717
warning: 3 warnings emitted
1818

0 commit comments

Comments
 (0)