Skip to content

Commit 19eed7e

Browse files
wip
1 parent 35ab731 commit 19eed7e

File tree

45 files changed

+207
-197
lines changed

Some content is hidden

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

45 files changed

+207
-197
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,7 @@ dependencies = [
34803480
name = "rustc_attr_parsing"
34813481
version = "0.0.0"
34823482
dependencies = [
3483+
"itertools",
34833484
"rustc_abi",
34843485
"rustc_ast",
34853486
"rustc_ast_pretty",

compiler/rustc_attr_parsing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
itertools = "0.12"
89
rustc_abi = { path = "../rustc_abi" }
910
rustc_ast = { path = "../rustc_ast" }
1011
rustc_ast_pretty = { path = "../rustc_ast_pretty" }

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use crate::parser::{ArgParser, MetaItemParser, PathParser};
6464
use crate::session_diagnostics::{
6565
AttributeParseError, AttributeParseErrorReason, InvalidTarget, UnknownMetaItem,
6666
};
67+
use itertools::Itertools;
6768

6869
type GroupType<S> = LazyLock<GroupTypeInner<S>>;
6970

@@ -916,31 +917,28 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
916917
match accept.allowed_targets.is_allowed(target) {
917918
AllowedResult::Allowed => {}
918919
AllowedResult::Warn => {
920+
let allowed_targets = accept.allowed_targets.allowed_targets();
919921
emit_lint(AttributeLint {
920922
id: target_id,
921923
span: attr.span,
922924
kind: AttributeLintKind::InvalidTarget {
923925
name: parts[0],
924926
article: target.article(),
925927
target: target.name(),
928+
only: if allowed_targets.len() == 1 { "only " } else { "" },
929+
applied: allowed_targets_applied(&allowed_targets, target)
926930
},
927931
});
928932
}
929933
AllowedResult::Error => {
930934
let allowed_targets = accept.allowed_targets.allowed_targets();
931-
932-
933935
self.dcx().emit_err(InvalidTarget {
934936
span: attr.span,
935937
name: parts[0],
936938
article: target.article(),
937939
target: target.name(),
938940
only: if allowed_targets.len() == 1 { "only " } else { "" },
939-
applied: if allowed_targets.len() == 1 {
940-
target.name()
941-
} else {
942-
"..."
943-
}.to_string()
941+
applied: allowed_targets_applied(&allowed_targets, target)
944942
});
945943
}
946944
}
@@ -1031,6 +1029,13 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
10311029
}
10321030
}
10331031

1032+
pub(crate) fn allowed_targets_applied(allowed_targets: &[Target], target: Target) -> String {
1033+
if allowed_targets.len() == 1 {
1034+
return format!("{} {}", allowed_targets[0].article(), allowed_targets[0].name())
1035+
}
1036+
allowed_targets.iter().map(|t| t.name()).join(", ")
1037+
}
1038+
10341039
/// Parse a single integer.
10351040
///
10361041
/// Used by attributes that take a single integer as argument, such as

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ 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 } => lint_emitter
37+
&AttributeLintKind::InvalidTarget { name, article, target, ref applied, only } => lint_emitter
3838
.emit_node_span_lint(
3939
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
4040
*id,
4141
*span,
42-
session_diagnostics::InvalidTargetLint { name, article, target },
42+
session_diagnostics::InvalidTargetLint { name, article, target, applied: applied.clone(), only },
4343
),
4444
}
4545
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ pub(crate) struct InvalidTargetLint {
488488
pub name: Symbol,
489489
pub article: &'static str,
490490
pub target: &'static str,
491+
pub applied: String,
492+
pub only: &'static str,
491493
}
492494

493495
#[derive(Diagnostic)]

compiler/rustc_hir/src/lints.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ pub enum AttributeLintKind {
3434
UnusedDuplicate { this: Span, other: Span, warning: bool },
3535
IllFormedAttributeInput { suggestions: Vec<String> },
3636
EmptyAttribute { first_span: Span },
37-
InvalidTarget { name: Symbol, article: &'static str, target: &'static str },
37+
InvalidTarget { name: Symbol, article: &'static str, target: &'static str, applied: String,
38+
only: &'static str, },
3839
}

compiler/rustc_hir/src/target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl Target {
310310
MethodKind::Inherent => "inherent method",
311311
MethodKind::Trait { body: false } => "required trait method",
312312
MethodKind::Trait { body: true } => "provided trait method",
313-
MethodKind::TraitImpl => "provided trait method",
313+
MethodKind::TraitImpl => "trait method in implementation bloock",
314314
},
315315
Target::AssocTy => "associated type",
316316
Target::ForeignFn => "foreign function",

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 a crate
1010
LL | #![unsafe(naked)]
1111
| ^^^^^^^^^^^^^^^^^
1212
|
13-
help: `#[naked]` can be applied to...
13+
help: `#[naked]` can be applied to function, inherent method, provided trait method, trait method in implementation bloock
1414
--> $DIR/naked-invalid-attr.rs:4:1
1515
|
1616
LL | #![unsafe(naked)]
@@ -22,7 +22,7 @@ error: `#[naked]` attribute cannot be used on a foreign function
2222
LL | #[unsafe(naked)]
2323
| ^^^^^^^^^^^^^^^^
2424
|
25-
help: `#[naked]` can be applied to...
25+
help: `#[naked]` can be applied to function, inherent method, provided trait method, trait method in implementation bloock
2626
--> $DIR/naked-invalid-attr.rs:9:5
2727
|
2828
LL | #[unsafe(naked)]
@@ -34,7 +34,7 @@ error: `#[naked]` attribute cannot be used on a struct
3434
LL | #[unsafe(naked)]
3535
| ^^^^^^^^^^^^^^^^
3636
|
37-
help: `#[naked]` can be applied to...
37+
help: `#[naked]` can be applied to function, inherent method, provided trait method, trait method in implementation bloock
3838
--> $DIR/naked-invalid-attr.rs:13:1
3939
|
4040
LL | #[unsafe(naked)]
@@ -46,7 +46,7 @@ error: `#[naked]` attribute cannot be used on a struct field
4646
LL | #[unsafe(naked)]
4747
| ^^^^^^^^^^^^^^^^
4848
|
49-
help: `#[naked]` can be applied to...
49+
help: `#[naked]` can be applied to function, inherent method, provided trait method, trait method in implementation bloock
5050
--> $DIR/naked-invalid-attr.rs:16:5
5151
|
5252
LL | #[unsafe(naked)]
@@ -58,7 +58,7 @@ error: `#[naked]` attribute cannot be used on a required trait method
5858
LL | #[unsafe(naked)]
5959
| ^^^^^^^^^^^^^^^^
6060
|
61-
help: `#[naked]` can be applied to...
61+
help: `#[naked]` can be applied to function, inherent method, provided trait method, trait method in implementation bloock
6262
--> $DIR/naked-invalid-attr.rs:22:5
6363
|
6464
LL | #[unsafe(naked)]
@@ -70,7 +70,7 @@ error: `#[naked]` attribute cannot be used on a closure
7070
LL | #[unsafe(naked)]
7171
| ^^^^^^^^^^^^^^^^
7272
|
73-
help: `#[naked]` can be applied to...
73+
help: `#[naked]` can be applied to function, inherent method, provided trait method, trait method in implementation bloock
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 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 function, inherent method, provided trait method, trait method in implementation bloock, closure
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 function, inherent method, provided trait method, trait method in implementation bloock, closure
88
--> $DIR/inline-attribute-enum-variant-error.rs:4:5
99
|
1010
LL | #[inline]

0 commit comments

Comments
 (0)