|
|
@@ -147,12 +147,18 @@ static auto ArgumentDeduction(SourceLocation source_loc, TypeEnv deduced,
|
|
|
}
|
|
|
case Value::Kind::TupleValue: {
|
|
|
if (arg->kind() != Value::Kind::TupleValue) {
|
|
|
- ExpectType(source_loc, "argument deduction", param, arg);
|
|
|
+ FATAL_COMPILATION_ERROR(source_loc)
|
|
|
+ << "type error in argument deduction\n"
|
|
|
+ << "expected: " << *param << "\n"
|
|
|
+ << "actual: " << *arg;
|
|
|
}
|
|
|
const auto& param_tup = cast<TupleValue>(*param);
|
|
|
const auto& arg_tup = cast<TupleValue>(*arg);
|
|
|
if (param_tup.Elements().size() != arg_tup.Elements().size()) {
|
|
|
- ExpectType(source_loc, "argument deduction", param, arg);
|
|
|
+ FATAL_COMPILATION_ERROR(source_loc)
|
|
|
+ << "mismatch in tuple sizes, expected "
|
|
|
+ << param_tup.Elements().size() << " but got "
|
|
|
+ << arg_tup.Elements().size();
|
|
|
}
|
|
|
for (size_t i = 0; i < param_tup.Elements().size(); ++i) {
|
|
|
if (param_tup.Elements()[i].name != arg_tup.Elements()[i].name) {
|
|
|
@@ -168,12 +174,18 @@ static auto ArgumentDeduction(SourceLocation source_loc, TypeEnv deduced,
|
|
|
}
|
|
|
case Value::Kind::StructType: {
|
|
|
if (arg->kind() != Value::Kind::StructType) {
|
|
|
- ExpectType(source_loc, "argument deduction", param, arg);
|
|
|
+ FATAL_COMPILATION_ERROR(source_loc)
|
|
|
+ << "type error in argument deduction\n"
|
|
|
+ << "expected: " << *param << "\n"
|
|
|
+ << "actual: " << *arg;
|
|
|
}
|
|
|
const auto& param_struct = cast<StructType>(*param);
|
|
|
const auto& arg_struct = cast<StructType>(*arg);
|
|
|
if (param_struct.fields().size() != arg_struct.fields().size()) {
|
|
|
- ExpectType(source_loc, "argument deduction", param, arg);
|
|
|
+ FATAL_COMPILATION_ERROR(source_loc)
|
|
|
+ << "mismatch in struct field counts, expected "
|
|
|
+ << param_struct.fields().size() << " but got "
|
|
|
+ << arg_struct.fields().size();
|
|
|
}
|
|
|
for (size_t i = 0; i < param_struct.fields().size(); ++i) {
|
|
|
if (param_struct.fields()[i].first != arg_struct.fields()[i].first) {
|
|
|
@@ -189,7 +201,10 @@ static auto ArgumentDeduction(SourceLocation source_loc, TypeEnv deduced,
|
|
|
}
|
|
|
case Value::Kind::FunctionType: {
|
|
|
if (arg->kind() != Value::Kind::FunctionType) {
|
|
|
- ExpectType(source_loc, "argument deduction", param, arg);
|
|
|
+ FATAL_COMPILATION_ERROR(source_loc)
|
|
|
+ << "type error in argument deduction\n"
|
|
|
+ << "expected: " << *param << "\n"
|
|
|
+ << "actual: " << *arg;
|
|
|
}
|
|
|
const auto& param_fn = cast<FunctionType>(*param);
|
|
|
const auto& arg_fn = cast<FunctionType>(*arg);
|
|
|
@@ -202,7 +217,10 @@ static auto ArgumentDeduction(SourceLocation source_loc, TypeEnv deduced,
|
|
|
}
|
|
|
case Value::Kind::PointerType: {
|
|
|
if (arg->kind() != Value::Kind::PointerType) {
|
|
|
- ExpectType(source_loc, "argument deduction", param, arg);
|
|
|
+ FATAL_COMPILATION_ERROR(source_loc)
|
|
|
+ << "type error in argument deduction\n"
|
|
|
+ << "expected: " << *param << "\n"
|
|
|
+ << "actual: " << *arg;
|
|
|
}
|
|
|
return ArgumentDeduction(source_loc, deduced,
|
|
|
cast<PointerType>(*param).Type(),
|
|
|
@@ -341,11 +359,11 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
|
|
std::vector<FieldInitializer> new_args;
|
|
|
std::vector<TupleElement> arg_types;
|
|
|
auto new_types = types;
|
|
|
- for (const auto& arg : cast<TupleLiteral>(*e).Fields()) {
|
|
|
- auto arg_res = TypeCheckExp(arg.expression, new_types, values);
|
|
|
+ for (auto& arg : cast<TupleLiteral>(*e).fields()) {
|
|
|
+ auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
|
|
|
new_types = arg_res.types;
|
|
|
- new_args.push_back(FieldInitializer(arg.name, arg_res.exp));
|
|
|
- arg_types.push_back({.name = arg.name, .value = arg_res.type});
|
|
|
+ new_args.push_back(FieldInitializer(arg.name(), arg_res.exp));
|
|
|
+ arg_types.push_back({.name = arg.name(), .value = arg_res.type});
|
|
|
}
|
|
|
auto tuple_e = arena->New<TupleLiteral>(e->source_loc(), new_args);
|
|
|
auto tuple_t = arena->New<TupleValue>(std::move(arg_types));
|
|
|
@@ -355,26 +373,26 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
|
|
std::vector<FieldInitializer> new_args;
|
|
|
VarValues arg_types;
|
|
|
auto new_types = types;
|
|
|
- for (const auto& arg : cast<StructLiteral>(*e).fields()) {
|
|
|
- auto arg_res = TypeCheckExp(arg.expression, new_types, values);
|
|
|
+ for (auto& arg : cast<StructLiteral>(*e).fields()) {
|
|
|
+ auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
|
|
|
new_types = arg_res.types;
|
|
|
- new_args.push_back(FieldInitializer(arg.name, arg_res.exp));
|
|
|
- arg_types.push_back({arg.name, arg_res.type});
|
|
|
+ new_args.push_back(FieldInitializer(arg.name(), arg_res.exp));
|
|
|
+ arg_types.push_back({arg.name(), arg_res.type});
|
|
|
}
|
|
|
auto new_e = arena->New<StructLiteral>(e->source_loc(), new_args);
|
|
|
auto type = arena->New<StructType>(std::move(arg_types));
|
|
|
return TCExpression(new_e, type, new_types);
|
|
|
}
|
|
|
case Expression::Kind::StructTypeLiteral: {
|
|
|
- const auto& struct_type = cast<StructTypeLiteral>(*e);
|
|
|
+ auto& struct_type = cast<StructTypeLiteral>(*e);
|
|
|
std::vector<FieldInitializer> new_args;
|
|
|
auto new_types = types;
|
|
|
- for (const auto& arg : struct_type.fields()) {
|
|
|
- auto arg_res = TypeCheckExp(arg.expression, new_types, values);
|
|
|
+ for (auto& arg : struct_type.fields()) {
|
|
|
+ auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
|
|
|
new_types = arg_res.types;
|
|
|
Nonnull<const Value*> type = interpreter.InterpExp(values, arg_res.exp);
|
|
|
new_args.push_back(
|
|
|
- FieldInitializer(arg.name, ReifyType(type, e->source_loc())));
|
|
|
+ FieldInitializer(arg.name(), ReifyType(type, e->source_loc())));
|
|
|
}
|
|
|
auto new_e = arena->New<StructTypeLiteral>(e->source_loc(), new_args);
|
|
|
Nonnull<const Value*> type;
|