Commit Graph

3163 Commits

Author SHA1 Message Date
Tim Kientzle
6fd39741d6 Fix a number of issues (review feedback)
* Fix how we were looking for common parent class
* Use a constant hashValue for types that are Equatable but not Hashable
2023-10-30 15:57:47 -07:00
Doug Gregor
1b42ca3673 Handle any Error and Never thrown error types during runtime uniquing.
When forming runtime metadata for a function type that has either `any
Error` or `Never` as the specified thrown error type, drop the thrown
error type and normalize down to (untyped) `throws` or non-throwing,
as appropriate.
2023-10-29 19:29:09 -07:00
Doug Gregor
40e07cf900 [Typed throws] IR generation and runtime support for function type metadata
Extend function type metadata with an entry for the thrown error type,
so that thrown error types are represented at runtime as well. Note
that this required the introduction of "extended" function type
flags into function type metadata, because we would have used the last
bit. Do so, and define one extended flag bit as representing typed
throws.

Add `swift_getExtendedFunctionTypeMetadata` to the runtime to build
function types that have the extended flags and a thrown error type.
Teach IR generation to call this function to form the metadata, when
appropriate.

Introduce all of the runtime mangling/demangling support needed for
thrown error types.
2023-10-29 09:12:32 -07:00
Doug Gregor
e82854d860 Reimplement TargetFunctionTypeMetadata using TrailingObjects
The layout is the same, but this eliminates the handwritten address
computations.
2023-10-29 09:12:32 -07:00
Doug Gregor
4da1032f93 Add name mangling support for functions with a thrown error type 2023-10-29 09:12:32 -07:00
Tim Kientzle
a840c69392 Use Swift Equatable and Hashable conformances from ObjC
If a Swift type implements Equatable and/or Hashable and
we then pass that object into ObjC, we want ObjC
`isEqual:` and `hashValue` to use that.  This allows
ObjC code to build ObjC collections of Swift objects.

* Support for Hashable struct/enum types was implemented in #4124
* Support for Equatable struct/enum types was implemented in #68720
* This implements support for Hashable and Equatable _class_ types

Caveats:
1. This does a lot of dynamic lookup work for each operation, so is
   inherently rather slow.  Unlike the struct/enum case, there is no convenient
   place to cache the conformance information, so it's not clear that there is a
   viable way to make it significantly faster.
2. This is a behavioral change to low-level support code.  There is a
   risk of breaking code that may be relying on the old behavior.
2023-10-27 12:01:44 -07:00
Tim Kientzle
aed05b9a36 Obj-C class metatypes should never satisfy Obj-C existentials
Given this
```
@objc protocol P { func f() }
class C: P { func f() {} }
```
the casts `C.self is any P` and `C.self as? any P` should always fail,
because the metatype of `C.self` does not have an `f()` implementation.

These casts previously succeeded because the runtime implementation
deferred to Obj-C's `[o conformsToProtocol:p]`, and that behaves
differently depending on whether `o` is a class instance or
a Class itself.

Since these casts should never succeed, I've just modified the
Swift runtime logic to fail whenever the source of the cast
is an Obj-C Class and the target is a protocol existential.

Resolves: rdar://106973771
2023-10-26 11:57:56 -07:00
Tim Kientzle
3bdbbb5048 Merge pull request #68952 from tbkka/tbkka-rdar111422725-more-SwiftValue-casting
[Casting] Make more casts look inside `__SwiftValue`
2023-10-24 07:15:57 -07:00
Mike Ash
fc6cfbbf32 [Runtime] Have MetadataCacheKey::operator== try memcmp before deeper comparison.
Experiment shows that the vast majority (~99.99%) of keys with equal hashes have equal bytes. Fast-path that case by doing a memcmp before doing the deeper comparison.

rdar://110478978
2023-10-23 13:04:28 -04:00
Dario Rexin
0c36e44f2e [Runtime] Cleanup function names in BytecodeLayouts.cpp 2023-10-13 10:22:02 -07:00
Alejandro Alonso
de8233a5d6 Revert "Merge pull request #68844 from Azoy/gather-no-parameters"
This reverts commit ccaf427fc3, reversing
changes made to bf45c55530.
2023-10-10 19:36:20 -07:00
Alejandro Alonso
ccaf427fc3 Merge pull request #68844 from Azoy/gather-no-parameters
[Runtime] Wrap _checkGenericRequirements for _instantiateCheckedGenericMetadata
2023-10-10 11:08:33 -07:00
Alejandro Alonso
3ddec4fd83 Wrap _checkGenericRequirements instead for _instantiateCheckedGenericMetadata 2023-10-09 15:02:29 -07:00
Arnold Schwaighofer
b0424759d7 Add support for objective c protocol symbolic references
Using symbolic references instead of a text based mangling avoids the
expensive type descriptor scan when objective c protocols are requested.

