Skip to content

Commit 90e27a8

Browse files
wip
1 parent 19eed7e commit 90e27a8

File tree

63 files changed

+547
-469
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+547
-469
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ attr_parsing_empty_attribute =
1010
unused attribute
1111
.suggestion = remove this attribute
1212
13-
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$article} {$target}
13+
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$target}
1414
.help = `#[{$name}]` can {$only}be applied to {$applied}
15-
attr_parsing_invalid_target_lint = `#[{$name}]` attribute cannot be used on {$article} {$target}
15+
attr_parsing_invalid_target_lint = `#[{$name}]` attribute cannot be used on {$target}
1616
.warn = {-attr_parsing_previously_accepted}
1717
.help = `#[{$name}]` can be applied to...
1818

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ use std::collections::BTreeMap;
33
use std::ops::{Deref, DerefMut};
44
use std::sync::LazyLock;
55

6+
use itertools::Itertools;
67
use private::Sealed;
78
use rustc_ast::{self as ast, LitKind, MetaItemLit, NodeId};
89
use rustc_errors::{DiagCtxtHandle, Diagnostic};
910
use rustc_feature::{AttributeTemplate, Features};
1011
use rustc_hir::attrs::AttributeKind;
1112
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
12-
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, HirId, Target};
13+
use rustc_hir::{
14+
AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, HirId, MethodKind, Target,
15+
};
1316
use rustc_session::Session;
1417
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1518

@@ -64,7 +67,6 @@ use crate::parser::{ArgParser, MetaItemParser, PathParser};
6467
use crate::session_diagnostics::{
6568
AttributeParseError, AttributeParseErrorReason, InvalidTarget, UnknownMetaItem,
6669
};
67-
use itertools::Itertools;
6870

6971
type GroupType<S> = LazyLock<GroupTypeInner<S>>;
7072

@@ -690,11 +692,14 @@ impl AllowedTargets {
690692
AllowedTargets::AllowAll => unreachable!(),
691693
AllowedTargets::AllowList(list) => list,
692694
AllowedTargets::AllowListWarnRest(list) => list,
693-
}.iter().filter_map(|target| match target {
695+
}
696+
.iter()
697+
.filter_map(|target| match target {
694698
Allow(target) => Some(*target),
695699
Warn(_) => None,
696700
Error(_) => None,
697-
}).collect()
701+
})
702+
.collect()
698703
}
699704
}
700705

@@ -923,10 +928,16 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
923928
span: attr.span,
924929
kind: AttributeLintKind::InvalidTarget {
925930
name: parts[0],
926-
article: target.article(),
927-
target: target.name(),
928-
only: if allowed_targets.len() == 1 { "only " } else { "" },
929-
applied: allowed_targets_applied(&allowed_targets, target)
931+
target: target.plural_name(),
932+
only: if allowed_targets.len() == 1 {
933+
"only "
934+
} else {
935+
""
936+
},
937+
applied: allowed_targets_applied(
938+
allowed_targets,
939+
target,
940+
),
930941
},
931942
});
932943
}
@@ -935,10 +946,9 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
935946
self.dcx().emit_err(InvalidTarget {
936947
span: attr.span,
937948
name: parts[0],
938-
article: target.article(),
939-
target: target.name(),
949+
target: target.plural_name(),
940950
only: if allowed_targets.len() == 1 { "only " } else { "" },
941-
applied: allowed_targets_applied(&allowed_targets, target)
951+
applied: allowed_targets_applied(allowed_targets, target),
942952
});
943953
}
944954
}
@@ -1029,11 +1039,40 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
10291039
}
10301040
}
10311041

