Skip to content

Commit 6aaf96e

Browse files
committed
Migrate InvalidAttrAtCrateLevel
1 parent 3719b08 commit 6aaf96e

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,6 @@ passes_unknown_lang_item = definition of an unknown language item: `{$name}`
284284
.label = definition of unknown language item `{$name}`
285285
286286
passes_local_duplicate_lang_item = found duplicate lang item `{$name}`
287+
288+
passes_invalid_attr_at_crate_level = `{$name}` attribute cannot be used at crate level
289+
.suggestion = perhaps you meant to use an outer attribute

compiler/rustc_passes/src/check_attr.rs

+5-20
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};
@@ -2156,25 +2156,10 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
21562156
if attr.style == AttrStyle::Inner {
21572157
for attr_to_check in ATTRS_TO_CHECK {
21582158
if attr.has_name(*attr_to_check) {
2159-
let mut err = tcx.sess.struct_span_err(
2160-
attr.span,
2161-
&format!(
2162-
"`{}` attribute cannot be used at crate level",
2163-
attr_to_check.to_ident_string()
2164-
),
2165-
);
2166-
// Only emit an error with a suggestion if we can create a
2167-
// string out of the attribute span
2168-
if let Ok(src) = tcx.sess.source_map().span_to_snippet(attr.span) {
2169-
let replacement = src.replace("#!", "#");
2170-
err.span_suggestion_verbose(
2171-
attr.span,
2172-
"perhaps you meant to use an outer attribute",
2173-
replacement,
2174-
rustc_errors::Applicability::MachineApplicable,
2175-
);
2176-
}
2177-
err.emit();
2159+
tcx.sess.emit_err(InvalidAttrAtCrateLevel {
2160+
span: attr.span,
2161+
name: *attr_to_check,
2162+
});
21782163
}
21792164
}
21802165
}

compiler/rustc_passes/src/errors.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::{io::Error, path::Path};
22

3-
use rustc_errors::{Applicability, MultiSpan};
3+
use rustc_errors::{Applicability, MultiSpan, ErrorGuaranteed};
44
use rustc_hir::Target;
55
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
6+
use rustc_session::SessionDiagnostic;
67
use rustc_span::{Span, Symbol};
78

89
#[derive(LintDiagnostic)]
@@ -698,3 +699,28 @@ pub struct UnknownLangItem {
698699
pub span: Span,
699700
pub name: Symbol,
700701
}
702+
703+
pub struct InvalidAttrAtCrateLevel {
704+
pub span: Span,
705+
pub name: Symbol,
706+
}
707+
708+
impl SessionDiagnostic<'_> for InvalidAttrAtCrateLevel {
709+
fn into_diagnostic(self, sess: &'_ rustc_session::parse::ParseSess) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
710+
let mut diag = sess.struct_err(rustc_errors::fluent::passes::invalid_attr_at_crate_level);
711+
diag.set_span(self.span);
712+
diag.set_arg("name", self.name);
713+
// Only emit an error with a suggestion if we can create a string out
714+
// of the attribute span
715+
if let Ok(src) = sess.source_map().span_to_snippet(self.span) {
716+
let replacement = src.replace("#!", "#");
717+
diag.span_suggestion_verbose(
718+
self.span,
719+
rustc_errors::fluent::passes::suggestion,
720+
replacement,
721+
rustc_errors::Applicability::MachineApplicable,
722+
);
723+
}
724+
diag
725+
}
726+
}

0 commit comments

Comments
 (0)