Skip to content

Commit 827af15

Browse files
committed
[interop] ensure std::pair is imported into Swift
1 parent 07ccee2 commit 827af15

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,13 +2712,18 @@ namespace {
27122712
return Impl.importDecl(decl->getSpecializedTemplate(),
27132713
Impl.CurrentVersion);
27142714

2715+
bool isPair = decl->getSpecializedTemplate()->isInStdNamespace() &&
2716+
decl->getSpecializedTemplate()->getName() == "pair";
2717+
27152718
// Before we go any further, check if we've already got tens of thousands
27162719
// of specializations. If so, it means we're likely instantiating a very
27172720
// deep/complex template, or we've run into an infinite loop. In either
27182721
// case, its not worth the compile time, so bail.
27192722
// TODO: this could be configurable at some point.
2720-
if (llvm::size(decl->getSpecializedTemplate()->specializations()) >
2721-
1000) {
2723+
size_t specializationLimit = !isPair ? 1000 : 10000;
2724+
if (!isPair &&
2725+
llvm::size(decl->getSpecializedTemplate()->specializations()) >
2726+
specializationLimit) {
27222727
std::string name;
27232728
llvm::raw_string_ostream os(name);
27242729
decl->printQualifiedName(os);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ module StdSet {
2222
header "std-set.h"
2323
requires cplusplus
2424
}
25+
26+
module StdPair {
27+
header "std-pair.h"
28+
requires cplusplus
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <utility>
4+
5+
using PairInts = std::pair<int, int>;
6+
7+
// FIXME: return pair by value, but it causes IRGen crash atm.
8+
inline const PairInts &getIntPair() {
9+
static PairInts value = { -5, 12 };
10+
return value;
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
2+
//
3+
// REQUIRES: executable_test
4+
//
5+
// REQUIRES: OS=macosx || OS=linux-gnu
6+
7+
import StdlibUnittest
8+
import StdPair
9+
import CxxStdlib
10+
import Cxx
11+
12+
var StdPairTestSuite = TestSuite("StdPair")
13+
14+
StdPairTestSuite.test("StdPair.elements") {
15+
var pi = getIntPair().pointee
16+
expectEqual(pi.first, -5)
17+
expectEqual(pi.second, 12)
18+
pi.first = 11
19+
expectEqual(pi.first, 11)
20+
expectEqual(pi.second, 12)
21+
}
22+
23+
runAllTests()

0 commit comments

Comments
 (0)