Skip to content

Commit 35ab731

Browse files
wip
1 parent 43d9b17 commit 35ab731

16 files changed

+54
-24
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ attr_parsing_empty_attribute =
1111
.suggestion = remove this attribute
1212
1313
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$article} {$target}
14-
.help = `#[{$name}]` can be applied to...
14+
.help = `#[{$name}]` can {$only}be applied to {$applied}
1515
attr_parsing_invalid_target_lint = `#[{$name}]` attribute cannot be used on {$article} {$target}
1616
.warn = {-attr_parsing_previously_accepted}
1717
.help = `#[{$name}]` can be applied to...

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,18 @@ impl AllowedTargets {
683683
}
684684
}
685685
}
686+
687+
pub(crate) fn allowed_targets(&self) -> Vec<Target> {
688+
match self {
689+
AllowedTargets::AllowAll => unreachable!(),
690+
AllowedTargets::AllowList(list) => list,
691+
AllowedTargets::AllowListWarnRest(list) => list,
692+
}.iter().filter_map(|target| match target {
693+
Allow(target) => Some(*target),
694+
Warn(_) => None,
695+
Error(_) => None,
696+
}).collect()
697+
}
686698
}
687699

688700
#[derive(Debug, Eq, PartialEq)]
@@ -915,11 +927,20 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
915927
});
916928
}
917929
AllowedResult::Error => {
930+
let allowed_targets = accept.allowed_targets.allowed_targets();
931+
932+
918933
self.dcx().emit_err(InvalidTarget {
919934
span: attr.span,
920935
name: parts[0],
921936
article: target.article(),
922937
target: target.name(),
938+
only: if allowed_targets.len() == 1 { "only " } else { "" },
939+
applied: if allowed_targets.len() == 1 {
940+
target.name()
941+
} else {
942+
"..."
943+
}.to_string()
923944
});
924945
}
925946
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ pub(crate) struct InvalidTarget {
499499
pub name: Symbol,
500500
pub article: &'static str,
501501
pub target: &'static str,
502+
pub applied: String,
503+
pub only: &'static str,
502504
}
503505

504506
#[derive(Diagnostic)]

compiler/rustc_hir/src/target.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ impl Target {
261261
}
262262
}
263263

264+
pub fn is_function_like(&self) -> bool {
265+
match self {
266+
Target::Fn | Target::Closure | Target::Method(_) | Target::ForeignFn => true,
267+
_ => false,
268+
}
269+
}
270+
264271
pub fn article(self) -> &'static str {
265272
match self {
266273
Target::ExternCrate

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 a function param
1010
LL | fn function(#[inline] param: u32) {
1111
| ^^^^^^^^^
1212
|
13-
help: `#[inline]` can be applied to...
13+
help: `#[inline]` can be applied to ...
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 a enum variant
44
LL | #[inline]
55
| ^^^^^^^^^
66
|
7-
help: `#[inline]` can be applied to...
7+
help: `#[inline]` can be applied to ...
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 a struct
44
LL | #[inline]
55
| ^^^^^^^^^
66
|
7-
help: `#[inline]` can be applied to...
7+
help: `#[inline]` can be applied to ...
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 a static item
44
LL | #[track_caller]
55
| ^^^^^^^^^^^^^^^
66
|
7-
help: `#[track_caller]` can be applied to...
7+
help: `#[track_caller]` can be applied to ...
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 a crate
44
LL | #![inline = ""]
55
| ^^^^^^^^^^^^^^^
66
|
7-
help: `#[inline]` can be applied to...
7+
help: `#[inline]` can be applied to ...
88
--> $DIR/lint_on_root.rs:3:1
99
|
1010
LL | #![inline = ""]

tests/ui/attributes/malformed-fn-align.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ error: `#[rustc_align]` attribute cannot be used on a struct
7575
LL | #[rustc_align(16)]
7676
| ^^^^^^^^^^^^^^^^^^
7777
|
78-
help: `#[rustc_align]` can be applied to...
78+
help: `#[rustc_align]` can be applied to ...
7979
--> $DIR/malformed-fn-align.rs:44:1
8080
|
8181
LL | #[rustc_align(16)]
@@ -87,7 +87,7 @@ error: `#[rustc_align]` attribute cannot be used on a constant item
8787
LL | #[rustc_align(32)]
8888
| ^^^^^^^^^^^^^^^^^^
8989
|
90-
help: `#[rustc_align]` can be applied to...
90+
help: `#[rustc_align]` can be applied to ...
9191
--> $DIR/malformed-fn-align.rs:47:1
9292
|
9393
LL | #[rustc_align(32)]
@@ -99,7 +99,7 @@ error: `#[rustc_align]` attribute cannot be used on a module
9999
LL | #[rustc_align(32)]
100100
| ^^^^^^^^^^^^^^^^^^
101101
|
102-
help: `#[rustc_align]` can be applied to...
102+
help: `#[rustc_align]` can be applied to ...
103103
--> $DIR/malformed-fn-align.rs:50:1
104104
|
105105
LL | #[rustc_align(32)]
@@ -111,7 +111,7 @@ error: `#[rustc_align]` attribute cannot be used on a use
111111
LL | #[rustc_align(32)]
112112
| ^^^^^^^^^^^^^^^^^^
113113
|
114-
help: `#[rustc_align]` can be applied to...
114+
help: `#[rustc_align]` can be applied to ...
115115
--> $DIR/malformed-fn-align.rs:53:1
116116
|
117117
LL | #[rustc_align(32)]

0 commit comments

Comments
 (0)