Skip to content

Commit 2c3351c

Browse files
naritanaraCleanCut
andcommitted
Migrate InvalidAttrAtCrateLevel
Co-authored-by: Nathan Stocks <[email protected]> Co-authored-by: rdvdev2 <[email protected]>
1 parent 0315d7c commit 2c3351c

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,6 @@ passes_unknown_lang_item = definition of an unknown language item: `{$name}`
290290
.label = definition of unknown language item `{$name}`
291291
292292
passes_local_duplicate_lang_item = found duplicate lang item `{$name}`
293+
294+
passes_invalid_attr_at_crate_level = `{$name}` attribute cannot be used at crate level
295+
.suggestion = perhaps you meant to use an outer attribute

compiler/rustc_passes/src/check_attr.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! conflicts between multiple such attributes attached to the same
55
//! item.
66
7-
use crate::errors::{self, DebugVisualizerUnreadable};
7+
use crate::errors::{self, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel};
88
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
99
use rustc_data_structures::fx::FxHashMap;
1010
use rustc_errors::{fluent, struct_span_err, Applicability, MultiSpan};
@@ -1867,7 +1867,7 @@ impl CheckAttrVisitor<'_> {
18671867
span: meta_item.span,
18681868
file: &file,
18691869
error: err,
1870-
} );
1870+
});
18711871
false
18721872
}
18731873
}
@@ -2178,25 +2178,11 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
21782178
if attr.style == AttrStyle::Inner {
21792179
for attr_to_check in ATTRS_TO_CHECK {
21802180
if attr.has_name(*attr_to_check) {
2181-
let mut err = tcx.sess.struct_span_err(
2182-
attr.span,
2183-
&format!(
2184-
"`{}` attribute cannot be used at crate level",
2185-
attr_to_check.to_ident_string()
2186-
),
2187-
);
2188-
// Only emit an error with a suggestion if we can create a
2189-
// string out of the attribute span
2190-
if let Ok(src) = tcx.sess.source_map().span_to_snippet(attr.span) {
2191-
let replacement = src.replace("#!", "#");
2192-
err.span_suggestion_verbose(
2193-
attr.span,
2194-
"perhaps you meant to use an outer attribute",
2195-
replacement,
2196-
rustc_errors::Applicability::MachineApplicable,
2197-
);
2198-
}
2199-
err.emit();
2181+
tcx.sess.emit_err(InvalidAttrAtCrateLevel {
2182+
span: attr.span,
2183+
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
2184+
name: *attr_to_check,
2185+
});
22002186
}
22012187
}
22022188
}

compiler/rustc_passes/src/errors.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{io::Error, path::Path};
22

3-
use rustc_errors::{Applicability, MultiSpan};
3+
use rustc_errors::{Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
44
use rustc_hir::Target;
55
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
66
use rustc_span::{Span, Symbol};
@@ -714,3 +714,33 @@ pub struct UnknownLangItem {
714714
pub span: Span,
715715
pub name: Symbol,
716716
}
717+
718+
pub struct InvalidAttrAtCrateLevel {
719+
pub span: Span,
720+
pub snippet: Option<String>,
721+
pub name: Symbol,
722+
}
723+
724+
impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
725+
fn into_diagnostic(
726+
self,
727+
handler: &'_ rustc_errors::Handler,
728+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
729+
let mut diag =
730+
handler.struct_err(rustc_errors::fluent::passes::invalid_attr_at_crate_level);
731+
diag.set_span(self.span);
732+
diag.set_arg("name", self.name);
733+
// Only emit an error with a suggestion if we can create a string out
734+
// of the attribute span
735+
if let Some(src) = self.snippet {
736+
let replacement = src.replace("#!", "#");
737+
diag.span_suggestion_verbose(
738+
self.span,
739+
rustc_errors::fluent::passes::suggestion,
740+
replacement,
741+
rustc_errors::Applicability::MachineApplicable,
742+
);
743+
}
744+
diag
745+
}
746+
}

compiler/rustc_passes/src/lang_items.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
88
//! * Functions called by the compiler itself.
99
10-
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
1110
use crate::check_attr::target_from_impl_item;
11+
use crate::errors::{LangItemOnIncorrectTarget, UnknownLangItem};
1212
use crate::weak_lang_items;
1313

1414
use rustc_errors::{pluralize, struct_span_err};
@@ -52,10 +52,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
5252
}
5353
// Unknown lang item.
5454
_ => {
55-
self.tcx.sess.emit_err(UnknownLangItem {
56-
span,
57-
name: value,
58-
});
55+
self.tcx.sess.emit_err(UnknownLangItem { span, name: value });
5956
}
6057
}
6158
}

0 commit comments

Comments
 (0)