Skip to content

[Sema] Restrict new optional-to-archetype casting behaviour to Swift 5 mode #19217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@ CHANGELOG
Swift 5.0
---------

* [SR-7251][]:

In Swift 5 mode, attempting to declare a static property with the same name as a
nested type is now always correctly rejected. Previously, it was possible to
perform such a redeclaration in an extension of a generic type.

For example:
```swift
struct Foo<T> {}
extension Foo {
struct i {}

// compiler error: Invalid redeclaration of 'i'
// (prior to Swift 5, this did not produce an error)
static var i: Int { return 0 }
}
```

* [SR-4248][]:

In Swift 5 mode, when casting an optional value to a generic placeholder type,
the compiler will be more conservative with the unwrapping of the value. The
result of such a cast now more closely matches the result you would get in a
non-generic context.

For example:
```swift
func forceCast<U>(_ value: Any?, to type: U.Type) -> U {
return value as! U
}

let value: Any? = 42
print(forceCast(value, to: Any.self))
// prints: Optional(42)
// (prior to Swift 5, this would print: 42)

print(value as! Any)
// prints: Optional(42)
```

* [SE-0227][]:

Key paths now support the `\.self` keypath, which is a `WritableKeyPath`
Expand Down Expand Up @@ -7190,3 +7230,5 @@ Swift 1.0
[SR-2388]: <https://bugs.swift.org/browse/SR-2388>
[SR-2394]: <https://bugs.swift.org/browse/SR-2394>
[SR-2608]: <https://bugs.swift.org/browse/SR-2608>
[SR-4248]: <https://bugs.swift.org/browse/SR-4248>
[SR-7251]: <https://bugs.swift.org/browse/SR-7251>
7 changes: 5 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3413,8 +3413,11 @@ namespace {
// If the destination value type is 'AnyObject' when performing a
// bridging operation, or if the destination value type could dynamically
// be an optional type, leave any extra optionals on the source in place.
if (isBridgeToAnyObject || destValueType->canDynamicallyBeOptionalType(
/* includeExistential */ false)) {
// Only apply the latter condition in Swift 5 mode to best preserve
// compatibility with Swift 4.1's casting behaviour.
if (isBridgeToAnyObject || (tc.Context.isSwiftVersionAtLeast(5) &&
destValueType->canDynamicallyBeOptionalType(
/*includeExistential*/ false))) {
auto destOptionalsCount = destOptionals.size() - destExtraOptionals;
if (srcOptionals.size() > destOptionalsCount) {
srcType = srcOptionals[destOptionalsCount];
Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/generic_casts.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// RUN: %target-swift-emit-silgen -module-name generic_casts -Xllvm -sil-full-demangle -enable-sil-ownership %s | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-runtime %s
// RUN: %target-swift-emit-silgen -swift-version 5 -module-name generic_casts -Xllvm -sil-full-demangle -enable-sil-ownership %s | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-runtime %s

protocol ClassBound : class {}
protocol NotClassBound {}
Expand Down
64 changes: 64 additions & 0 deletions test/SILGen/generic_casts_swift4.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

// RUN: %target-swift-emit-silgen -swift-version 4 -module-name generic_casts -Xllvm -sil-full-demangle -enable-sil-ownership %s | %FileCheck %s

// The below tests are to ensure we maintain compatibility with Swift 4.1's
// behaviour when casting to an archetype – the compiler assumes a non-optional
// archetype is non-optional, and therefore can unwrap the source.

// CHECK-LABEL: sil hidden @$s13generic_casts32optional_any_to_opaque_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_to_opaque_archetype<T>(_ x: Any?) -> T {
return x as! T
// CHECK: bb0([[RET:%.*]] : @trivial $*T, {{%.*}} : @trivial $*Optional<Any>):
// CHECK: unconditional_checked_cast_addr Any in {{%.*}} : $*Any to T in [[RET]] : $*T
}

// CHECK-LABEL: sil hidden @$s13generic_casts46optional_any_conditionally_to_opaque_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_conditionally_to_opaque_archetype<T>(_ x: Any?) -> T? {
return x as? T
// CHECK: checked_cast_addr_br take_always Any in {{%.*}} : $*Any to T in {{%.*}} : $*T
}

// CHECK-LABEL: sil hidden @$s13generic_casts32optional_any_is_opaque_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_is_opaque_archetype<T>(_ x: Any?, _: T) -> Bool {
return x is T
// CHECK: checked_cast_addr_br take_always Any in {{%.*}} : $*Any to T in {{%.*}} : $*T
}

// CHECK-LABEL: sil hidden @$s13generic_casts016optional_any_to_C17_opaque_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_to_optional_opaque_archetype<T>(_ x: Any?) -> T? {
return x as! T?
// CHECK: unconditional_checked_cast_addr Any in {{%.*}} : $*Any to T in {{%.*}} : $*T
}

// CHECK-LABEL: sil hidden @$s13generic_casts030optional_any_conditionally_to_C17_opaque_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_conditionally_to_optional_opaque_archetype<T>(_ x: Any?) -> T?? {
return x as? T?
// CHECK: checked_cast_addr_br take_always Any in {{%.*}} : $*Any to T in {{%.*}} : $*T
}

// CHECK-LABEL: sil hidden @$s13generic_casts016optional_any_is_C17_opaque_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_is_optional_opaque_archetype<T>(_ x: Any?, _: T) -> Bool {
return x is T?
// Because the levels of optional are the same, 'is' doesn't transform into an 'as?',
// so we just cast directly without digging into the optional operand.
// CHECK: checked_cast_addr_br take_always Optional<Any> in {{%.*}} : $*Optional<Any> to Optional<T> in {{%.*}} : $*Optional<T>
}

// CHECK-LABEL: sil hidden @$s13generic_casts31optional_any_to_class_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_to_class_archetype<T : AnyObject>(_ x: Any?) -> T {
return x as! T
// CHECK: unconditional_checked_cast_addr Any in {{%.*}} : $*Any to T in {{%.*}} : $*T
}

// CHECK-LABEL: sil hidden @$s13generic_casts45optional_any_conditionally_to_class_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_conditionally_to_class_archetype<T : AnyObject>(_ x: Any?) -> T? {
return x as? T
// CHECK: checked_cast_addr_br take_always Any in {{%.*}} : $*Any to T in {{%.*}} : $*T
}

// CHECK-LABEL: sil hidden @$s13generic_casts31optional_any_is_class_archetype{{[_0-9a-zA-Z]*}}F
func optional_any_is_class_archetype<T : AnyObject>(_ x: Any?, _: T) -> Bool {
return x is T
// CHECK: checked_cast_addr_br take_always Any in {{%.*}} : $*Any to T in {{%.*}} : $*T
}