Skip to content

Commit 21169d1

Browse files
committed
Reinstate generalization of Optional.unsafelyUnwrapped
1 parent 480f870 commit 21169d1

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
lines changed

stdlib/public/core/Optional.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ extension Optional where Wrapped: ~Copyable {
320320
}
321321
}
322322

323-
extension Optional {
323+
extension Optional where Wrapped: ~Escapable {
324324
/// The wrapped value of this instance, unwrapped without checking whether
325325
/// the instance is `nil`.
326326
///
@@ -345,12 +345,14 @@ extension Optional {
345345
/// will never be equal to `nil` and only after you've tried using the
346346
/// postfix `!` operator.
347347
@inlinable
348+
@_preInverseGenerics
348349
@unsafe
349350
public var unsafelyUnwrapped: Wrapped {
350-
// FIXME: Generalize this for ~Escapable wrapped types. Unfortunately this
351-
// currently hides it from C++ interop, breaking the stdlib overlay.
352-
// (rdar://145800780)
351+
// FIXME: Generalize this for ~Copyable wrapped types. Note that the current
352+
// implementation is copying the value, so that generalization will need to
353+
// be emitted into clients -- `@_preInverseGenerics` will not cut it.
353354
@inline(__always)
355+
@lifetime(copy self)
354356
get {
355357
if let x = self {
356358
return x

test/Interop/CxxToSwiftToCxx/bridge-cxx-enum-back-to-cxx-execution.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %empty-directory(%t)
22
// RUN: split-file %s %t
33

4-
// RUN: %target-swift-frontend -parse-as-library %platform-module-dir/Swift.swiftmodule/%module-target-triple.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -typecheck -module-name Swift -parse-stdlib -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr -emit-clang-header-path %t/Swift.h -experimental-skip-all-function-bodies
4+
// RUN: %target-swift-frontend -parse-as-library %platform-module-dir/Swift.swiftmodule/%module-target-triple.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -typecheck -module-name Swift -parse-stdlib -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr -emit-clang-header-path %t/Swift.h -experimental-skip-all-function-bodies -enable-experimental-feature LifetimeDependence
55
// RUN: %target-swift-frontend -typecheck %t/use-cxx-types.swift -typecheck -module-name UseCxx -emit-clang-header-path %t/UseCxx.h -I %t -enable-experimental-cxx-interop -clang-header-expose-decls=all-public
66

77
// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-swift-cxx-types.cpp -I %t -o %t/swift-cxx-execution.o -g
@@ -11,6 +11,7 @@
1111
// RUN: %target-run %t/swift-cxx-execution
1212

1313
// REQUIRES: executable_test
14+
// REQUIRES: swift_feature_LifetimeDependence
1415

1516
//--- header.h
1617
enum class SomeEnum {

test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx-execution.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %empty-directory(%t)
22
// RUN: split-file %s %t
33

4-
// RUN: %target-swift-frontend -parse-as-library %platform-module-dir/Swift.swiftmodule/%module-target-triple.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -typecheck -module-name Swift -parse-stdlib -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr -emit-clang-header-path %t/Swift.h -experimental-skip-all-function-bodies
4+
// RUN: %target-swift-frontend -parse-as-library %platform-module-dir/Swift.swiftmodule/%module-target-triple.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -typecheck -module-name Swift -parse-stdlib -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr -emit-clang-header-path %t/Swift.h -experimental-skip-all-function-bodies -enable-experimental-feature LifetimeDependence
55

66
// RUN: %target-swift-frontend -typecheck %t/use-cxx-types.swift -typecheck -module-name UseCxx -emit-clang-header-path %t/UseCxx.h -I %t -enable-experimental-cxx-interop -clang-header-expose-decls=all-public
77

@@ -12,6 +12,7 @@
1212
// RUN: %target-run %t/swift-cxx-execution | %FileCheck %s
1313

1414
// REQUIRES: executable_test
15+
// REQUIRES: swift_feature_LifetimeDependence
1516

1617
//--- header.h
1718

test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -parse-as-library %platform-module-dir/Swift.swiftmodule/%module-target-triple.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -typecheck -module-name Swift -parse-stdlib -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr-or-stdlib -emit-clang-header-path %t/Swift.h -experimental-skip-all-function-bodies
2+
// RUN: %target-swift-frontend -parse-as-library %platform-module-dir/Swift.swiftmodule/%module-target-triple.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -typecheck -module-name Swift -parse-stdlib -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr-or-stdlib -emit-clang-header-path %t/Swift.h -experimental-skip-all-function-bodies -enable-experimental-feature LifetimeDependence
33
// RUN: %FileCheck %s < %t/Swift.h
44

55
// RUN: %check-interop-cxx-header-in-clang(%t/Swift.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-private-field -Wno-unused-function -Wc++98-compat-extra-semi)
66
// RUN: %check-interop-cxx-header-in-clang(%t/Swift.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-private-field -Wno-unused-function -Wc++98-compat-extra-semi -DDEBUG=1)
77

8+
// REQUIRES: swift_feature_LifetimeDependence
9+
810
// CHECK: namespace swift SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("swift") {
911

1012
// CHECK: template<class T_0_0>

test/api-digester/Outputs/stability-stdlib-source-base.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ Struct CollectionOfOne is now with @_addressableForDependencies
367367
Protocol CodingKey has added inherited protocol SendableMetatype
368368
Protocol Error has added inherited protocol SendableMetatype
369369

370+
Accessor Optional.unsafelyUnwrapped.Get() has generic signature change from <Wrapped> to <Wrapped where Wrapped : ~Escapable>
370371
Accessor UnsafeBufferPointer.indices.Get() has generic signature change from <Element> to <Element where Element : ~Copyable>
371372
Accessor UnsafeMutableBufferPointer.indices.Get() has generic signature change from <Element> to <Element where Element : ~Copyable>
372373

test/api-digester/stability-stdlib-abi-without-asserts.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,10 @@ Struct EnumeratedSequence has added a conformance to an existing protocol Bidire
837837
Struct EnumeratedSequence has added a conformance to an existing protocol Collection
838838
Struct EnumeratedSequence has added a conformance to an existing protocol RandomAccessCollection
839839

840+
Accessor Optional.unsafelyUnwrapped.Get() has generic signature change from <Wrapped> to <Wrapped where Wrapped : ~Escapable>
841+
Accessor Optional.unsafelyUnwrapped.Get() has mangled name changing from 'Swift.Optional.unsafelyUnwrapped.getter : A' to '(extension in Swift):Swift.Optional< where A: ~Swift.Escapable>.unsafelyUnwrapped.getter : A'
842+
Var Optional.unsafelyUnwrapped has mangled name changing from 'Swift.Optional.unsafelyUnwrapped : A' to '(extension in Swift):Swift.Optional< where A: ~Swift.Escapable>.unsafelyUnwrapped : A'
843+
Var Optional.unsafelyUnwrapped is now with @_preInverseGenerics
840844
Accessor Optional._unsafelyUnwrappedUnchecked.Get() has generic signature change from <Wrapped> to <Wrapped where Wrapped : ~Escapable>
841845
Accessor Optional._unsafelyUnwrappedUnchecked.Get() has mangled name changing from 'Swift.Optional._unsafelyUnwrappedUnchecked.getter : A' to '(extension in Swift):Swift.Optional< where A: ~Swift.Escapable>._unsafelyUnwrappedUnchecked.getter : A'
842846
Var Optional._unsafelyUnwrappedUnchecked has mangled name changing from 'Swift.Optional._unsafelyUnwrappedUnchecked : A' to '(extension in Swift):Swift.Optional< where A: ~Swift.Escapable>._unsafelyUnwrappedUnchecked : A'

0 commit comments

Comments
 (0)