Skip to content

Commit

Permalink
Tweak suggestion span
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 18, 2023
1 parent 93e62a2 commit 56bb470
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 30 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_errors::{
use rustc_hir::{self as hir, ExprKind, Target};
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::{MainDefinition, Ty};
use rustc_span::{Span, Symbol, DUMMY_SP};
use rustc_span::{BytePos, Span, Symbol, DUMMY_SP};

use crate::check_attr::ProcMacroKind;
use crate::lang_items::Duplicate;
Expand Down Expand Up @@ -864,12 +864,11 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
diag.set_arg("name", self.name);
// Only emit an error with a suggestion if we can create a string out
// of the attribute span
if let Some(src) = self.snippet {
let replacement = src.replace("#!", "#");
if let Some(src) = self.snippet && src.starts_with("#![") {
diag.span_suggestion_verbose(
self.span,
self.span.with_lo(self.span.lo() + BytePos(1)).with_hi(self.span.lo() + BytePos(2)),
fluent::passes_suggestion,
replacement,
String::new(),
rustc_errors::Applicability::MachineApplicable,
);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/attributes/unix_sigpipe/unix_sigpipe-crate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ LL | #![unix_sigpipe = "inherit"]
|
help: perhaps you meant to use an outer attribute
|
LL | #[unix_sigpipe = "inherit"]
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL - #![unix_sigpipe = "inherit"]
LL + #[unix_sigpipe = "inherit"]
|

error: aborting due to previous error

25 changes: 15 additions & 10 deletions tests/ui/derives/issue-36617.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ LL | #![derive(Copy)]
|
help: perhaps you meant to use an outer attribute
|
LL | #[derive(Copy)]
| ~~~~~~~~~~~~~~~
LL - #![derive(Copy)]
LL + #[derive(Copy)]
|

error: `test` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:4:1
Expand All @@ -57,8 +58,9 @@ LL | #![test]
|
help: perhaps you meant to use an outer attribute
|
LL | #[test]
| ~~~~~~~
LL - #![test]
LL + #[test]
|

error: `test_case` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:7:1
Expand All @@ -68,8 +70,9 @@ LL | #![test_case]
|
help: perhaps you meant to use an outer attribute
|
LL | #[test_case]
| ~~~~~~~~~~~~
LL - #![test_case]
LL + #[test_case]
|

error: `bench` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:10:1
Expand All @@ -79,8 +82,9 @@ LL | #![bench]
|
help: perhaps you meant to use an outer attribute
|
LL | #[bench]
| ~~~~~~~~
LL - #![bench]
LL + #[bench]
|

error: `global_allocator` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:13:1
Expand All @@ -90,8 +94,9 @@ LL | #![global_allocator]
|
help: perhaps you meant to use an outer attribute
|
LL | #[global_allocator]
| ~~~~~~~~~~~~~~~~~~~
LL - #![global_allocator]
LL + #[global_allocator]
|

error: aborting due to 10 previous errors

3 changes: 2 additions & 1 deletion tests/ui/feature-gates/issue-43106-gating-of-bench.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ LL | #![bench = "4100"]
|
help: perhaps you meant to use an outer attribute
|
LL | #[bench = "4100"]
LL - #![bench = "4100"]
LL + #[bench = "4100"]
|

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ LL | #![macro_export]
|
help: perhaps you meant to use an outer attribute
|
LL | #[macro_export]
LL - #![macro_export]
LL + #[macro_export]
|

error: `rustc_main` attribute cannot be used at crate level
Expand All @@ -143,8 +144,9 @@ LL | #![rustc_main]
|
help: perhaps you meant to use an outer attribute
|
LL | #[rustc_main]
| ~~~~~~~~~~~~~
LL - #![rustc_main]
LL + #[rustc_main]
|

error: `start` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:16:1
Expand All @@ -154,7 +156,8 @@ LL | #![start]
|
help: perhaps you meant to use an outer attribute
|
LL | #[start]
LL - #![start]
LL + #[start]
|

error: `repr` attribute cannot be used at crate level
Expand All @@ -165,7 +168,8 @@ LL | #![repr()]
|
help: perhaps you meant to use an outer attribute
|
LL | #[repr()]
LL - #![repr()]
LL + #[repr()]
|

error: `path` attribute cannot be used at crate level
Expand All @@ -176,7 +180,8 @@ LL | #![path = "3800"]
|
help: perhaps you meant to use an outer attribute
|
LL | #[path = "3800"]
LL - #![path = "3800"]
LL + #[path = "3800"]
|

error: `automatically_derived` attribute cannot be used at crate level
Expand All @@ -187,7 +192,8 @@ LL | #![automatically_derived]
|
help: perhaps you meant to use an outer attribute
|
LL | #[automatically_derived]
LL - #![automatically_derived]
LL + #[automatically_derived]
|

error[E0518]: attribute should be applied to function or closure
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/feature-gates/issue-43106-gating-of-test.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ LL | #![test = "4200"]
|
help: perhaps you meant to use an outer attribute
|
LL | #[test = "4200"]
LL - #![test = "4200"]
LL + #[test = "4200"]
|

error: aborting due to 2 previous errors
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/imports/issue-28134.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ LL | #![test]
|
help: perhaps you meant to use an outer attribute
|
LL | #[test]
| ~~~~~~~
LL - #![test]
LL + #[test]
|

error: aborting due to 2 previous errors

5 changes: 3 additions & 2 deletions tests/ui/span/issue-43927-non-ADT-derive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
|
help: perhaps you meant to use an outer attribute
|
LL | #[derive(Debug, PartialEq, Eq)] // should be an outer attribute!
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL - #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
LL + #[derive(Debug, PartialEq, Eq)] // should be an outer attribute!
|

error: aborting due to 2 previous errors

0 comments on commit 56bb470

Please sign in to comment.