Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bindings are sometimes incorrectly generated type + Borrow suffix vs & type #299

Closed
calvinrp opened this issue May 20, 2024 · 2 comments
Closed

Comments

@calvinrp
Copy link
Collaborator

calvinrp commented May 20, 2024

cargo component new --lib --target calvinrp:scratch-example --registry https://calvinrp.wa.dev scratch-example

generates:

#[allow(warnings)]
mod bindings;

use bindings::exports::calvinrp::scratch_example::service::{Guest, Key};

struct Component;

impl Guest for Component {
    fn sign(key: KeyBorrow) -> String {
        unimplemented!()
    }
}

bindings::export!(Component with_types_in bindings);

where
sign method's key arg should be &Key instead of KeyBorrow
and fails to compile since the KeyBorrow type does not exist

The WIT for the calvinrp:scratch-example is shown here:

package calvinrp:scratch-example@0.0.1;

/// An example world for the component to target.
world example {
    export service;
}

interface service {
  use other.{key};

  sign: func(key: borrow<key>) -> string;
}

interface other {
  resource key {}
}
@calvinrp calvinrp changed the title Bindings are sometimes type + Borrow suffix vs & type Bindings are sometimes incorrectly generated type + Borrow suffix vs & type May 20, 2024
@calvinrp
Copy link
Collaborator Author

OK, figured it out. If the interface types are exported in the target world, then wit-bindgen-rust uses SomeBorrow instead of &Some. But cargo-component doesn't implement the same logic.

PR in the works

calvinrp added a commit that referenced this issue May 22, 2024
Expanded test cases to cover issue #299

Also, when specifying an exact semver requirement, adds that exact requirement to the generated Cargo.toml. There was mismatch before.

wit-bindgen-rust generates different bindings for borrow<> of resources, depending on whether that type is exported in the world or not. &SomeType vs SomeTypeBorrow in the generated bindings. Fixed the logic here to match that behavior. The revised test case should better cover future potential breakage.

If interested, here is some of the logic on the wit-bindgen side:
https://github.com/bytecodealliance/wit-bindgen/blob/main/crates/rust/src/interface.rs#L2203-L2225
@calvinrp
Copy link
Collaborator Author

#300 fixes

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

No branches or pull requests

1 participant