rdar://111536582
2023-10-05 13:11:32 -07:00
Tim Kientzle
c078479ce9 [Casting] Make more casts look inside __SwiftValue
The following sequence of casts would previously succeed
```
struct S {}
let s = S() as AnyObject
s as? NSObject
```
The final cast here should fail, since `S` clearly is not a
subclass of NSObject.  But it would previously succeed because
the `as AnyObject` would package the struct into an ObjC-compatible
`__SwiftValue` class.  This latter is an NSObject subclass.

This bug was fixed in the main `swift_dynamicCast` runtime function
some time ago, but not in the `swift_dynamicCastObjCClass` which
is chosen by IRGen to optimize casts to ObjC class types.
This PR changes the latter to test for `__SwiftValue` and fall
back to the former in that case in order to get the correct
handling.  Falling back to `swift_dynamicCast` also ensures that
the contents of the `__SwiftValue` are correctly unwrapped/bridged/etc
as necessary to fully support Swift casting semantics.

Resolves: rdar://111422725

TODO: I've left an XFAILed test here about the behavior of `type(of:)`
with `__SwiftValue` boxes.
2023-10-03 14:50:23 -07:00
Tony Allevato
5f5b24f96e [C++20] Make operator{==,!=}s const.
In C++20, the compiler will synthesize a version of the operator
with its arguments reversed to ease commutativity. This reversed
version is ambiguous with the hand-written operator when the
argument is const but `this` isn't.
2023-10-03 17:10:57 -04:00
Mike Ash
e2b4d2f465 [Runtime] Fix lazy ivar list copying.
The `ivar` reference still pointed to the original list, so the first modification went to the wrong place. Change `ivar` to a pointer, and re-point it when we copy the list.

rdar://116339597
2023-10-02 16:06:01 -04:00
Tim Kientzle
b35987fdcd Merge pull request #68720 from tbkka/tbkka-SwiftValue-Equatable
Make ObjC isEqual: delegate to Equatable when Hashable isn't available
2023-09-29 12:28:56 -07:00
Tim Kientzle
b1b78184dc Make the caches be uintptr_t
This simplified a lot of things.  (Thanks @MikeAsh for the
suggestion!)

Also reorganized the code a bit.
2023-09-28 12:02:31 -07:00
Mike Ash
641870a205 [Runtime] Lazily copy ivar list in initObjCClass.
There's often nothing that needs to be fixed up in the ivar list. In that case, we can avoid copying it. This saves time and memory, and allows the class rodata to be in immutable memory.

rdar://116189946
2023-09-28 14:19:04 -04:00
Tim Kientzle
5cff70337a Cache Equatable conformance if Hashable is not present 2023-09-27 19:15:37 -07:00
Alastair Houghton
c9efa69ef1 [Linux] Make the backtracing symbol reference conditional.
On the LSan builds, we disable the backtracer, which results in a
link error because we completely remove the code from libswiftCore.

rdar://116105222
2023-09-27 09:56:22 +01:00
Alastair Houghton
62c4999249 Merge pull request #68669 from al45tair/eng/PR-115774613
[Linux] Force the inclusion of the backtracing code when static linking.
2023-09-26 22:16:06 +01:00
Tim Kientzle
659d1cfb13 Make ObjC isEqual: delegate to Equatable when Hashable isn't available
When a Swift struct gets bridged to Obj-C, we box it into an opaque
`_SwiftValue` Obj-C object.  This object previously supported the
Obj-C `isEqual:` and `hash` selectors by dispatching to the Swift
Hashable conformance, if present.

This does not work if the Swift struct conforms to Equatable but
does not conform to Hashable.  This case seems to have been
overlooked in PR #4124.

This PR extends the earlier work to support `isEqual:` by
first checking for a Hashable conformance, then falling back
on an Equatable conformance if there is no Hashable conformance.

Resolves rdar://114294889
2023-09-22 17:58:22 -07:00
Slava Pestov
156fd080e7 Merge pull request #68700 from slavapestov/fix-rdar115459973
ASTDemangler: Fix round-tripping of SILBoxTypeWithLayout
2023-09-22 19:25:11 -04:00
Slava Pestov
57b702be07 ASTDemangler: Fix round-tripping of SILBoxTypeWithLayout
The more awkward setup with pushGenericParams()/popGenericParams()
is required so that when demangling requirements and field types
we get the correct pack-ness for each type parameter. The pack-ness
is encoded as part of the generic signature, and not as part of
the type parameter mangling itself.

