Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/tests/LValue.test.cpp
2723 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/TypeInfer.h"
3
4
#include "Fixture.h"
5
#include "ScopedFlags.h"
6
7
#include "doctest.h"
8
9
using namespace Luau;
10
11
static void merge(TypeArena& arena, RefinementMap& l, const RefinementMap& r)
12
{
13
Luau::merge(
14
l,
15
r,
16
[&arena](TypeId a, TypeId b) -> TypeId
17
{
18
// TODO: normalize here also.
19
std::unordered_set<TypeId> s;
20
21
if (auto utv = get<UnionType>(follow(a)))
22
s.insert(begin(utv), end(utv));
23
else
24
s.insert(a);
25
26
if (auto utv = get<UnionType>(follow(b)))
27
s.insert(begin(utv), end(utv));
28
else
29
s.insert(b);
30
31
std::vector<TypeId> options(s.begin(), s.end());
32
return options.size() == 1 ? options[0] : arena.addType(UnionType{std::move(options)});
33
}
34
);
35
}
36
37
static LValue mkSymbol(const std::string& s)
38
{
39
return Symbol{AstName{s.data()}};
40
}
41
42
struct LValueFixture
43
{
44
BuiltinTypes builtinTypes;
45
};
46
47
TEST_SUITE_BEGIN("LValue");
48
49
TEST_CASE_FIXTURE(LValueFixture, "Luau_merge_hashmap_order")
50
{
51
std::string a = "a";
52
std::string b = "b";
53
std::string c = "c";
54
55
RefinementMap m{{
56
{mkSymbol(b), builtinTypes.stringType},
57
{mkSymbol(c), builtinTypes.numberType},
58
}};
59
60
RefinementMap other{{
61
{mkSymbol(a), builtinTypes.stringType},
62
{mkSymbol(b), builtinTypes.stringType},
63
{mkSymbol(c), builtinTypes.booleanType},
64
}};
65
66
TypeArena arena;
67
merge(arena, m, other);
68
69
REQUIRE_EQ(3, m.size());
70
REQUIRE(m.count(mkSymbol(a)));
71
REQUIRE(m.count(mkSymbol(b)));
72
REQUIRE(m.count(mkSymbol(c)));
73
74
CHECK_EQ("string", toString(m[mkSymbol(a)]));
75
CHECK_EQ("string", toString(m[mkSymbol(b)]));
76
CHECK_EQ("boolean | number", toString(m[mkSymbol(c)]));
77
}
78
79
TEST_CASE_FIXTURE(LValueFixture, "Luau_merge_hashmap_order2")
80
{
81
std::string a = "a";
82
std::string b = "b";
83
std::string c = "c";
84
85
RefinementMap m{{
86
{mkSymbol(a), builtinTypes.stringType},
87
{mkSymbol(b), builtinTypes.stringType},
88
{mkSymbol(c), builtinTypes.numberType},
89
}};
90
91
RefinementMap other{{
92
{mkSymbol(b), builtinTypes.stringType},
93
{mkSymbol(c), builtinTypes.booleanType},
94
}};
95
96
TypeArena arena;
97
merge(arena, m, other);
98
99
REQUIRE_EQ(3, m.size());
100
REQUIRE(m.count(mkSymbol(a)));
101
REQUIRE(m.count(mkSymbol(b)));
102
REQUIRE(m.count(mkSymbol(c)));
103
104
CHECK_EQ("string", toString(m[mkSymbol(a)]));
105
CHECK_EQ("string", toString(m[mkSymbol(b)]));
106
CHECK_EQ("boolean | number", toString(m[mkSymbol(c)]));
107
}
108
109
TEST_CASE_FIXTURE(LValueFixture, "one_map_has_overlap_at_end_whereas_other_has_it_in_start")
110
{
111
std::string a = "a";
112
std::string b = "b";
113
std::string c = "c";
114
std::string d = "d";
115
std::string e = "e";
116
117
RefinementMap m{{
118
{mkSymbol(a), builtinTypes.stringType},
119
{mkSymbol(b), builtinTypes.numberType},
120
{mkSymbol(c), builtinTypes.booleanType},
121
}};
122
123
RefinementMap other{{
124
{mkSymbol(c), builtinTypes.stringType},
125
{mkSymbol(d), builtinTypes.numberType},
126
{mkSymbol(e), builtinTypes.booleanType},
127
}};
128
129
TypeArena arena;
130
merge(arena, m, other);
131
132
REQUIRE_EQ(5, m.size());
133
REQUIRE(m.count(mkSymbol(a)));
134
REQUIRE(m.count(mkSymbol(b)));
135
REQUIRE(m.count(mkSymbol(c)));
136
REQUIRE(m.count(mkSymbol(d)));
137
REQUIRE(m.count(mkSymbol(e)));
138
139
CHECK_EQ("string", toString(m[mkSymbol(a)]));
140
CHECK_EQ("number", toString(m[mkSymbol(b)]));
141
CHECK_EQ("boolean | string", toString(m[mkSymbol(c)]));
142
CHECK_EQ("number", toString(m[mkSymbol(d)]));
143
CHECK_EQ("boolean", toString(m[mkSymbol(e)]));
144
}
145
146
TEST_CASE_FIXTURE(LValueFixture, "hashing_lvalue_global_prop_access")
147
{
148
std::string t1 = "t";
149
std::string x1 = "x";
150
151
LValue t_x1{Field{std::make_shared<LValue>(Symbol{AstName{t1.data()}}), x1}};
152
153
std::string t2 = "t";
154
std::string x2 = "x";
155
156
LValue t_x2{Field{std::make_shared<LValue>(Symbol{AstName{t2.data()}}), x2}};
157
158
CHECK_EQ(t_x1, t_x1);
159
CHECK_EQ(t_x1, t_x2);
160
CHECK_EQ(t_x2, t_x2);
161
162
CHECK_EQ(LValueHasher{}(t_x1), LValueHasher{}(t_x1));
163
CHECK_EQ(LValueHasher{}(t_x1), LValueHasher{}(t_x2));
164
CHECK_EQ(LValueHasher{}(t_x2), LValueHasher{}(t_x2));
165
166
RefinementMap m;
167
m[t_x1] = builtinTypes.stringType;
168
m[t_x2] = builtinTypes.numberType;
169
170
CHECK_EQ(1, m.size());
171
}
172
173
TEST_CASE_FIXTURE(LValueFixture, "hashing_lvalue_local_prop_access")
174
{
175
std::string t1 = "t";
176
std::string x1 = "x";
177
178
AstLocal localt1{AstName{t1.data()}, Location(), nullptr, 0, 0, nullptr, false};
179
LValue t_x1{Field{std::make_shared<LValue>(Symbol{&localt1}), x1}};
180
181
std::string t2 = "t";
182
std::string x2 = "x";
183
184
AstLocal localt2{AstName{t2.data()}, Location(), &localt1, 0, 0, nullptr, false};
185
LValue t_x2{Field{std::make_shared<LValue>(Symbol{&localt2}), x2}};
186
187
CHECK_EQ(t_x1, t_x1);
188
CHECK_NE(t_x1, t_x2);
189
CHECK_EQ(t_x2, t_x2);
190
191
CHECK_EQ(LValueHasher{}(t_x1), LValueHasher{}(t_x1));
192
CHECK_NE(LValueHasher{}(t_x1), LValueHasher{}(t_x2));
193
CHECK_EQ(LValueHasher{}(t_x2), LValueHasher{}(t_x2));
194
195
RefinementMap m;
196
m[t_x1] = builtinTypes.stringType;
197
m[t_x2] = builtinTypes.numberType;
198
199
CHECK_EQ(2, m.size());
200
}
201
202
TEST_SUITE_END();
203
204