Skip to content

Commit d25c5a0

Browse files
authored
Unrolled build for rust-lang#116868
Rollup merge of rust-lang#116868 - estebank:suggestion, r=petrochenkov Tweak suggestion span for outer attr and point at item following invalid inner attr After: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn main() {} | ------------ the inner attribute doesn't annotate this function | help: perhaps you meant to use an outer attribute | LL - #![unix_sigpipe = "inherit"] LL + #[unix_sigpipe = "inherit"] | ``` Before: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: perhaps you meant to use an outer attribute | LL | #[unix_sigpipe = "inherit"] | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` CC rust-lang#89566.
2 parents 54e57e6 + 27919ce commit d25c5a0

11 files changed

+160
-60
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

+21-1
Original file line numberDiff line numberDiff line change
@@ -2534,10 +2534,30 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
25342534
if attr.style == AttrStyle::Inner {
25352535
for attr_to_check in ATTRS_TO_CHECK {
25362536
if attr.has_name(*attr_to_check) {
2537+
let item = tcx
2538+
.hir()
2539+
.items()
2540+
.map(|id| tcx.hir().item(id))
2541+
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
2542+
.map(|item| errors::ItemFollowingInnerAttr {
2543+
span: item.ident.span,
2544+
kind: item.kind.descr(),
2545+
});
25372546
tcx.sess.emit_err(errors::InvalidAttrAtCrateLevel {
25382547
span: attr.span,
2539-
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
2548+
sugg_span: tcx
2549+
.sess
2550+
.source_map()
2551+
.span_to_snippet(attr.span)
2552+
.ok()
2553+
.filter(|src| src.starts_with("#!["))
2554+
.map(|_| {
2555+
attr.span
2556+
.with_lo(attr.span.lo() + BytePos(1))
2557+
.with_hi(attr.span.lo() + BytePos(2))
2558+
}),
25402559
name: *attr_to_check,
2560+
item,
25412561
});
25422562
}
25432563
}

compiler/rustc_passes/src/errors.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,15 @@ pub struct UnknownLangItem {
856856

857857
pub struct InvalidAttrAtCrateLevel {
858858
pub span: Span,
859-
pub snippet: Option<String>,
859+
pub sugg_span: Option<Span>,
860860
pub name: Symbol,
861+
pub item: Option<ItemFollowingInnerAttr>,
862+
}
863+
864+
#[derive(Clone, Copy)]
865+
pub struct ItemFollowingInnerAttr {
866+
pub span: Span,
867+
pub kind: &'static str,
861868
}
862869

863870
impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
@@ -871,15 +878,18 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
871878
diag.set_arg("name", self.name);
872879
// Only emit an error with a suggestion if we can create a string out
873880
// of the attribute span
874-
if let Some(src) = self.snippet {
875-
let replacement = src.replace("#!", "#");
881+
if let Some(span) = self.sugg_span {
876882
diag.span_suggestion_verbose(
877-
self.span,
883+
span,
878884
fluent::passes_suggestion,
879-
replacement,
885+
String::new(),
880886
rustc_errors::Applicability::MachineApplicable,
881887
);
882888
}
889+
if let Some(item) = self.item {
890+
diag.set_arg("kind", item.kind);
891+
diag.span_label(item.span, fluent::passes_invalid_attr_at_crate_level_item);
892+
}
883893
diag
884894
}
885895
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ 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
|
9-
LL | #[unix_sigpipe = "inherit"]
10-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
LL - #![unix_sigpipe = "inherit"]
13+
LL + #[unix_sigpipe = "inherit"]
14+
|
1115

1216
error: aborting due to previous error
1317

tests/ui/derives/issue-36617.stderr

+30-10
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,75 @@ 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
|
49-
LL | #[derive(Copy)]
50-
| ~~~~~~~~~~~~~~~
52+
LL - #![derive(Copy)]
53+
LL + #[derive(Copy)]
54+
|
5155

5256
error: `test` attribute cannot be used at crate level
5357
--> $DIR/issue-36617.rs:4:1
5458
|
5559
LL | #![test]
5660
| ^^^^^^^^
61+
...
62+
LL | fn main() {}
63+
| ---- the inner attribute doesn't annotate this function
5764
|
5865
help: perhaps you meant to use an outer attribute
5966
|
60-
LL | #[test]
61-
| ~~~~~~~
67+
LL - #![test]
68+
LL + #[test]
69+
|
6270

6371
error: `test_case` attribute cannot be used at crate level
6472
--> $DIR/issue-36617.rs:7:1
6573
|
6674
LL | #![test_case]
6775
| ^^^^^^^^^^^^^
76+
...
77+
LL | fn main() {}
78+
| ---- the inner attribute doesn't annotate this function
6879
|
6980
help: perhaps you meant to use an outer attribute
7081
|
71-
LL | #[test_case]
72-
| ~~~~~~~~~~~~
82+
LL - #![test_case]
83+
LL + #[test_case]
84+
|
7385

7486
error: `bench` attribute cannot be used at crate level
7587
--> $DIR/issue-36617.rs:10:1
7688
|
7789
LL | #![bench]
7890
| ^^^^^^^^^
91+
...
92+
LL | fn main() {}
93+
| ---- the inner attribute doesn't annotate this function
7994
|
8095
help: perhaps you meant to use an outer attribute
8196
|
82-
LL | #[bench]
83-
| ~~~~~~~~
97+
LL - #![bench]
98+
LL + #[bench]
99+
|
84100

85101
error: `global_allocator` attribute cannot be used at crate level
86102
--> $DIR/issue-36617.rs:13:1
87103
|
88104
LL | #![global_allocator]
89105
| ^^^^^^^^^^^^^^^^^^^^
106+
...
107+
LL | fn main() {}
108+
| ---- the inner attribute doesn't annotate this function
90109
|
91110
help: perhaps you meant to use an outer attribute
92111
|
93-
LL | #[global_allocator]
94-
| ~~~~~~~~~~~~~~~~~~~
112+
LL - #![global_allocator]
113+
LL + #[global_allocator]
114+
|
95115

96116
error: aborting due to 10 previous errors
97117

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ 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
|
17-
LL | #[bench = "4100"]
20+
LL - #![bench = "4100"]
21+
LL + #[bench = "4100"]
1822
|
1923

2024
error: aborting due to 2 previous errors

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)