Commit Graph

339 Commits

Author SHA1 Message Date
Joe Groff
07a3dfb1d1 SILGen: Have emitSemanticStore cast off concurrency annotations.
If a function is being semantically treated as having different concurrency
annotations because of a `@preconcurrency` import or language mode setting,
then SILGen may try to store an argument or result for a call using a value
that differs only in concurrency annotations, which can be safely bitcast
away.

Fixes rdar://154520999
2025-07-17 11:16:55 -07:00
Joe Groff
79b59f1977 Lower imported C structs and unions as addressable-for-dependencies.
C code is highly likely to want to use pointers as references between dependent
structs, and we would like to be able to readily map these to lifetime-dependent
Swift values. Making C types addressable-for-dependencies ensures that any function
producing a dependency on such a value receives a stable in-memory address for that
value, allowing borrows and inout accesses to always be representable as pointers.

rdar://153648393
2025-07-07 08:46:43 -07:00
Allan Shortlidge
62986b7779 Tests: Expand SILGen tests for custom availability domain queries. 2025-06-23 15:57:16 -07:00
Slava Pestov
140abae262 Merge pull request #81046 from slavapestov/fix-rdar149801864-6.2
[6.2] Demangle: Implement missing Node::Kind::DependentProtocolConformanceOpaque
2025-04-28 11:55:04 -04:00
Slava Pestov
58ecc36830 Add test case that becomes intractably slow with TypeTransformer::transformSubMap() fix
This was reported as rdar://149315905.
2025-04-23 18:00:20 -04:00
Slava Pestov
eaa49f6a84 Demangle: Implement missing Node::Kind::DependentProtocolConformanceOpaque
If a retroactive conformance is conditional and the subject type of a
conditional requirement is an opaque archetype, the mangler would
emit a mangling using the "HO" operator which the demangler did not
understand.

Implement de/remangler support for the "HO" operator.

Fixes rdar://149801864.
2025-04-23 16:40:46 -04:00
Joe Groff
9e5e240704 SILGen: Fix inconsistency between vtable layout and vtable entry emission.
As part of the criteria to determine whether a method override requires a new
vtable entry in addition to overriding the base class's vtable entry for the
method, we compare the visibility of the base class to that of the derived
class, since if further subclasses of the derived class cannot see the original
base class's method descriptor, they would not be able to directly override its
vtable entry.

