Skip to content

Commit a9350cc

Browse files
committed
C++Interop: Don't import c++ template functions with default arguments
SwiftDeclSynthesizer::makeDefaultArgument does not create substituion map while creating the implicit FuncDecl which leads to TypeChecker complaining about the absence when there are type parameters. Avoid importing such C++ functions for now.
1 parent f02448b commit a9350cc

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,12 +2616,10 @@ bool ClangImporter::Implementation::isDefaultArgSafeToImport(
26162616
return true;
26172617
}
26182618

2619-
static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
2620-
const clang::ParmVarDecl *param,
2621-
const Identifier &name,
2622-
const swift::Type &swiftParamTy,
2623-
const bool isInOut,
2624-
const bool isParamTypeImplicitlyUnwrapped) {
2619+
static std::optional<ParamDecl *> getParameterInfo(
2620+
ClangImporter::Implementation *impl, const clang::ParmVarDecl *param,
2621+
const Identifier &name, const swift::Type &swiftParamTy, const bool isInOut,
2622+
const bool isParamTypeImplicitlyUnwrapped, const bool isTemplated) {
26252623
// Figure out the name for this parameter.
26262624
Identifier bodyName = impl->importFullName(param, impl->CurrentVersion)
26272625
.getBaseIdentifier(impl->SwiftContext);
@@ -2660,6 +2658,13 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
26602658
if (param->hasDefaultArg() && !isInOut &&
26612659
!isa<clang::CXXConstructorDecl>(param->getDeclContext()) &&
26622660
impl->isDefaultArgSafeToImport(param)) {
2661+
2662+
// If this was a default argument expression with template parameter, return
2663+
// std::nullopt
2664+
if (isTemplated) {
2665+
return std::nullopt;
2666+
}
2667+
26632668
SwiftDeclSynthesizer synthesizer(*impl);
26642669
if (CallExpr *defaultArgExpr = synthesizer.makeDefaultArgument(
26652670
param, swiftParamTy, paramInfo->getParameterNameLoc())) {
@@ -2723,8 +2728,15 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
27232728
name = argNames[index];
27242729

27252730
auto paramInfo = getParameterInfo(this, param, name, swiftParamTy, isInOut,
2726-
isParamTypeImplicitlyUnwrapped);
2727-
parameters.push_back(paramInfo);
2731+
isParamTypeImplicitlyUnwrapped,
2732+
swiftParamTy->hasTypeParameter());
2733+
if (!paramInfo.has_value()) {
2734+
addImportDiagnostic(param,
2735+
Diagnostic(diag::parameter_type_not_imported, param),
2736+
param->getSourceRange().getBegin());
2737+
return nullptr;
2738+
}
2739+
parameters.push_back(*paramInfo);
27282740
++index;
27292741
}
27302742

@@ -3349,8 +3361,9 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
33493361
++nameIndex;
33503362

33513363
// Set up the parameter info
3352-
auto paramInfo = getParameterInfo(this, param, name, swiftParamTy, isInOut,
3353-
isParamTypeImplicitlyUnwrapped);
3364+
auto paramInfo = *getParameterInfo(this, param, name, swiftParamTy, isInOut,
3365+
isParamTypeImplicitlyUnwrapped,
3366+
/*isTemplated*/ false);
33543367

33553368
// Determine whether we have a default argument.
33563369
if (kind == SpecialMethodKind::Regular ||

test/Interop/Cxx/templates/defaulted-template-type-parameter-module-interface.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=DefaultedTemplateTypeParameter -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
2+
// REQUIRES: rdar136703069
23

34
// CHECK: struct X {
45
// CHECK: init<T>(_: T)

test/Interop/Cxx/templates/defaulted-template-type-parameter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
22
//
33
// REQUIRES: executable_test
4+
// REQUIRES: rdar136703069
45

56
import DefaultedTemplateTypeParameter
67
import StdlibUnittest

0 commit comments

Comments
 (0)