Skip to content

[cxx-interop] Mark un-specialized class templates as unavailable in Swift. #60786

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
Oct 26, 2022
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
2 changes: 1 addition & 1 deletion SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct DiagnosticFixIt {
let bridgedDiagnosticFixIt = swift.DiagnosticInfo.FixIt(
swift.CharSourceRange(start.bridged, UInt32(byteLength)),
bridgedTextRef,
llvm.ArrayRef<swift.DiagnosticArgument>())
ArrayRefOfDiagnosticArgument())
fn(bridgedDiagnosticFixIt)
}
}
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void DiagnosticEngine_diagnose(swift::DiagnosticEngine &, swift::SourceLoc loc,
swift::CharSourceRange highlight,
BridgedArrayRef fixIts);

using ArrayRefOfDiagnosticArgument = llvm::ArrayRef<swift::DiagnosticArgument>;

SWIFT_END_NULLABILITY_ANNOTATIONS

#endif // SWIFT_AST_ASTBRIDGING_H
7 changes: 7 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3497,6 +3497,13 @@ namespace {

auto structDecl = Impl.createDeclWithClangNode<StructDecl>(
decl, AccessLevel::Public, loc, name, loc, None, genericParamList, dc);

auto attr = AvailableAttr::createPlatformAgnostic(
Impl.SwiftContext, "Un-specialized class templates are not currently "
"supported. Please use a specialization of this "
"type.");
structDecl->getAttrs().add(attr);

return structDecl;
}

Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/namespace/templates-module-interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct ForwardDeclaredClassTemplate<> {
// CHECK-NEXT: }
// CHECK-NEXT: static func forwardDeclaredFunctionTemplateOutOfLine<T>(_: T) -> UnsafePointer<CChar>!
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS112TemplatesNS237ForwardDeclaredClassTemplateOutOfLineIcEE {
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct ForwardDeclaredClassTemplateOutOfLine<> {
// CHECK-NEXT: }
// CHECK-NEXT: typealias BasicClassTemplateChar = TemplatesNS1.TemplatesNS3.__CxxTemplateInstN12TemplatesNS112TemplatesNS318BasicClassTemplateIcEE
Expand All @@ -24,10 +26,12 @@
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct BasicClassTemplate<> {
// CHECK-NEXT: }
// CHECK-NEXT: typealias BasicClassTemplateChar = TemplatesNS1.__CxxTemplateInstN12TemplatesNS118BasicClassTemplateIcEE
// CHECK-NEXT: static func basicFunctionTemplateDefinedInDefs<T>(_: T) -> UnsafePointer<CChar>!
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct BasicClassTemplateDefinedInDefs<> {
// CHECK-NEXT: }
// CHECK-NEXT: typealias UseTemplate = TemplatesNS4.__CxxTemplateInstN12TemplatesNS417HasSpecializationIcEE
Expand All @@ -36,6 +40,7 @@
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS112TemplatesNS318BasicClassTemplateIcEE {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct BasicClassTemplate<> {
// CHECK-NEXT: }
// CHECK-NEXT: }
Expand All @@ -53,6 +58,7 @@
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIiEE {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct HasSpecialization<> {
// CHECK-NEXT: }
// CHECK-NEXT: }
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// CHECK-NEXT: struct __CxxTemplateInstN3NS115ForwardDeclaredIiEE {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct ForwardDeclared<T> {
// CHECK-NEXT: }
// CHECK-NEXT: struct __CxxTemplateInstN3NS14DeclIiEE {
Expand All @@ -13,6 +14,7 @@
// CHECK-NEXT: var fwd: NS1.__CxxTemplateInstN3NS115ForwardDeclaredIiEE
// CHECK-NEXT: static let intValue: NS1.__CxxTemplateInstN3NS14DeclIiEE.MyInt
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct Decl<T> {
// CHECK-NEXT: }
// CHECK-NEXT: typealias di = NS1.__CxxTemplateInstN3NS14DeclIiEE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ClassTemplateForSwiftModule

public func makeWrappedMagicNumber() -> MagicWrapper<IntWrapper> {
public func makeWrappedMagicNumber() -> MagicWrapperSpec {
let t = IntWrapper(value: 42)
return MagicWrapper<IntWrapper>(t: t)
}

public func readWrappedMagicNumber(_ i: inout MagicWrapper<IntWrapper>) -> CInt {
public func readWrappedMagicNumber(_ i: inout MagicWrapperSpec) -> CInt {
return i.getValuePlusArg(13)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ struct MagicWrapper {
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
};

using MagicWrapperSpec = MagicWrapper<IntWrapper>;

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_FOR_SWIFT_MODULE_H
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking)
//
// REQUIRES: executable_test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop 2>&1 | %FileCheck %s
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop -disable-availability-checking 2>&1 | %FileCheck %s

import ClassTemplateInstantiationErrors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop 2>&1 | %FileCheck %s
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop -disable-availability-checking 2>&1 | %FileCheck %s

import ClassTemplateInstantiationErrors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking)
//
// REQUIRES: executable_test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop -disable-availability-checking | %FileCheck %s

import ClassTemplateInstantiationErrors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking)
//
// REQUIRES: executable_test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xcc -fno-exceptions)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xcc -fno-exceptions -Xfrontend -disable-availability-checking)
//
// REQUIRES: executable_test

Expand Down
2 changes: 1 addition & 1 deletion test/Interop/Cxx/templates/dependent-types-silgen.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-silgen %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s
// RUN: %target-swift-emit-silgen %s -I %S/Inputs -enable-experimental-cxx-interop -disable-availability-checking | %FileCheck %s

import DependentTypes

Expand Down
2 changes: 1 addition & 1 deletion test/Interop/Cxx/templates/dependent-types.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none -Xfrontend -disable-availability-checking)
//
// REQUIRES: executable_test
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %empty-directory(%t)
// RUN: %target-swiftxx-frontend -emit-module -o %t/SwiftClassTemplateModule.swiftmodule %S/Inputs/SwiftClassInstantiationModule.swift -I %S/Inputs -enable-library-evolution -swift-version 5
// RUN: %target-swiftxx-frontend -emit-module -o %t/SwiftClassTemplateModule.swiftmodule %S/Inputs/SwiftClassInstantiationModule.swift -I %S/Inputs -enable-library-evolution -swift-version 5 -disable-availability-checking
// RUN: %target-swift-ide-test -print-module -module-to-print=SwiftClassTemplateModule -I %t/ -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// rdar://101431096
// XFAIL: *

// RUN: %empty-directory(%t)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyp I'll let you decide if you want to land this PR before fixing this test and the next one. Sorry I did such a terrible job "fixing" these. I couldn't always understand the error, so maybe we can work on them offline.

// RUN: split-file %s %t

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// rdar://101431096
// XFAIL: *

// RUN: %empty-directory(%t)
// RUN: split-file %s %t

Expand Down