Fixes the ASTDemangler issue from rdar://problem/115459973.
2023-09-22 11:27:18 -04:00
Dario Rexin
70e7df902e Merge pull request #68675 from drexin/wip-remove-nul
[IRGen+Runtime] Remove unnecessary null terminator in layout strings
2023-09-21 14:56:07 -07:00
Dario Rexin
75f6103898 [IRGen+Runtime] Remove unnecessary null terminator in layout strings
The last offset also signals the end of the layout string, so the null terminator is unnecessary
2023-09-21 10:46:19 -07:00
Dario Rexin
56d0975668 [Runtime] Add layout string handling in swift_arrayAssignWithCopyFrontToBack
By using a specialize function, we only call through the witness table and fetch the layout string once for the whoe buffer, instead of once per element.
2023-09-21 09:52:46 -07:00
Alastair Houghton
598bce2404 [Linux] Force the inclusion of the backtracing code when static linking.
When we're statically linking the standard library, we need to force
the inclusion of the backtracing code in the runtime, otherwise we don't
get on-crash backtraces.

Also, add a test to make sure that this works.

rdar://115774613
2023-09-21 15:03:51 +01:00
Alejandro Alonso
60774fb722 Merge pull request #68450 from Azoy/fix-array-like
[IRGen] Some fixes for raw layout types and noncopyable types in the stdlib
2023-09-13 21:49:48 -07:00
Alejandro Alonso
ab143ee3ee Some fixes for raw layout types and noncopyable types in the stdlib 2023-09-11 20:45:09 -07:00
Dario Rexin
c1b07f0f99 Merge pull request #68283 from drexin/wip-narf
[Runtime] Use handleRefCountsAssignWithCopy in swift_generic_assignWi…
2023-09-09 23:09:31 -07:00
Alejandro Alonso
1375ce6247 Merge pull request #68359 from Azoy/packs-pack-packs
[Runtime] Add a function to check type creation
2023-09-07 07:24:55 -07:00
Alejandro Alonso
80187e8096 Expect caller heap pack and rename
Fix comment
2023-09-06 21:37:00 -07:00
Alejandro Alonso
3dbad6e1fd Add a runtime function to check type creation 2023-09-06 11:32:22 -07:00
Evan Wilde
176bf3417e Merge pull request #65076 from etcwilde/ewilde/metadata-cleanup
Replace mismatched `delete` with `swift_cxx_deleteObject`
2023-09-01 20:31:29 -07:00
Dario Rexin
ee28da2bf2 [Runtime] Use handleRefCountsAssignWithCopy in swift_generic_assignWithCopy
rdar://114840977

This was accidentally committed after debugging an issue
2023-09-01 14:53:52 -07:00
Dario Rexin
6e3d1ae726 [Runtime] Check if objc pointers are tagged before masking
In layout string value witnesses runtime functions, if an objc pointer is tagged, there is no ref counting necessary.
2023-08-30 18:50:38 -07:00
Dario Rexin
8fe4c77b3e [Runtime] Properly mask pointers before ref counting and other fixes
The fixes are all around enum support. Some tag comparisons and pointer masking were incorrect and led to crashes.

rdar://114575149
2023-08-30 17:54:47 -07:00
Saleem Abdulrasool
898f9efe3e Merge pull request #68099 from hjyamauchi/abort
Fix a fatal error not-terminating issue in Windows
2023-08-27 12:08:32 -07:00
Hiroshi Yamauchi
5fc3ad3a81 Fix a fatal error not-terminating issue in Windows.
Fix the issue that fatal errors in certain cases don't terminate the
process and the process keeps running in Windows by disabling the
exception swallowing that supressed the illegal instruction exceptions
coming from llvm.trap.
2023-08-25 13:04:36 -07:00
Dario Rexin
030daee5ad Merge pull request #68042 from drexin/wip-layout-strings-work
Improve layout strings runtime code and fix several issues
2023-08-24 16:03:34 -07:00
Kshitij Jain
d5a3e2e630 [AutoDiff] Fixes memory leaks in autodiff linear map context allocation builtins (#67944)
When the differentiating a function containing loops, we allocate a linear map context object on the heap. This context object may store non-trivial objects, such as closures, that need to be released explicitly. Fix the autodiff linear map context allocation builtins to correctly release such objects and not just free the memory they occupy.
2023-08-24 13:57:10 -07:00
Dario Rexin
e3a1bac859 [Runtime] Use copy of addrOffset in single payload assign with copy 2023-08-24 11:01:14 -07:00
Dario Rexin
21f4064380 [Runtime] Fix singlePayloadEnumSimpleAssignWithCopy 2023-08-21 10:59:15 -07:00
Dario Rexin
fd967fd9e3 [Runtime] Properly handle boxed references in layout string instantiation 2023-08-21 10:58:25 -07:00
Dario Rexin
7e3f56de24 [Runtime] Fix generic existentials in layout strings 2023-08-21 10:57:33 -07:00
Dario Rexin
56048ac19f [Runtime+IRGen] Fix existential offset for multiple protocol witnesses 2023-08-21 10:57:06 -07:00
Dario Rexin
8b3aaf5423 [Runtime] Compute end before looping over single payload ref count 2023-08-21 10:56:42 -07:00