Skip to content

Proc-macro helper attributes on lifetime and const parameters fail to resolve if a built-in derive macro is present #132561

Closed
@PonasKovas

Description

@PonasKovas

I was making a derive proc macro and stumbled upon this weird error, and was told on the rust discord that this might be a bug. Basically, I have a proc macro that uses a helper attribute like this:

#[proc_macro_derive(Test, attributes(test_helper))]
pub fn derive(input: TokenStream) -> TokenStream {
    ...
}

The #[test_helper] attribute is used on generics - including lifetime generics.

When I use it normally it works fine, but if I try to derive it together with another trait, such as Clone - it fails to compile, saying that #[test_helper] was not found in the scope.

#[derive(Test, Clone)]
struct MyStruct<#[test_helper] 'a> {
    inner: &'a (),
}
error: cannot find attribute `test_helper` in this scope
 --> src/lib.rs:3:19
  |
3 | struct MyStruct<#[test_helper] 'a> {
  |                   ^^^^^^^^^^^

There is no error if I put the attribute on a type generic or on a field.

cargo expand-ed code:

struct MyStruct<#[test_helper] 'a> {
    inner: &'a (),
}
#[automatically_derived]
impl<#[test_helper] 'a> ::core::clone::Clone for MyStruct<'a> {
    #[inline]
    fn clone(&self) -> MyStruct<'a> {
        MyStruct {
            inner: ::core::clone::Clone::clone(&self.inner),
        }
    }
}

I have also made a minimal reproducible example.

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (b3f75cc87 2024-11-02)
binary: rustc
commit-hash: b3f75cc872cfd306860c3ad76a239e719015f855
commit-date: 2024-11-02
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.3

If this is really a bug, I can try to investigate if anyone can help me and point me in the right direction.

Metadata

Metadata

Assignees

Labels

A-attributesArea: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions