Skip to content

Commit 96d51cc

Browse files
committed
Add tests testing more complicated template scenarios for #32950.
Specifically: * class template with variadic parameter * class template with non-type parameter * class template with template template parameter
1 parent 840f1a4 commit 96d51cc

8 files changed

+147
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H
2+
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H
3+
4+
template<class T, auto Size>
5+
struct MagicArray {
6+
T t[Size];
7+
};
8+
9+
typedef MagicArray<int, 2> MagicIntPair;
10+
11+
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_TEMPLATE_PARAMETER_H
2+
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_TEMPLATE_PARAMETER_H
3+
4+
struct IntWrapper {
5+
int value;
6+
int getValue() const { return value; }
7+
};
8+
9+
template<class T>
10+
struct MagicWrapper {
11+
T t;
12+
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
13+
};
14+
15+
template<template <class> class V>
16+
struct TemplatedMagicWrapper {
17+
V<IntWrapper> i;
18+
int getValuePlusTwiceTheArg(int arg) const { return i.getValuePlusArg(arg) + arg; }
19+
};
20+
21+
typedef TemplatedMagicWrapper<MagicWrapper> TemplatedWrappedMagicInt;
22+
typedef MagicWrapper<IntWrapper> WrappedMagicInt;
23+
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_TEMPLATE_PARAMETER_H
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_VARIADIC_H
2+
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_VARIADIC_H
3+
4+
template <class... Ts> struct Tuple {};
5+
6+
template <>
7+
struct Tuple<> {
8+
void set() {}
9+
};
10+
11+
template <class T, class... Ts>
12+
struct Tuple<T, Ts...> : Tuple<Ts...> {
13+
Tuple(T t, Ts... ts) : Tuple<Ts...>(ts...), _t(t) {}
14+
15+
void set(T t, Ts... ts) { _t = t; Tuple<Ts...>::set(ts...); }
16+
17+
T first() { return _t; }
18+
Tuple<Ts...> rest() { return *this; }
19+
20+
T _t;
21+
};
22+
23+
struct IntWrapper {
24+
int value;
25+
int getValue() const { return value; }
26+
};
27+
28+
typedef Tuple<IntWrapper, IntWrapper> Pair;
29+
30+
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_VARIADIC_H

test/Interop/Cxx/templates/Inputs/module.modulemap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,15 @@ module Mangling {
3737
module LinkageOfSwiftSymbolsForImportedTypes {
3838
header "linkage-of-swift-symbols-for-imported-types.h"
3939
}
40+
41+
module ClassTemplateVariadic {
42+
header "class-template-variadic.h"
43+
}
44+
45+
module ClassTemplateNonTypeParameter {
46+
header "class-template-non-type-parameter.h"
47+
}
48+
49+
module ClassTemplateTemplateParameter {
50+
header "class-template-template-parameter.h"
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
5+
import ClassTemplateNonTypeParameter
6+
import StdlibUnittest
7+
8+
var TemplatesTestSuite = TestSuite("TemplatesTestSuite")
9+
10+
TemplatesTestSuite.test("non-type-parameter") {
11+
var pair = MagicIntPair(t: (1, 2))
12+
expectEqual(pair.t, (1, 2))
13+
}
14+
15+
runAllTests()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
5+
import ClassTemplateTemplateParameter
6+
import StdlibUnittest
7+
8+
var TemplatesTestSuite = TestSuite("TemplatesTestSuite")
9+
10+
TemplatesTestSuite.test("template-template-parameter") {
11+
let myInt = IntWrapper(value: 42)
12+
var magicInt = WrappedMagicInt(t: myInt)
13+
var templatedWrappedMagicInt = TemplatedWrappedMagicInt(i: magicInt)
14+
expectEqual(templatedWrappedMagicInt.getValuePlusTwiceTheArg(10), 62)
15+
}
16+
17+
runAllTests()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
//
5+
// We can't yet call member functions correctly on Windows (SR-13129).
6+
// XFAIL: OS=windows-msvc
7+
8+
import ClassTemplateVariadic
9+
import StdlibUnittest
10+
11+
var TemplatesTestSuite = TestSuite("TemplatesTestSuite")
12+
13+
TemplatesTestSuite.test("variadic-class-template") {
14+
let a = IntWrapper(value: 10)
15+
let b = IntWrapper(value: 20)
16+
17+
var pair = Pair()
18+
pair.set(a, b)
19+
20+
var pairA = pair.first()
21+
var restB = pair.rest()
22+
var pairB = restB.first()
23+
expectEqual(pairA.getValue(), 10)
24+
expectEqual(pairB.getValue(), 20)
25+
}
26+
27+
runAllTests()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | grep 'define.*swiftcc.*$' | grep -o '[[:alnum:]]*__CxxTemplateInst[[:alnum:]]*' | xargs %swift-demangle | %FileCheck %s
2+
3+
import Mangling
4+
5+
public func receiveInstantiation(_ i: inout WrappedMagicInt) {}
6+
7+
public func returnInstantiation() -> WrappedMagicInt {
8+
return WrappedMagicInt()
9+
}
10+
11+
// CHECK: $s10demangling20receiveInstantiationyySo34__CxxTemplateInst12MagicWrapperIiEVzF ---> demangling.receiveInstantiation(inout __C.__CxxTemplateInst12MagicWrapperIiE) -> ()
12+
// CHECK: $s10demangling19returnInstantiationSo34__CxxTemplateInst12MagicWrapperIiEVyF ---> demangling.returnInstantiation() -> __C.__CxxTemplateInst12MagicWrapperIiE

0 commit comments

Comments
 (0)