Commit Graph

58 Commits

Author SHA1 Message Date
Hamish Knight
5e1279516b [AST] NFC: Rename LookupConformanceInModuleRequest -> LookupConformanceRequest
This request no longer takes a module argument.
2025-11-10 21:25:07 +00:00
Hamish Knight
954b08cae5 [AST] Remove UnresolvedType
We have now removed all uses of this type.
2025-10-03 09:50:42 +01:00
Kavon Farvardin
19c68332b0 nfc: introduce TypeBase::isCopyable
The days of double-negative conditions is over.
2025-08-28 12:13:11 -07:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Slava Pestov
b91ed32698 AST: Cache isNoncopyable() and isEscapable() bits inside TypeBase 2025-07-07 17:57:32 -04:00
John McCall
3439e0caab Fix a bunch of bugs with the isolation of local funcs. Since we
use local funcs to implement `defer`, this also fixes several
bugs with that feature, such as it breaking in nonisolated
functions when a default isolation is in effect in the source file.

Change how we compute isolation of local funcs. The rule here is
supposed to be that non-`@Sendable` local funcs are isolated the
same as their enclosing context. Unlike closure expressions, this
is unconditional: in instance-isolated functions, the isolation
does not depend on whether `self` is captured. But the computation
was wrong: it didn't translate global actor isolation between
contexts, it didn't turn parameter isolation into capture isolation,
and it fell through for several other kinds of parent isolation,
causing the compiler to try to apply default isolation instead.
I've extracted the logic from the closure expression path into a
common function and used it for both paths.

The capture computation logic was forcing a capture of the
enclosing isolation in local funcs, but only for async functions.
Presumably this was conditional because async functions need the
isolation for actor hops, but sync functions don't really need it.
However, this was causing crashes with `-enable-actor-data-race-checks`.
(I didn't investigate whether it also failed with the similar
assertion we do with preconcurrency.) For now, I've switched this
to capture the isolated instance unconditionally. If we need to
be more conservative by either only capturing when data-race checks
are enabled or disabling the checks when the isolation isn't captured,
we can look into that.

Fix a bug in capture isolation checking. We were ignoring captures
of nonisolated declarations in order to implement the rule that
permits `nonisolated(unsafe)` variables to be captured in
non-sendable closures. This check needs to only apply to variables!
The isolation of a local func has nothing to do with its sendability
as a capture.

That fix exposed a problem where we were being unnecessarily
restrictive with generic local func declarations because we didn't
consider them to have sendable type. This was true even if the
genericity was purely from being declared in a generic context,
but it doesn't matter, they ought to be sendable regardless.

Finally, fix a handful of bugs where global actor types were not
remapped properly in SILGen.
2025-06-29 01:23:04 -04:00
Andrew Trick
cc357f4f32 Add Feature: NonescapableAccessorOnTrivial
To guard the new UnsafeMutablePointer.mutableSpan APIs.

This allows older compilers to ignore the new APIs. Otherwise, the type checker
will crash on the synthesized _read accessor for a non-Escapable type:

    error: cannot infer lifetime dependence on the '_read' accessor because 'self'
    is BitwiseCopyable, specify '@lifetime(borrow self)'

I don't know why the _read is synthesized in these cases, but apparently it's
always been that way.

Fixes: rdar://153773093 ([nonescapable] add a compiler feature to guard
~Escapable accessors when self is trivial)
2025-06-20 15:59:13 -07:00
Pavel Yaskevich
aaf153d7a1 [Concurrency] Fix SendableMetatype conformance failures to behave like Sendable ones
No warnings with minimal checking, warnings with `strict-concurrency=complete` and
if declaration is `@preconcurrency` until next major swift version.

Resolves: rdar://151911135
Resolves: https://github.com/swiftlang/swift/issues/81739
2025-06-06 08:48:29 -07:00
Doug Gregor
53707a121c Collapse two implementations into ExistentialLayout::containsNonMarkerProtocols 2025-04-14 08:42:32 -07:00
Doug Gregor
8626404de3 [SE-0470] Treat metatype of archetype/existential type with no protocol requirements as Sendable
A metatype for an archetype or existential with no (non-marker)
protocol requirements cannot, by definition, carry any (isolated)
protocol conformances with it, so it's safe to treat such metatypes as
Sendable.
2025-04-13 15:42:02 -07:00
Doug Gregor
3380331e7e [SE-0470] Enable isolated conformances by default
The IsolatedConformances feature moves to a normal, supported feature.
Remove all of the experimental-feature flags on test cases and such.

The InferIsolatedConformances feature moves to an upcoming feature for
Swift 7. This should become an adoptable feature, adding "nonisolated"
where needed.
2025-04-13 15:41:53 -07:00
Joe Groff
562d7dc832 Merge pull request #80438 from jckarter/substitute-away-escapable-lifetime-deps
Type substitution eliminates dependencies with Escapable targets.
2025-04-02 16:56:54 -07:00
Joe Groff
6b605f41cb Type substitution eliminates dependencies with Escapable targets.
When a generic function has potentially Escapable outputs, those outputs
declare lifetime dependencies, which have no effect when substitution
leads to those types becoming `Escapable` in a concrete context.
This means that type substitution should canonically eliminate lifetime
dependencies targeting Escapable parameters or returns, and that
type checking should allow a function value with potentially-Escapable
lifetime dependencies to bind to a function type without those dependencies
when the target of the dependencies is Escapable.

