Commit Graph

53 Commits

Author SHA1 Message Date
Rintaro Ishizaki
6f24696878 Merge pull request #81612 from calda/cal--trailing-comma-missing-from-6.1
Add trailing comma support in cases missing from Swift 6.1
2025-05-22 11:30:28 -07:00
Cal Stephens
1a3d71cc53 Add trailing comma support in cases missing from Swift 6.1 2025-05-19 09:19:49 -07:00
Erik Eckstein
a768037d0b IRGen: fix failing unconditional class casts
When unconditionally casting from a base to a final derived class, e.g. `base as! Derived`, the program did not abort with a trap.
Instead the resulting null-pointer caused a crash later in the program.
This fix inserts a trap condition for the failing case of such a cast.

rdar://151462303
2025-05-19 16:56:47 +02:00
Tony Allevato
d94bd80c62 Add support for raw identifiers.
Raw identifiers are backtick-delimited identifiers that can contain any
non-identifier character other than the backtick itself, CR, LF, or other
non-printable ASCII code units, and which are also not composed entirely
of operator characters.
2025-03-11 17:18:43 -04:00
Nate Cook
ff86a69dd7 Add a StdlibUnitTest modifier for requiring a stdlib version (#79838)
It would be great if this could also set the availability for the `code:`
block, but this at least cuts out some boilerplate.
2025-03-08 13:07:20 -06:00
Tim Kientzle
c262248a27 [ObjC Bridging] Consistently bridge block types verbatim
A `@convention(block)` closure in Swift is completely compatible with Objective-C
and does not need to be wrapped in a `__SwiftValue` box for use.

Previously, it was bridged verbatim when appearing by itself, but
could end up boxed when it went through array bridging.

The test verifies that:
* Objective-C does not see a `__SwiftValue` box
* Swift `type(of:)` does not see a `__SwiftValue` box
* Objective-C can actually call the closure

Resolves rdar://138132321
2024-11-08 13:49:21 -08:00
Tim Kientzle
4d1b944803 Just skip tests on unsupported versions 2024-04-10 16:04:10 -07:00
Tim Kientzle
5251750999 Conditionalize tests to supported OSes only 2024-04-10 15:30:29 -07:00
Tim Kientzle
810792843b Fill in extended existential support for type(of:) and mirror
type(of:) now returns the dynamic type of the contents of
an extended existential (just like it does for a regular existential)

Mirror can now reflect fields of a value stored inside an extended
existential (just like it can with a regular existential).  This
requires type(of:) support, since Mirror internals use that to
determine how to reflect a value.

Resolves rdar://102906195
2024-04-09 17:37:57 -07:00
Mishal Shah
af112c1591 Update the Swift version to 6.0 from 5.11 2024-02-19 17:47:16 -08:00
Tim Kientzle
ccc6ef8e68 Merge pull request #70978 from tbkka/tbkka-revert-PR68952
Revert #68952 [Casting] Make more casts look inside __SwiftValue
2024-02-02 11:03:04 -08:00
Tim Kientzle
62f8780bb0 Revert "Obj-C class metatypes should never satisfy Obj-C existentials"
This reverts commit aed05b9a36.

This needs some additional work.  Reverting out until it can be fixed.
2024-01-22 11:27:42 -08:00
Tim Kientzle
4ea5ed1309 Revert #68952 [Casting] Make more casts look inside __SwiftValue
Turns out that our Obj-C bridging relies on this inconsistent behavior,
so it can quite likely never be actually fixed.
2024-01-17 18:14:57 -08:00
Mike Ash
1770b3d3d7 [Test] Disable some runtime tests in back deployment testing.
These tests are testing changes that aren't present in older runtimes:

test/stdlib/SwiftValueNSObject.swift
test/stdlib/SwiftObjectNSObject.swift
test/stdlib/BridgeEquatableToObjC.swift

And this test looks for some wording that's different on older runtimes. This one already does availability checks, so we change the requirements to SwiftStdlib 5.11:

test/Casting/CastTraps.swift.gyb
2023-12-07 12:59:09 -05:00
Tim Kientzle
079c83ba9e Reword message a bit more based on review feedback 2023-11-08 11:04:43 -08:00
Tim Kientzle
2a0c5890fe Update tests to match the changed text 2023-11-07 11:19:12 -08: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
7af257a087 Only test NSObject interop on Obj-C platforms 2023-10-04 07:49:56 -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
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
Dario Rexin
2f8c1a402c [IRGen] Make pointers to accessor functions in layout strings relative (#64155)
* [IRGen] Make pointers to accessor functions in layout strings relative

rdar://106319336

Pointers embedded in static layout strings should always be relative, so layout strings can reside in read-only memory.

* Properly handle reference storage ownership

* Pass layout tag and metadata / type layout ppointers separately

* Layout string instantiation fully working

* Fix cases where hasLayoutString flag was not set when it should have

* Update include/swift/ABI/Metadata.h
2023-03-17 09:02:51 -07:00
swift-ci
4f3f73b99d Merge pull request #63500 from tbkka/tbkka-rdar90269352-unwrap-AnyHashable
Add a test to verify that AnyHashable gets eagerly unwrapped
2023-02-07 16:57:25 -08:00
Tim Kientzle
50745047e9 Include explicit Hashable/Equatable for test class 2023-02-07 14:21:28 -08:00
Tim Kientzle
f7ab029d51 Add a test to verify that AnyHashable gets eagerly unwrapped
To allow for better compiler optimizations, the
runtime should guarantee that AnyHashable does
not get boxed unnecessarily.

In particular, there was a concern that casting
an `AnyHashable` containing a class reference
to `AnyObject` would box the AnyHashable instead
of unwrapping it.

Further testing shows that the runtime is NOT
doing this unnecessary boxing at current.

Resolves rdar://90269352
2023-02-07 12:35:12 -08:00
Tim Kientzle
455b6d6980 Enable the tests for this particular cast when running against a sufficiently-new runtime 2023-02-06 15:58:26 -08:00
Tim Kientzle
b91f29dfcb SR-14635: __SwiftValue should be transparent to casting
This PR changes the casting machinery to avoid casting `__SwiftValue` boxes
directly.  This forces the caster to instead unwrap `__SwiftValue` boxes and
retry with the inner content.  This results in boxed values being cast like the
inner content.

This fixes the behavior in situations like the following:
```
   let t = ...
   let s = t as Any as! AnyObject
   // `s` is now a `__SwiftValue` box
   // Next line should be true iff t conforms to NSCopying
   // Prior to this change, it always succeeds
   s is NSCopying
```

After this change, the above cast succeeds only if `t` actually
conforms to `NSCopying`.

This is a follow-on to PR#37683.
Related to: SR-14635
2023-02-02 08:45:50 -08:00
Erik Eckstein
2527d3765e IRGen: fix how metadata is loaded for fast class casting
Pass the correct type to `emitHeapMetadataRefForHeapObject()`.
Fixes a runtime crash in case a class protocol is cast to a final swift class, where the actual object is an ObjectiveC class instance.

rdar://99626888
2022-09-22 08:46:18 +02:00
Anthony Latsis
a2ece9e298 Gardening: Migrate test suite to GH issues: Casting 2022-08-12 06:52:42 +03:00
Slava Pestov
c80d8e6b1c IRGen: Exclude classes defined in generic contexts from fast cast optimization
This is a regression from https://github.com/apple/swift/pull/41784.

Fixes rdar://problem/93822961.
2022-07-12 04:11:32 -04:00
Robert Widmann
541aa8dc7a Merge pull request #59507 from CodaFi/metastability 2022-06-16 20:37:13 -06:00
Robert Widmann
8bb5270d20 [Gardening] Remove a Copy-Pasted Header 2022-06-16 18:12:00 -06:00
Robert Widmann
c06e811b09 Fixup Extended Existential Metatype Casts
The fix here is two-fold:

1) Teach SILGen that it cannot use the scalar casting paths for extended existentials
2) Teach the runtime casting entrypoint to unwrap as much metatype structure as possible
   before arriving at a 'Self' type bound for the requirement checking paths.

