Skip to content

Commit 05f4448

Browse files
committed
[clang][Interp][NFC] Add eval-order test
Demonstrate that this isn't yet working right.
1 parent ad9f38d commit 05f4448

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

clang/test/AST/Interp/eval-order.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// RUN: %clang_cc1 -std=c++1z -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu
2+
// RUN: %clang_cc1 -std=c++1z -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu -fexperimental-new-constant-interpreter
3+
4+
// ref-no-diagnostics
5+
// expected-no-diagnostics
6+
7+
/// Check that assignment operators evaluate their operands right-to-left.
8+
/// Copied from test/SemaCXX/constant-expression-cxx1z.cpp
9+
///
10+
/// As you can see from the FIXME comments, some of these are not yet working correctly
11+
/// in the new interpreter.
12+
namespace EvalOrder {
13+
template<typename T> struct lvalue {
14+
T t;
15+
constexpr T &get() { return t; }
16+
};
17+
18+
struct UserDefined {
19+
int n = 0;
20+
constexpr UserDefined &operator=(const UserDefined&) { return *this; }
21+
constexpr UserDefined &operator+=(const UserDefined&) { return *this; }
22+
constexpr void operator<<(const UserDefined&) const {}
23+
constexpr void operator>>(const UserDefined&) const {}
24+
constexpr void operator+(const UserDefined&) const {}
25+
constexpr void operator[](int) const {}
26+
};
27+
constexpr UserDefined ud;
28+
29+
struct NonMember {};
30+
constexpr void operator+=(NonMember, NonMember) {}
31+
constexpr void operator<<(NonMember, NonMember) {}
32+
constexpr void operator>>(NonMember, NonMember) {}
33+
constexpr void operator+(NonMember, NonMember) {}
34+
constexpr NonMember nm;
35+
36+
constexpr void f(...) {}
37+
38+
// Helper to ensure that 'a' is evaluated before 'b'.
39+
struct seq_checker {
40+
bool done_a = false;
41+
bool done_b = false;
42+
43+
template <typename T> constexpr T &&a(T &&v) {
44+
done_a = true;
45+
return (T &&)v;
46+
}
47+
template <typename T> constexpr T &&b(T &&v) {
48+
if (!done_a)
49+
throw "wrong";
50+
done_b = true;
51+
return (T &&)v;
52+
}
53+
54+
constexpr bool ok() { return done_a && done_b; }
55+
};
56+
57+
// SEQ(expr), where part of the expression is tagged A(...) and part is
58+
// tagged B(...), checks that A is evaluated before B.
59+
#define A sc.a
60+
#define B sc.b
61+
#define SEQ(...) static_assert([](seq_checker sc) { void(__VA_ARGS__); return sc.ok(); }({}))
62+
63+
// Longstanding sequencing rules.
64+
SEQ((A(1), B(2)));
65+
SEQ((A(true) ? B(2) : throw "huh?"));
66+
SEQ((A(false) ? throw "huh?" : B(2)));
67+
SEQ(A(true) && B(true));
68+
SEQ(A(false) || B(true));
69+
70+
// From P0145R3:
71+
72+
// Rules 1 and 2 have no effect ('b' is not an expression).
73+
74+
// Rule 3: a->*b
75+
// SEQ(A(ud).*B(&UserDefined::n)); FIXME
76+
// SEQ(A(&ud)->*B(&UserDefined::n)); FIXME
77+
78+
// Rule 4: a(b1, b2, b3)
79+
// SEQ(A(f)(B(1), B(2), B(3))); FIXME
80+
81+
// Rule 5: b = a, b @= a
82+
// SEQ(B(lvalue<int>().get()) = A(0)); FIXME
83+
// SEQ(B(lvalue<UserDefined>().get()) = A(ud)); FIXME
84+
SEQ(B(lvalue<int>().get()) += A(0));
85+
// SEQ(B(lvalue<UserDefined>().get()) += A(ud)); FIXME
86+
// SEQ(B(lvalue<NonMember>().get()) += A(nm)); FIXME
87+
88+
// Rule 6: a[b]
89+
constexpr int arr[3] = {};
90+
SEQ(A(arr)[B(0)]);
91+
SEQ(A(+arr)[B(0)]);
92+
// SEQ(A(0)[B(arr)]); FIXME
93+
// SEQ(A(0)[B(+arr)]); FIXME
94+
SEQ(A(ud)[B(0)]);
95+
96+
// Rule 7: a << b
97+
SEQ(A(1) << B(2));
98+
SEQ(A(ud) << B(ud));
99+
SEQ(A(nm) << B(nm));
100+
101+
// Rule 8: a >> b
102+
SEQ(A(1) >> B(2));
103+
SEQ(A(ud) >> B(ud));
104+
SEQ(A(nm) >> B(nm));
105+
106+
// No particular order of evaluation is specified in other cases, but we in
107+
// practice evaluate left-to-right.
108+
// FIXME: Technically we're expected to check for undefined behavior due to
109+
// unsequenced read and modification and treat it as non-constant due to UB.
110+
SEQ(A(1) + B(2));
111+
SEQ(A(ud) + B(ud));
112+
SEQ(A(nm) + B(nm));
113+
SEQ(f(A(1), B(2)));
114+
#undef SEQ
115+
#undef A
116+
#undef B
117+
}

0 commit comments

Comments
 (0)