Skip to content

Trait not found in defined_traits #700

@BowTiedWoo

Description

@BowTiedWoo

When passing an optional trait reference to a function and calling it with contract-call?, the call fails with DefineFunctionBadSignature.
This happens even though the trait is correctly used via use-trait.

    #[test]
    fn test_embedded_trait() {
        let foo_trait = "
            (define-trait foo
                (
                    (do-it () (response bool uint))
                )
            )
            "
        .to_string();

        let foo_impl = "
            (impl-trait .foo.foo)

            (define-public (do-it)
                (ok true)
            )
            "
        .to_string();

        let call_foo = "
            (use-trait foo .foo.foo)
            (define-public (call-do-it (opt-f (optional <foo>)))
                (match opt-f
                    f (contract-call? f do-it)
                    (ok false)
                )
            )
            "
        .to_string();

        let mut env = TestEnvironment::new(StacksEpochId::Epoch21, ClarityVersion::Clarity2);
        env.init_contract_with_snippet("foo", &foo_trait).expect("Failed to init contract.");
        env.init_contract_with_snippet("foo-impl", &foo_impl).expect("Failed to init contract.");
        env.init_contract_with_snippet("call-foo", &call_foo).expect("Failed to init contract.");

        crosscheck_with_env(
            "(contract-call? .call-foo call-do-it (some .foo-impl))",
            Ok(Some(Value::Bool(true))),
            env,
        );
    }

The call should return (ok true), but it fails with DefineFunctionBadSignature. Debugging shows that defined_traits is empty when calling contract-call? with foo-impl contract.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions