Migrate InvalidAttrAtCrateLevel
Co-authored-by: Nathan Stocks <cleancut@github.com> Co-authored-by: rdvdev2 <rdvdev2@gmail.com>
This commit is contained in:
parent
0315d7c9db
commit
2c3351c9a6
4 changed files with 43 additions and 27 deletions
|
@ -290,3 +290,6 @@ passes_unknown_lang_item = definition of an unknown language item: `{$name}`
|
||||||
.label = definition of unknown language item `{$name}`
|
.label = definition of unknown language item `{$name}`
|
||||||
|
|
||||||
passes_local_duplicate_lang_item = found duplicate lang 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
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
//! conflicts between multiple such attributes attached to the same
|
//! conflicts between multiple such attributes attached to the same
|
||||||
//! item.
|
//! item.
|
||||||
|
|
||||||
use crate::errors::{self, DebugVisualizerUnreadable};
|
use crate::errors::{self, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel};
|
||||||
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
|
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_errors::{fluent, struct_span_err, Applicability, MultiSpan};
|
use rustc_errors::{fluent, struct_span_err, Applicability, MultiSpan};
|
||||||
|
@ -1867,7 +1867,7 @@ impl CheckAttrVisitor<'_> {
|
||||||
span: meta_item.span,
|
span: meta_item.span,
|
||||||
file: &file,
|
file: &file,
|
||||||
error: err,
|
error: err,
|
||||||
} );
|
});
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2178,25 +2178,11 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
|
||||||
if attr.style == AttrStyle::Inner {
|
if attr.style == AttrStyle::Inner {
|
||||||
for attr_to_check in ATTRS_TO_CHECK {
|
for attr_to_check in ATTRS_TO_CHECK {
|
||||||
if attr.has_name(*attr_to_check) {
|
if attr.has_name(*attr_to_check) {
|
||||||
let mut err = tcx.sess.struct_span_err(
|
tcx.sess.emit_err(InvalidAttrAtCrateLevel {
|
||||||
attr.span,
|
span: attr.span,
|
||||||
&format!(
|
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
|
||||||
"`{}` attribute cannot be used at crate level",
|
name: *attr_to_check,
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{io::Error, path::Path};
|
use std::{io::Error, path::Path};
|
||||||
|
|
||||||
use rustc_errors::{Applicability, MultiSpan};
|
use rustc_errors::{Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
|
||||||
use rustc_hir::Target;
|
use rustc_hir::Target;
|
||||||
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
|
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
|
||||||
use rustc_span::{Span, Symbol};
|
use rustc_span::{Span, Symbol};
|
||||||
|
@ -714,3 +714,33 @@ pub struct UnknownLangItem {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub name: Symbol,
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
|
//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
|
||||||
//! * Functions called by the compiler itself.
|
//! * Functions called by the compiler itself.
|
||||||
|
|
||||||
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
|
|
||||||
use crate::check_attr::target_from_impl_item;
|
use crate::check_attr::target_from_impl_item;
|
||||||
|
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
|
||||||
use crate::weak_lang_items;
|
use crate::weak_lang_items;
|
||||||
|
|
||||||
use rustc_errors::{pluralize, struct_span_err};
|
use rustc_errors::{pluralize, struct_span_err};
|
||||||
|
@ -52,10 +52,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
|
||||||
}
|
}
|
||||||
// Unknown lang item.
|
// Unknown lang item.
|
||||||
_ => {
|
_ => {
|
||||||
self.tcx.sess.emit_err(UnknownLangItem {
|
self.tcx.sess.emit_err(UnknownLangItem { span, name: value });
|
||||||
span,
|
|
||||||
name: value,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue