Preserve cfg attrs for contracttrait default functions#1869
Conversation
|
Claude encountered an error —— View job PR Review In Progress
|
There was a problem hiding this comment.
Pull request overview
This PR fixes #[contracttrait] / #[contractimpl(contracttrait)] codegen when default trait methods (and overrides) are #[cfg(...)]-gated by preserving pass-through function attributes during macro serialization and by making downstream wrapper/args/test registration generation cfg-aware. It also adds regression coverage for cfg-disabled defaults, cfg-enabled defaults, cfg-gated overrides, and cfg-gated #[contractimpl] functions, including a cross-crate case (issue #1868).
Changes:
- Preserve pass-through function attributes (notably
#[cfg(...)]) when recording default#[contracttrait]methods and when encoding impl override info for later comparison. - Make generated args helpers and native test registration constructors respect cfg gating (avoid referencing cfg-disabled wrapper symbols).
- Add new contract tests + snapshots exercising cfg-disabled defaults, cfg-enabled defaults, cfg-gated overrides, and cross-crate behavior; update expanded-code fixtures accordingly.
Reviewed changes
Copilot reviewed 59 out of 60 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/contracttrait_trait/test_snapshots/test/test_cfg_gated_disabled_override_uses_default_fn.1.json | New snapshot covering cfg-disabled override falling back to default. |
| tests/contracttrait_trait/test_snapshots/test/test_cfg_gated_default_with_unconditional_override.1.json | New snapshot covering unconditional override behavior with cfg-gated defaults. |
| tests/contracttrait_trait/test_snapshots/test/test_cfg_gated_contracttrait_default_fn.1.json | New snapshot for cfg-gated #[contracttrait] default fn behavior. |
| tests/contracttrait_trait/test_snapshots/test/test_cfg_gated_contractimpl_fn.1.json | New snapshot for cfg-gated #[contractimpl] fn behavior. |
| tests/contracttrait_trait/src/lib.rs | Adds cfg-gated contracttrait + multiple impl/test scenarios. |
| tests/contracttrait_trait/Cargo.toml | Adds cfg-gated-fn feature (enabled by default for this test crate). |
| tests/contracttrait_impl_full/test_snapshots/test/test_cross_crate_cfg_gated_default_fn.1.json | New snapshot for cross-crate cfg-gated default fn case. |
| tests/contracttrait_impl_full/src/lib.rs | Imports and uses new CfgGated trait for cross-crate test. |
| tests/contracttrait_impl_full/Cargo.toml | Adds cfg-gated-fn feature for cross-crate test configuration. |
| tests-expanded/test_workspace_contract_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_udt_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_tuples_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_spec_shaking_v2_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_spec_shaking_v1_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_mutability_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_multiimpl_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_modular_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_macros_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_logging_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_invoke_contract_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_import_contract_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_generics_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_fuzz_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_events_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_events_ref_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_errors_wasm32v1-none.rs | Expanded fixture updates reflecting cfg-aware arg generation changes. |
| tests-expanded/test_errors_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_empty2_tests.rs | Expanded fixture updates reflecting removal of empty/no-op registration ctor. |
| tests-expanded/test_empty_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_contracttrait_trait_wasm32v1-none.rs | Expanded fixture updates showing new CfgGated trait/client/args/spec generation. |
| tests-expanded/test_contracttrait_path_super_tests.rs | Expanded fixture updates reflecting removal of empty/no-op registration ctor. |
| tests-expanded/test_contracttrait_path_self_tests.rs | Expanded fixture updates reflecting removal of empty/no-op registration ctor. |
| tests-expanded/test_contracttrait_path_relative_tests.rs | Expanded fixture updates reflecting removal of empty/no-op registration ctor. |
| tests-expanded/test_contracttrait_path_global_wasm32v1-none.rs | Expanded fixture updates reflecting args doc/attr pass-through changes. |
| tests-expanded/test_contracttrait_path_global_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_contracttrait_path_crate_tests.rs | Expanded fixture updates reflecting removal of empty/no-op registration ctor. |
| tests-expanded/test_contracttrait_impl_partial_wasm32v1-none.rs | Expanded fixture updates reflecting args doc/attr pass-through changes. |
| tests-expanded/test_contracttrait_impl_partial_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_contracttrait_impl_full_wasm32v1-none.rs | Expanded fixture updates reflecting new CfgGated usage in full impl case. |
| tests-expanded/test_contract_data_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_constructor_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_bn254_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_bls_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_auth_tests.rs | Expanded fixture updates reflecting pass-through attrs applied to args/ctors. |
| tests-expanded/test_associated_types_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_associated_types_contracttrait_tests.rs | Expanded fixture updates reflecting removal of empty/no-op registration ctor. |
| tests-expanded/test_associated_type_chained_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_add_u64_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_add_u128_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_add_i128_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| tests-expanded/test_account_wasm32v1-none.rs | Expanded fixture updates reflecting pass-through attrs applied to args. |
| tests-expanded/test_account_tests.rs | Expanded fixture updates due to ctor hashing/registration changes. |
| soroban-sdk/docs/contracttrait.md | Documentation updates describing preserved cfg attrs and limitations (cfg_attr). |
| soroban-sdk-macros/src/lib.rs | Updates to pass full fn metadata into registration ctor generation. |
| soroban-sdk-macros/src/derive_fn.rs | Makes native registration ctor generation cfg-aware and per-function. |
| soroban-sdk-macros/src/derive_contractimpl_trait_macro.rs | Serializes default trait fns including pass-through attrs; encodes impl fns with attrs for cfg-aware override matching. |
| soroban-sdk-macros/src/derive_contractimpl_trait_default_fns_not_overridden.rs | Implements cfg-aware default-vs-override selection and cfg-gated default emission. |
| soroban-sdk-macros/src/derive_args.rs | Applies pass-through attrs (incl. #[cfg]) to generated Args methods. |
98712ca to
c9d922d
Compare
|
Claude encountered an error —— View job PR Review
SummaryThe change is well-scoped and the cfg-propagation logic is correct across the three places it needs to take effect ( Pushback on the Copilot inline comment
This is incorrect. Verification of the override-vs-default selection
The Per-fn registration ctorsThe rewrite of The hash includes the fn ident and all pass-through attrs except doc, so two cfg-gated variants of the same fn name within one trait impl produce distinct ctor identifiers. The 64-char hex with leading underscores keeps Minor observations (non-blocking)
ConclusionI have no required changes. The Copilot inline comment on |

What
Preserve pass-through function attributes, including
#[cfg(...)], when#[contracttrait]records default trait functions for later#[contractimpl(contracttrait)]code generation.This also makes generated args and native test registration code cfg-aware, and adds regression coverage for cfg-disabled defaults, cfg-enabled defaults, cfg-gated overrides, and regular
#[contractimpl]cfg-gated functions.Why
#[contracttrait]previously serialized only doc attributes for default trait methods. If a default method was cfg-gated, the generated trait helper lost that cfg and later generated wrappers for a method that Rust had compiled out of the trait.Cfg-gated overrides also need special handling: an override should suppress the trait default only when the override's cfg is active. Otherwise, the default implementation should remain available.
Native test registration needed the same treatment because registration ctors referenced generated raw wrapper functions. If a wrapper was cfg-disabled but its registration was not, the generated test code could reference a missing symbol.
Fixes #1868
Known limitations
cfg_attris not interpreted for function gating. Use direct#[cfg(...)]on#[contracttrait]default functions and#[contractimpl]functions.For cross-crate
#[contracttrait]default functions, function-level cfgs are evaluated in the implementing contract crate's configuration context when generating wrappers. Shared trait crates should prefer cfg-gating the whole trait item, or require implementing contracts to define matching features for function-level cfgs.