mirror of
https://github.com/apple/swift.git
synced 2026-06-27 12:25:55 +02:00
f08823757a
Stop creating ImplicitlyUnwrappedOptional<T> so that we can remove it
from the type system.
Enable the code that generates disjunctions for Optional<T> and
rewrites expressions based on the original declared type being 'T!'.
Most of the changes supporting this were previously merged to master,
but some things were difficult to merge to master without actually
removing IUOs from the type system:
- Dynamic member lookup and dynamic subscripting
- Changes to ensure the bridging peephole still works
Past commits have attempted to retain as much fidelity with how we
were printing things as possible. There are some cases where we still
are not printing things the same way:
- In diagnostics we will print '?' rather than '!'
- Some SourceKit and Code Completion output where we print a Type
rather than Decl.
Things like module printing via swift-ide-test attempt to print '!'
any place that we now have Optional types that were declared as IUOs.
There are some diagnostics regressions related to the fact that we can
no longer "look through" IUOs. For the same reason some output and
functionality changes in Code Completion. I have an idea of how we can
restore these, and have opened a bug to investigate doing so.
There are some small source compatibility breaks that result from
this change:
- Results of dynamic lookup that are themselves declared IUO can in
rare circumstances be inferred differently. This shows up in
test/ClangImporter/objc_parse.swift, where we have
var optStr = obj.nsstringProperty
Rather than inferring optStr to be 'String!?', we now infer this to
be 'String??', which is in line with the expectations of SE-0054.
The fact that we were only inferring the outermost IUO to be an
Optional in Swift 4 was a result of the incomplete implementation of
SE-0054 as opposed to a particular design. This should rarely cause
problems since in the common-case of actually using the property rather
than just assigning it to a value with inferred type, we will behave
the same way.
- Overloading functions with inout parameters strictly by a difference
in optionality (i.e. Optional<T> vs. ImplicitlyUnwrappedOptional<T>)
will result in an error rather than the diagnostic that was added
in Swift 4.1.
- Any place where '!' was being used where it wasn't supposed to be
allowed by SE-0054 will now treat the '!' as if it were '?'.
Swift 4.1 generates warnings for these saying that putting '!'
in that location is deprecated. These locations include for example
typealiases or any place where '!' is nested in another type like
`Int!?` or `[Int!]`.
This commit effectively means ImplicitlyUnwrappedOptional<T> is no
longer part of the type system, although I haven't actually removed
all of the code dealing with it yet.
ImplicitlyUnwrappedOptional<T> is is dead, long live implicitly
unwrapped Optional<T>!
Resolves rdar://problem/33272674.
68 lines
3.4 KiB
Swift
68 lines
3.4 KiB
Swift
// RUN: %empty-directory(%t)
|
|
|
|
// FIXME: BEGIN -enable-source-import hackaround
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t %clang-importer-sdk-path/swift-modules/Foundation.swift
|
|
// FIXME: END -enable-source-import hackaround
|
|
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -typecheck %s -verify
|
|
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) %s -dump-ast -verify 2>&1 | %FileCheck %s
|
|
|
|
// REQUIRES: objc_interop
|
|
|
|
import Foundation
|
|
|
|
func testDowncastObjectToArray(obj: AnyObject, objImplicit: AnyObject!) {
|
|
var nsstrArr1 = (obj as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}}{{39-40=}}
|
|
var strArr1 = (obj as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}}{{35-36=}}
|
|
|
|
var nsstrArr2 = (objImplicit as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}}{{47-48=}}
|
|
var strArr2 = (objImplicit as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}}{{43-44=}}
|
|
}
|
|
|
|
func testArrayDowncast(arr: [AnyObject], arrImplicit: [AnyObject]!) {
|
|
var nsstrArr1 = (arr as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}} {{39-40=}}
|
|
var strArr1 = (arr as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}} {{35-36=}}
|
|
|
|
var nsstrArr2 = (arrImplicit as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}} {{47-48=}}
|
|
var strArr2 = (arrImplicit as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}} {{43-44=}}
|
|
}
|
|
|
|
func testDowncastNSArrayToArray(nsarray: NSArray) {
|
|
_ = nsarray as! [NSString]
|
|
_ = nsarray as! [String]
|
|
}
|
|
|
|
// CHECK-LABEL: testDowncastOptionalObject
|
|
func testDowncastOptionalObject(obj: AnyObject?!) -> [String]? {
|
|
// CHECK: (optional_evaluation_expr implicit type='[String]?'
|
|
// CHECK-NEXT: (inject_into_optional implicit type='[String]?'
|
|
// CHECK: (forced_checked_cast_expr type='[String]'{{.*value_cast}}
|
|
// CHECK: (bind_optional_expr implicit type='AnyObject'
|
|
// CHECK-NEXT: (force_value_expr implicit type='AnyObject?'
|
|
// CHECK-NEXT: (declref_expr type='AnyObject??'
|
|
return obj as! [String]?
|
|
}
|
|
|
|
// CHECK-LABEL: testDowncastOptionalObjectConditional
|
|
func testDowncastOptionalObjectConditional(obj: AnyObject?!) -> [String]?? {
|
|
// CHECK: (optional_evaluation_expr implicit type='[String]??'
|
|
// CHECK-NEXT: (inject_into_optional implicit type='[String]??'
|
|
// CHECK-NEXT: (optional_evaluation_expr implicit type='[String]?'
|
|
// CHECK-NEXT: (inject_into_optional implicit type='[String]?'
|
|
// CHECK-NEXT: (bind_optional_expr implicit type='[String]'
|
|
// CHECK-NEXT: (conditional_checked_cast_expr type='[String]?' {{.*value_cast}} writtenType='[String]?'
|
|
// CHECK-NEXT: (bind_optional_expr implicit type='AnyObject'
|
|
// CHECK-NEXT: (bind_optional_expr implicit type='AnyObject?'
|
|
// CHECK-NEXT: (declref_expr type='AnyObject??'
|
|
return obj as? [String]?
|
|
}
|
|
|
|
// Do not crash examining the casted-to (or tested) type if it is
|
|
// invalid (null or error_type).
|
|
class rdar28583595 : NSObject {
|
|
public func test(i: Int) {
|
|
if i is Array {} // expected-error {{generic parameter 'Element' could not be inferred}}
|
|
// expected-note@-1 {{explicitly specify the generic arguments to fix this issue}}
|
|
}
|
|
}
|