Those of you who know me know that I like to build my own variant stdlib for my platform. This mostly works well, but sometimes the tight coupling to the compiler trips me up when I update.
This time, I've copied the new RawSpan, MutableRawSpan, Span, MutableSpan into my stdlib and compiled it with the latest (matching) compiler.
I'm getting some odd errors like invalid use of borrow dependence with consuming ownership on the constructors of these new types*.
I found that if I comment out the annotations such as @lifetime(borrow pointer) in various places then I can make it closer to compiling (I've got another few small bits of work to iron out but it looks like it's basically working).
My question is, how important are these annotations?
Will the code flat out break/crash all the time if I don't do it? (Their absence causes undefined behaviour.)
Or is it more like they improve efficiency, reducing object lifetimes down to a minimum etc. so without them the object the span references might last a bit longer than it should?
For my embedded setup, I'm thinking probably unless it creates undefined/unstable behaviour, I'll omit them, because they're unlikely to be important on our platform.
Any help greatly received!
Cheers,
Carl
For those interested, debugging into the compiler the error is coming from the compiler thinking that UnsafeMutableRawPointer (etc) do not conform to BitwiseCopyable. Not sure why... but I've spent over a week trying to figure it out and I think it needs people with better compiler knowledge than me, so I'm giving up.
These new types are intended to provide a safe way of performing operations that previously required dropping down to unsafe buffer types.
The lifetime annotations are the foundation upon which these new types are built, without which safety cannot be guaranteed, thus nullifying the entire purpose of these types.
OK, well that's a really clear answer. Thank you! I suspected as much.
I think I got to the bottom of why it's not compiling on my stdlib after some debugging.
The proximal issue was UnsafeMutableRawPointer seemingly not conforming to BitwiseCopyable. This confused me until I realised the nominal type in the debugger where it was checking conformance was actually Optional.
What was missing from my stdlib was just...
extension Optional: BitwiseCopyable
where Wrapped: BitwiseCopyable & ~Escapable {}
That seems to fix the issue (I have other small issues but I'm working on them... should be able to release soon now).
Thanks all!
p.s. My stdlib is here for anyone curious: GitHub - carlos4242/microswift-stdlib. This isn't the copy I'm maintaining because I can't (yet) be bothered with git submodules etc. but it's a one shot copy from my current. Don't expect it to compile and it doesn't have all the makefiles, etc. but I thought people might find it interesting.
Will the code flat out break/crash all the time if I don't do it? (Their absence causes undefined behaviour.)
Lifetime dependencies are needed when returning a ~Escapable type or mutating one.
We have limited lifetime dependence inference and absence of @_lifetime annotations should not cause undefined behavior. If the inference is not possible and @_lifetime annotation is absent, there will be a diagnostic.