Skip to content

Revert "[cxx-interop] Check the presence of copy constructor correctly" #77616

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 1 commit into from
Nov 14, 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
24 changes: 8 additions & 16 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7652,26 +7652,18 @@ static bool hasCopyTypeOperations(const clang::CXXRecordDecl *decl) {
if (decl->isInStdNamespace() && decl->getIdentifier() &&
decl->getName() == "_Optional_construct_base")
return true;
// Hack for std::vector::const_iterator from libstdc++, which uses an extra
// parameter on its copy constructor, which has a defaulted enable_if value.
auto namespaceContext = dyn_cast_or_null<clang::NamespaceDecl>(
decl->getEnclosingNamespaceContext());
if (namespaceContext && namespaceContext->getIdentifier() &&
namespaceContext->getName() == "__gnu_cxx" && decl->getIdentifier() &&
decl->getName() == "__normal_iterator")
return true;
// Hack for certain build configurations of SwiftCompilerSources
// (rdar://138924133).
if (decl->getIdentifier() && decl->getName() == "BridgedSwiftObject")
return true;

// If we have no way of copying the type we can't import the class
// at all because we cannot express the correct semantics as a swift
// struct.
return llvm::any_of(decl->ctors(), [](clang::CXXConstructorDecl *ctor) {
return ctor->isCopyConstructor() && !ctor->isDeleted() &&
ctor->getAccess() == clang::AccessSpecifier::AS_public;
});
if (llvm::any_of(decl->ctors(), [](clang::CXXConstructorDecl *ctor) {
return ctor->isCopyConstructor() &&
(ctor->isDeleted() || ctor->getAccess() != clang::AS_public);
}))
return false;

// TODO: this should probably check to make sure we actually have a copy ctor.
return true;
}

static bool hasMoveTypeOperations(const clang::CXXRecordDecl *decl) {
Expand Down
19 changes: 0 additions & 19 deletions test/Interop/Cxx/class/Inputs/constructors.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ struct TemplatedConstructorWithExtraArg {
TemplatedConstructorWithExtraArg(T value, U other) { }
};

struct TemplatedCopyConstructor {
int x = 0;

TemplatedCopyConstructor(int x) : x(x) {}

template <class T>
TemplatedCopyConstructor(const T &value) : x(value.x) {}
};

struct TemplatedCopyConstructorWithExtraArg {
int x = 0;

TemplatedCopyConstructorWithExtraArg(int x) : x(x) {}

template <class T>
TemplatedCopyConstructorWithExtraArg(const T &value, int add = 0)
: x(value.x + add) {}
};

struct __attribute__((swift_attr("import_unsafe")))
HasUserProvidedCopyConstructor {
int numCopies;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=Constructors -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s

// CHECK: struct TemplatedCopyConstructor
// CHECK: struct TemplatedCopyConstructorWithExtraArg

// Make sure we don't import non-copyable types because we will have no way to
// represent and copy/move these in swift with correct semantics.
// CHECK-NOT: DeletedCopyConstructor
7 changes: 0 additions & 7 deletions test/Interop/Cxx/class/constructors-typechecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import Constructors

func takesCopyable<T: Copyable>(_ x: T.Type) {}

let explicit = ExplicitDefaultConstructor()

let implicit = ImplicitDefaultConstructor()
Expand All @@ -14,8 +12,3 @@ let onlyCopyAndMove = CopyAndMoveConstructor() // expected-warning {{'init()' is
let deletedExplicitly = DefaultConstructorDeleted() // expected-error {{missing argument for parameter 'a' in call}}

let withArg = ConstructorWithParam(42)

let _ = TemplatedCopyConstructor(123)
let _ = TemplatedCopyConstructorWithExtraArg(123)
takesCopyable(TemplatedCopyConstructor.self)
takesCopyable(TemplatedCopyConstructorWithExtraArg.self)