Commit Graph

15 Commits

Author SHA1 Message Date
tbkka
f29d049d09 In backwards compatibility mode, be more permissive of Obj-C null references (#35825)
The new cast logic checks and aborts if a non-nullable pointer has a null value
at runtime.  However, because this was tolerated by the old casting logic, some
apps inadvertently rely on being able to cast such null references.

This change adds specific checks for null pointers in compatibility mode and
handles them similarly to the previous casting logic:
 * Casting to Obj-C or CF type: casting a null pointer succeeds
 * Casting to class-constrained existential: casting a null pointer succeeds
 * Casting to a Swift class type: cast fails without crashing
 * Bridging from Obj-C class to Swift struct type: cast fails without crashing
This also adds a guard to avoid trying to lookup the dynamic type of a null
class reference.

In non-compatibility mode, all of the above cause an immediate runtime crash.
The changes here are only intended to support existing binaries running against
new Swift runtimes.

Resolves rdar://72323929
2021-02-09 07:31:22 -08: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
5aaabfd5a2 Fix spelling of DynamicCastResult 2021-01-29 09:00:45 -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
Tim Kientzle
7d686cde77 Remove no-longer-needed external declaration of swift_dynamicCast_OLD 2021-01-05 18:02:09 -08:00
Tim Kientzle
e5c4e2d9f9 Add a hook for a future cast optimization
NSString->AnyHashable casts are remarkably common.  We
should make them faster someday.
2020-12-17 15:18:08 -08:00
Tim Kientzle
4d78405bc0 Conditionalize null ptr check when casting
Background: We've noticed a lot of problems from Obj-C APIs that returned null
even though they were declared to never do so.  These mismatches subvert Swift's
type system and can lead to hard-to-diagnose crashes much later in the program.
This fatal error was introduced into the primary casting function to help catch
such problems closer to the point where they occur so developers could more
easily identify and fix them.

However, there's been some concern about what this means for old binaries, so
we're considering a check here that would allow the old behavior in certain
cases yet to be determined.  This PR adds the framework for such a check.

Resolves rdar://72323929
2020-12-15 12:28:42 -08:00
tbkka
ef05015951 Remove old swift_dynamicCast implementation (#34789)
The new implementation has been the default for a while now and
seems to be stable and relatively bug-free.
2020-11-18 10:16:56 -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
John McCall
0fb407943f [NFC] Rename swift_runtime_unreachable to swift_unreachable and make it use LLVM's support when available. 2020-10-03 02:54:56 -04:00
Xiaodi Wu
45299909bc [runtime] Silence unreachable code warning (#33912) 2020-09-21 07:30:43 -04:00
tbkka
a61e7044c4 Witness tables have special ptrauth requirements (#33876) 2020-09-10 08:24:20 -07:00
Denys Shabalin
78c957f485 Move fallthrough one line up 2020-09-09 17:14:16 +02:00
Denys Shabalin
87906df2fe Add missing SWIFT_FALLTHROUGH to the tryCastToString 2020-09-07 16:09:38 +02:00
tbkka
524cfae1b2 [Dynamic Casting] Overhauled Runtime (#33561)
* Dynamic Cast Rework: Runtime

This is a completely refactored version of the core swift_dynamicCast
runtime method.

This fixes a number of bugs, especially in the handling of multiply-wrapped
types such as Optional within Any.  The result should be much closer to the
behavior specified by `docs/DynamicCasting.md`.

Most of the type-specific logic is simply copied over from the
earlier implementation, but the overall structure has been changed
to be uniformly recursive.  In particular, this provides uniform
handling of Optional, existentials, Any and other common "box"
types along all paths.  The consistent structure should also be
easier to update in the future with new general types.

Benchmarking does not show any noticable performance implications.

**Temporarily**, the old implementation is still available.  Setting the
environment variable `SWIFT_OLD_DYNAMIC_CAST_RUNTIME` before launching a program
will use the old runtime implementation.  This is only to facilitate testing;
once the new implementation is stable, I expect to completely remove the old
implementation.
2020-08-27 11:06:40 -07:00