forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinitest.cc
423 lines (366 loc) · 17.3 KB
/
Minitest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "rewriter/Minitest.h"
#include "ast/Helpers.h"
#include "ast/ast.h"
#include "ast/treemap/treemap.h"
#include "core/Context.h"
#include "core/Names.h"
#include "core/core.h"
#include "core/errors/rewriter.h"
#include "rewriter/rewriter.h"
using namespace std;
namespace sorbet::rewriter {
namespace {
class ConstantMover {
uint32_t classDepth = 0;
vector<ast::ExpressionPtr> movedConstants = {};
public:
ast::ExpressionPtr createConstAssign(ast::Assign &asgn) {
auto loc = asgn.loc;
auto raiseUnimplemented = ast::MK::RaiseUnimplemented(loc);
if (auto cast = ast::cast_tree<ast::Cast>(asgn.rhs)) {
if (cast->cast == core::Names::let()) {
auto rhs = ast::MK::Let(loc, move(raiseUnimplemented), cast->typeExpr.deepCopy());
return ast::MK::Assign(asgn.loc, move(asgn.lhs), move(rhs));
}
}
return ast::MK::Assign(asgn.loc, move(asgn.lhs), move(raiseUnimplemented));
}
void postTransformAssign(core::MutableContext ctx, ast::ExpressionPtr &tree) {
auto *asgn = ast::cast_tree<ast::Assign>(tree);
if (auto *cnst = ast::cast_tree<ast::UnresolvedConstantLit>(asgn->lhs)) {
if (ast::isa_tree<ast::UnresolvedConstantLit>(asgn->rhs)) {
movedConstants.emplace_back(move(tree));
tree = ast::MK::EmptyTree();
return;
}
auto name = ast::MK::Symbol(cnst->loc, cnst->cnst);
// if the constant is already in a T.let, preserve it, otherwise decay it to unsafe
movedConstants.emplace_back(createConstAssign(*asgn));
auto module = ast::MK::Constant(asgn->loc, core::Symbols::Module());
tree = ast::MK::Send2(asgn->loc, move(module), core::Names::constSet(), asgn->loc.copyWithZeroLength(),
move(name), move(asgn->rhs));
return;
}
}
// classdefs define new constants, so we always move those if they're the "top-level" classdef (i.e. if we have
// nested classdefs, we should only move the outermost one)
void preTransformClassDef(core::MutableContext ctx, ast::ExpressionPtr &classDef) {
classDepth++;
}
void postTransformClassDef(core::MutableContext ctx, ast::ExpressionPtr &classDef) {
classDepth--;
if (classDepth == 0) {
movedConstants.emplace_back(move(classDef));
classDef = ast::MK::EmptyTree();
}
}
// we move sends if they are other minitest `describe` blocks, as those end up being classes anyway: consequently,
// we treat those the same way we treat classes
void preTransformSend(core::MutableContext ctx, ast::ExpressionPtr &tree) {
auto *send = ast::cast_tree<ast::Send>(tree);
if (send->recv.isSelfReference() && send->numPosArgs() == 1 && send->fun == core::Names::describe()) {
classDepth++;
}
}
void postTransformSend(core::MutableContext ctx, ast::ExpressionPtr &tree) {
auto *send = ast::cast_tree<ast::Send>(tree);
if (send->recv.isSelfReference() && send->numPosArgs() == 1 && send->fun == core::Names::describe()) {
classDepth--;
if (classDepth == 0) {
movedConstants.emplace_back(move(tree));
tree = ast::MK::EmptyTree();
return;
}
}
}
vector<ast::ExpressionPtr> getMovedConstants() {
return move(movedConstants);
}
ast::ExpressionPtr addConstantsToExpression(core::LocOffsets loc, ast::ExpressionPtr expr) {
auto consts = getMovedConstants();
if (consts.empty()) {
return expr;
} else {
ast::InsSeq::STATS_store stats;
for (auto &m : consts) {
stats.emplace_back(move(m));
}
return ast::MK::InsSeq(loc, std::move(stats), move(expr));
}
}
};
ast::ExpressionPtr addSigVoid(ast::ExpressionPtr expr) {
return ast::MK::InsSeq1(expr.loc(), ast::MK::SigVoid(expr.loc(), {}), std::move(expr));
}
} // namespace
ast::ExpressionPtr recurse(core::MutableContext ctx, bool isClass, ast::ExpressionPtr body);
ast::ExpressionPtr prepareBody(core::MutableContext ctx, bool isClass, ast::ExpressionPtr body) {
body = recurse(ctx, isClass, std::move(body));
if (auto bodySeq = ast::cast_tree<ast::InsSeq>(body)) {
for (auto &exp : bodySeq->stats) {
exp = recurse(ctx, isClass, std::move(exp));
}
bodySeq->expr = recurse(ctx, isClass, std::move(bodySeq->expr));
}
return body;
}
string to_s(core::Context ctx, ast::ExpressionPtr &arg) {
auto argLit = ast::cast_tree<ast::Literal>(arg);
string argString;
if (argLit != nullptr) {
if (argLit->isString()) {
return argLit->asString().show(ctx);
} else if (argLit->isSymbol()) {
return argLit->asSymbol().show(ctx);
}
}
auto argConstant = ast::cast_tree<ast::UnresolvedConstantLit>(arg);
if (argConstant != nullptr) {
return argConstant->cnst.show(ctx);
}
return arg.toString(ctx);
}
// This returns `true` for expressions which can be moved from class to method scope without changing their meaning, and
// `false` otherwise. This mostly encompasses literals (arrays, hashes, basic literals), constants, and sends that only
// involve the other things described.
bool canMoveIntoMethodDef(const ast::ExpressionPtr &exp) {
if (ast::isa_tree<ast::Literal>(exp)) {
return true;
} else if (auto *list = ast::cast_tree<ast::Array>(exp)) {
return absl::c_all_of(list->elems, [](auto &elem) { return canMoveIntoMethodDef(elem); });
} else if (auto *hash = ast::cast_tree<ast::Hash>(exp)) {
return absl::c_all_of(hash->keys, [](auto &elem) { return canMoveIntoMethodDef(elem); }) &&
absl::c_all_of(hash->values, [](auto &elem) { return canMoveIntoMethodDef(elem); });
} else if (auto *send = ast::cast_tree<ast::Send>(exp)) {
if (!canMoveIntoMethodDef(send->recv)) {
return false;
}
for (auto &arg : send->nonBlockArgs()) {
if (!canMoveIntoMethodDef(arg)) {
return false;
}
}
return true;
} else if (ast::isa_tree<ast::UnresolvedConstantLit>(exp)) {
return true;
}
return false;
}
// if the thing can be moved into a method def, then the thing we iterate over can be copied into the body of the
// method, and otherwise we replace it with a synthesized 'nil'
ast::ExpressionPtr getIteratee(ast::ExpressionPtr &exp) {
if (canMoveIntoMethodDef(exp)) {
return exp.deepCopy();
} else {
return ast::MK::RaiseUnimplemented(exp.loc());
}
}
// this applies to each statement contained within a `test_each`: if it's an `it`-block, then convert it appropriately,
// otherwise flag an error about it
ast::ExpressionPtr runUnderEach(core::MutableContext ctx, core::NameRef eachName,
const ast::InsSeq::STATS_store &destructuringStmts, ast::ExpressionPtr stmt,
ast::MethodDef::ARGS_store &args, ast::ExpressionPtr &iteratee) {
// this statement must be a send
if (auto *send = ast::cast_tree<ast::Send>(stmt)) {
// the send must be a call to `it` with a single argument (the test name) and a block with no arguments
if (send->fun == core::Names::it() && send->numPosArgs() == 1 && send->hasBlock() &&
send->block()->args.size() == 0) {
// we use this for the name of our test
auto argString = to_s(ctx, send->getPosArg(0));
auto name = ctx.state.enterNameUTF8("<it '" + argString + "'>");
// pull constants out of the block
ConstantMover constantMover;
ast::ExpressionPtr body = move(send->block()->body);
ast::TreeWalk::apply(ctx, constantMover, body);
// pull the arg and the iteratee in and synthesize `iterate.each { |arg| body }`
ast::MethodDef::ARGS_store new_args;
for (auto &arg : args) {
new_args.emplace_back(arg.deepCopy());
}
// add the destructuring statements to the block if they're present
if (!destructuringStmts.empty()) {
ast::InsSeq::STATS_store stmts;
for (auto &stmt : destructuringStmts) {
stmts.emplace_back(stmt.deepCopy());
}
body = ast::MK::InsSeq(body.loc(), std::move(stmts), std::move(body));
}
auto blk = ast::MK::Block(send->loc, move(body), std::move(new_args));
auto each = ast::MK::Send0Block(send->loc, iteratee.deepCopy(), core::Names::each(),
send->loc.copyWithZeroLength(), move(blk));
// put that into a method def named the appropriate thing
auto method = addSigVoid(ast::MK::SyntheticMethod0(send->loc, send->loc, move(name), move(each)));
// add back any moved constants
return constantMover.addConstantsToExpression(send->loc, move(method));
}
}
// if any of the above tests were not satisfied, then mark this statement as being invalid here
if (auto e = ctx.beginError(stmt.loc(), core::errors::Rewriter::BadTestEach)) {
e.setHeader("Only valid `{}`-blocks can appear within `{}`", "it", eachName.show(ctx));
}
return stmt;
}
bool isDestructuringArg(core::GlobalState &gs, const ast::MethodDef::ARGS_store &args, const ast::ExpressionPtr &expr) {
auto *local = ast::cast_tree<ast::UnresolvedIdent>(expr);
if (local == nullptr || local->kind != ast::UnresolvedIdent::Kind::Local) {
return false;
}
auto name = local->name;
if (name.kind() != core::NameKind::UNIQUE || name.dataUnique(gs)->original != core::Names::destructureArg()) {
return false;
}
return absl::c_find_if(args, [name](auto &argExpr) {
auto *arg = ast::cast_tree<ast::UnresolvedIdent>(argExpr);
return arg && arg->name == name;
}) != args.end();
}
// When given code that looks like
//
// test_each(pairs) do |(x, y)|
// # ...
// end
//
// Sorbet desugars it to essentially
//
// test_each(pairs) do |<destructureArg$1|
// x = <destructureArg$1>[0]
// y = <destructureArg$1>[1]
//
// # ...
// end
//
// which would otherwise defeat the "Only valid it-blocks can appear within test_each" error message.
//
// Because this case is so common, we have special handling to detect "contains only valid it-blocks
// plus desugared destruturing assignments."
bool isDestructuringInsSeq(core::GlobalState &gs, const ast::MethodDef::ARGS_store &args, ast::InsSeq *body) {
return absl::c_all_of(body->stats, [&gs, &args](auto &stat) {
auto *insSeq = ast::cast_tree<ast::InsSeq>(stat);
if (insSeq == nullptr) {
return false;
}
auto *assign = ast::cast_tree<ast::Assign>(insSeq->stats.front());
return assign && isDestructuringArg(gs, args, assign->rhs);
});
}
// this just walks the body of a `test_each` and tries to transform every statement
ast::ExpressionPtr prepareTestEachBody(core::MutableContext ctx, core::NameRef eachName, ast::ExpressionPtr body,
ast::MethodDef::ARGS_store &args, ast::InsSeq::STATS_store destructuringStmts,
ast::ExpressionPtr &iteratee) {
if (auto *bodySeq = ast::cast_tree<ast::InsSeq>(body)) {
if (isDestructuringInsSeq(ctx, args, bodySeq)) {
ENFORCE(destructuringStmts.empty(), "Nested destructuring statements");
destructuringStmts.reserve(bodySeq->stats.size());
std::move(bodySeq->stats.begin(), bodySeq->stats.end(), std::back_inserter(destructuringStmts));
return prepareTestEachBody(ctx, eachName, std::move(bodySeq->expr), args, std::move(destructuringStmts),
iteratee);
}
for (auto &exp : bodySeq->stats) {
exp = runUnderEach(ctx, eachName, destructuringStmts, std::move(exp), args, iteratee);
}
bodySeq->expr = runUnderEach(ctx, eachName, destructuringStmts, std::move(bodySeq->expr), args, iteratee);
} else {
body = runUnderEach(ctx, eachName, destructuringStmts, std::move(body), args, iteratee);
}
return body;
}
ast::ExpressionPtr runSingle(core::MutableContext ctx, bool isClass, ast::Send *send) {
if (!send->hasBlock()) {
return nullptr;
}
auto *block = send->block();
if (!send->recv.isSelfReference()) {
return nullptr;
}
if ((send->fun == core::Names::testEach() || send->fun == core::Names::testEachHash()) && send->numPosArgs() == 1) {
if ((send->fun == core::Names::testEach() && block->args.size() < 1) ||
(send->fun == core::Names::testEachHash() && block->args.size() != 2)) {
if (auto e = ctx.beginError(block->loc, core::errors::Rewriter::BadTestEach)) {
e.setHeader("Wrong number of parameters for `{}` block: expected `{}`, got `{}`", send->fun.show(ctx),
send->fun == core::Names::testEach() ? "at least 1" : "2", block->args.size());
}
return nullptr;
}
// if this has the form `test_each(expr) { |arg | .. }`, then start by trying to convert `expr` into a thing we
// can freely copy into methoddef scope
auto iteratee = getIteratee(send->getPosArg(0));
// and then reconstruct the send but with a modified body
return ast::MK::Send(
send->loc, ast::MK::Self(send->loc), send->fun, send->funLoc, 1,
ast::MK::SendArgs(
move(send->getPosArg(0)),
ast::MK::Block(block->loc,
prepareTestEachBody(ctx, send->fun, std::move(block->body), block->args, {}, iteratee),
std::move(block->args))),
send->flags);
}
if (send->fun == core::Names::testEachHash() && send->numKwArgs() > 0) {
auto errLoc = send->getKwKey(0).loc().join(send->getKwValue(send->numKwArgs() - 1).loc());
if (auto e = ctx.beginError(errLoc, core::errors::Rewriter::BadTestEach)) {
e.setHeader("`{}` expects a single `{}` argument, not keyword args", "test_each_hash", "Hash");
if (send->numPosArgs() == 0 && errLoc.exists()) {
auto replaceLoc = ctx.locAt(errLoc);
e.replaceWith("Wrap with curly braces", replaceLoc, "{{{}}}", replaceLoc.source(ctx).value());
}
}
}
if (send->numPosArgs() == 0 && (send->fun == core::Names::before() || send->fun == core::Names::after())) {
auto name = send->fun == core::Names::after() ? core::Names::afterAngles() : core::Names::initialize();
ConstantMover constantMover;
ast::TreeWalk::apply(ctx, constantMover, block->body);
auto method = addSigVoid(
ast::MK::SyntheticMethod0(send->loc, send->loc, name, prepareBody(ctx, isClass, std::move(block->body))));
return constantMover.addConstantsToExpression(send->loc, move(method));
}
if (send->numPosArgs() != 1) {
return nullptr;
}
auto &arg = send->getPosArg(0);
auto argString = to_s(ctx, arg);
if (send->fun == core::Names::describe()) {
ast::ClassDef::ANCESTORS_store ancestors;
// Avoid subclassing the containing context when it's a module, as that will produce an error in typed: false
// files
if (isClass) {
ancestors.emplace_back(ast::MK::Self(arg.loc()));
}
ast::ClassDef::RHS_store rhs;
const bool bodyIsClass = true;
rhs.emplace_back(prepareBody(ctx, bodyIsClass, std::move(block->body)));
auto name = ast::MK::UnresolvedConstant(arg.loc(), ast::MK::EmptyTree(),
ctx.state.enterNameConstant("<describe '" + argString + "'>"));
return ast::MK::Class(send->loc, send->loc, std::move(name), std::move(ancestors), std::move(rhs));
} else if (send->fun == core::Names::it()) {
ConstantMover constantMover;
ast::TreeWalk::apply(ctx, constantMover, block->body);
auto name = ctx.state.enterNameUTF8("<it '" + argString + "'>");
const bool bodyIsClass = false;
auto method = addSigVoid(ast::MK::SyntheticMethod0(send->loc, send->loc, std::move(name),
prepareBody(ctx, bodyIsClass, std::move(block->body))));
method = ast::MK::InsSeq1(send->loc, send->getPosArg(0).deepCopy(), move(method));
return constantMover.addConstantsToExpression(send->loc, move(method));
}
return nullptr;
}
ast::ExpressionPtr recurse(core::MutableContext ctx, bool isClass, ast::ExpressionPtr body) {
auto bodySend = ast::cast_tree<ast::Send>(body);
if (bodySend) {
auto change = runSingle(ctx, isClass, bodySend);
if (change) {
return change;
}
}
return body;
}
vector<ast::ExpressionPtr> Minitest::run(core::MutableContext ctx, bool isClass, ast::Send *send) {
vector<ast::ExpressionPtr> stats;
if (ctx.state.runningUnderAutogen) {
return stats;
}
auto exp = runSingle(ctx, isClass, send);
if (exp != nullptr) {
stats.emplace_back(std::move(exp));
}
return stats;
}
}; // namespace sorbet::rewriter