Skip to content

Commit 56bb470

Browse files
committed
Tweak suggestion span
CC #89566.
1 parent 93e62a2 commit 56bb470

8 files changed

+45
-30
lines changed

compiler/rustc_passes/src/errors.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_errors::{
1212
use rustc_hir::{self as hir, ExprKind, Target};
1313
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1414
use rustc_middle::ty::{MainDefinition, Ty};
15-
use rustc_span::{Span, Symbol, DUMMY_SP};
15+
use rustc_span::{BytePos, Span, Symbol, DUMMY_SP};
1616

1717
use crate::check_attr::ProcMacroKind;
1818
use crate::lang_items::Duplicate;
@@ -864,12 +864,11 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
864864
diag.set_arg("name", self.name);
865865
// Only emit an error with a suggestion if we can create a string out
866866
// of the attribute span
867-
if let Some(src) = self.snippet {
868-
let replacement = src.replace("#!", "#");
867+
if let Some(src) = self.snippet && src.starts_with("#![") {
869868
diag.span_suggestion_verbose(
870-
self.span,
869+
self.span.with_lo(self.span.lo() + BytePos(1)).with_hi(self.span.lo() + BytePos(2)),
871870
fluent::passes_suggestion,
872-
replacement,
871+
String::new(),
873872
rustc_errors::Applicability::MachineApplicable,
874873
);
875874
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ LL | #![unix_sigpipe = "inherit"]
66
|
77
help: perhaps you meant to use an outer attribute
88
|
9-
LL | #[unix_sigpipe = "inherit"]
10-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
LL - #![unix_sigpipe = "inherit"]
10+
LL + #[unix_sigpipe = "inherit"]
11+
|
1112

1213
error: aborting due to previous error
1314

tests/ui/derives/issue-36617.stderr

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ LL | #![derive(Copy)]
4646
|
4747
help: perhaps you meant to use an outer attribute
4848
|
49-
LL | #[derive(Copy)]
50-
| ~~~~~~~~~~~~~~~
49+
LL - #![derive(Copy)]
50+
LL + #[derive(Copy)]
51+
|
5152

5253
error: `test` attribute cannot be used at crate level
5354
--> $DIR/issue-36617.rs:4:1
@@ -57,8 +58,9 @@ LL | #![test]
5758
|
5859
help: perhaps you meant to use an outer attribute
5960
|
60-
LL | #[test]
61-
| ~~~~~~~
61+
LL - #![test]
62+
LL + #[test]
63+
|
6264

6365
error: `test_case` attribute cannot be used at crate level
6466
--> $DIR/issue-36617.rs:7:1
@@ -68,8 +70,9 @@ LL | #![test_case]
6870
|
6971
help: perhaps you meant to use an outer attribute
7072
|
71-
LL | #[test_case]
72-
| ~~~~~~~~~~~~
73+
LL - #![test_case]
74+
LL + #[test_case]
75+
|
7376

7477
error: `bench` attribute cannot be used at crate level
7578
--> $DIR/issue-36617.rs:10:1
@@ -79,8 +82,9 @@ LL | #![bench]
7982
|
8083
help: perhaps you meant to use an outer attribute
8184
|
82-
LL | #[bench]
83-
| ~~~~~~~~
85+
LL - #![bench]
86+
LL + #[bench]
87+
|
8488

8589
error: `global_allocator` attribute cannot be used at crate level
8690
--> $DIR/issue-36617.rs:13:1
@@ -90,8 +94,9 @@ LL | #![global_allocator]
9094
|
9195
help: perhaps you meant to use an outer attribute
9296
|
93-
LL | #[global_allocator]
94-
| ~~~~~~~~~~~~~~~~~~~
97+
LL - #![global_allocator]
98+
LL + #[global_allocator]
99+
|
95100

96101
error: aborting due to 10 previous errors
97102

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ LL | #![bench = "4100"]
1414
|
1515
help: perhaps you meant to use an outer attribute
1616
|
17-
LL | #[bench = "4100"]
17+
LL - #![bench = "4100"]
18+
LL + #[bench = "4100"]
1819
|
1920

2021
error: aborting due to 2 previous errors

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ LL | #![macro_export]
132132
|
133133
help: perhaps you meant to use an outer attribute
134134
|
135-
LL | #[macro_export]
135+
LL - #![macro_export]
136+
LL + #[macro_export]
136137
|
137138

138139
error: `rustc_main` attribute cannot be used at crate level
@@ -143,8 +144,9 @@ LL | #![rustc_main]
143144
|
144145
help: perhaps you meant to use an outer attribute
145146
|
146-
LL | #[rustc_main]
147-
| ~~~~~~~~~~~~~
147+
LL - #![rustc_main]
148+
LL + #[rustc_main]
149+
|
148150

149151
error: `start` attribute cannot be used at crate level
150152
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:16:1
@@ -154,7 +156,8 @@ LL | #![start]
154156
|
155157
help: perhaps you meant to use an outer attribute
156158
|
157-
LL | #[start]
159+
LL - #![start]
160+
LL + #[start]
158161
|
159162

160163
error: `repr` attribute cannot be used at crate level
@@ -165,7 +168,8 @@ LL | #![repr()]
165168
|
166169
help: perhaps you meant to use an outer attribute
167170
|
168-
LL | #[repr()]
171+
LL - #![repr()]
172+
LL + #[repr()]
169173
|
170174

171175
error: `path` attribute cannot be used at crate level
@@ -176,7 +180,8 @@ LL | #![path = "3800"]
176180
|
177181
help: perhaps you meant to use an outer attribute
178182
|
179-
LL | #[path = "3800"]
183+
LL - #![path = "3800"]
184+
LL + #[path = "3800"]
180185
|
181186

182187
error: `automatically_derived` attribute cannot be used at crate level
@@ -187,7 +192,8 @@ LL | #![automatically_derived]
187192
|
188193
help: perhaps you meant to use an outer attribute
189194
|
190-
LL | #[automatically_derived]
195+
LL - #![automatically_derived]
196+
LL + #[automatically_derived]
191197
|
192198

193199
error[E0518]: attribute should be applied to function or closure

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ LL | #![test = "4200"]
1414
|
1515
help: perhaps you meant to use an outer attribute
1616
|
17-
LL | #[test = "4200"]
17+
LL - #![test = "4200"]
18+
LL + #[test = "4200"]
1819
|
1920

2021
error: aborting due to 2 previous errors

tests/ui/imports/issue-28134.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ LL | #![test]
1414
|
1515
help: perhaps you meant to use an outer attribute
1616
|
17-
LL | #[test]
18-
| ~~~~~~~
17+
LL - #![test]
18+
LL + #[test]
19+
|
1920

2021
error: aborting due to 2 previous errors
2122

tests/ui/span/issue-43927-non-ADT-derive.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
1414
|
1515
help: perhaps you meant to use an outer attribute
1616
|
17-
LL | #[derive(Debug, PartialEq, Eq)] // should be an outer attribute!
18-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
LL - #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
18+
LL + #[derive(Debug, PartialEq, Eq)] // should be an outer attribute!
19+
|
1920

2021
error: aborting due to 2 previous errors
2122

0 commit comments

Comments
 (0)