forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstantAssumeType.cc
43 lines (34 loc) · 1.17 KB
/
ConstantAssumeType.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
#include "rewriter/ConstantAssumeType.h"
#include "ast/Helpers.h"
#include "ast/ast.h"
#include "core/Names.h"
#include "core/core.h"
using namespace std;
namespace sorbet::rewriter {
void ConstantAssumeType::run(core::MutableContext ctx, ast::Assign *asgn) {
if (ctx.state.runningUnderAutogen) {
return;
}
if (ctx.file.data(ctx).strictLevel <= core::StrictLevel::False) {
// Only do this transformation in files that are typed: true or higher, so that we know that
// if this assumption about the type is wrong, that it will get checked down the line.
return;
}
auto lhs = ast::cast_tree<ast::UnresolvedConstantLit>(asgn->lhs);
if (lhs == nullptr) {
return;
}
auto send = ast::cast_tree<ast::Send>(asgn->rhs);
if (send == nullptr) {
return;
}
if (send->fun != core::Names::new_()) {
return;
}
if (!(ast::isa_tree<ast::UnresolvedConstantLit>(send->recv) || ast::isa_tree<ast::ConstantLit>(send->recv))) {
return;
}
auto type = send->recv.deepCopy();
asgn->rhs = ast::MK::AssumeType(asgn->rhs.loc(), move(asgn->rhs), move(type));
}
}; // namespace sorbet::rewriter