Fixes rdar://147533059.
2025-04-02 08:54:45 -07:00
Doug Gregor
2f5c6d21bb Collapse experimental feature StrictSendableMetatypes into IsolatedConformances 2025-03-28 16:55:33 -07:00
Doug Gregor
e24598bca1 Use a marker protocol SendableMetatype to model T.Type: Sendable
Introduce a marker protocol SendableMetatype that is used to indicate
when the metatype of a type will conform to Sendable. Specifically,
`T: SendableMetatype` implies `T.Type: Sendable`. When strict
metatype sendability is enabled, metatypes are only sendable when `T:
SendableMetatype`.

All nominal types implicitly conform to `SendableMetatype`, as do the
various builtin types, function types, etc. The `Sendable` marker
protocol now inherits from `SendableMetatype`, so that `T: Sendable`
implies `T.Type: Sendable`.

Thank you Slava for the excellent idea!
2025-02-13 22:48:05 -08:00
Doug Gregor
37d71f362e Add StrictSendableMetatypes to require Sendable requirements on metatypes
Introduce a new experimental feature StrictSendableMetatypes that stops
treating all metatypes as `Sendable`. Instead, metatypes of generic
parameters and existentials are only considered Sendable if their
corresponding instance types are guaranteed to be Sendable.

Start with enforcing this property within region isolation. Track
metatype creation instructions and put them in the task's isolation
domain, so that transferring them into another isolation domain
produces a diagnostic. As an example:

    func f<T: P>(_: T.Type) {
      let x: P.Type = T.self
      Task.detached {
        x.someStaticMethod() // oops, T.Type is not Sendable
      }
    }
2025-02-12 20:21:57 -08:00
Slava Pestov
47156e006b AST: Introduce ProtocolConformanceRef::forAbstract() 2024-11-16 16:16:06 -05:00
Meghana Gupta
133602de73 Add Escapable conformance to some builtin functions 2024-11-11 23:18:29 -08:00
Joe Groff
a184782a38 Introduce a Builtin.FixedArray type.
`Builtin.FixedArray<let N: Int, T: ~Copyable & ~Escapable>` has the layout of `N` elements of type `T` laid out
sequentially in memory (with the tail padding of every element occupied by the array). This provides a primitive
on which the standard library `Vector` type can be built.
2024-10-22 16:21:45 -07:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Slava Pestov
6475efc691 AST: lookupConformance() returns an abstract conformance given a type parameter 2024-08-08 23:27:42 -04:00
Slava Pestov
3fcda140bb AST: ModuleDecl::checkConformance() is a static method 2024-07-06 12:05:46 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Slava Pestov
b7117aa877 AST: Start untangling ModuleDecl from conformance lookup 2024-07-06 12:05:45 -04:00
Slava Pestov
252abb5365 Sema: Key the implied Sendable conformance behavior off of -swift-version instead of -strict-concurrency
Also add some comments to explain what in the world is going on
with the new checks.
2024-07-06 12:05:45 -04:00
Slava Pestov
bb48d32ec4 Sema: Implied 'Sendable' conformance should be unconditional
A conditional conformance to a protocol does not usually imply
a conformance to the protocol's inherited protocols, because
we have no way to guess what the conditional requirements
should be.

A carveout was added for 'Sendable', so that protocols could
inherit from 'Sendable' retroactively. However, the 'Sendable'
conformance would become conditional, which causes us to
reject a _second_ conditional conformance to such a protocol:

    struct G<T> {}
    protocol P: Sendable {}
    protocol Q: Sendable {}

    extension G: P where T: P {}
    extension G: Q where T: Q {}

To make this work, tweak the code so that an implied conformance
has the same generic signature as the conforming type, that is,
we force it to be unconditional.

