Skip to content

NCGenerics: add test for feature flag & force-enable all #73522

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 5 commits into from
May 13, 2024
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
3 changes: 3 additions & 0 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ LangOptions::LangOptions() {
#endif

// Note: Introduce default-on language options here.
Features.insert(Feature::NoncopyableGenerics);
Features.insert(Feature::BorrowingSwitch);
Features.insert(Feature::MoveOnlyPartialConsumption);

// Enable any playground options that are enabled by default.
#define PLAYGROUND_OPTION(OptionName, Description, DefaultOn, HighPerfOn) \
Expand Down
14 changes: 14 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5795,6 +5795,20 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
}

if (auto var = dyn_cast<VarDecl>(decl)) {
auto oldContext = var->getDeclContext();
auto oldTypeDecl = oldContext->getSelfNominalTypeDecl();
// If the base type is non-copyable, we cannot synthesize the accessor,
// because its implementation would use `UnsafePointer<BaseTy>`, and that
// triggers a bug when building SwiftCompilerSources. (rdar://128013193)
//
// We cannot use `ty->isNoncopyable()` here because that would create a
// cyclic dependency between ModuleQualifiedLookupRequest and
// LookupConformanceInModuleRequest, so we check for the presence of
// move-only attribute that is implicitly added to non-copyable C++ types by
// ClangImporter.
if (oldTypeDecl->getAttrs().hasAttribute<MoveOnlyAttr>())
return nullptr;

auto rawMemory = allocateMemoryForDecl<VarDecl>(var->getASTContext(),
sizeof(VarDecl), false);
auto out =
Expand Down
20 changes: 20 additions & 0 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,26 @@ namespace {
return importFunctionPointerLikeType(*type, pointeeType);
}

// We cannot specify UnsafePointer<T> with a non-copyable type T just yet,
// because it triggers a bug when building SwiftCompilerSources.
// (rdar://128013193)
//
// We cannot use `ty->isNoncopyable()` here because that would create a
// cyclic dependency between ModuleQualifiedLookupRequest and
// LookupConformanceInModuleRequest, so we check for the presence of
// move-only attribute that is implicitly added to non-copyable C++ types
// by ClangImporter.
if (pointeeType && pointeeType->getAnyNominal() &&
pointeeType->getAnyNominal()
->getAttrs()
.hasAttribute<MoveOnlyAttr>()) {
auto opaquePointerDecl = Impl.SwiftContext.getOpaquePointerDecl();
if (!opaquePointerDecl)
return Type();
return {opaquePointerDecl->getDeclaredInterfaceType(),
ImportHint::OtherPointer};
}

PointerTypeKind pointerKind;
if (quals.hasConst()) {
pointerKind = PTK_UnsafePointer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %target-swift-emit-irgen -I %S/Inputs -cxx-interoperability-mode=swift-6 %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s
// RUN: %target-swift-emit-irgen -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s

// REQUIRES: rdar128013193

import MoveOnlyCxxValueType

func testGetX() -> CInt {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %target-swift-emit-sil -I %S/Inputs -cxx-interoperability-mode=swift-6 %s -validate-tbd-against-ir=none | %FileCheck %s
// RUN: %target-swift-emit-sil -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s -validate-tbd-against-ir=none | %FileCheck %s

// REQUIRES: rdar128013193

import MoveOnlyCxxValueType

func testGetX() -> CInt {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnlyCxxValueType -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -source-filename=x | %FileCheck %s --check-prefix=CHECK-NCG
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnlyCxxValueType -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -source-filename=x | %FileCheck %s --check-prefix=CHECK

// CHECK-NCG: func getNonCopyablePtr() -> UnsafeMutablePointer<NonCopyable>
// CHECK-NCG: func getNonCopyableDerivedPtr() -> UnsafeMutablePointer<NonCopyableDerived>
// CHECK: func getNonCopyablePtr() -> OpaquePointer
// CHECK: func getNonCopyableDerivedPtr() -> OpaquePointer

// FIXME: would prefer to have this (rdar://128013193)
// func getNonCopyablePtr() -> UnsafeMutablePointer<NonCopyable>
// func getNonCopyableDerivedPtr() -> UnsafeMutablePointer<NonCopyableDerived>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
// -- FIXME: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -D HAS_RDAR_128013193)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9 -O)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O)

Expand Down Expand Up @@ -27,10 +28,12 @@ MoveOnlyCxxValueType.test("Test derived move only type member access") {
var k = c.method(-3)
expectEqual(k, -6)
expectEqual(c.method(1), 2)
#if HAS_RDAR_128013193
k = c.x
expectEqual(k, 2)
c.x = 11
expectEqual(c.x, 11)
#endif
k = c.mutMethod(-13)
expectEqual(k, -13)
}
Expand All @@ -56,6 +59,7 @@ MoveOnlyCxxValueType.test("Test move only field access in holder") {
expectEqual(c.x.x, 5)
}

#if HAS_RDAR_128013193
MoveOnlyCxxValueType.test("Test move only field access in derived holder") {
var c = NonCopyableHolderDerivedDerived(-11)
var k = borrowNC(c.x)
Expand All @@ -69,5 +73,6 @@ MoveOnlyCxxValueType.test("Test move only field access in derived holder") {
c.x.mutMethod(5)
expectEqual(c.x.x, 5)
}
#endif

runAllTests()
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
// -- FIXME: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -D HAS_RDAR_128013193)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9 -O)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O)
// -- FIXME: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O -D HAS_RDAR_128013193)
//
// REQUIRES: executable_test

Expand Down Expand Up @@ -90,6 +92,7 @@ MoveOnlyCxxOperators.test("testNonCopyableHolderValueMutDeref pointee value") {
expectEqual(k.x, k2.x)
}

#if HAS_RDAR_128013193
MoveOnlyCxxOperators.test("NonCopyableHolderConstDerefDerivedDerived pointee borrow") {
let holder = NonCopyableHolderConstDerefDerivedDerived(11)
var k = borrowNC(holder.pointee)
Expand Down Expand Up @@ -157,5 +160,6 @@ MoveOnlyCxxOperators.test("testNonCopyableHolderValueMutDerefDerivedDerived poin
var k2 = holder.pointee
expectEqual(k.x, k2.x)
}
#endif

runAllTests()
20 changes: 20 additions & 0 deletions test/ModuleInterface/noncopyable_generics_feature_flag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %t/Library.swift -module-name Library
// RUN: rm -f %t/Library.swiftmodule
// RUN: %target-swift-frontend -I %t -typecheck -verify %t/test.swift


//--- Library.swift

public struct Hello<T: ~Copyable> {
public init() {}
}

//--- test.swift
import Library

struct NC: ~Copyable {}

let x: Hello<NC> = .init()
48 changes: 24 additions & 24 deletions test/Sema/moveonly_enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum Foo3 {
}

func test_switch(x: consuming Foo3) {
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
switch x {
default:
break
}
Expand All @@ -32,7 +32,7 @@ func test_switch(x: consuming Foo3) {
break
}

switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{13-13=consume }}
switch (x) {
default:
break
}
Expand All @@ -43,7 +43,7 @@ func test_switch(x: consuming Foo3) {
}

let _: () -> () = {
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{16-16=consume }}
switch x {
default:
break
}
Expand All @@ -57,7 +57,7 @@ func test_switch(x: consuming Foo3) {
}

let _: () -> () = {
switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{17-17=consume }}
switch (x) {
default:
break
}
Expand All @@ -72,19 +72,19 @@ func test_switch(x: consuming Foo3) {
}

func test_if_case(x: consuming Foo3) {
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{27-27=consume }}
if case .bar(let y) = x { _ = y }

guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{30-30=consume }}
guard case .bar(let y) = x else { return }
_ = y

if case .bar(let z) = consume x { _ = z }

guard case .bar(let z) = consume x else { return }
_ = z

if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{28-28=consume }}
if case .bar(let a) = (x) { _ = a }

guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
guard case .bar(let a) = (x) else { return }
_ = a

if case .bar(let b) = (consume x) { _ = b }
Expand All @@ -93,11 +93,11 @@ func test_if_case(x: consuming Foo3) {
_ = b

let _: () -> () = {
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
if case .bar(let y) = x { _ = y }
}

let _: () -> () = {
guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{34-34=consume }}
guard case .bar(let y) = x else { return }
_ = y
}

Expand All @@ -111,11 +111,11 @@ func test_if_case(x: consuming Foo3) {
}

let _: () -> () = {
if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{32-32=consume }}
if case .bar(let a) = (x) { _ = a }
}

let _: () -> () = {
guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{35-35=consume }}
guard case .bar(let a) = (x) else { return }
_ = a
}

Expand All @@ -130,7 +130,7 @@ func test_if_case(x: consuming Foo3) {
}

func test_switch_b(x: __owned Foo3) {
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
switch x {
default:
break
}
Expand All @@ -140,7 +140,7 @@ func test_switch_b(x: __owned Foo3) {
break
}

switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{13-13=consume }}
switch (x) {
default:
break
}
Expand All @@ -151,7 +151,7 @@ func test_switch_b(x: __owned Foo3) {
}

let _: () -> () = {
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{16-16=consume }}
switch x {
default:
break
}
Expand All @@ -165,7 +165,7 @@ func test_switch_b(x: __owned Foo3) {
}

let _: () -> () = {
switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{17-17=consume }}
switch (x) {
default:
break
}
Expand All @@ -180,19 +180,19 @@ func test_switch_b(x: __owned Foo3) {
}

func test_if_case_b(x: __owned Foo3) {
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{27-27=consume }}
if case .bar(let y) = x { _ = y }

guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{30-30=consume }}
guard case .bar(let y) = x else { return }
_ = y

if case .bar(let z) = consume x { _ = z }

guard case .bar(let z) = consume x else { return }
_ = z

if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{28-28=consume }}
if case .bar(let a) = (x) { _ = a }

guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
guard case .bar(let a) = (x) else { return }
_ = a

if case .bar(let b) = (consume x) { _ = b }
Expand All @@ -201,11 +201,11 @@ func test_if_case_b(x: __owned Foo3) {
_ = b

let _: () -> () = {
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
if case .bar(let y) = x { _ = y }
}

let _: () -> () = {
guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{34-34=consume }}
guard case .bar(let y) = x else { return }
_ = y
}

Expand All @@ -219,11 +219,11 @@ func test_if_case_b(x: __owned Foo3) {
}

let _: () -> () = {
if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{32-32=consume }}
if case .bar(let a) = (x) { _ = a }
}

let _: () -> () = {
guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{35-35=consume }}
guard case .bar(let a) = (x) else { return }
_ = a
}

Expand Down