Skip to content

Commit 7f21516

Browse files
committed
Adjust interpreter tests in response to review discussion.
1 parent e1783e4 commit 7f21516

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

clang/test/AST/ByteCode/functions.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// RUN: %clang_cc1 -pedantic -std=c++14 -verify=ref,both %s
66
// RUN: %clang_cc1 -pedantic -std=c++20 -verify=ref,both %s
77

8+
#define fold(x) (__builtin_constant_p(0) ? (x) : (x))
9+
810
constexpr void doNothing() {}
911
constexpr int gimme5() {
1012
doNothing();
@@ -654,15 +656,26 @@ namespace {
654656
}
655657

656658
namespace FunctionCast {
657-
// When folding, we allow functions to be cast to different types. Such
658-
// cast functions cannot be called, even if they're constexpr.
659+
// When folding, we allow functions to be cast to different types. We only
660+
// allow calls if the dynamic type of the pointer matches the type of the
661+
// call.
659662
constexpr int f() { return 1; }
663+
constexpr void* f2() { return nullptr; }
664+
constexpr int f3(int a) { return a; }
660665
typedef double (*DoubleFn)();
661666
typedef int (*IntFn)();
662-
int a[(int)DoubleFn(f)()]; // both-error {{variable length array}} \
663-
// expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} \
664-
// both-warning {{are a Clang extension}}
665-
int b[(int)IntFn(f)()]; // ok
667+
typedef int* (*IntPtrFn)();
668+
constexpr int test1 = (int)DoubleFn(f)(); // both-error {{constant expression}} both-note {{reinterpret_cast}}
669+
// FIXME: We should print a note explaining the error.
670+
constexpr int test2 = (int)fold(DoubleFn(f))(); // both-error {{constant expression}}
671+
constexpr int test3 = (int)IntFn(f)(); // no-op cast
672+
constexpr int test4 = fold(IntFn(DoubleFn(f)))();
673+
constexpr int test5 = IntFn(fold(DoubleFn(f)))(); // both-error {{constant expression}} \
674+
// both-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
675+
// FIXME: Interpreter is less strict here.
676+
constexpr int test6 = fold(IntPtrFn(f2))() == nullptr; // ref-error {{constant expression}}
677+
// FIXME: The following crashes interpreter
678+
// constexpr int test6 = fold(IntFn(f3)());
666679
}
667680

668681
#if __cplusplus >= 202002L

0 commit comments

Comments
 (0)