Skip to content

Commit 671107b

Browse files
committed
Point at item following invalid crate-level inner attribute
1 parent 56bb470 commit 671107b

10 files changed

+152
-41
lines changed

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ passes_invalid_attr_at_crate_level =
393393
`{$name}` attribute cannot be used at crate level
394394
.suggestion = perhaps you meant to use an outer attribute
395395
396+
passes_invalid_attr_at_crate_level_item =
397+
the inner attribute doesn't annotate this {$kind}
398+
396399
passes_invalid_deprecation_version =
397400
invalid deprecation version found
398401
.label = invalid deprecation version

compiler/rustc_passes/src/check_attr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,15 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
25222522
];
25232523

25242524
for attr in attrs {
2525+
let item = tcx
2526+
.hir()
2527+
.items()
2528+
.map(|id| tcx.hir().item(id))
2529+
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
2530+
.map(|item| errors::ItemFollowingInnerAttr {
2531+
span: item.span,
2532+
kind: item.kind.descr(),
2533+
});
25252534
// This function should only be called with crate attributes
25262535
// which are inner attributes always but lets check to make sure
25272536
if attr.style == AttrStyle::Inner {
@@ -2531,6 +2540,7 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
25312540
span: attr.span,
25322541
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
25332542
name: *attr_to_check,
2543+
item,
25342544
});
25352545
}
25362546
}

compiler/rustc_passes/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,13 @@ pub struct InvalidAttrAtCrateLevel {
851851
pub span: Span,
852852
pub snippet: Option<String>,
853853
pub name: Symbol,
854+
pub item: Option<ItemFollowingInnerAttr>,
855+
}
856+
857+
#[derive(Clone, Copy)]
858+
pub struct ItemFollowingInnerAttr {
859+
pub span: Span,
860+
pub kind: &'static str,
854861
}
855862

856863
impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
@@ -872,6 +879,10 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
872879
rustc_errors::Applicability::MachineApplicable,
873880
);
874881
}
882+
if let Some(item) = self.item {
883+
diag.set_arg("kind", item.kind);
884+
diag.span_label(item.span, fluent::passes_invalid_attr_at_crate_level_item);
885+
}
875886
diag
876887
}
877888
}

tests/ui/attributes/unix_sigpipe/unix_sigpipe-crate.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: `unix_sigpipe` attribute cannot be used at crate level
33
|
44
LL | #![unix_sigpipe = "inherit"]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
LL |
7+
LL | fn main() {}
8+
| ------------ the inner attribute doesn't annotate this function
69
|
710
help: perhaps you meant to use an outer attribute
811
|

tests/ui/derives/issue-36617.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ error: `derive` attribute cannot be used at crate level
4343
|
4444
LL | #![derive(Copy)]
4545
| ^^^^^^^^^^^^^^^^
46+
...
47+
LL | fn main() {}
48+
| ------------ the inner attribute doesn't annotate this function
4649
|
4750
help: perhaps you meant to use an outer attribute
4851
|
@@ -55,6 +58,9 @@ error: `test` attribute cannot be used at crate level
5558
|
5659
LL | #![test]
5760
| ^^^^^^^^
61+
...
62+
LL | fn main() {}
63+
| ------------ the inner attribute doesn't annotate this function
5864
|
5965
help: perhaps you meant to use an outer attribute
6066
|
@@ -67,6 +73,9 @@ error: `test_case` attribute cannot be used at crate level
6773
|
6874
LL | #![test_case]
6975
| ^^^^^^^^^^^^^
76+
...
77+
LL | fn main() {}
78+
| ------------ the inner attribute doesn't annotate this function
7079
|
7180
help: perhaps you meant to use an outer attribute
7281
|
@@ -79,6 +88,9 @@ error: `bench` attribute cannot be used at crate level
7988
|
8089
LL | #![bench]
8190
| ^^^^^^^^^
91+
...
92+
LL | fn main() {}
93+
| ------------ the inner attribute doesn't annotate this function
8294
|
8395
help: perhaps you meant to use an outer attribute
8496
|
@@ -91,6 +103,9 @@ error: `global_allocator` attribute cannot be used at crate level
91103
|
92104
LL | #![global_allocator]
93105
| ^^^^^^^^^^^^^^^^^^^^
106+
...
107+
LL | fn main() {}
108+
| ------------ the inner attribute doesn't annotate this function
94109
|
95110
help: perhaps you meant to use an outer attribute
96111
|

tests/ui/feature-gates/issue-43106-gating-of-bench.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ error: `bench` attribute cannot be used at crate level
1111
|
1212
LL | #![bench = "4100"]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
...
15+
LL | fn main() {}
16+
| ------------ the inner attribute doesn't annotate this function
1417
|
1518
help: perhaps you meant to use an outer attribute
1619
|

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
//~^ ERROR attribute should be applied to function or closure
3333
mod inline {
3434
//~^ NOTE not a function or closure
35+
//~| NOTE the inner attribute doesn't annotate this module
36+
//~| NOTE the inner attribute doesn't annotate this module
37+
//~| NOTE the inner attribute doesn't annotate this module
38+
//~| NOTE the inner attribute doesn't annotate this module
39+
//~| NOTE the inner attribute doesn't annotate this module
40+
//~| NOTE the inner attribute doesn't annotate this module
3541

3642
mod inner { #![inline] }
3743
//~^ ERROR attribute should be applied to function or closure

0 commit comments

Comments
 (0)