Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix attribute printing in an error.
The current code assumes that the attribute is just an identifier, and
so misprints paths.
  • Loading branch information
nnethercote committed Apr 16, 2025
commit 400e8e5dc819b1fa7f994fb60dbe8b5112ec3fd2
5 changes: 4 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
allowed_target: Target,
) {
if target != allowed_target {
let path = attr.path();
let path: Vec<_> = path.iter().map(|s| s.as_str()).collect();
let attr_name = path.join("::");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this happens a lot throughout the compiler it's a bit ugly to keep iterating and joining these. In HIR I think I had a solution for this, with hir::AttributePath which implements Display. I'll add this to my own TODO list, to make this nicer at the same time when AttributeExt is removed :)

self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr.span(),
errors::OnlyHasEffectOn {
attr_name: attr.name_or_empty(),
attr_name,
target_name: allowed_target.name().replace(' ', "_"),
},
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ pub(crate) struct UselessAssignment<'a> {
#[derive(LintDiagnostic)]
#[diag(passes_only_has_effect_on)]
pub(crate) struct OnlyHasEffectOn {
pub attr_name: Symbol,
pub attr_name: String,
pub target_name: String,
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/attributes/check-builtin-attr-ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
struct Foo {
#[should_panic::skip]
//~^ ERROR failed to resolve
//~| ERROR `#[]` only has an effect on functions
//~| ERROR `#[should_panic::skip]` only has an effect on functions
pub field: u8,

#[should_panic::a::b::c]
//~^ ERROR failed to resolve
//~| ERROR `#[]` only has an effect on functions
//~| ERROR `#[should_panic::a::b::c]` only has an effect on functions
pub field2: u8,
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/attributes/check-builtin-attr-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `den
LL | #[deny::skip]
| ^^^^ use of unresolved module or unlinked crate `deny`

error: `#[]` only has an effect on functions
error: `#[should_panic::skip]` only has an effect on functions
--> $DIR/check-builtin-attr-ice.rs:45:5
|
LL | #[should_panic::skip]
Expand All @@ -28,7 +28,7 @@ note: the lint level is defined here
LL | #![deny(unused_attributes)]
| ^^^^^^^^^^^^^^^^^

error: `#[]` only has an effect on functions
error: `#[should_panic::a::b::c]` only has an effect on functions
--> $DIR/check-builtin-attr-ice.rs:50:5
|
LL | #[should_panic::a::b::c]
Expand Down