Rollup merge of #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 #89566.
This commit is contained in:
commit
a69fb480a4
11 changed files with 160 additions and 60 deletions
|
@ -2534,10 +2534,30 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
|
|||
if attr.style == AttrStyle::Inner {
|
||||
for attr_to_check in ATTRS_TO_CHECK {
|
||||
if attr.has_name(*attr_to_check) {
|
||||
let item = tcx
|
||||
.hir()
|
||||
.items()
|
||||
.map(|id| tcx.hir().item(id))
|
||||
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
|
||||
.map(|item| errors::ItemFollowingInnerAttr {
|
||||
span: item.ident.span,
|
||||
kind: item.kind.descr(),
|
||||
});
|
||||
tcx.sess.emit_err(errors::InvalidAttrAtCrateLevel {
|
||||
span: attr.span,
|
||||
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
|
||||
sugg_span: tcx
|
||||
.sess
|
||||
.source_map()
|
||||
.span_to_snippet(attr.span)
|
||||
.ok()
|
||||
.filter(|src| src.starts_with("#!["))
|
||||
.map(|_| {
|
||||
attr.span
|
||||
.with_lo(attr.span.lo() + BytePos(1))
|
||||
.with_hi(attr.span.lo() + BytePos(2))
|
||||
}),
|
||||
name: *attr_to_check,
|
||||
item,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -856,8 +856,15 @@ pub struct UnknownLangItem {
|
|||
|
||||
pub struct InvalidAttrAtCrateLevel {
|
||||
pub span: Span,
|
||||
pub snippet: Option<String>,
|
||||
pub sugg_span: Option<Span>,
|
||||
pub name: Symbol,
|
||||
pub item: Option<ItemFollowingInnerAttr>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ItemFollowingInnerAttr {
|
||||
pub span: Span,
|
||||
pub kind: &'static str,
|
||||
}
|
||||
|
||||
impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
|
||||
|
@ -871,15 +878,18 @@ 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(span) = self.sugg_span {
|
||||
diag.span_suggestion_verbose(
|
||||
self.span,
|
||||
span,
|
||||
fluent::passes_suggestion,
|
||||
replacement,
|
||||
String::new(),
|
||||
rustc_errors::Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
if let Some(item) = self.item {
|
||||
diag.set_arg("kind", item.kind);
|
||||
diag.span_label(item.span, fluent::passes_invalid_attr_at_crate_level_item);
|
||||
}
|
||||
diag
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue