Skip to content

Defer field lookup in @adapt_structure to fix nested-macro use.#107

Merged
maleadt merged 1 commit into
masterfrom
tb/adapt_structure_generated
Apr 10, 2026
Merged

Defer field lookup in @adapt_structure to fix nested-macro use.#107
maleadt merged 1 commit into
masterfrom
tb/adapt_structure_generated

Conversation

@maleadt

@maleadt maleadt commented Apr 10, 2026

Copy link
Copy Markdown
Member

The macro previously called Core.eval(__module__, T) at expansion time, which fails when @adapt_structure is invoked inside another macro that wraps its body in a function (e.g. @testset), because the struct definition has not run yet at macroexpand time. Switch to a @generated function so field names are queried at codegen time, when the type is guaranteed to exist. The emitted IR is identical to the original (direct getfield + %new), so there is no performance regression.

Fixes #102

The macro previously called `Core.eval(__module__, T)` at expansion
time, which fails when @adapt_structure is invoked inside another
macro that wraps its body in a function (e.g. @testset), because the
struct definition has not run yet at macroexpand time. Switch to a
@generated function so field names are queried at codegen time, when
the type is guaranteed to exist. The emitted IR is identical to the
original (direct getfield + %new), so there is no performance regression.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@maleadt maleadt merged commit 6292519 into master Apr 10, 2026
21 of 22 checks passed
@maleadt maleadt deleted the tb/adapt_structure_generated branch April 10, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using Adapt within @testset

1 participant