mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Tuple restrictions (#486)
* implement tuple restrictions: (1) named fields after positional, (2) order matters * after_named_member => seen_named_member
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/interpreter/interpreter.h"
|
||||
#include "executable_semantics/tracing_flag.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -96,6 +97,26 @@ auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
// whether it's a position that expects a value, a pattern, or a type.
|
||||
auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
const Value* expected, TCContext context) -> TCResult {
|
||||
if (tracing_output) {
|
||||
switch (context) {
|
||||
case TCContext::ValueContext:
|
||||
std::cout << "checking expression ";
|
||||
break;
|
||||
case TCContext::PatternContext:
|
||||
std::cout << "checking pattern, ";
|
||||
if (expected) {
|
||||
std::cout << "expecting ";
|
||||
PrintValue(expected, std::cerr);
|
||||
}
|
||||
std::cout << ", ";
|
||||
break;
|
||||
case TCContext::TypeContext:
|
||||
std::cout << "checking type ";
|
||||
break;
|
||||
}
|
||||
PrintExp(e);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
switch (e->tag) {
|
||||
case ExpressionKind::PatternVariable: {
|
||||
if (context != TCContext::PatternContext) {
|
||||
@@ -116,6 +137,8 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
} else {
|
||||
t = expected;
|
||||
}
|
||||
} else if (expected) {
|
||||
ExpectType(e->line_num, "pattern variable", t, expected);
|
||||
}
|
||||
auto new_e = MakeVarPat(e->line_num, *e->u.pattern_variable.name,
|
||||
ReifyType(t, e->line_num));
|
||||
@@ -153,19 +176,32 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
new std::vector<std::pair<std::string, const Expression*>>();
|
||||
auto arg_types = new std::vector<std::pair<std::string, Address>>();
|
||||
auto new_types = types;
|
||||
if (expected && expected->tag != ValKind::TupleV) {
|
||||
std::cerr << e->line_num << ": compilation error, didn't expect a tuple"
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
if (expected &&
|
||||
e->u.tuple.fields->size() != expected->u.tuple.elts->size()) {
|
||||
std::cerr << e->line_num
|
||||
<< ": compilation error, tuples of different length"
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
int i = 0;
|
||||
for (auto arg = e->u.tuple.fields->begin();
|
||||
arg != e->u.tuple.fields->end(); ++arg, ++i) {
|
||||
const Value* arg_expected = nullptr;
|
||||
if (expected && expected->tag == ValKind::TupleV) {
|
||||
std::optional<Address> expected_field =
|
||||
FindTupleField(arg->first, expected);
|
||||
if (expected_field == std::nullopt) {
|
||||
std::cerr << e->line_num << ": compilation error, missing field "
|
||||
<< arg->first << std::endl;
|
||||
if ((*expected->u.tuple.elts)[i].first != arg->first) {
|
||||
std::cerr << e->line_num
|
||||
<< ": compilation error, field names do not match, "
|
||||
<< "expected " << (*expected->u.tuple.elts)[i].first
|
||||
<< " but got " << arg->first << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
arg_expected = state->ReadFromMemory(*expected_field, e->line_num);
|
||||
arg_expected = state->ReadFromMemory(
|
||||
(*expected->u.tuple.elts)[i].second, e->line_num);
|
||||
}
|
||||
auto arg_res =
|
||||
TypeCheckExp(arg->second, new_types, values, arg_expected, context);
|
||||
|
||||
@@ -339,13 +339,12 @@ auto TypeEqual(const Value* t1, const Value* t2) -> bool {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < t1->u.tuple.elts->size(); ++i) {
|
||||
std::optional<Address> t2_field =
|
||||
FindTupleField((*t1->u.tuple.elts)[i].first, t2);
|
||||
if (t2_field == std::nullopt) {
|
||||
if ((*t1->u.tuple.elts)[i].first != (*t2->u.tuple.elts)[i].first) {
|
||||
return false;
|
||||
}
|
||||
if (!TypeEqual(state->ReadFromMemory((*t1->u.tuple.elts)[i].second, 0),
|
||||
state->ReadFromMemory(*t2_field, 0))) {
|
||||
if (!TypeEqual(
|
||||
state->ReadFromMemory((*t1->u.tuple.elts)[i].second, 0),
|
||||
state->ReadFromMemory((*t2->u.tuple.elts)[i].second, 0))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -354,9 +353,13 @@ auto TypeEqual(const Value* t1, const Value* t2) -> bool {
|
||||
case ValKind::IntTV:
|
||||
case ValKind::BoolTV:
|
||||
case ValKind::ContinuationTV:
|
||||
case ValKind::TypeTV:
|
||||
return true;
|
||||
default:
|
||||
std::cerr << "TypeEqual used to compare non-type values" << std::endl;
|
||||
PrintValue(t1, std::cerr);
|
||||
std::cerr << std::endl;
|
||||
PrintValue(t2, std::cerr);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user