Skip to content

Commit

Permalink
Migrate InvalidAttrAtCrateLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdev2 committed Sep 2, 2022
1 parent 3719b08 commit 6aaf96e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/passes.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,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
25 changes: 5 additions & 20 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -2156,25 +2156,10 @@ 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,
name: *attr_to_check,
});
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{io::Error, path::Path};

use rustc_errors::{Applicability, MultiSpan};
use rustc_errors::{Applicability, MultiSpan, ErrorGuaranteed};
use rustc_hir::Target;
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_session::SessionDiagnostic;
use rustc_span::{Span, Symbol};

#[derive(LintDiagnostic)]
Expand Down Expand Up @@ -698,3 +699,28 @@ pub struct UnknownLangItem {
pub span: Span,
pub name: Symbol,
}

pub struct InvalidAttrAtCrateLevel {
pub span: Span,
pub name: Symbol,
}

impl SessionDiagnostic<'_> for InvalidAttrAtCrateLevel {
fn into_diagnostic(self, sess: &'_ rustc_session::parse::ParseSess) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.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 Ok(src) = sess.source_map().span_to_snippet(self.span) {
let replacement = src.replace("#!", "#");
diag.span_suggestion_verbose(
self.span,
rustc_errors::fluent::passes::suggestion,
replacement,
rustc_errors::Applicability::MachineApplicable,
);
}
diag
}
}

0 comments on commit 6aaf96e

Please sign in to comment.