Skip to content

fix: handle PhantomData in enum schema derive#369

Merged
r-near merged 4 commits into
near:masterfrom
sueun-dev:fix-phantom-data-schema-enum
Jun 3, 2026
Merged

fix: handle PhantomData in enum schema derive#369
r-near merged 4 commits into
near:masterfrom
sueun-dev:fix-phantom-data-schema-enum

Conversation

@sueun-dev

Copy link
Copy Markdown
Contributor

Fixes #329.

This fixes #[derive(BorshSchema)] for enum variants that contain PhantomData<T>.

What changed:

  • added a schema-only mode for generic parameter detection that can include PhantomData<T> when enum variant schema params are computed
  • kept the existing PhantomData<T> skip behavior for the other derive paths
  • added a regression test for enum Parametrized<T> { Item(PhantomData<T>) } using a marker type that does not implement BorshSchema

Why:
The struct case already works, but the enum variant case from #329 expands into code that tries to use the outer generic parameter from a generated inner item. I checked this with a throwaway crate: clean origin/master fails with E0401, while the patched local Borsh passes and can call MyEnum::<NotSchema>::add_definitions_recursively.

Checked:

  • cargo test -p borsh --features unstable__schema schema::test_phantom_data
  • cargo test -p borsh --features unstable__schema schema::test_generic_enums
  • cargo test -p borsh --features unstable__schema
  • cargo test -p borsh-derive --features schema
  • cargo fmt --check
  • cargo clippy -p borsh-derive --features schema -- -D warnings
  • cargo clippy -p borsh --features unstable__schema -- -D warnings

@sueun-dev sueun-dev requested a review from a team as a code owner June 2, 2026 18:22

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 95b9a6c85b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread borsh-derive/src/internals/schema/mod.rs
@sueun-dev sueun-dev force-pushed the fix-phantom-data-schema-enum branch from 95b9a6c to d4f0eb5 Compare June 2, 2026 18:25
@sueun-dev

Copy link
Copy Markdown
Contributor Author

Small process note: I force-pushed once to fold in a CI-only fix before review started. Sorry about that. I will avoid rewriting this PR branch from here and use normal follow-up commits for any future changes.

@r-near r-near left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks mostly fine, just need to handle that Codex comment. Also could you add the mixed param case as a regression test?

@r-near

r-near commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Confirmed fixed on the new commits. tried my repro again plus a few adversarial variants and they all compile now.

Nice that the rewrite also catches params referenced in the predicate's bounds, not just the bounded type. LGTM. :)

@r-near r-near enabled auto-merge (squash) June 3, 2026 19:02
@r-near r-near merged commit 1133023 into near:master Jun 3, 2026
6 checks passed
@sueun-dev sueun-dev deleted the fix-phantom-data-schema-enum branch June 3, 2026 19:08
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.

#[derive(BorshSchema)] chokes on PhantomData in enums

2 participants