1032-
pub(crate) fn allowed_targets_applied(allowed_targets: &[Target], target: Target) -> String {
1042+
pub(crate) fn allowed_targets_applied(allowed_targets: Vec<Target>, target: Target) -> String {
10331043
if allowed_targets.len() == 1 {
1034-
return format!("{} {}", allowed_targets[0].article(), allowed_targets[0].name())
1044+
return allowed_targets[0].singular_name().to_string();
1045+
}
1046+
1047+
const FUNCTION_LIKE: &[Target] = &[
1048+
Target::Fn,
1049+
Target::Closure,
1050+
Target::Method(MethodKind::Inherent),
1051+
Target::Method(MethodKind::Trait { body: false }),
1052+
Target::Method(MethodKind::Trait { body: true }),
1053+
Target::Method(MethodKind::TraitImpl),
1054+
Target::ForeignFn,
1055+
];
1056+
const METHOD_LIKE: &[Target] = &[
1057+
Target::Method(MethodKind::Inherent),
1058+
Target::Method(MethodKind::Trait { body: false }),
1059+
Target::Method(MethodKind::Trait { body: true }),
1060+
Target::Method(MethodKind::TraitImpl),
1061+
];
1062+
const IMPL_LIKE: &[Target] = &[Target::Impl { of_trait: false }, Target::Impl { of_trait: true }];
1063+
1064+
1065+
1066+
1067+
allowed_targets.iter().map(|t| t.plural_name()).join(", ")
1068+
}
1069+
1070+
fn filter_targets(allowed_targets: &mut Vec<Target>, target_group: &'static [Target], target_group_name: &'static str, target: Target) {
1071+
if target_group.contains(&target) {
1072+
return
10351073
}
1036-
allowed_targets.iter().map(|t| t.name()).join(", ")
1074+
allowed_targets.retain(|t| !target_group.contains(t));
1075+
10371076
}
10381077

10391078
/// Parse a single integer.

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emi
3434
*first_span,
3535
session_diagnostics::EmptyAttributeList { attr_span: *first_span },
3636
),
37-
&AttributeLintKind::InvalidTarget { name, article, target, ref applied, only } => lint_emitter
38-
.emit_node_span_lint(
37+
&AttributeLintKind::InvalidTarget { name, target, ref applied, only } => {
38+
lint_emitter.emit_node_span_lint(
3939
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
4040
*id,
4141
*span,
42-
session_diagnostics::InvalidTargetLint { name, article, target, applied: applied.clone(), only },
43-
),
42+
session_diagnostics::InvalidTargetLint {
43+
name,
44+
target,
45+
applied: applied.clone(),
46+
only,
47+
},
48+
)
49+
}
4450
}
4551
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ pub(crate) struct EmptyAttributeList {
486486
#[help]
487487
pub(crate) struct InvalidTargetLint {
488488
pub name: Symbol,
489-
pub article: &'static str,
490489
pub target: &'static str,
491490
pub applied: String,
492491
pub only: &'static str,
@@ -499,7 +498,6 @@ pub(crate) struct InvalidTarget {
499498
#[help]
500499
pub span: Span,
501500
pub name: Symbol,
502-
pub article: &'static str,
503501
pub target: &'static str,
504502
pub applied: String,
505503
pub only: &'static str,

compiler/rustc_hir/src/lints.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,21 @@ pub struct AttributeLint<Id> {
3131

3232
#[derive(Clone, Debug, HashStable_Generic)]
3333
pub enum AttributeLintKind {
34-
UnusedDuplicate { this: Span, other: Span, warning: bool },
35-
IllFormedAttributeInput { suggestions: Vec<String> },
36-
EmptyAttribute { first_span: Span },
37-
InvalidTarget { name: Symbol, article: &'static str, target: &'static str, applied: String,
38-
only: &'static str, },
34+
UnusedDuplicate {
35+
this: Span,
36+
other: Span,
37+
warning: bool,
38+
},
39+
IllFormedAttributeInput {
40+
suggestions: Vec<String>,
41+
},
42+
EmptyAttribute {
43+
first_span: Span,
44+
},
45+
InvalidTarget {
46+
name: Symbol,
47+
target: &'static str,
48+
applied: String,
49+
only: &'static str,
50+
},
3951
}

compiler/rustc_hir/src/target.rs

Lines changed: 91 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@ pub enum Target {
6767
PatField,
6868
ExprField,
6969
WherePredicate,
70-
71-
// Used for attributes
7270
MacroCall,
7371
Crate,
7472
Delegation,
7573
}
7674

7775
impl Display for Target {
7876
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79-
write!(f, "{}", Self::name(*self))
77+
write!(f, "{}", Self::plural_name(*self))
8078
}
8179
}
8280

@@ -261,74 +259,105 @@ impl Target {
261259
}
262260
}
263261

264-
pub fn is_function_like(&self) -> bool {
262+
pub fn singular_name(self) -> &'static str {
265263
match self {
266-
Target::Fn | Target::Closure | Target::Method(_) | Target::ForeignFn => true,
267-
_ => false,
268-
}
269-
}
270-
271-
pub fn article(self) -> &'static str {
272-
match self {
273-
Target::ExternCrate
274-
| Target::Enum
275-
| Target::Impl { .. }
276-
| Target::Expression
277-
| Target::Arm
278-
| Target::AssocConst
279-
| Target::AssocTy => "an",
280-
_ => "a",
264+
Target::ExternCrate => "an extern crate",
265+
Target::Use => "a use statement",
266+
Target::Static => "a static",
267+
Target::Const => "a constant",
268+
Target::Fn => "a function",
269+
Target::Closure => "a closure",
270+
Target::Mod => "a module",
271+
Target::ForeignMod => "a foreign module",
272+
Target::GlobalAsm => "a global asm",
273+
Target::TyAlias => "a type alias",
274+
Target::Enum => "an enum",
275+
Target::Variant => "an enum variant",
276+
Target::Struct => "a struct",
277+
Target::Field => "a struct field",
278+
Target::Union => "a union",
279+
Target::Trait => "a trait",
280+
Target::TraitAlias => "a trait alias",
281+
Target::Impl { of_trait: false } => "an inherent impl block",
282+
Target::Impl { of_trait: true } => "a trait impl block",
283+
Target::Expression => "an expression",
284+
Target::Statement => "a statement",
285+
Target::Arm => "a match arm",
286+
Target::AssocConst => "an associated const",
287+
Target::Method(kind) => match kind {
288+
MethodKind::Inherent => "an inherent method",
289+
MethodKind::Trait { body: false } => "a required trait method",
290+
MethodKind::Trait { body: true } => "a provided trait method",
291+
MethodKind::TraitImpl => "a trait method in an impl block",
292+
},
293+
Target::AssocTy => "an associated type",
294+
Target::ForeignFn => "a foreign function",
295+
Target::ForeignStatic => "a foreign static",
296+
Target::ForeignTy => "a foreign type",
297+
Target::GenericParam { kind, has_default: _ } => match kind {
298+
GenericParamKind::Type => "a type parameter",
299+
GenericParamKind::Lifetime => "a lifetime parameter",
300+
GenericParamKind::Const => "a const parameter",
301+
},
302+
Target::MacroDef => "a macro def",
303+
Target::Param => "a function param",
304+
Target::PatField => "a pattern field",
305+
Target::ExprField => "a struct field",
306+
Target::WherePredicate => "a where predicate",
307+
Target::MacroCall => "a macro call",
308+
Target::Crate => "a crate",
309+
Target::Delegation => "a delegation",
281310
}
282311
}
283312

284-
pub fn name(self) -> &'static str {
313+
pub fn plural_name(self) -> &'static str {
285314
match self {
286-
Target::ExternCrate => "extern crate",
287-
Target::Use => "use",
288-
Target::Static => "static item",
289-
Target::Const => "constant item",
290-
Target::Fn => "function",
291-
Target::Closure => "closure",
292-
Target::Mod => "module",
293-
Target::ForeignMod => "foreign module",
294-
Target::GlobalAsm => "global asm",
295-
Target::TyAlias => "type alias",
296-
Target::Enum => "enum",
297-
Target::Variant => "enum variant",
298-
Target::Struct => "struct",
299-
Target::Field => "struct field",
300-
Target::Union => "union",
301-
Target::Trait => "trait",
302-
Target::TraitAlias => "trait alias",
303-
Target::Impl { of_trait: false } => "inherent implementation block",
304-
Target::Impl { of_trait: true } => "trait implementation block",
305-
Target::Expression => "expression",
306-
Target::Statement => "statement",
307-
Target::Arm => "match arm",
308-
Target::AssocConst => "associated const",
315+
Target::ExternCrate => "extern crates",
316+
Target::Use => "use statements",
317+
Target::Static => "statics",
318+
Target::Const => "constants",
319+
Target::Fn => "functions",
320+
Target::Closure => "closures",
321+
Target::Mod => "modules",
322+
Target::ForeignMod => "foreign modules",
323+
Target::GlobalAsm => "global asms",
324+
Target::TyAlias => "type aliases",
325+
Target::Enum => "enums",
326+
Target::Variant => "enum variants",
327+
Target::Struct => "structs",
328+
Target::Field => "struct fields",
329+
Target::Union => "unions",
330+
Target::Trait => "traits",
331+
Target::TraitAlias => "trait aliases",
332+
Target::Impl { of_trait: false } => "inherent impl blocks",
333+
Target::Impl { of_trait: true } => "trait impl blocks",
334+
Target::Expression => "expressions",
335+
Target::Statement => "statements",
336+
Target::Arm => "match arms",
337+
Target::AssocConst => "associated consts",
309338
Target::Method(kind) => match kind {
310-
MethodKind::Inherent => "inherent method",
311-
MethodKind::Trait { body: false } => "required trait method",
312-
MethodKind::Trait { body: true } => "provided trait method",
313-
MethodKind::TraitImpl => "trait method in implementation bloock",
339+
MethodKind::Inherent => "inherent methods",
340+
MethodKind::Trait { body: false } => "required trait methods",
341+
MethodKind::Trait { body: true } => "provided trait methods",
342+
MethodKind::TraitImpl => "trait method in an impl blocks",
314343
},
315-
Target::AssocTy => "associated type",
316-
Target::ForeignFn => "foreign function",
317-
Target::ForeignStatic => "foreign static item",
318-
Target::ForeignTy => "foreign type",
344+
Target::AssocTy => "associated types",
345+
Target::ForeignFn => "foreign functions",
346+
Target::ForeignStatic => "foreign statics",
347+
Target::ForeignTy => "foreign types",
319348
Target::GenericParam { kind, has_default: _ } => match kind {
320-
GenericParamKind::Type => "type parameter",
321-
GenericParamKind::Lifetime => "lifetime parameter",
322-
GenericParamKind::Const => "const parameter",
349+
GenericParamKind::Type => "type parameters",
350+
GenericParamKind::Lifetime => "lifetime parameters",
351+
GenericParamKind::Const => "const parameters",
323352
},
324-
Target::MacroDef => "macro def",
325-
Target::Param => "function param",
326-
Target::PatField => "pattern field",
327-
Target::ExprField => "struct field",
328-
Target::WherePredicate => "where predicate",
329-
Target::MacroCall => "macro call",
330-
Target::Crate => "crate",
331-
Target::Delegation => "delegation",
353+
Target::MacroDef => "macro defs",
354+
Target::Param => "function params",
355+
Target::PatField => "pattern fields",
356+
Target::ExprField => "struct fields",
357+
Target::WherePredicate => "where predicates",
358+
Target::MacroCall => "macro calls",
359+
Target::Crate => "crates",
360+
Target::Delegation => "delegations",
332361
}
333362
}
334363
}

0 commit comments

Comments
 (0)