C annotations `counted_by` and `noescape` not producing Span consuming function signatures

Hello again :waving_hand:

I’m trying to introduce memory safety to my Swift project which integrates some C functions. For this I referred to this WWDC session

Unfortunately the __noescape annotation is not being recognized at all by the compiler. By using __attribute((__noescape__)) I can proceed and the __counted_by(N) annotation is actually recognized (on macOS).

The problem is that on the Swift side the expected function signature containing Span as parameter is not produced. I still get the same old UnsafePointer as before.

For context the function I’m trying to integrate has the following signature:

int ecdsa_signature_parse_der_lax(secp256k1_ecdsa_signature* sig, const unsigned char *__counted_by(inputLen) input __attribute((__noescape__)), size_t inputlen);

For further context here’s a pull request with the attempted change to the project. I’m using Swift 6.2 and the latest Xcode of course. The failed check also shows how on Linux it doesn’t even recognize the __counted_by(N) annotation (missing C language extension?).

I’m even using the latest available C2x draft standard for the library but that doesn’t seem to make a difference. Also not sure how to enable the bounds safety language extension from the package manifest.

Anyway whatever insight anyone can provide I will appreciate! I’m aware of the possibility of this being yet another feature showcased but not delivered :frowning:

Thanks in advance!

From Adoption Guide for -fbounds-safety — Clang 22.0.0 | Feature Flags
Pass -fbounds-safety as a Clang compilation flag for the C file that you want to adopt. We recommend adopting the model file by file, because adoption requires some effort to add bounds annotations and fix compiler diagnostics.

You might also want to make sure that the compiler you are using understands all of your annotations. For example__noescape__ doesn’t appear valid and you probably were refrencing noescape

1 Like

I just watched that video and then went and played with technology. Here’s what I had to do to get this working in Xcode 26.1…

On the C side:

On the Swift side, set Other Swift Flags to [-enable-experimental-feature, SafeInteropWrappers].

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

4 Likes

That worked, thank you so much! How could I miss the experimental feature flag, oh well.

For Linux I also had to #include <ptrcheck.h> to get __counted_by(N) to compile.

Otherwise it worked like a charm and I no longer need to use unsafe buffers to call my C functions!

Cheers!