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

Swift array count: expected return to have type code 'q', but found 'Q' #566

Closed
1tgr opened this issue Jan 20, 2024 · 3 comments · Fixed by #567
Closed

Swift array count: expected return to have type code 'q', but found 'Q' #566

1tgr opened this issue Jan 20, 2024 · 3 comments · Fixed by #567
Labels
A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates bug Something isn't working

Comments

@1tgr
Copy link

1tgr commented Jan 20, 2024

I'm exposing this Rust function to Swift:

#[no_mangle]
pub extern "C" fn time_series_eval(
    exprs: *mut NSArray<NSString>,
    error: &mut *mut NSError,
) -> *mut NSDictionary<NSString, NSArray> {
    let exprs = exprs.as_ref().unwrap().iter();
    // do something with exprs
}

I call it like this from the Swift side:

var error: NSError?
let result = time_series_eval(["abc"], &error)

But the Rust panics:

~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/icrate-0.1.0/src/generated/Foundation/NSEnumerator.rs:6:1:
invalid message send to -[Swift.__SwiftDeferredNSArray countByEnumeratingWithState:objects:count:]: expected return to have type code 'q', but found 'Q'

I think this is telling me that Swift arrays and objc2's arrays don't agree on signed vs unsigned?

@madsmtm madsmtm added bug Something isn't working A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates labels Jan 21, 2024
@madsmtm
Copy link
Owner

madsmtm commented Jan 21, 2024

I think this is telling me that Swift arrays and objc2's arrays don't agree on signed vs unsigned?

Rather, Swift is lazy and uses Int instead of the more correct type UInt here; the difference doesn't really matter to them, as arrays likely won't be able to ever get so large that it matters.

I've opened #567 to fix this, although I'm a bit unsure what the right approach is. In the meantime the easiest fix for you is probably to do one of:

  • Patch in Ignore signed-ness in encoding #567 locally (using the patch.crates-io Cargo.toml key).
  • Re-implement the iteration yourself using objectAtIndex:.
  • Disable debug assertions.

@1tgr
Copy link
Author

1tgr commented Jan 22, 2024

Thanks for the quick response. I took a local copy of the repo and did a quick hack equivalent to #567 that avoided the problem and I'll try taking a Git dependency on #567 for now.

@madsmtm
Copy link
Owner

madsmtm commented May 20, 2024

I went with a Cargo feature flag called relax-sign-encoding, you should be able to use that instead once I release objc2 v0.5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants