Add TupleValue::Empty for the common empty tuple case (#708)

This commit is contained in:
Jon Meow
2021-08-06 10:06:35 -07:00
committed by GitHub
parent f7d8b7f2e2
commit 5749413b28
3 changed files with 8 additions and 4 deletions
@@ -759,8 +759,7 @@ void StepPattern() {
if (act->pos == 0) {
if (tuple.Fields().empty()) {
frame->todo.Pop(1);
frame->todo.Push(Action::MakeValAction(
global_arena->New<TupleValue>(std::vector<TupleElement>())));
frame->todo.Push(Action::MakeValAction(&TupleValue::Empty()));
} else {
const Pattern* p1 = tuple.Fields()[0].pattern;
frame->todo.Push(Action::MakePatternAction(p1));
@@ -855,8 +855,7 @@ auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types, Env values)
// TODO: Check that main doesn't have any parameters.
}
auto res = TypeCheckStmt(f->body, param_res.types, values, return_type);
bool void_return = TypeEqual(
return_type, global_arena->New<TupleValue>(std::vector<TupleElement>()));
bool void_return = TypeEqual(return_type, &TupleValue::Empty());
auto body = CheckOrEnsureReturn(res.stmt, void_return, f->line_num);
return global_arena->New<FunctionDefinition>(
f->line_num, f->name, f->deduced_parameters, f->param_pattern,
+6
View File
@@ -233,6 +233,12 @@ class AlternativeValue : public Value {
// A function value.
class TupleValue : public Value {
public:
// An empty tuple, also known as the unit type.
static const TupleValue& Empty() {
static const TupleValue empty = TupleValue(std::vector<TupleElement>());
return empty;
}
explicit TupleValue(std::vector<TupleElement> elements)
: Value(Kind::TupleValue), elements(std::move(elements)) {}