Skip to content

Commit ac131df

Browse files
authored
Merge pull request #60786 from zoecarver/unavailable-attr-class-templates
[cxx-interop] Mark un-specialized class templates as unavailable in Swift.
2 parents ea781bf + 6408d3a commit ac131df

19 files changed

+38
-13
lines changed

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct DiagnosticFixIt {
4646
let bridgedDiagnosticFixIt = swift.DiagnosticInfo.FixIt(
4747
swift.CharSourceRange(start.bridged, UInt32(byteLength)),
4848
bridgedTextRef,
49-
llvm.ArrayRef<swift.DiagnosticArgument>())
49+
ArrayRefOfDiagnosticArgument())
5050
fn(bridgedDiagnosticFixIt)
5151
}
5252
}

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ void DiagnosticEngine_diagnose(swift::DiagnosticEngine &, swift::SourceLoc loc,
3838
swift::CharSourceRange highlight,
3939
BridgedArrayRef fixIts);
4040

41+
using ArrayRefOfDiagnosticArgument = llvm::ArrayRef<swift::DiagnosticArgument>;
42+
4143
SWIFT_END_NULLABILITY_ANNOTATIONS
4244

4345
#endif // SWIFT_AST_ASTBRIDGING_H

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,6 +3497,13 @@ namespace {
34973497

34983498
auto structDecl = Impl.createDeclWithClangNode<StructDecl>(
34993499
decl, AccessLevel::Public, loc, name, loc, None, genericParamList, dc);
3500+
3501+
auto attr = AvailableAttr::createPlatformAgnostic(
3502+
Impl.SwiftContext, "Un-specialized class templates are not currently "
3503+
"supported. Please use a specialization of this "
3504+
"type.");
3505+
structDecl->getAttrs().add(attr);
3506+
35003507
return structDecl;
35013508
}
35023509

test/Interop/Cxx/namespace/templates-module-interface.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
// CHECK-NEXT: init()
88
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
99
// CHECK-NEXT: }
10+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
1011
// CHECK-NEXT: struct ForwardDeclaredClassTemplate<> {
1112
// CHECK-NEXT: }
1213
// CHECK-NEXT: static func forwardDeclaredFunctionTemplateOutOfLine<T>(_: T) -> UnsafePointer<CChar>!
1314
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS112TemplatesNS237ForwardDeclaredClassTemplateOutOfLineIcEE {
1415
// CHECK-NEXT: init()
1516
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
1617
// CHECK-NEXT: }
18+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
1719
// CHECK-NEXT: struct ForwardDeclaredClassTemplateOutOfLine<> {
1820
// CHECK-NEXT: }
1921
// CHECK-NEXT: typealias BasicClassTemplateChar = TemplatesNS1.TemplatesNS3.__CxxTemplateInstN12TemplatesNS112TemplatesNS318BasicClassTemplateIcEE
@@ -24,10 +26,12 @@
2426
// CHECK-NEXT: init()
2527
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
2628
// CHECK-NEXT: }
29+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
2730
// CHECK-NEXT: struct BasicClassTemplate<> {
2831
// CHECK-NEXT: }
2932
// CHECK-NEXT: typealias BasicClassTemplateChar = TemplatesNS1.__CxxTemplateInstN12TemplatesNS118BasicClassTemplateIcEE
3033
// CHECK-NEXT: static func basicFunctionTemplateDefinedInDefs<T>(_: T) -> UnsafePointer<CChar>!
34+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
3135
// CHECK-NEXT: struct BasicClassTemplateDefinedInDefs<> {
3236
// CHECK-NEXT: }
3337
// CHECK-NEXT: typealias UseTemplate = TemplatesNS4.__CxxTemplateInstN12TemplatesNS417HasSpecializationIcEE
@@ -36,6 +40,7 @@
3640
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS112TemplatesNS318BasicClassTemplateIcEE {
3741
// CHECK-NEXT: init()
3842
// CHECK-NEXT: }
43+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
3944
// CHECK-NEXT: struct BasicClassTemplate<> {
4045
// CHECK-NEXT: }
4146
// CHECK-NEXT: }
@@ -53,6 +58,7 @@
5358
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIiEE {
5459
// CHECK-NEXT: init()
5560
// CHECK-NEXT: }
61+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
5662
// CHECK-NEXT: struct HasSpecialization<> {
5763
// CHECK-NEXT: }
5864
// CHECK-NEXT: }

test/Interop/Cxx/namespace/templates-with-forward-decl-module-interface.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// CHECK-NEXT: struct __CxxTemplateInstN3NS115ForwardDeclaredIiEE {
55
// CHECK-NEXT: init()
66
// CHECK-NEXT: }
7+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
78
// CHECK-NEXT: struct ForwardDeclared<T> {
89
// CHECK-NEXT: }
910
// CHECK-NEXT: struct __CxxTemplateInstN3NS14DeclIiEE {
@@ -13,6 +14,7 @@
1314
// CHECK-NEXT: var fwd: NS1.__CxxTemplateInstN3NS115ForwardDeclaredIiEE
1415
// CHECK-NEXT: static let intValue: NS1.__CxxTemplateInstN3NS14DeclIiEE.MyInt
1516
// CHECK-NEXT: }
17+
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
1618
// CHECK-NEXT: struct Decl<T> {
1719
// CHECK-NEXT: }
1820
// CHECK-NEXT: typealias di = NS1.__CxxTemplateInstN3NS14DeclIiEE
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import ClassTemplateForSwiftModule
22

3-
public func makeWrappedMagicNumber() -> MagicWrapper<IntWrapper> {
3+
public func makeWrappedMagicNumber() -> MagicWrapperSpec {
44
let t = IntWrapper(value: 42)
55
return MagicWrapper<IntWrapper>(t: t)
66
}
77

8-
public func readWrappedMagicNumber(_ i: inout MagicWrapper<IntWrapper>) -> CInt {
8+
public func readWrappedMagicNumber(_ i: inout MagicWrapperSpec) -> CInt {
99
return i.getValuePlusArg(13)
1010
}

test/Interop/Cxx/templates/Inputs/class-template-for-swift-module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ struct MagicWrapper {
1212
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
1313
};
1414

15+
using MagicWrapperSpec = MagicWrapper<IntWrapper>;
16+
1517
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_FOR_SWIFT_MODULE_H

test/Interop/Cxx/templates/class-template-instantiation-existing-specialization.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking)
22
//
33
// REQUIRES: executable_test
44

test/Interop/Cxx/templates/class-template-instantiation-typechecker-calls-method-with-error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop 2>&1 | %FileCheck %s
1+
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop -disable-availability-checking 2>&1 | %FileCheck %s
22

33
import ClassTemplateInstantiationErrors
44

test/Interop/Cxx/templates/class-template-instantiation-typechecker-constructor-calls-method-with-error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop 2>&1 | %FileCheck %s
1+
// RUN: not %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop -disable-availability-checking 2>&1 | %FileCheck %s
22

33
import ClassTemplateInstantiationErrors
44

0 commit comments

Comments
 (0)