Fixes rdar://122754849
Fixes https://github.com/swiftlang/swift/issues/71544
2024-07-02 17:04:41 -04:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Kavon Farvardin
b3d548218b NCGenerics: omit flag in interfaces
Also cleans-up legacy code and tests that are no longer needed.
2024-04-18 21:57:44 -07:00
Slava Pestov
bc85d66b96 AST: Don't call ProtocolDecl::getSuperclass() in lookupExistentialConformance() 2024-03-21 14:49:04 -04:00
Kavon Farvardin
b5840cf745 NCGenerics: workaround lldb bug 2024-03-15 14:43:40 -07:00
Kavon Farvardin
84a44f8769 Sema: remove more unneeded legacy code 2024-03-14 23:10:44 -07:00
Kavon Farvardin
149c052ec5 use new noncopyable types infrastructure
The infrastructure underpinning the new feature NoncopyableGenerics is
mature enough to be used.
2024-03-14 23:10:44 -07:00
Michael Gottesman
6e18ffe729 Merge pull request #72231 from gottesmm/pr-1390a8c8ab469f02b49e34ee396ccfe604f2a8f7
[Concurrency] Change TypeBase::isSendable() to return false for unavailable conformances.
2024-03-11 14:30:41 -07:00
Michael Gottesman
8f149b4d0e [Concurrency] Change TypeBase::isSendable() to return false for unavailable conformances.
rdar://122174496
2024-03-11 11:29:52 -07:00
Pavel Yaskevich
b17f0b534c Merge pull request #71855 from xedin/fix-sendable-keypath-conformance-issues
[AST] Special handling for existentials with superclass and marker pr…
2024-03-08 13:06:01 -08:00
Konrad `ktoso` Malawski
b7ff16baf7 [Distributed] Only synthesize Codable for DA where the ID is Codable (#72081) 2024-03-07 22:40:00 -08:00
Pavel Yaskevich
c84a34677a [AST] Special handling for existentials with superclass and marker protocols
Even if protocol is not self-conforming it should be okay to produce
a conformance based on superclass if protocol bounds of the existential
are all marker protocols.

Superclass concrete conformance is wrapped into an inherited conformance
in such cases to reference the existential.
2024-03-07 13:05:40 -08:00
Slava Pestov
41df661160 AST: Use a builtin conformance for unconditional Copyable/Escapable
This generalizes what we were already doing for classes.
2024-03-07 15:07:47 -05:00
Slava Pestov
aa4a1b2b6e AST: The generic conformance of a specialized conformance is always normal 2024-03-07 12:22:33 -05:00
Slava Pestov
08ba88f71b Sema: Remove deriveConformanceForInvertible() 2024-03-07 12:20:17 -05:00
Slava Pestov
dcea491dbe Sema: Remove inference of conditional Copyable conformance from generic parameters 2024-03-06 22:47:54 -05:00
Joe Groff
fe7049ed13 SIL: More accurate for type lowering whether a type is trivial based on conditional Copyable requirements.
We want a conditionally-copyable type to still be classified as trivial in cases
where it's bitwise-copyable, has a trivial deinit, and is Copyable. The previous
implementation here only checked at the declaration level whether a type was
Copyable or not; get a more accurate answer by consulting the combination
of information in the substituted type and abstraction pattern we have
available during type lowering so that we classify definitely-copyable substitutions
of a conditionally-copyable type as trivial. Should fix rdar://123654553 and
rdar://123658878.
2024-02-27 15:01:20 -08:00
Pavel Yaskevich
b643cd8fb6 [AST/Sema] NonCopyableGenerics: Remove all but one direct use of getMarking(...) 2024-02-16 17:55:52 -08:00
Kavon Farvardin
8a7fd33101 Merge pull request #71560 from kavon/ncgenerics-test-fixes-kavon-v10
Ncgenerics test fixes kavon v10
2024-02-13 11:59:54 -08:00
Kavon Farvardin
3a45393e17 NCGenerics: break cycle with SuperclassTypeRequest
With NoncopyableGenerics, we get a cycle involving
`SuperclassTypeRequest` with this program:

  public struct RawMarkupHeader {}
  final class RawMarkup: ManagedBuffer<RawMarkupHeader, RawMarkup> { }

Because we generally don't support the following kind of relationship:

  class Base<T: P>: P {}
  class Derived: Base<Derived> {}

This commit works around the root-cause, which is that Derived's
synthesized conformance to Copyable gets superceded by the inherited one
from Base. Instead of recording conformances in the ConformanceLookup
table at all, create builtin conformances on the fly, since classes
cannot be conditionally Copyable or Escapable.
2024-02-13 09:19:12 -08:00
Kavon Farvardin
98e6c7b935 ConformanceLookup: filter ReferenceStorageType
There's a few uses of ReferenceStorageTypes being substituted for
generic parameters, at least in the test suite, such as
`Optional<@sil_unmanaged ..>` and just plain `<@sil_unowned ..>`.

Before, conformance lookup could (and did) give bogus answers when asked
 if the type satisfies any conformance requirements. Now with
 NoncopyableGenerics, we will interpret such conformance lookups as
 being asked of the referent type, ignoring the SIL ownership wrapping
 it.
2024-02-13 09:19:12 -08:00
Pavel Yaskevich
0e75312450 [AST] Handle pack element types in LookupConformanceInModuleRequest 2024-02-12 16:43:18 -08:00
Kavon Farvardin
32a0ea8734 ConformanceLookup: catch ReferenceStorageType
While this doesn't provde defence agianst incorrect conformance
responses in the general case, it should flush out a lot of incorrect
ones.
2024-02-11 20:40:03 -08:00
Kavon Farvardin
e913712bf5 Sema: drop @lvalue when building tuple conformance 2024-02-11 20:40:03 -08:00
Kavon Farvardin
d5eb76e2fb NCGenerics: remove optimistic assertion
I thought it might be impossible to recursively lookupConformance of
Copyable, unlike for Sendable. Turns out there are still situations
where it can happen, that aren't invalid.

This alone fixes `ClangImporter/objc_bridging_generics.swift` and will
help reveal any invalid request cycles.
2024-02-11 20:40:03 -08:00