The code here mirrors the destructuring check we're doing in remote mirrors.

rdar://95166916
2022-06-16 14:35:20 -06:00
Robert Widmann
9e4670a605 Implement Casting For Extended Existentials
Implement casting to and from extended existentials. This is done by slightly generalizing the conditional conformances checking infrastructure.

Unfortunately, casts for reference types and metatypes are unsound because IRGen is peepholing all non-opaque existential conversions with a helper. I’ll disable that in a follow-up.

rdar://92197049
2022-06-15 21:35:15 -06:00
Erik Eckstein
860078a79a IRGen: fast casting to final classes
When casting a class instance to a final class, we can directly compare the isa-pointer with address of the metadata.
This avoids a call to `swift_dynamicCastClass`.
It also avoids a call to the metadata accessor of the class (which calls `swift_getInitializedObjCClass`).
For comparing the metadata pointers it's not required that the metadata is fully initialized.
2022-04-07 16:36:21 +02:00
tbkka
184488d3f2 Conditionalize the fix for SR-14635 (#40822)
* Refactor Bincompat

Organize everything around internal functions that test for
a particular OS version.

Correctly handle cases where we don't know the version of the app.

Make all bincompat functions consistently return `true` for the
legacy semantics, `false` for new semantics.  Consistently name
them all to reflect this.

* Conditionalize the support for SR-14635

SR-14635 pointed out a hole in the updated dynamic casting logic
that allowed certain casts that should have been illegal.

In particular, when casting certain types to Obj-C protocols,
the Swift value gets boxed; we would permit the cast to succeed
whenever the resulting box satisfied the protocol.  For example,
this allowed any Swift value to be cast to `NSCopying` regardless of
whether or not it implemented the required `copy(with:)` method.

This was fixed in #37683 to reject such casts but of course some folks were
depending on this behavior to pass Swift data into Obj-C functions.
(The properly supported approach for passing arbitrary Swift data into
Obj-C functions is to cast the Swift value to `AnyObject`.)

This change makes that new behavior conditional.  For now,
the legacy semantics are enabled on Apple platforms and the
new semantics are in use everywhere else.  This will allow
us to gradually enable enforcement of the new behavior over
time.

* Just skip this test on Apple platforms, since it is inconsistently implemented there (and is therefore not really testable)
2022-01-14 11:56:25 -08:00
Karoy Lorentey
e2cfab4f28 [stdlib][test] Adopt availability macros in tests 2021-10-31 15:00:58 -07:00
Doug Gregor
eeeea49764 Remove -enable-experimental-concurrency almost everywhere. 2021-07-26 21:24:43 -07:00
Mike Ash
c3aa44368a [Runtime] Fix crash in protocol conformance checks on KVO artificial subclasses.
tryGetCompleteMetadataNonblocking crashes on artificial subclasses due to the NULL type descriptor. Explicitly check for artificial subclasses in getSuperclassForMaybeIncompleteMetadata and immediately return their Superclass field. Artificial subclasses are always fully initialized so we don't need to do anything special for them.

rdar://72583931
2021-06-17 14:45:58 -04:00
Mishal Shah
23c3b15f5f Support Xcode 13 beta
* Updating availability versions
* Remove all remaining overlays in stdlib/public/Darwin/*:
   - ObjectiveC
   - Dispatch
   - CoreFoundation
   - CoreGraphics
   - Foundation
2021-06-07 12:04:31 -07:00
tbkka
c1fce93d0e SR-14635: Casts to NSCopying should not always succeed (#37683)
* SR-14635: Casts to NSCopying should not always succeed

The runtime dynamic casting logic explores a variety of strategies for
each cast request.  One of the last options is to wrap the source
in a `__SwiftValue` box so it can bridge to Obj-C.  The previous
code was overly aggressive about such boxing; it performed the boxing
for any source type and only checked to verify that the `__SwiftValue`
box itself was compatible with the destination.

Among other oddities, this results in the behavior discussed
in SR-14635, where any Swift or Obj-C type will always successfully cast
to NSCopying because `__SwiftValue` is compatible with NSCopying.

This is actually two subtly different issues:

* Class types should not be subject to `__SwiftValue` boxing at all.
  Casting class types to class existentials is already handled elsewhere,
  so this function should just reject any source with class type.

* Non-class types should be boxed only when being assigned to
  an AnyObject (an "unconstrained class existential").  If
  the class existential has constraints, it is by definition
  a class-constrained existential which should not receive
  any non-class object.

To solve these, this PR disables `__SwiftValue` boxing in two cases:
1. If the source is a class (reference) type.
2. If the destination has constraints

Resolves SR-14635
Resolves rdar://78224322

* Avoid boxing class metatypes on Darwin

But continue boxing
 * Non-class metatypes on all platforms
 * All metatypes on non-Darwin platforms

Obj-C interop requires that we do not box class metatypes;
those must be usable as simple pointers when passed to Obj-C.
But no other metatype is object-compatible, so we have to
continue boxing everything else.

* Split out ObjC-specific test cases
2021-06-01 11:27:57 -07:00
Mishal Shah
3116eed2e4 Update the branch to build with Xcode 12.5 (#37046) 2021-04-23 16:24:11 -07:00
Nate Chandler
2decef1c58 [Test] Disabled Casting/Casts.swift for back_deployment_runtime.
rdar://76566242
2021-04-13 15:19:01 -07:00
tbkka
26ab27648e Casting from AnyHashable to AnyHashable should never create another wrapper (#36470)
* Casting from AnyHashable to AnyHashable should never create another wrapper

This adds a conformance for _HasCustomAnyHashableRepresentation to
AnyHashable that simply returns self.  This ensures that anytime
you try to create a new AnyHashable wrapper for an existing
AnyHashable, you just get back the original.

Resolves rdar://75180619

* Move the `Struct AnyHashable` change to `without-asserts` list

As suggested by @lorentey
2021-03-22 09:03:29 -07:00
Tim Kientzle
086dc14008 Don't link-check the Optional/AnyHashable workaround
This restores the earlier behavior of Optionals cast to
AnyHashable, so that [String?:Any] dictionaries cast
to [AnyHashable:Any] can be indexed by plain String
keys.

This is a little problematic because it's not consistent with the
compiler-optimized casts.

But the ability to index such dictionaries by plain String
keys seems important to preserve.  SR-9047 will expand
Optional/AnyHashable interoperability so that such
indexing works without this special case.

An earlier proposal would link-check this, changing the behavior
depending on the caller.  But it's not really workable to
change the behavior seen by intermediate frameworks depending
on the app they're being called by.
2021-02-01 09:54:03 -08:00
Tim Kientzle
067beb18f9 Accessing nil-keyed elements is trickier than I thought 2021-01-29 10:21:32 -08:00
Tim Kientzle
14df4d1310 Compatibility for Optional -> AnyHashable casts
As part of making casting more consistent, the behavior of Optional -> AnyHashable casts
was changed in some cases.  This PR provides a hook for re-enabling the old behavior
in certain contexts.

Background: Most of the time, casts from String? to AnyHashable get optimized
to just injects the String? into the AnyHashable, so the following
has long been true and remains true in Swift 5.4:
```
let s = "abc"
let o: String? = s

// Next test is true because s is promoted to Optional<String>
print(s == o)

// Next test is false: Optional<String> and String are different types
print(s as AnyHashable == o as AnyHashable)
```

But when casts ended up going through the runtime, Swift 5.3 behaved
differently, as you could see by casting a dictionary with `String?` keys (in
the generic array code, key and value casts always use the runtime logic).  In
the following code, both print statements behave differently in Swift 5.4 than
before:
```
let a: [String?:String] = ["Foo":"Bar"]
let b = a as [AnyHashable:Any]
print(b["Foo"] == "Bar")  // Works before Swift 5.4
print(b["Foo" as String?] == "Bar") // Works in Swift 5.4 and later
```

Old behavior: The `String?` keys would get unwrapped to `String` before being injected into AnyHashable.  This allows the first to work but strangely breaks the second.

New behavior: The `String?` keys do not get unwrapped.  This breaks the first but makes the second work.

TODO: The long-term goal, of course, is for `AnyHashable("Foo" as String?)` to
test equal to `AnyHashable("Foo")` (and hash the same, of course).  In that
case, all of the tests above will succeed.

Resolves rdar://73301155
2021-01-29 08:24:56 -08:00
tbkka
9e4bdb65be More dynamic casting tests (#34732)
Add regression tests for a number of casting cases that were fixed by recent work:

* SR-13812 (aka rdar://70999129)

Verify that the new casting logic correctly handles
a generic `is` test for a struct conforming to a protocol.

* SR-8964 (aka rdar://45217461)

Test casts from Any! holding Error types

* SR-1999 (aka rdar://48769924)

* SR-6279 (aka rdar://35321438

* rdar://59844232

* SR-12025

Note that SR-12025 is not actually fixed yet; this test is marked "expected failure" until it is.
2020-11-16 17:14:19 -08:00
tbkka
d92f1d58f8 Dynamic Casting: Properly unwrap existential metatype sources (#34469)
* Dynamic Casting: Properly unwrap existential metatype sources

Existential metatypes are really just existentials that hold metatypes.  As
such, they should be handled in the general casting logic in much the same way
as regular existentials: They should generally be ignored by most casting logic,
and unwrapped as necessary at the top level.

In particular, the previous code would fail to correctly handle the following
cast from an existential metatype (`AnyObject.Type`) to an existential
(`AnyObject`):
```
  class C {}
  let a = C.self as AnyObject.Type
  let b = a as! AnyObject
```
With the old code, `b` above would hold a reference to a `__SwiftValue` box
containing the type reference.  The correct result would simply store the type
reference directly in `b`.  These two are only really distinguishable in that
the correct form permits `a === b` to return `true`.

Fixes rdar://70582753

Note: This is not yet fully supported on Linux.  Basically, metatypes on Linux are not currently
fully compatible with reference-counted class pointers, which prevents us from
fully supporting metatype operations on Linux that we support on macOS.
2020-10-29 14:46:10 -07:00
tbkka
b0675c0dd6 [DynamicCast] Rely on runtime when casts can't be optimized (#33761)
* [DynamicCast] Rely on runtime when casts can't be optimized

The Swift compiler renders `as?` operations in Swift into variations of the
`checked_cast_br` instructions in SIL.  Subsequent optimization passes may alter
or eliminate these instructions.  Any remaining instructions after optimization
are translated by IRGen into runtime calls.

At least, that's the theory.  Unfortunately, the current IRGen logic does not
recognize all of the casting instruction variants and renders one particular
unrecognized variant as a simple nil load.  Specifically, this occurs for
optimized casts of metatypes to AnyObject:
```
  let a = Int.self
  let b = a as? AnyObject
  // b should not be nil here
```

This PR changes this case in IRGen to instead generate a call to
`swift_dynamicCast`, deferring this case to the runtime.

Future:  Someday, the compiler should be taught to correctly optimize
this cast away.
2020-10-28 15:59:13 -07:00
tbkka
7da78f77ba Verify that nil can cast to AnyHashable (#34434)
Optional conditionally conforms to Hashable, so for
example `Optional<Int>` must cast to AnyHashable
_even if it contains `nil`_.  This was broken up
through Swift 5.3 and was fixed by the new dynamic
cast runtime.

Add a test to ensure this stays fixed.
2020-10-28 11:48:11 -07:00