Skip to content

[cxx-interop] Do not instantiate templates in unevaluated contexts #61552

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
Oct 13, 2022
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: 9 additions & 0 deletions lib/IRGen/GenClangDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class ClangDeclFinder
return true;
}

// Do not traverse unevaluated expressions. Doing to might result in compile
// errors if we try to instantiate an un-instantiatable template.

bool VisitCXXNoexceptExpr(clang::CXXNoexceptExpr *NEE) { return false; }

bool VisitCXXTypeidExpr(clang::CXXTypeidExpr *TIE) {
return TIE->isPotentiallyEvaluated();
}

bool shouldVisitTemplateInstantiations() const { return true; }
bool shouldVisitImplicitCode() const { return true; }
};
Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/templates/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ module DependentTypes {
header "dependent-types.h"
requires cplusplus
}

module UnevaluatedContext {
header "unevaluated-context.h"
requires cplusplus
}
33 changes: 33 additions & 0 deletions test/Interop/Cxx/templates/Inputs/unevaluated-context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_UNEVALUATED_CONTEXT_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_UNEVALUATED_CONTEXT_H

template <typename _Tp>
_Tp __declval(long);

template <typename _Tp>
struct __declval_protector {
static const bool __stop = false;
};

template <typename _Tp>
auto declval() noexcept -> decltype(__declval<_Tp>(0)) {
static_assert(__declval_protector<_Tp>::__stop,
"declval() must not be used!");
return __declval<_Tp>(0);
}

template <class T>
class Vec {
public:
void push_back(const T &__x) {
if (!noexcept(declval<T *>()))
;
}
};

inline void initVector() {
Vec<int> vv;
vv.push_back(0);
}

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_UNEVALUATED_CONTEXT_H
14 changes: 14 additions & 0 deletions test/Interop/Cxx/templates/unevaluated-context.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
//
// REQUIRES: executable_test

import StdlibUnittest
import UnevaluatedContext

var UnevaluatedTestSuite = TestSuite("UnevaluatedContext")

UnevaluatedTestSuite.test("declval") {
initVector()
}

runAllTests()