Skip to content

Commit 6e4cbab

Browse files
wip
1 parent 90e27a8 commit 6e4cbab

27 files changed

+157
-136
lines changed

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,40 +1039,75 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
10391039
}
10401040
}
10411041

1042-
pub(crate) fn allowed_targets_applied(allowed_targets: Vec<Target>, target: Target) -> String {
1043-
if allowed_targets.len() == 1 {
1044-
return allowed_targets[0].singular_name().to_string();
1045-
}
1046-
1042+
/// Takes a list of `allowed_targets` for an attribute, and the `target` the attribute was applied to.
1043+
/// Does some heuristic-based filtering to remove uninteresting targets, and formats the targets into a string
1044+
pub(crate) fn allowed_targets_applied(mut allowed_targets: Vec<Target>, target: Target) -> String {
1045+
// We define groups of "similar" targets.
1046+
// If at least two of the targets are allowed, and the `target` is not in the group,
1047+
// we collapse the entire group to a single entry to simplify the target list
10471048
const FUNCTION_LIKE: &[Target] = &[
10481049
Target::Fn,
10491050
Target::Closure,
1051+
Target::ForeignFn,
10501052
Target::Method(MethodKind::Inherent),
10511053
Target::Method(MethodKind::Trait { body: false }),
10521054
Target::Method(MethodKind::Trait { body: true }),
10531055
Target::Method(MethodKind::TraitImpl),
1054-
Target::ForeignFn,
10551056
];
10561057
const METHOD_LIKE: &[Target] = &[
10571058
Target::Method(MethodKind::Inherent),
10581059
Target::Method(MethodKind::Trait { body: false }),
10591060
Target::Method(MethodKind::Trait { body: true }),
10601061
Target::Method(MethodKind::TraitImpl),
10611062
];
1062-
const IMPL_LIKE: &[Target] = &[Target::Impl { of_trait: false }, Target::Impl { of_trait: true }];
1063-
1064-
1065-
1063+
const IMPL_LIKE: &[Target] =
1064+
&[Target::Impl { of_trait: false }, Target::Impl { of_trait: true }];
1065+
const ADT_LIKE: &[Target] = &[Target::Struct, Target::Enum];
1066+
1067+
let mut added_fake_targets = Vec::new();
1068+
filter_targets(
1069+
&mut allowed_targets,
1070+
FUNCTION_LIKE,
1071+
"functions",
1072+
target,
1073+
&mut added_fake_targets,
1074+
);
1075+
filter_targets(&mut allowed_targets, METHOD_LIKE, "methods", target, &mut added_fake_targets);
1076+
filter_targets(&mut allowed_targets, IMPL_LIKE, "impl blocks", target, &mut added_fake_targets);
1077+
filter_targets(&mut allowed_targets, ADT_LIKE, "data types", target, &mut added_fake_targets);
1078+
1079+
// If there is now only 1 target left, show that as the only possible target
1080+
if allowed_targets.len() + added_fake_targets.len() == 1 {
1081+
return allowed_targets
1082+
.get(0)
1083+
.map(|t| t.singular_name())
1084+
.or(added_fake_targets.get(0).copied())
1085+
.unwrap()
1086+
.to_string();
1087+
}
10661088

1067-
allowed_targets.iter().map(|t| t.plural_name()).join(", ")
1089+
added_fake_targets
1090+
.iter()
1091+
.copied()
1092+
.chain(allowed_targets.iter().map(|t| t.plural_name()))
1093+
.join(", ")
10681094
}
10691095

1070-
fn filter_targets(allowed_targets: &mut Vec<Target>, target_group: &'static [Target], target_group_name: &'static str, target: Target) {
1096+
fn filter_targets(
1097+
allowed_targets: &mut Vec<Target>,
1098+
target_group: &'static [Target],
1099+
target_group_name: &'static str,
1100+
target: Target,
1101+
added_fake_targets: &mut Vec<&'static str>,
1102+
) {
10711103
if target_group.contains(&target) {
1072-
return
1104+
return;
1105+
}
1106+
if allowed_targets.iter().filter(|at| target_group.contains(at)).count() < 2 {
1107+
return;
10731108
}
10741109
allowed_targets.retain(|t| !target_group.contains(t));
1075-
1110+
added_fake_targets.push(target_group_name);
10761111
}
10771112

10781113
/// Parse a single integer.

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ 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, target, ref applied, only } => {
38-
lint_emitter.emit_node_span_lint(
37+
&AttributeLintKind::InvalidTarget { name, target, ref applied, only } => lint_emitter
38+
.emit_node_span_lint(
3939
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
4040
*id,
4141
*span,
@@ -45,7 +45,6 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emi
4545
applied: applied.clone(),
4646
only,
4747
},
48-
)
49-
}
48+
),
5049
}
5150
}

compiler/rustc_hir/src/lints.rs

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

3232
#[derive(Clone, Debug, HashStable_Generic)]
3333
pub enum AttributeLintKind {
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-
},
34+
UnusedDuplicate { this: Span, other: Span, warning: bool },
35+
IllFormedAttributeInput { suggestions: Vec<String> },
36+
EmptyAttribute { first_span: Span },
37+
InvalidTarget { name: Symbol, target: &'static str, applied: String, only: &'static str },
5138
}

