Skip to content

feat(v2): Compile-time required constraints for AnchorAccount#4503

Open
chop0 wants to merge 2 commits into
otter-sec:anchor-nextfrom
chop0:custom-account-constraints
Open

feat(v2): Compile-time required constraints for AnchorAccount#4503
chop0 wants to merge 2 commits into
otter-sec:anchor-nextfrom
chop0:custom-account-constraints

Conversation

@chop0
Copy link
Copy Markdown

@chop0 chop0 commented May 2, 2026

Custom AnchorAccount types can now declare a type-level list of AccountConstraint markers that every #[account(...)] usage must apply. The derive macro emits a const-block superset assertion per field; missing required constraints fail to compile.

  • traits.rs: ConstraintList, Find<C, Idx> (frunk Here/There index trick), IsSuperset<R, I>, plus required_constraints![] helper macro.
  • AnchorAccount gains type RequiredConstraints: ConstraintList. Built-in wrappers default to (); Box forwards from inner T.
  • derive: per-field assertion built from attrs.namespaced and spliced beside the impl TryAccounts block.
  • tests-v2/programs/custom-constraints: TestAccount (1 required) and TestMultiAccount (2 required) wrappers + handlers covering single, multi (reverse-order), Box forwarding, update(...) satisfaction.
  • tests-v2/tests/ui_required_constraints: trybuild compile-fail snapshots for missing and partial requirements.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 2, 2026

@chop0 is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@jamie-osec
Copy link
Copy Markdown
Collaborator

Can you fix the compile-fail test issues?

@eteen12
Copy link
Copy Markdown
Contributor

eteen12 commented May 14, 2026

@chop0 id be happy to help with the compile tests if you want, i saw in the discord u were gonna take a look this morning, but if you want me to help out let me know

chop0 added 2 commits May 18, 2026 10:32
Custom AnchorAccount types can now declare a type-level list of
AccountConstraint markers that every #[account(...)] usage must apply.
The derive macro emits a const-block superset assertion per field;
missing required constraints fail to compile.

- traits.rs: ConstraintList, Find<C, Idx> (frunk Here/There index trick),
  IsSuperset<R, I>, plus required_constraints![] helper macro.
- AnchorAccount gains `type RequiredConstraints: ConstraintList`.
  Built-in wrappers default to (); Box<T> forwards from inner T.
- derive: per-field assertion built from attrs.namespaced and spliced
  beside the impl TryAccounts block.
- tests-v2/programs/custom-constraints: TestAccount<T> (1 required) and
  TestMultiAccount<T> (2 required) wrappers + handlers covering single,
  multi (reverse-order), Box<TestAccount> forwarding, update(...)
  satisfaction.
- tests-v2/tests/ui_required_constraints: trybuild compile-fail snapshots
  for missing and partial requirements.
…doctests

trybuild compares the full stderr text including rustc-version-specific
trait-suggestion formatting and the surrounding `unexpected_cfg` warning.
The snapshots captured locally don't match CI's rustc, causing the
ui_required_constraints test to fail.

Replace with `compile_fail` doctests on `IsSuperset` in lang-v2/src/traits.rs.
These verify the trait machinery rejects missing-required and partial-coverage
provided lists without comparing stderr text — stable across rustc versions.
The positive-case verification continues to be the custom-constraints test
crate's handler structs (HandleRequired, HandleRequiredMulti, etc.) compiling.

- Remove tests-v2/tests/ui_required_constraints/ and the test driver.
- Remove trybuild from tests-v2 dev-dependencies.
- Add 3 doctests on IsSuperset: one positive (superset works) and two
  compile_fail (missing single, missing one of two).
@chop0 chop0 force-pushed the custom-account-constraints branch from b2af1d6 to 58a4fa2 Compare May 18, 2026 17:52
@jamie-osec jamie-osec closed this May 18, 2026
@jamie-osec jamie-osec reopened this May 18, 2026
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.

3 participants