However, there was a subtle inconsistency in our checks in two different
places: `NeedsNewVTableEntryRequest::evaluate` compared the visibility of the
derived method to its immediate override method, whereas
`SILGenModule::emitVTableMethod` compared visibility with the ultimate base
method that originated the vtable entry in question. Internal imports create
a situation where this leads to inconsistent answers, causing us to emit
a vtable thunk but without a corresponding new vtable entry, causing the thunk
to become self-recursive (rdar://147360093). If the base class is in a separate
module:

```
// Module1
open class Base {
    public required init() {}
}
```

and an intermediate class inherits that base class, it must `public import`
the module in the file defining the class to do so:

```
// Module2

public import Module1

open class MidDerived: Base { }
```

However, another file in the same module can further inherit that class, but
with only an `internal import` of the base module:

```
// Also Module2

import Module1

open class MostDerived: MidDerived { }
```

The implicit `init()` is `public` in each class, but from the perspective of
`MostDerived.init`, `Base.init` is `internal` because of the import. However,
the fact that its immediate parent is `public` should be sufficient evidence
that its original definition had visibility to `Base`, so no thunk should
be necessary.
2025-03-19 15:34:50 -07:00
Becca Royal-Gordon
9cffd4528d Fix objcImpl SILGen crash with initial value
The initial value expressions of stored properties in objcImpl classes were being incorrectly marked as serializable. As a result, the compiler would crash with a SIL verification failure if one of them called a non-public function or initializer.

Fix this problem by not marking these initial value expression functions as serializable. Code in other modules should not call them anyway, since they think of the class as a pure ObjC class.

Fixes rdar://114874429.
2024-07-23 16:17:30 -07:00
Pavel Yaskevich
1e9e351a3f [SILGen] Fix any Sendable to Any bridging when result should be placed in a buffer
If the context indicates that the result of `id-to-Any` should be
placed in some pre-allocated buffer, let's not attempt marker existential
transformation because that would much the expected layout already.
2024-06-17 09:45:00 -07:00
Nate Chandler
2d03664e45 [TypeLowering] Return pseudogeneric @autoreleasing
Previously, when a pseudogeneric function returned a type parameter, it
was unowned.  That resulted in invalid OSSA out of SILGen.  Here, this
is fixed to use the @autoreleasing convention.

rdar://64375208
2024-05-31 18:42:25 -07:00
Artem Chikin
1f14158a1d Introduce VisionOS Platform
This change introduces a new compilation target platform to the Swift compiler - visionOS.

- Changes to the compiler build infrastrucuture to support building compiler-adjacent artifacts and test suites for the new target.
- Addition of the new platform kind definition.
- Support for the new platform in language constructs such as compile-time availability annotations or runtime OS version queries.
- Utilities to read out Darwin platform SDK info containing platform mapping data.
- Utilities to support re-mapping availability annotations from iOS to visionOS (e.g. 'updateIntroducedPlatformForFallback', 'updateDeprecatedPlatformForFallback', 'updateObsoletedPlatformForFallback').
- Additional tests exercising platform-specific availability handling and availability re-mapping fallback code-path.
- Changes to existing test suite to accomodate the new platform.
2024-04-10 09:38:02 -07:00
Slava Pestov
3630ce7130 AST: Fix swift::substOpaqueTypesWithUnderlyingTypes() to pass PreservePackExpansionLevel
Fixes rdar://problem/123645784.
2024-04-04 19:36:25 -04:00
Allan Shortlidge
cf3031794f SILGen: Avoid expanding #line directives with invalid source locations.
Synthesized stub constructors contain `#line` directives and under certain
circumstances these directives can have invalid source locations. SILGen would
trigger an assert (or invoke UB in non-asserts builds) when attempting to
expand these directives into literal values. Check the source location for
validity before translating the location to a location in the outermost source
file.

Resolves rdar://121971741
2024-02-20 20:07:16 -08:00
Slava Pestov
0b0ad738dd AST: Record a HadError bit in DelayedConformanceDiags 2024-01-20 19:16:58 -05:00
Holly Borla
df858a5ab9 Merge pull request #70553 from hborla/bridge-to-sendable 2023-12-20 06:10:35 -08:00
Holly Borla
d3c42649b7 [SILGen] Handle marker existentials in emitCBridgedToNativeValue. 2023-12-19 19:02:21 -08:00
Allan Shortlidge
b4e1c4883e Tests: Add serialization of vtable for package class tests.
Missing from https://github.com/apple/swift/pull/70100/.
2023-12-18 18:12:54 -08:00
Nate Chandler
98acb40d99 [SIL] Addr-only enums have non-none ownership.
Values produced by address-only `enum` instructions have non-none
ownership.  And because `enum` is representation-changing, they have
`owned` ownership.

This corresponds at the opaque values SIL stage to the fact that at the
address-lowered SIL stage the storage location has non-trivial
initialization which must be `destroy_addr`d, regardless of whether an
empty case was stored to it.
2023-11-16 11:03:14 -08:00
Allan Shortlidge
2150ff6adf Tests: Fix skip-function-bodies-clang-enum-init-raw-value.swift on Windows.
Declare an explicit type for the enum to make SILGen output consistent across platforms.
2023-10-10 16:52:31 -07:00
Allan Shortlidge
1d8fc104c3 AST/SILGen: Requestify function body skipping.
Function bodies are skipped during typechecking when one of the
-experimental-skip-*-function-bodies flags is passed to the frontend. This was
implemented by setting the "body kind" of an `AbstractFunctionDecl` during decl
checking in `TypeCheckDeclPrimary`. This approach had a couple of issues:

- It is incompatible with skipping function bodies during lazy typechecking,
  since the skipping is only evaluated during a phase of eager typechecking.
- It prevents skipped function bodies from being parsed on-demand ("skipped" is
  a state that is distinct from "parsed", when they ought to be orthogonal).
  This needlessly prevented complete module interfaces from being emitted with
  -experimental-skip-all-function-bodies.

Storing the skipped status of a function separately from body kind and
requestifying the determination of whether to skip a function solves these
problems.

Resolves rdar://116020403
2023-09-28 19:18:35 -07:00
Michael Spencer
b2640e15e4 [test] Rename all module.map files to module.modulemap
`module.map` as a module map name has been discouraged since 2014, and
Clang will soon warn on its usage. This patch renames all instances of
`module.map` in the Swift tests to `module.modulemap` in preparation
for this change to Clang.

rdar://106123303
2023-08-21 15:58:59 -07:00
Joe Groff
d948213c72 Merge pull request #67972 from jckarter/revert-abstractstoragedecl-originally-defined-in-check
Revert "AST: Spot fix for AbstractStorageDecl::isResilient()"
2023-08-16 20:19:56 -07:00
Joe Groff
e2db4d221c Revert "AST: Spot fix for AbstractStorageDecl::isResilient()"
This reverts commit d5b354fd5f. It causes miscompiles when
accessing properties declared with `@_originallyDefinedIn` that are now defined in modules
with library evolution enabled from the module that the property was originally defined in.
Just because the property used to be declared in the current module doesn't mean it can
bypass the stable ABI of the module that the property now belongs to.

It looks like the logic that this PR replaced is also faulty, since `@_originallyDefinedIn`
really oughtn't factor into the resilience computation at any level, but let's unwind one
level of brokenness at a time.

Fixes rdar://113935401.
2023-08-16 14:59:43 -07:00
Pavel Yaskevich
4bd60aeb4e [Tests] NFC: Remove/adjust runtime metadata related test cases 2023-08-15 12:17:31 -07:00
Nate Chandler
cc3038dcf6 [OpaqueValues] Transforms emit addr-only loads.
In opaque values mode, when transforming, load address even when their
type is address-only.
2023-08-02 20:16:58 -07:00
Slava Pestov
385a7beb45 SIL: Bridge 'any Sendable' via the id-as-Any path
Fixes rdar://problem/107264240.
2023-04-25 13:53:11 -04:00
Andrew Trick
64e1ce26a8 Add unit tests for implicit inout to raw pointer diagnostics. 2023-02-27 21:51:17 -08:00
Pavel Yaskevich
67b74a0633 [Sema] RuntimeMetadata: Diagnose missing explicit reflection metadata attributes
Diagnose situations where a sub-class or a protocol do not have all
of the reflection metadata attributes required by a superclass.

Resolves: rdar://103990788
2023-01-30 14:14:47 -08:00
Meghana Gupta
1dac5d48d3 Support for address discriminated pointers 2023-01-27 01:56:44 -08:00
swift-ci
281088eb73 Merge pull request #63204 from meg-gupta/ptrauthcodegenpr
Initial support for ptrauth qualified function pointers in C
2023-01-25 22:19:34 -08:00
Meghana Gupta
25d83406ad Generate begin_access [signed] when handling a field function pointer with __ptrauth qualifier 2023-01-25 14:03:19 -08:00
Pavel Yaskevich
94724f979d [Sema] RuntimeMetadata: Make sure that inferred attributes have source information
If an attribute is inferred from a protocol conformance it won't have any
source information, in such cases let's determine where an attribute could
have been inserted if it was spelled explicitly and use that location in
a synthesized initializer call.

Resolves: rdar://103938899
2023-01-09 11:23:45 -08:00
Doug Gregor
0e8d4c470e [SILGen] Fix formal type of global actor metatype.
Fixes a crash reported in rdar://103255322 .
2022-12-16 12:33:09 -08:00
Allan Shortlidge
466335b5d3 Tests: #_hasSymbol support for members of opaque/existential types.
Resolves rdar://101063734
2022-12-01 13:39:22 -08:00
Allan Shortlidge
182cc8de37 SILGen: Implement if #_hasSymbol() conditions.
Emit a call to a helper function that determines whether the symbols associated with the declaration referenced in the `#_hasSymbol(...)` condition are non-null at runtime. An upcoming change will emit a definition for this function during IRGen.

Resolves rdar://100130015
2022-10-13 21:00:24 -07:00
Allan Shortlidge
863b088696 NFC: Move global-actor.swift test case to SILGen directory. 2022-10-03 14:15:27 -07:00
Kavon Farvardin
a4d661d3bb Add tests for rdar://98018067 2022-08-29 21:02:03 -07:00
Joe Groff
6b6a557611 SILGen: Carry WMO of type lowering context to closure captures.
TypeConverter doesn't know by itself what SILModule it's currently lowering on
behalf of, so the existing code forming the TypeExpansionContext for opaque types
incorrectly set the isWholeModule flag to always false. This created a miscompile
when a public API contained a closure that captured a value involving private types
from another file in the same module because of mismatched type expansion contexts
inside and outside the closure. Fixes rdar://93821679
2022-06-06 19:51:40 -07:00
Joe Groff
48a5089f18 SILGen: Handle objc async bridging when there is a combination of Any and other results.
Generalize the logic here to work with more than one indirect result, or a
combination of indirect result(s) and direct result(s). Fixes rdar://88949633.
2022-02-23 16:29:45 -08:00
Arnold Schwaighofer
1447cde65f AST: Fix ValueDecl::isNativeMethodReplacement
When we want to figure out whether a `@_dynamicReplacement(for:)` decl
should use native method replacement we need to check whether _the
replaced decl_ is objc dynamic in a generic class.

rdar://87428993
2022-01-24 14:19:21 -08:00
Slava Pestov
a15628b8b1 AST: Fix ReplaceOpaqueTypesWithUnderlyingTypes to handle nested types of opaque result types
Fixes rdar://problem/85137685.
2021-11-08 16:34:47 -05:00
tokizuoh
5ff98088e4 Remove unnecessary import sentence 2021-10-16 16:47:57 +09:00
Arnold Schwaighofer
c2b2f1331f SIL representation 2021-10-06 04:54:49 -07:00
Slava Pestov
ed752738cd ASTMangler: Use the right generic signature when mangling conformance access path components
Only the first component is written in terms of the original generic signature;
subsequent components are written in terms of their protocol's generic signature,
<T where T : P>.
2021-08-25 16:44:46 -04:00
Slava Pestov
76a5fd9bc6 SIL: Allow lowering Optional<T> against a nested type of an opaque result type
Only the root archetype is a OpaqueTypeArchetypeType; the nested types are
NestedArchetypeType. Use the correct predicate here.

Also remove a dead check in tuple lowering; the isa<OpaqueTypeArchetypeType>()
check is dominated by isTypeParameterOrOpaqueArchetype() which is always
true if isa<OpaqueTypeArchetypeType>() is true anyway.

Fixes rdar://problem/79597666.
2021-07-13 18:36:25 -04:00
Erik Eckstein
eecb9fa975 SILModule: track opened archetypes per function.
In theory we could map opened archetypes per module because opened archetypes _should_ be unique across the module.
But currently in some rare cases SILGen re-uses the same opened archetype in multiple functions.
The fix is to add the SILFunction to the map's key.
That also requires that we update the map whenever instructions are moved from one function to another.

This fixes a compiler crash.

rdar://76916931
2021-04-23 14:13:26 +02:00
Slava Pestov
c88c96778f AST: Fix issue where SIL type lowering did not commute with subst()
SIL type lowering uses ReplaceOpaqueTypesWithUnderlyingTypes to lower away
opaque result types when the current context can "see" the underlying type
of the opaque result type.

To determine if an opaque result type could be replaced with its
underlying type, we check if every nominal type declaration appearing
in the "fully substituted" underlying type is visible from the current
context.

To form the "fully substituted" type, we first apply the substitution map
stored in the opaque type decl, which replaces the 'Self' generic parameter
with the actual concrete result type.

Then, we apply the substitution map stored in the archetype itself.

However, calling subst() on an opaque archetype modifies the substitution
map stored in the archetype.

What this means in practice is that if I have an opaque type declaration
that depends on its outer generic parameters, the fully-substituted type
that I see here depends on whether subst() has been performed or not.

For example, suppose I have the following:

    protocol P {}
    struct S : P {}
    extension P {
      func foo() -> some Any { return [self] }
    }

The opaque result decl for P.foo() maps 'Self' to 'Array<Self>'. Now,
imagine someone calls foo() with the substitution 'Self := S'. The
fully-substituted underlying type is 'Array<S>'. If 'S' is private, we
will decide that we cannot replace the opaque result type with its
underlying type. However, if we had performed the opaque type replacement
_before_ substituting 'Self := S', then we will end up replacing the
opaque result type.

To re-state this in yet another form, we want the following identity to
hold here:

    getLoweredType(opaqueType).subst(subMap) == getLoweredType(opaqueType.subst(subMap))

We can ensure that this holds by only applying the archetype's
substitutions _after_ we check visibility.

Fixes rdar://problem/76556368.
2021-04-15 22:23:07 -04:00
Slava Pestov
095824eb2a AST: Fix a bug in ReplaceOpaqueTypesWithUnderlyingTypes
There was a logic error in canSubstituteTypeInto(). An existing test
(test/IRGen/opaque_result_type_private_underlying.swift) triggered
this issue with a change I'm working on. Upon further inspection
I noticed the problem can already be triggered in the same test if
we build with -O.

The problem was that we were too agressive with substituting away
opaque types away in the body of an inlinable function in a
non-resilient build. While opaque types are not resilient in this
case, we still need to keep them around if the underlying type is
insufficiently visible.
2021-04-08 01:47:27 -04:00
Slava Pestov
111ae11302 SIL: Fix vtable layout with @usableFromInline methods
A @usableFromInline method is public at the ABI level, so we do not
need to introduce a new vtable entry.

Fixes <rdar://problem/74489981>.
2021-02-18 15:38:34 -05:00
Becca (née Brent) Royal-Gordon
6c015ca619 Merge pull request #35665 from beccadax/i-have-some-notes-for-you
Add support for access notes
2021-02-11 20:07:20 -08:00