Skip to content

Commit cead675

Browse files
wip
1 parent 37ca219 commit cead675

File tree

98 files changed

+565
-531
lines changed

Some content is hidden

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

98 files changed

+565
-531
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ attr_parsing_empty_attribute =
1010
unused attribute
1111
.suggestion = remove this attribute
1212
13-
attr_parsing_invalid_target = this attribute is not allowed on this target
13+
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$article} {$target}
1414
1515
attr_parsing_empty_confusables =
1616
expected at least one confusable name

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ use crate::attributes::transparency::TransparencyParser;
6161
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
6262
use crate::context::MaybeWarn::{Allow, Error, Warn};
6363
use crate::parser::{ArgParser, MetaItemParser, PathParser};
64-
use crate::session_diagnostics::{AttributeParseError, AttributeParseErrorReason, UnknownMetaItem};
64+
use crate::session_diagnostics::{
65+
AttributeParseError, AttributeParseErrorReason, InvalidTarget, UnknownMetaItem,
66+
};
6567

6668
type GroupType<S> = LazyLock<GroupTypeInner<S>>;
6769

@@ -905,16 +907,20 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
905907
emit_lint(AttributeLint {
906908
id: target_id,
907909
span: n.item.span(),
908-
kind: AttributeLintKind::InvalidTarget {},
910+
kind: AttributeLintKind::InvalidTarget {
911+
name: parts[0],
912+
article: target.article(),
913+
target: target.name(),
914+
},
909915
});
910916
}
911917
AllowedResult::Error => {
912-
self.dcx()
913-
.struct_span_err(
914-
n.item.span(),
915-
"this attribute is not allowed on this target",
916-
)
917-
.emit();
918+
self.dcx().emit_err(InvalidTarget {
919+
span: n.item.span(),
920+
name: parts[0],
921+
article: target.article(),
922+
target: target.name(),
923+
});
918924
}
919925
}
920926
}

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +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 {} => lint_emitter.emit_node_span_lint(
38-
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
39-
*id,
40-
*span,
41-
session_diagnostics::InvalidTarget {},
42-
),
37+
&AttributeLintKind::InvalidTarget { name, article, target } => lint_emitter
38+
.emit_node_span_lint(
39+
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
40+
*id,
41+
*span,
42+
session_diagnostics::InvalidTargetLint { name, article, target },
43+
),
4344
}
4445
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,21 @@ pub(crate) struct EmptyAttributeList {
482482

483483
#[derive(LintDiagnostic)]
484484
#[diag(attr_parsing_invalid_target)]
485-
pub(crate) struct InvalidTarget {}
485+
pub(crate) struct InvalidTargetLint {
486+
pub name: Symbol,
487+
pub article: &'static str,
488+
pub target: &'static str,
489+
}
490+
491+
#[derive(Diagnostic)]
492+
#[diag(attr_parsing_invalid_target)]
493+
pub(crate) struct InvalidTarget {
494+
#[primary_span]
495+
pub span: Span,
496+
pub name: Symbol,
497+
pub article: &'static str,
498+
pub target: &'static str,
499+
}
486500

487501
#[derive(Diagnostic)]
488502
#[diag(attr_parsing_invalid_alignment_value, code = E0589)]

compiler/rustc_hir/src/lints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::fingerprint::Fingerprint;
22
use rustc_macros::HashStable_Generic;
3-
use rustc_span::Span;
3+
use rustc_span::{Span, Symbol};
44

55
use crate::HirId;
66

@@ -34,5 +34,5 @@ pub enum AttributeLintKind {
3434
UnusedDuplicate { this: Span, other: Span, warning: bool },
3535
IllFormedAttributeInput { suggestions: Vec<String> },
3636
EmptyAttribute { first_span: Span },
37-
InvalidTarget {},
37+
InvalidTarget { name: Symbol, article: &'static str, target: &'static str },
3838
}

compiler/rustc_hir/src/target.rs

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

264+
pub fn article(self) -> &'static str {
265+
match self {
266+
Target::ExternCrate
267+
| Target::Enum
268+
| Target::Impl { .. }
269+
| Target::Expression
270+
| Target::Arm
271+
| Target::AssocConst
272+
| Target::AssocTy => "an",
273+
_ => "a",
274+
}
275+
}
276+
264277
pub fn name(self) -> &'static str {
265278
match self {
266279
Target::ExternCrate => "extern crate",

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ passes_linkage =
379379
.label = not a function definition or static
380380
381381
passes_loop_match_attr =
382-
`#[loop_match]` this attribute is not allowed on this target
382+
`#[loop_match]` attribute cannot be used on
383383
.label = not a loop
384384
385385
passes_macro_export =

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
// Checks that the #[unsafe(naked)] attribute can be placed on function definitions only.
22
//
33
//@ needs-asm-support
4-
#![unsafe(naked)] //~ ERROR this attribute is not allowed on this target
4+
#![unsafe(naked)] //~ ERROR attribute cannot be used on
55

66
use std::arch::naked_asm;
77

88
extern "C" {
9-
#[unsafe(naked)] //~ ERROR this attribute is not allowed on this target
9+
#[unsafe(naked)] //~ ERROR attribute cannot be used on
1010
fn f();
1111
}
1212

13-
#[unsafe(naked)] //~ ERROR this attribute is not allowed on this target
13+
#[unsafe(naked)] //~ ERROR attribute cannot be used on
1414
#[repr(C)]
1515
struct S {
16-
#[unsafe(naked)] //~ ERROR this attribute is not allowed on this target
16+
#[unsafe(naked)] //~ ERROR attribute cannot be used on
1717
a: u32,
1818
b: u32,
1919
}
2020

2121
trait Invoke {
22-
#[unsafe(naked)] //~ ERROR this attribute is not allowed on this target
22+
#[unsafe(naked)] //~ ERROR attribute cannot be used on
2323
extern "C" fn invoke(&self);
2424
}
2525

@@ -48,7 +48,7 @@ impl S {
4848
}
4949

5050
fn main() {
51-
#[unsafe(naked)] //~ ERROR this attribute is not allowed on this target
51+
#[unsafe(naked)] //~ ERROR attribute cannot be used on
5252
|| {};
5353
}
5454

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `a`
44
LL | #[::a]
55
| ^ use of unresolved module or unlinked crate `a`
66

7-
error: this attribute is not allowed on this target
7+
error: `#[naked]` attribute cannot be used on a crate
88
--> $DIR/naked-invalid-attr.rs:4:11
99
|
1010
LL | #![unsafe(naked)]
1111
| ^^^^^
1212

13-
error: this attribute is not allowed on this target
13+
error: `#[naked]` attribute cannot be used on a foreign function
1414
--> $DIR/naked-invalid-attr.rs:9:14
1515
|
1616
LL | #[unsafe(naked)]
1717
| ^^^^^
1818

19-
error: this attribute is not allowed on this target
19+
error: `#[naked]` attribute cannot be used on a struct
2020
--> $DIR/naked-invalid-attr.rs:13:10
2121
|
2222
LL | #[unsafe(naked)]
2323
| ^^^^^
2424

25-
error: this attribute is not allowed on this target
25+
error: `#[naked]` attribute cannot be used on a struct field
2626
--> $DIR/naked-invalid-attr.rs:16:14
2727
|
2828
LL | #[unsafe(naked)]
2929
| ^^^^^
3030

31-
error: this attribute is not allowed on this target
31+
error: `#[naked]` attribute cannot be used on a required trait method
3232
--> $DIR/naked-invalid-attr.rs:22:14
3333
|
3434
LL | #[unsafe(naked)]
3535
| ^^^^^
3636

37-
error: this attribute is not allowed on this target
37+
error: `#[naked]` attribute cannot be used on a closure
3838
--> $DIR/naked-invalid-attr.rs:51:14
3939
|
4040
LL | #[unsafe(naked)]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This checks that incorrect params on function parameters are caught
22

33
fn function(#[inline] param: u32) {
4-
//~^ ERROR this attribute is not allowed on this target
4+
//~^ ERROR attribute cannot be used on
55
//~| ERROR allow, cfg, cfg_attr, deny, expect, forbid, and warn are the only allowed built-in attributes
66
}
77

0 commit comments

Comments
 (0)