Skip to content

Commit d51a8c0

Browse files
committed
Use attribute name in message for "outer attr used as inner attr" errors
1 parent 6d6a08c commit d51a8c0

File tree

7 files changed

+87
-67
lines changed

7 files changed

+87
-67
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ passes_object_lifetime_err =
462462
{$repr}
463463
464464
passes_outer_crate_level_attr =
465-
crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
465+
crate-level attribute should be an inner attribute: add an exclamation mark: `#![{$attr_name}]`
466466
467467
468468
passes_panic_unwind_without_std =

compiler/rustc_passes/src/check_attr.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
371371
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name))
372372
{
373373
match style {
374-
Some(ast::AttrStyle::Outer) => self.tcx.emit_node_span_lint(
375-
UNUSED_ATTRIBUTES,
376-
hir_id,
377-
attr.span(),
378-
errors::OuterCrateLevelAttr,
379-
),
374+
Some(ast::AttrStyle::Outer) => {
375+
let attr_name = attr
376+
.ident()
377+
.as_ref()
378+
.map(|ident| ident.as_str())
379+
.unwrap_or_else(|| "foo")
380+
.to_string();
381+
382+
self.tcx.emit_node_span_lint(
383+
UNUSED_ATTRIBUTES,
384+
hir_id,
385+
attr.span(),
386+
errors::OuterCrateLevelAttr { attr_name },
387+
)
388+
}
380389
Some(ast::AttrStyle::Inner) | None => self.tcx.emit_node_span_lint(
381390
UNUSED_ATTRIBUTES,
382391
hir_id,
@@ -1817,12 +1826,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18171826
{
18181827
if hir_id != CRATE_HIR_ID {
18191828
match style {
1820-
Some(ast::AttrStyle::Outer) => self.tcx.emit_node_span_lint(
1821-
UNUSED_ATTRIBUTES,
1822-
hir_id,
1823-
attr.span(),
1824-
errors::OuterCrateLevelAttr,
1825-
),
1829+
Some(ast::AttrStyle::Outer) => {
1830+
let attr_name = attr
1831+
.ident()
1832+
.as_ref()
1833+
.map(|ident| ident.as_str())
1834+
.unwrap_or_else(|| "foo")
1835+
.to_string();
1836+
1837+
self.tcx.emit_node_span_lint(
1838+
UNUSED_ATTRIBUTES,
1839+
hir_id,
1840+
attr.span(),
1841+
errors::OuterCrateLevelAttr { attr_name },
1842+
)
1843+
}
18261844
Some(ast::AttrStyle::Inner) | None => self.tcx.emit_node_span_lint(
18271845
UNUSED_ATTRIBUTES,
18281846
hir_id,

compiler/rustc_passes/src/errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ pub(crate) struct MixedExportNameAndNoMangle {
6464

6565
#[derive(LintDiagnostic)]
6666
#[diag(passes_outer_crate_level_attr)]
67-
pub(crate) struct OuterCrateLevelAttr;
67+
pub(crate) struct OuterCrateLevelAttr {
68+
pub(crate) attr_name: String,
69+
}
6870

6971
#[derive(LintDiagnostic)]
7072
#[diag(passes_inner_crate_level_attr)]

0 commit comments

Comments
 (0)