compiler/rustc_hir/src/target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl Target {
339339
MethodKind::Inherent => "inherent methods",
340340
MethodKind::Trait { body: false } => "required trait methods",
341341
MethodKind::Trait { body: true } => "provided trait methods",
342-
MethodKind::TraitImpl => "trait method in an impl blocks",
342+
MethodKind::TraitImpl => "trait methods in impl blocks",
343343
},
344344
Target::AssocTy => "associated types",
345345
Target::ForeignFn => "foreign functions",

tests/ui/asm/naked-invalid-attr.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: `#[naked]` attribute cannot be used on crates
1010
LL | #![unsafe(naked)]
1111
| ^^^^^^^^^^^^^^^^^
1212
|
13-
help: `#[naked]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks
13+
help: `#[naked]` can be applied to functions
1414
--> $DIR/naked-invalid-attr.rs:4:1
1515
|
1616
LL | #![unsafe(naked)]
@@ -22,7 +22,7 @@ error: `#[naked]` attribute cannot be used on foreign functions
2222
LL | #[unsafe(naked)]
2323
| ^^^^^^^^^^^^^^^^
2424
|
25-
help: `#[naked]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks
25+
help: `#[naked]` can be applied to methods, functions
2626
--> $DIR/naked-invalid-attr.rs:9:5
2727
|
2828
LL | #[unsafe(naked)]
@@ -34,7 +34,7 @@ error: `#[naked]` attribute cannot be used on structs
3434
LL | #[unsafe(naked)]
3535
| ^^^^^^^^^^^^^^^^
3636
|
37-
help: `#[naked]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks
37+
help: `#[naked]` can be applied to functions
3838
--> $DIR/naked-invalid-attr.rs:13:1
3939
|
4040
LL | #[unsafe(naked)]
@@ -46,7 +46,7 @@ error: `#[naked]` attribute cannot be used on struct fields
4646
LL | #[unsafe(naked)]
4747
| ^^^^^^^^^^^^^^^^
4848
|
49-
help: `#[naked]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks
49+
help: `#[naked]` can be applied to functions
5050
--> $DIR/naked-invalid-attr.rs:16:5
5151
|
5252
LL | #[unsafe(naked)]
@@ -58,7 +58,7 @@ error: `#[naked]` attribute cannot be used on required trait methods
5858
LL | #[unsafe(naked)]
5959
| ^^^^^^^^^^^^^^^^
6060
|
61-
help: `#[naked]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks
61+
help: `#[naked]` can be applied to functions, inherent methods, provided trait methods, trait methods in impl blocks
6262
--> $DIR/naked-invalid-attr.rs:22:5
6363
|
6464
LL | #[unsafe(naked)]
@@ -70,7 +70,7 @@ error: `#[naked]` attribute cannot be used on closures
7070
LL | #[unsafe(naked)]
7171
| ^^^^^^^^^^^^^^^^
7272
|
73-
help: `#[naked]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks
73+
help: `#[naked]` can be applied to methods, functions
7474
--> $DIR/naked-invalid-attr.rs:51:5
7575
|
7676
LL | #[unsafe(naked)]

tests/ui/attributes/attrs-on-params.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: `#[inline]` attribute cannot be used on function params
1010
LL | fn function(#[inline] param: u32) {
1111
| ^^^^^^^^^
1212
|
13-
help: `#[inline]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks, closures
13+
help: `#[inline]` can be applied to functions
1414
--> $DIR/attrs-on-params.rs:3:13
1515
|
1616
LL | fn function(#[inline] param: u32) {

tests/ui/attributes/inline-attribute-enum-variant-error.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: `#[inline]` attribute cannot be used on enum variants
44
LL | #[inline]
55
| ^^^^^^^^^
66
|
7-
help: `#[inline]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks, closures
7+
help: `#[inline]` can be applied to functions
88
--> $DIR/inline-attribute-enum-variant-error.rs:4:5
99
|
1010
LL | #[inline]

tests/ui/attributes/inline/attr-usage-inline.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: `#[inline]` attribute cannot be used on structs
44
LL | #[inline]
55
| ^^^^^^^^^
66
|
7-
help: `#[inline]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks, closures
7+
help: `#[inline]` can be applied to functions
88
--> $DIR/attr-usage-inline.rs:7:1
99
|
1010
LL | #[inline]

tests/ui/attributes/issue-105594-invalid-attr-validation.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: `#[track_caller]` attribute cannot be used on statics
44
LL | #[track_caller]
55
| ^^^^^^^^^^^^^^^
66
|
7-
help: `#[track_caller]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks, required trait methods, foreign functions, closures
7+
help: `#[track_caller]` can be applied to functions
88
--> $DIR/issue-105594-invalid-attr-validation.rs:6:1
99
|
1010
LL | #[track_caller]

tests/ui/attributes/lint_on_root.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: `#[inline]` attribute cannot be used on crates
44
LL | #![inline = ""]
55
| ^^^^^^^^^^^^^^^
66
|
7-
help: `#[inline]` can be applied to functions, inherent methods, provided trait methods, trait method in an impl blocks, closures
7+
help: `#[inline]` can be applied to functions
88
--> $DIR/lint_on_root.rs:3:1
99
|
1010
LL | #![inline = ""]

0 commit comments

Comments
 (0)