Skip to content

Commit 64a8aa5

Browse files
Rollup merge of #164607 - mejrs:lint_name, r=JonathanBrouwer
destringlyfy lint infrastructure Currently, lints are passed as strings and parsed/split/reformatted/compared/etc in many many places. It's not always clear whether that string includes the tool or not. Instead, they are passed around as: ```rust pub struct LintName { pub tool: Option<Symbol>, pub name: Symbol, } ``` r? @JonathanBrouwer
2 parents 026faeb + 361557a commit 64a8aa5

30 files changed

Lines changed: 424 additions & 396 deletions

File tree

‎compiler/rustc_attr_ir/src/lint.rs‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use rustc_macros::{Decodable, Encodable, PrintAttribute, StableHash};
2-
use rustc_span::{Ident, Span, Symbol, sym};
2+
use rustc_span::{Span, Symbol, sym};
3+
use rustc_structures::LintName;
34

45
use crate::{HashIgnoredAttrId, PrintAttribute};
6+
57
#[derive(Clone, Copy, Debug, StableHash, Encodable, Decodable, PrintAttribute)]
68
pub enum LintCheckKind {
79
Allow,
@@ -43,21 +45,7 @@ impl LintCheckKind {
4345
/// ```
4446
#[derive(Clone, Debug, StableHash, Encodable, Decodable, PrintAttribute)]
4547
pub struct LintCheck {
46-
/// The lint's tool name, if present.
47-
pub tool: Option<Ident>,
48-
/// The lint's name, minus the tool name if any.
49-
///
50-
/// For example:
51-
/// - `clippy::blah` -> `blah`
52-
/// - `unstable_imports` -> `unstable_imports`
53-
/// - `custom_tool::blah::bleh` -> `blah::bleh`
54-
pub lint_name: Symbol,
55-
/// The span of the lint name.
56-
///
57-
/// For example `#[deny(foo, bar, reason = "reason")]`
58-
/// produces multiple `LintCheck`s, one with a span pointing to `foo`
59-
/// and another pointing to `bar`.
60-
pub lint_span: Span,
48+
pub lint_name: LintName,
6149
pub kind: LintCheckKind,
6250
pub reason: Option<Symbol>,
6351
/// Needed by `LintExpectationId` to track fulfilled expectations
@@ -68,4 +56,10 @@ pub struct LintCheck {
6856
/// inside one attribute are all smooshed and flattened together, so
6957
/// multiple `LintCheck`s can have the same `attr_span`.
7058
pub attr_span: Span,
59+
/// The span of the entire lint name.
60+
///
61+
/// #[deny(foo::bar::baz)]
62+
/// ^^^^^^^^^^^^^
63+
pub lint_span: Span,
64+
pub tool_span: Option<Span>,
7165
}

‎compiler/rustc_attr_ir/src/pretty_printing.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use rustc_data_structures::fx::FxIndexMap;
1414
use rustc_span::def_id::DefId;
1515
use rustc_span::hygiene::Transparency;
1616
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
17-
use rustc_structures::{CollapseMacroDebuginfo, CrateType, Limit, NativeLibKind, SanitizerSet};
17+
use rustc_structures::{
18+
CollapseMacroDebuginfo, CrateType, Limit, LintName, NativeLibKind, SanitizerSet,
19+
};
1820
use thin_vec::ThinVec;
1921

2022
use crate::HashIgnoredAttrId;
@@ -223,4 +225,5 @@ print_debug!(
223225
CrateType,
224226
NativeLibKind,
225227
CollapseMacroDebuginfo,
228+
LintName,
226229
);

‎compiler/rustc_attr_parsing/src/attributes/lint.rs‎

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use rustc_ast::join_path_syms;
21
use rustc_attr_ir::AttributeKind;
32
use rustc_attr_ir::lint::{LintCheck, LintCheckKind};
43
use rustc_attr_ir::target::{AssocCtxt, MethodKind, Target};
54
use rustc_lint_defs::builtin::UNUSED_ATTRIBUTES;
6-
use rustc_span::{Symbol, sym};
5+
use rustc_span::sym;
6+
use rustc_structures::LintName;
77
use thin_vec::ThinVec;
88

99
use crate::attributes::{AcceptMapping, AttributeParser, AttributeStability};
@@ -52,33 +52,21 @@ impl LintParser {
5252
match item {
5353
MetaItemOrLitParser::MetaItemParser(p) => {
5454
match p.args() {
55-
ArgParser::NoArgs => {
56-
let (tool, lint_name) = match &*p.path().0.segments {
57-
[] => unreachable!(),
58-
[lint_name] => (None, lint_name.ident.name),
59-
[tool, lint_name] => (Some(tool.ident), lint_name.ident.name),
60-
// Not reachable for any builtin lints,
61-
// but reachable using `register_tool`.
62-
[tool, multi_segment_lint_name @ ..] => (
63-
Some(tool.ident),
64-
Symbol::intern(&join_path_syms(
65-
multi_segment_lint_name
66-
.iter()
67-
.map(|seg| seg.ident.name),
68-
)),
69-
),
70-
};
71-
72-
self.lints.push(LintCheck {
73-
tool,
74-
lint_name,
75-
lint_span: p.span(),
76-
kind,
77-
attr_id,
78-
reason,
79-
attr_span,
80-
})
81-
}
55+
ArgParser::NoArgs => self.lints.push(LintCheck {
56+
lint_name: LintName::new(
57+
p.path().0.segments.iter().map(|s| s.ident.name),
58+
),
59+
kind,
60+
attr_id,
61+
reason,
62+
attr_span,
63+
lint_span: p.span(),
64+
tool_span: if let [tool, _, ..] = &*p.path().0.segments {
65+
Some(tool.span())
66+
} else {
67+
None
68+
},
69+
}),
8270
// We've found a `reason = "reason"` but we're not the last element.
8371
ArgParser::NameValue(nv) if p.path().word_is(sym::reason) => {
8472
cx.emit_err(diagnostics::MalformedAttribute {

‎compiler/rustc_driver_impl/src/lib.rs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use rustc_session::output::invalid_output_for_target;
5959
use rustc_session::{EarlyDiagCtxt, Session, config};
6060
use rustc_span::def_id::LOCAL_CRATE;
6161
use rustc_span::{DUMMY_SP, FileName};
62-
use rustc_structures::CrateType;
62+
use rustc_structures::{CrateType, LintName};
6363
use rustc_target::json::ToJson;
6464
use rustc_target::spec::{Target, TargetTuple};
6565
use tracing::trace;
@@ -990,11 +990,9 @@ Available lint options:
990990
lints
991991
}
992992

993-
fn sort_lint_groups(
994-
lints: Vec<(&'static str, Vec<LintId>, bool)>,
995-
) -> Vec<(&'static str, Vec<LintId>)> {
993+
fn sort_lint_groups(lints: Vec<(LintName, Vec<LintId>, bool)>) -> Vec<(LintName, Vec<LintId>)> {
996994
let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect();
997-
lints.sort_by_key(|l| l.0);
995+
lints.sort_by(|left, right| Ord::cmp(&left.0, &right.0));
998996
lints
999997
}
1000998

@@ -1041,7 +1039,7 @@ Available lint options:
10411039
loaded_groups
10421040
.iter()
10431041
.chain(&builtin_groups)
1044-
.map(|&(s, _)| s.chars().count())
1042+
.map(|(s, _)| s.name.as_str().chars().count())
10451043
.max()
10461044
.unwrap_or(0),
10471045
);
@@ -1054,7 +1052,7 @@ Available lint options:
10541052

10551053
safe_println!("Lint groups provided by rustc:\n");
10561054

1057-
let print_lint_groups = |lints: Vec<(&'static str, Vec<LintId>)>, all_warnings| {
1055+
let print_lint_groups = |lints: Vec<(LintName, Vec<LintId>)>, all_warnings| {
10581056
safe_println!(" {} sub-lints", padded("name"));
10591057
safe_println!(" {} ---------", padded("----"));
10601058

@@ -1063,7 +1061,7 @@ Available lint options:
10631061
}
10641062

10651063
for (name, to) in lints {
1066-
let name = name.to_lowercase().replace('_', "-");
1064+
let name = name.to_string().to_lowercase().replace('_', "-");
10671065
let desc = to
10681066
.into_iter()
10691067
.map(|x| x.to_string().replace('_', "-"))

0 commit comments

Comments
 (0)