I've been writing a lot of code lately using ~Copyable and ~Escapable, and I keep butting heads with the compiler automatically inserting Copyable and Escapable constraints on protocols that refine ~Copyable and ~Escapable in generic contexts. This is probably the biggest ergonomics complaint people have about these protocols as it stands, and I have been trying to figure out how we could address that. The other day I had an idea, though I'm not sure we can retcon it at this point:
Fundamentally, the reason we need to be treating these negative protocols like this is that existing code needs to stay valid even as the protocols it references adopt ~Copyable and/or ~Escapable. However, there is tons of new code being written with protocols that were never Copyable and Escapable to start with! I feel that we've optimized for the wrong use case here.
Instead, I think it would work better for these constraints to propagate by default and the exception being existing code that is evolving to adopt these features, which does need to maintain source compatibility. For those cases, the protocols should be putting an attribute on their refinement to clarify that this is a change that needs to be made in a source-stable way and thus the loosened constraints should not propagate implicitly; something like @explicit, @nonImplicit, or @nonPropagating.
Thus, if you're taking a parameter of type Foo where protocol Foo: ~Escapable, you can just take some Foo rather than needing to spell out some Foo & ~Escapable. If, however, you're taking a Bar where protocol Bar: @nonPropagating ~Escapable, the constraint some Bar implicitly resolves to some Bar & Escapable unless you spell out some Bar & ~Escapable as before.
Food for thought! I wonder what sharp edges on this approach I'm missing.
I've been imagining we will eventually offer an attribute and module flag to control which protocols are conformed by default, so you could do
@suppress(Copyable) // strawman syntax
extension ... where ... /* no Copyable assumption */
and
default @supress(Copyable, Escapable)
for a whole file, and -suppress-conformance Copyable for a whole module.
but I have no idea if this is the direction we will take. I agree we will need to solve it! I believe the thinking is that supressing these protocols is not something that needs to be done even for most new types, and that they are relegated to "high performance" and specialized use cases. I like an attribute that propagates to members, methods, etc, since we have mechanisms like this today, although it's not great to have two ways of stating the same thing.
Yeah, at one point stuff like this was discussed, or perhaps a "noncopyable scope" where you can put declarations and everything in there is noncopyable by default. In hindsight this may have been better (or at least more concise) than the current design of needing to suppress everything that must be suppressed, but it certainly wasn't clear at the time.
Not relevant to your main point, but I want to add that I would prefer if we avoid adding any new command line flags to change compiler behavior, except for backward compatibility modes and new stuff that will eventually be "always on", like upcoming features.
Otherwise if you find a source snippet somewhere, or want to incorporate code from another code base, you'll have to fiddle with it to make it work with your chosen set of compiler flags. It might not even be obvious what changes are required, or how to "translate" from a global flag to the right set of attributes or per-file pragmas.
It also makes testing more difficult because now to get full coverage you have to test every feature with every combination of flags. The MainActor-by-default mode for example shipped with a number of simple language constructs not working, because none of the existing tests in the suite ran with that flag enabled.
i feel that ~Copyable is significantly less friendly than it needs to be, especially when protocol extensions are involved. i think it’s okay for ~Copyable on the primary declaration to change the default for all extensions on that declaration
This is arguably also true for custom operators, extensions of currency types, overloads, so on... But yes I agree these are undesirable qualities.
I'd agree module default main actor is extra bad for compiler testing / communicating about code snippets, but I'd blame it partially on it not doing a "simple" syntactic thing, it has:
a motivated, but bespoke set of rules and special cases (I don't think I have every emergent behavior in my head right now)
an implementation that introduces isolation in ways that are not visible to the (unfortunate, shrinking amount of) compiler code which checks for syntactic global actor isolation [1]
an implementation that does bonus different things between language versions [2]
enabling other "unrelated" experimental feature flags which are also undertested (so much so that they can't be enabled without enabling main actor by default in release compilers...)
In theory, we could do a flag for suppressing default conformance without a lot of this baggage. It would do something exactly equivalent to writing ~Protocol everywhere the compiler accepts it [3] and so "all" you need to do is test features with suppression of that protocol.
If we checked concurrency at SIL instead of Sema, it wouldn't matter how isolation was introduced, but we don't. Instead, we do funny stuff like copying isolation from protocol to conformer, and have a second check which only rejects writing an isolation on a given type... anywhere we accidentally treat "compiler emitted" differently from "programmer wrote" gives us so many bugs ↩︎
@preconcurrency @MainActor, but only in Swift 5, AFAIR. If you used it for a library (please please don't) you can't switch to Swift 6 without an ABI break ↩︎
TBF this is hard, since accepting it new places needs staging or is a source break... ↩︎
Yeah, this is exactly the thing I'm trying to solve. I want it to propagate by default, and I think the library evolution case we're inadvertently optimizing for with the current behavior is weird and should be marked as such!