Skip to content

decl_macro incremental compilation bug: missing field #112680

@dawnofmidnight

Description

@dawnofmidnight
Contributor

Note: This may be a duplicate, but I couldn't find one, so I'm going ahead and reporting it.

When using decl_macro, I'm encountering a strange incremental compilation bug. I have a struct, declared as follows:

use a::entity_impl;
entity_impl!(TestId, Test); // entity_impl! is the `decl_macro`

struct Test(u32);
// now uncomment the following, and comment the above
// struct Test;

To reproduce this, do the following:

  1. Run cargo clean, then cargo check.
  2. Comment line 3, and uncomment line 5.
  3. Run cargo check again. The error below is emitted.
  4. If cargo clean && cargo check is run again with either version, it compiles fine.

I don't understand why this would happen, but I do have the code, all of which can be found in this repository. a/src/lib.rs has the entity_impl macros 2.0 macro, while b/src/lib.rs has the struct & macro invocation shown above. I've minimized the example as best I can, but a normal (non-decl_macro) does not work, and I can't seem to reproduce in a single crate. If there's anything else needed, or anything I should try, please let me know.

Error message:

error[E0609]: no field `inner` on type `TestId`
 --> b/src/lib.rs:2:1
  |
2 | entity_impl!(TestId, Test);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: a field with a similar name exists: `inner`
  |
  = note: this error originates in the macro `entity_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0609`.
error: could not compile `b` (lib) due to previous error

Meta

rustc --version --verbose:

rustc 1.72.0-nightly (8c74a5d27 2023-06-14)
binary: rustc
commit-hash: 8c74a5d27c644a0f7a22bb2fa8dd3ff8257bc220
commit-date: 2023-06-14
host: x86_64-unknown-linux-gnu // also reproduces on x86_64-pc-windows-msvc
release: 1.72.0-nightly
LLVM version: 16.0.5

@rustbot label +A-incr-comp +A-macros-2.0
(I'm just guessing on these labels.)

Activity

added
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)
A-incr-compArea: Incremental compilation
S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
on Jun 16, 2023
Noratrieb

Noratrieb commented on Jun 16, 2023

@Noratrieb
Member

full code
a

#![feature(decl_macro)]

pub trait Entity {
    type Key;
    fn index_from_key(key: Self::Key) -> usize;
}

pub macro entity_impl($key_ty:ident, $val_ty:ident) {
    struct $key_ty {
        inner: usize,
    }

    impl ::a::Entity for $val_ty {
        type Key = $key_ty;

        fn index_from_key(key: Self::Key) -> usize {
            key.inner
        }
    }
}

// does not reproduce with this uncommented, and the macro above commented
// #[macro_export]
// macro_rules! entity_impl {
//     ($key_ty:ident, $val_ty:ident) => {
//         struct $key_ty {
//             inner: usize,
//         }

//         impl ::a::Entity for $val_ty {
//             type Key = $key_ty;

//             fn index_from_key(key: Self::Key) -> usize {
//                 key.inner
//             }
//         }
//     }
// }

b

use a::entity_impl;
entity_impl!(TestId, Test);

struct Test(u32);
// now uncomment the following, and comment the above
// struct Test;
added a commit that references this issue on Aug 21, 2024
221b53c
added a commit that references this issue on Aug 22, 2024
35d6344
added a commit that references this issue on Aug 22, 2024
ff4d021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)A-incr-compArea: Incremental compilationA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.F-decl_macro`#![feature(decl_macro)]`S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jyn514@Noratrieb@dawnofmidnight

      Issue actions

        decl_macro incremental compilation bug: missing field · Issue #112680 · rust-lang/rust