Skip to content

Commit 05a5bc4

Browse files
authored
Merge pull request #72608 from tshortli/non-copyable-generics-2
AST/Frontend/stdlib: Fix condfails for NoncopyableGenerics and IsolatedAny
2 parents fc6011c + 590d335 commit 05a5bc4

17 files changed

+80
-23
lines changed

include/swift/AST/PrintOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ struct PrintOptions {
385385
/// Suppress Noncopyable generics.
386386
bool SuppressNoncopyableGenerics = false;
387387

388+
/// Suppress printing of `borrowing` and `consuming`.
389+
bool SuppressNoncopyableOwnershipModifiers = false;
390+
388391
/// List of attribute kinds that should not be printed.
389392
std::vector<AnyAttrKind> ExcludeAttrList = {
390393
DeclAttrKind::Transparent, DeclAttrKind::Effects,

include/swift/Basic/Features.def

+6
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ EXPERIMENTAL_FEATURE(Embedded, true)
319319
/// Enables noncopyable generics
320320
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(NoncopyableGenerics, true)
321321

322+
// Alias for NoncopyableGenerics
323+
EXPERIMENTAL_FEATURE(NoncopyableGenerics2, true)
324+
322325
/// Allow destructuring stored `let` bindings in structs.
323326
EXPERIMENTAL_FEATURE(StructLetDestructuring, true)
324327

@@ -358,6 +361,9 @@ EXPERIMENTAL_FEATURE(ClosureIsolation, true)
358361
// Enable isolated(any) attribute on function types.
359362
CONDITIONALLY_SUPPRESSIBLE_EXPERIMENTAL_FEATURE(IsolatedAny, true)
360363

364+
// Alias for IsolatedAny
365+
EXPERIMENTAL_FEATURE(IsolatedAny2, true)
366+
361367
// Enable usability improvements for global-actor-isolated types.
362368
EXPERIMENTAL_FEATURE(GlobalActorIsolatedTypesUsability, false)
363369

lib/AST/ASTPrinter.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,16 @@ class PrintAST : public ASTVisitor<PrintAST> {
11381138
Printer.callPrintDeclPre(D, Options.BracketOptions);
11391139

11401140
if (Options.PrintCompatibilityFeatureChecks) {
1141-
printWithCompatibilityFeatureChecks(Printer, Options, D, [&]{
1141+
printWithCompatibilityFeatureChecks(Printer, Options, D, [&] {
1142+
// If we are in a scope where non-copyable generics are being suppressed
1143+
// and we are also printing a decl that has @_preInverseGenerics, make
1144+
// sure we also suppress printing ownership modifiers that were added
1145+
// to satisfy the requirements of non-copyability.
1146+
llvm::SaveAndRestore<bool> scope(
1147+
Options.SuppressNoncopyableOwnershipModifiers,
1148+
Options.SuppressNoncopyableGenerics &&
1149+
D->getAttrs().hasAttribute<PreInverseGenericsAttr>());
1150+
11421151
ASTVisitor::visit(D);
11431152
});
11441153
} else {
@@ -3123,9 +3132,12 @@ static void suppressingFeatureAssociatedTypeImplements(PrintOptions &options,
31233132
static void suppressingFeatureNoncopyableGenerics(
31243133
PrintOptions &options,
31253134
llvm::function_ref<void()> action) {
3135+
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
3136+
options.ExcludeAttrList.push_back(DeclAttrKind::PreInverseGenerics);
31263137
llvm::SaveAndRestore<bool> scope(
31273138
options.SuppressNoncopyableGenerics, true);
31283139
action();
3140+
options.ExcludeAttrList.resize(originalExcludeAttrCount);
31293141
}
31303142

31313143
/// Suppress the printing of a particular feature.
@@ -3680,10 +3692,14 @@ static void printParameterFlags(ASTPrinter &printer,
36803692
printer.printKeyword("inout", options, " ");
36813693
break;
36823694
case ParamSpecifier::Borrowing:
3683-
printer.printKeyword("borrowing", options, " ");
3695+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3696+
printer.printKeyword("borrowing", options, " ");
3697+
}
36843698
break;
36853699
case ParamSpecifier::Consuming:
3686-
printer.printKeyword("consuming", options, " ");
3700+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3701+
printer.printKeyword("consuming", options, " ");
3702+
}
36873703
break;
36883704
case ParamSpecifier::LegacyShared:
36893705
printer.printKeyword("__shared", options, " ");

lib/AST/FeatureSet.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ static bool usesFeatureRawLayout(Decl *decl) {
506506
UNINTERESTING_FEATURE(Embedded)
507507

508508
static bool usesFeatureNoncopyableGenerics(Decl *decl) {
509+
if (decl->getAttrs().hasAttribute<PreInverseGenericsAttr>())
510+
return true;
511+
509512
if (auto *valueDecl = dyn_cast<ValueDecl>(decl)) {
510513
if (isa<StructDecl, EnumDecl, ClassDecl>(decl)) {
511514
auto *nominalDecl = cast<NominalTypeDecl>(valueDecl);
@@ -568,6 +571,8 @@ static bool usesFeatureNoncopyableGenerics(Decl *decl) {
568571
return !inverseReqs.empty();
569572
}
570573

574+
UNINTERESTING_FEATURE(NoncopyableGenerics2)
575+
571576
static bool usesFeatureStructLetDestructuring(Decl *decl) {
572577
auto sd = dyn_cast<StructDecl>(decl);
573578
if (!sd)
@@ -676,6 +681,8 @@ static bool usesFeatureIsolatedAny(Decl *decl) {
676681
});
677682
}
678683

684+
UNINTERESTING_FEATURE(IsolatedAny2)
685+
679686
static bool usesFeatureGlobalActorIsolatedTypesUsability(Decl *decl) {
680687
return false;
681688
}

lib/Frontend/CompilerInvocation.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,12 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
883883
#else
884884
Opts.enableFeature(*feature);
885885
#endif
886+
887+
if (*feature == Feature::NoncopyableGenerics2)
888+
Opts.enableFeature(Feature::NoncopyableGenerics);
889+
890+
if (*feature == Feature::IsolatedAny2)
891+
Opts.enableFeature(Feature::IsolatedAny);
886892
}
887893

888894
// Hack: In order to support using availability macros in SPM packages, we

stdlib/cmake/modules/SwiftSource.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ function(_compile_swift_files
612612
list(APPEND swift_flags "-experimental-hermetic-seal-at-link")
613613
endif()
614614

615-
list(APPEND swift_flags "-enable-experimental-feature" "NoncopyableGenerics")
615+
list(APPEND swift_flags "-enable-experimental-feature" "NoncopyableGenerics2")
616616

617617
if(SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES)
618618
list(APPEND swift_flags "-enable-experimental-feature" "NonescapableTypes")

stdlib/public/Concurrency/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ endif()
6262

6363
list(APPEND SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS
6464
"-enable-experimental-feature"
65-
"IsolatedAny"
65+
"IsolatedAny2"
6666
)
6767

6868
list(APPEND SWIFT_RUNTIME_CONCURRENCY_C_FLAGS

stdlib/public/Concurrency/TaskGroup.swift

+2
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
782782
/// to set the child task's priority to the priority of the group.
783783
/// - operation: The operation to execute as part of the task group.
784784
@_alwaysEmitIntoClient
785+
@_allowFeatureSuppression(IsolatedAny)
785786
public mutating func addTask(
786787
priority: TaskPriority? = nil,
787788
operation: __owned @Sendable @escaping @isolated(any) () async throws -> ChildTaskResult
@@ -823,6 +824,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
823824
/// - Returns: `true` if the child task was added to the group;
824825
/// otherwise `false`.
825826
@_alwaysEmitIntoClient
827+
@_allowFeatureSuppression(IsolatedAny)
826828
public mutating func addTaskUnlessCancelled(
827829
priority: TaskPriority? = nil,
828830
operation: __owned @Sendable @escaping @isolated(any) () async throws -> ChildTaskResult

stdlib/public/core/Optional.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,22 @@ extension Optional where Wrapped: ~Copyable {
213213
}
214214
}
215215

216-
#if hasFeature(BorrowingSwitch)
217216
// FIXME(NCG): Make this public.
218217
@_alwaysEmitIntoClient
219218
public borrowing func _borrowingMap<U: ~Copyable, E: Error>(
220219
_ transform: (borrowing Wrapped) throws(E) -> U
221220
) throws(E) -> U? {
221+
#if $NoncopyableGenerics
222222
switch self {
223223
case .some(_borrowing y):
224224
return .some(try transform(y))
225225
case .none:
226226
return .none
227227
}
228+
#else
229+
fatalError("unsupported compiler")
230+
#endif
228231
}
229-
#endif
230232
}
231233

232234
extension Optional {
@@ -263,6 +265,7 @@ extension Optional {
263265
}
264266
}
265267

268+
@_disallowFeatureSuppression(NoncopyableGenerics)
266269
extension Optional where Wrapped: ~Copyable {
267270
// FIXME(NCG): Make this public.
268271
@_alwaysEmitIntoClient
@@ -277,7 +280,6 @@ extension Optional where Wrapped: ~Copyable {
277280
}
278281
}
279282

280-
#if hasFeature(BorrowingSwitch)
281283
// FIXME(NCG): Make this public.
282284
@_alwaysEmitIntoClient
283285
public func _borrowingFlatMap<U: ~Copyable, E: Error>(
@@ -290,7 +292,6 @@ extension Optional where Wrapped: ~Copyable {
290292
return .none
291293
}
292294
}
293-
#endif
294295
}
295296

296297
extension Optional {
@@ -546,7 +547,6 @@ public struct _OptionalNilComparisonType: ExpressibleByNilLiteral {
546547
}
547548
}
548549

549-
#if hasFeature(BorrowingSwitch)
550550
extension Optional where Wrapped: ~Copyable {
551551
/// Returns a Boolean value indicating whether an argument matches `nil`.
552552
///
@@ -735,9 +735,6 @@ extension Optional where Wrapped: ~Copyable {
735735
}
736736
}
737737
}
738-
#else
739-
#error("FIXME(NCG): Fill this out.")
740-
#endif
741738

742739
/// Performs a nil-coalescing operation, returning the wrapped value of an
743740
/// `Optional` instance or a default value.

stdlib/public/core/Result.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extension Result {
6161
}
6262
}
6363

64+
@_disallowFeatureSuppression(NoncopyableGenerics)
6465
extension Result where Success: ~Copyable {
6566
// FIXME(NCG): Make this public.
6667
@_alwaysEmitIntoClient
@@ -75,7 +76,6 @@ extension Result where Success: ~Copyable {
7576
}
7677
}
7778

78-
#if $BorrowingSwitch
7979
// FIXME(NCG): Make this public.
8080
@_alwaysEmitIntoClient
8181
public borrowing func _borrowingMap<NewSuccess: ~Copyable>(
@@ -88,7 +88,6 @@ extension Result where Success: ~Copyable {
8888
return .failure(failure)
8989
}
9090
}
91-
#endif
9291
}
9392

9493
extension Result where Success: ~Copyable {
@@ -131,6 +130,7 @@ extension Result where Success: ~Copyable {
131130
}
132131
}
133132

133+
@_disallowFeatureSuppression(NoncopyableGenerics)
134134
extension Result {
135135
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
136136
@usableFromInline
@@ -186,6 +186,7 @@ extension Result {
186186
}
187187
}
188188

189+
@_disallowFeatureSuppression(NoncopyableGenerics)
189190
extension Result where Success: ~Copyable {
190191
// FIXME(NCG): Make this public.
191192
@_alwaysEmitIntoClient
@@ -200,7 +201,6 @@ extension Result where Success: ~Copyable {
200201
}
201202
}
202203

203-
#if $BorrowingSwitch
204204
// FIXME(NCG): Make this public.
205205
@_alwaysEmitIntoClient
206206
public borrowing func _borrowingFlatMap<NewSuccess: ~Copyable>(
@@ -213,7 +213,6 @@ extension Result where Success: ~Copyable {
213213
return .failure(failure)
214214
}
215215
}
216-
#endif
217216
}
218217

219218
extension Result {

stdlib/public/core/UnsafeBufferPointer.swift.gyb

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
456456

457457
}
458458

459+
@_disallowFeatureSuppression(NoncopyableGenerics)
459460
extension Unsafe${Mutable}BufferPointer {
460461
/// Accesses the element at the specified position.
461462
///

stdlib/public/core/UnsafePointer.swift

+4
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ extension UnsafePointer where Pointee: ~Copyable {
286286
}
287287
}
288288

289+
@_disallowFeatureSuppression(NoncopyableGenerics)
289290
extension UnsafePointer {
290291
// This preserves the ABI of the original (pre-6.0) `pointee` property that
291292
// used to export a getter. The current one above would export a read
@@ -315,6 +316,7 @@ extension UnsafePointer where Pointee: ~Copyable {
315316
}
316317
}
317318

319+
@_disallowFeatureSuppression(NoncopyableGenerics)
318320
extension UnsafePointer {
319321
// This preserves the ABI of the original (pre-6.0) subscript that used to
320322
// export a getter. The current one above would export a read accessor, if it
@@ -843,6 +845,7 @@ extension UnsafeMutablePointer where Pointee: ~Copyable {
843845
}
844846
}
845847

848+
@_disallowFeatureSuppression(NoncopyableGenerics)
846849
extension UnsafeMutablePointer {
847850
// This preserves the ABI of the original (pre-6.0) `pointee` property that
848851
// used to export a getter. The current one above would export a read
@@ -1300,6 +1303,7 @@ extension UnsafeMutablePointer where Pointee: ~Copyable {
13001303
}
13011304
}
13021305

1306+
@_disallowFeatureSuppression(NoncopyableGenerics)
13031307
extension UnsafeMutablePointer {
13041308
// This preserves the ABI of the original (pre-6.0) subscript that used to
13051309
// export a getter. The current one above would export a read accessor, if it

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

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
// REQUIRES: executable_test
1515

16-
// FIXME(NCG): This test requires NoncopyableGenerics to be a "suppressible" compiler feature.
17-
// XFAIL: !noncopyable_generics
18-
1916
//--- header.h
2017

2118
#include <stdio.h>

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

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
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-
// FIXME(NCG): This test requires NoncopyableGenerics to be a "suppressible" compiler feature.
9-
// XFAIL: !noncopyable_generics
10-
118
// CHECK: namespace swift SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("swift") {
129

1310
// CHECK: template<class T_0_0>

test/ModuleInterface/Inputs/NoncopyableGenerics_Misc.swift

+6
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,9 @@ extension Outer.InnerStruct {
106106

107107
@_preInverseGenerics
108108
public func old_swap<T: ~Copyable>(_ a: inout T, _ b: inout T) {}
109+
110+
@_preInverseGenerics
111+
public func borrowsNoncopyable<T: ~Copyable>(_ t: borrowing T) {}
112+
113+
@_disallowFeatureSuppression(NoncopyableGenerics)
114+
public func suppressesNoncopyableGenerics<T: ~Copyable>(_ t: borrowing T) {}

test/ModuleInterface/isolated_any_suppression.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %empty-directory(%t)
22

33
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -module-name isolated_any -emit-module -o %t/isolated_any.swiftmodule -emit-module-interface-path - -enable-experimental-feature IsolatedAny %s | %FileCheck %s
4+
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -module-name isolated_any -emit-module -o %t/isolated_any.swiftmodule -emit-module-interface-path - -enable-experimental-feature IsolatedAny2 %s | %FileCheck %s
45

56
// CHECK: #if compiler(>=5.3) && $IsolatedAny
67
// CHECK-NEXT: {{^}}public func test1(fn: @isolated(any) @Sendable () -> ())

test/ModuleInterface/noncopyable_generics.swift

+15
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,23 @@ import NoncopyableGenerics_Misc
144144

145145
// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics
146146
// CHECK-MISC-NEXT: @_preInverseGenerics public func old_swap<T>(_ a: inout T, _ b: inout T) where T : ~Copyable
147+
// CHECK-MISC-NEXT: #else
148+
// CHECK-MISC-NOT: @_preInverseGenerics
149+
// CHECK-MISC-NEXT: public func old_swap<T>(_ a: inout T, _ b: inout T)
147150
// CHECK-MISC: #endif
148151

152+
// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics
153+
// CHECK-MISC-NEXT: @_preInverseGenerics public func borrowsNoncopyable<T>(_ t: borrowing T) where T : ~Copyable
154+
// CHECK-MISC-NEXT: #else
155+
// CHECK-MISC-NOT: @_preInverseGenerics
156+
// CHECK-MISC-NEXT: public func borrowsNoncopyable<T>(_ t: T)
157+
// CHECK-MISC-NEXT: #endif
158+
159+
// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics
160+
// CHECK-MISC-NEXT: public func suppressesNoncopyableGenerics<T>(_ t: borrowing T) where T : ~Copyable
161+
// CHECK-MISC-NEXT: #endif
162+
163+
149164
import Swiftskell
150165

151166
// CHECK: #if compiler(>=5.3) && $NoncopyableGenerics

0 commit comments

Comments
 (0)