1
Fork 0

Migrate InvalidAttrAtCrateLevel

Co-authored-by: Nathan Stocks <cleancut@github.com>
Co-authored-by: rdvdev2 <rdvdev2@gmail.com>
This commit is contained in:
rdvdev2 2022-09-02 05:19:03 +02:00 committed by Nathan Stocks
parent 0315d7c9db
commit 2c3351c9a6
4 changed files with 43 additions and 27 deletions

View file

@ -290,3 +290,6 @@ passes_unknown_lang_item = definition of an unknown language item: `{$name}`
.label = definition of unknown language item `{$name}`
passes_local_duplicate_lang_item = found duplicate lang item `{$name}`
passes_invalid_attr_at_crate_level = `{$name}` attribute cannot be used at crate level
.suggestion = perhaps you meant to use an outer attribute

View file

@ -4,7 +4,7 @@
//! conflicts between multiple such attributes attached to the same
//! item.
use crate::errors::{self, DebugVisualizerUnreadable};
use crate::errors::{self, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel};
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{fluent, struct_span_err, Applicability, MultiSpan};
@ -1867,7 +1867,7 @@ impl CheckAttrVisitor<'_> {
span: meta_item.span,
file: &file,
error: err,
} );
});
false
}
}
@ -2178,25 +2178,11 @@ 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 mut err = tcx.sess.struct_span_err(
attr.span,
&format!(
"`{}` attribute cannot be used at crate level",
attr_to_check.to_ident_string()
),
);
// Only emit an error with a suggestion if we can create a
// string out of the attribute span
if let Ok(src) = tcx.sess.source_map().span_to_snippet(attr.span) {
let replacement = src.replace("#!", "#");
err.span_suggestion_verbose(
attr.span,
"perhaps you meant to use an outer attribute",
replacement,
rustc_errors::Applicability::MachineApplicable,
);
}
err.emit();
tcx.sess.emit_err(InvalidAttrAtCrateLevel {
span: attr.span,
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
name: *attr_to_check,
});
}
}
}

View file

@ -1,6 +1,6 @@
use std::{io::Error, path::Path};
use rustc_errors::{Applicability, MultiSpan};
use rustc_errors::{Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
use rustc_hir::Target;
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_span::{Span, Symbol};
@ -714,3 +714,33 @@ pub struct UnknownLangItem {
pub span: Span,
pub name: Symbol,
}
pub struct InvalidAttrAtCrateLevel {
pub span: Span,
pub snippet: Option<String>,
pub name: Symbol,
}
impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
fn into_diagnostic(
self,
handler: &'_ rustc_errors::Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag =
handler.struct_err(rustc_errors::fluent::passes::invalid_attr_at_crate_level);
diag.set_span(self.span);
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("#!", "#");
diag.span_suggestion_verbose(
self.span,
rustc_errors::fluent::passes::suggestion,
replacement,
rustc_errors::Applicability::MachineApplicable,
);
}
diag
}
}

View file

@ -7,8 +7,8 @@
//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
//! * Functions called by the compiler itself.
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
use crate::check_attr::target_from_impl_item;
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
use crate::weak_lang_items;
use rustc_errors::{pluralize, struct_span_err};
@ -52,10 +52,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
}
// Unknown lang item.
_ => {
self.tcx.sess.emit_err(UnknownLangItem {
span,
name: value,
});
self.tcx.sess.emit_err(UnknownLangItem { span, name: value });
}
}
}