Skip to content

[interop] ensure std::pair is imported into Swift #64348

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
Mar 14, 2023
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
9 changes: 7 additions & 2 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2712,13 +2712,18 @@ namespace {
return Impl.importDecl(decl->getSpecializedTemplate(),
Impl.CurrentVersion);

bool isPair = decl->getSpecializedTemplate()->isInStdNamespace() &&
decl->getSpecializedTemplate()->getName() == "pair";

// Before we go any further, check if we've already got tens of thousands
// of specializations. If so, it means we're likely instantiating a very
// deep/complex template, or we've run into an infinite loop. In either
// case, its not worth the compile time, so bail.
// TODO: this could be configurable at some point.
if (llvm::size(decl->getSpecializedTemplate()->specializations()) >
1000) {
size_t specializationLimit = !isPair ? 1000 : 10000;
if (!isPair &&
llvm::size(decl->getSpecializedTemplate()->specializations()) >
specializationLimit) {
std::string name;
llvm::raw_string_ostream os(name);
decl->printQualifiedName(os);
Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ module StdSet {
header "std-set.h"
requires cplusplus
}

module StdPair {
header "std-pair.h"
requires cplusplus
}
11 changes: 11 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-pair.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <utility>

using PairInts = std::pair<int, int>;

// FIXME: return pair by value, but it causes IRGen crash atm.
inline const PairInts &getIntPair() {
static PairInts value = { -5, 12 };
return value;
}
23 changes: 23 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-pair.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
//
// REQUIRES: executable_test
//
// REQUIRES: OS=macosx || OS=linux-gnu

import StdlibUnittest
import StdPair
import CxxStdlib
import Cxx

var StdPairTestSuite = TestSuite("StdPair")

StdPairTestSuite.test("StdPair.elements") {
var pi = getIntPair().pointee
expectEqual(pi.first, -5)
expectEqual(pi.second, 12)
pi.first = 11
expectEqual(pi.first, 11)
expectEqual(pi.second, 12)
}

runAllTests()