Skip to content

Commit f1ecced

Browse files
committed
Cleaned up some code and fixed a recently introduced comforming/polymorphic-matching bug.
1 parent ce55ad0 commit f1ecced

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

include/UTIL/util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ void indent(FILE *file, length_t indentation_level);
8888
// Returns whether a string starts with another string
8989
bool string_starts_with(weak_cstr_t original, weak_cstr_t stub);
9090

91+
// ---------------- length_min ----------------
92+
// Returns the smaller of two length values
93+
static inline length_t length_min(length_t a, length_t b){
94+
return a <= b ? a : b;
95+
}
96+
9197
// ---------------- length_max ----------------
9298
// Returns the larger of two length values
9399
static inline length_t length_max(length_t a, length_t b){

src/IRGEN/ir_gen_args.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "IRGEN/ir_gen_type.h"
2020
#include "UTIL/ground.h"
2121
#include "UTIL/trait.h"
22+
#include "UTIL/util.h"
2223

2324
successful_t func_args_match(ast_func_t *func, ast_type_t *type_list, length_t type_list_length){
2425
ast_type_t *arg_types = func->arg_types;
@@ -105,11 +106,13 @@ errorcode_t func_args_polymorphable(ir_builder_t *builder, ast_func_t *poly_temp
105106
length_t required_arity = poly_template->arity;
106107

107108
// Ensure argument supplied meet length requirements
108-
if(
109-
required_arity < type_list_length &&
110-
!(poly_template->traits & AST_FUNC_VARARG) &&
111-
(conform_mode & CONFORM_MODE_VARIADIC ? !(poly_template->traits & AST_FUNC_VARIADIC) : true)
112-
) return FAILURE;
109+
if(required_arity < type_list_length){
110+
if(!(poly_template->traits & AST_FUNC_VARARG)){
111+
if(!(conform_mode & CONFORM_MODE_VARIADIC) || !(poly_template->traits & AST_FUNC_VARIADIC)){
112+
return FAILURE;
113+
}
114+
}
115+
}
113116

114117
// Determine whether we are missing arguments
115118
bool requires_use_of_defaults = required_arity > type_list_length;
@@ -150,7 +153,9 @@ errorcode_t func_args_polymorphable(ir_builder_t *builder, ast_func_t *poly_temp
150153
// Number of polymorphic paramater types that have been processed (used for cleanup)
151154
length_t i;
152155

153-
for(i = 0; i != required_arity; i++){
156+
length_t num_conforms = length_min(type_list_length, required_arity);
157+
158+
for(i = 0; i != num_conforms; i++){
154159
if(ast_type_has_polymorph(&poly_template_arg_types[i]))
155160
res = ir_gen_polymorphable(builder->compiler, builder->object, &poly_template_arg_types[i], &arg_types[i], &catalog, true);
156161
else
@@ -176,17 +181,22 @@ errorcode_t func_args_polymorphable(ir_builder_t *builder, ast_func_t *poly_temp
176181
goto polymorphic_failure;
177182
}
178183

179-
bool meets_return_matching_requirement = ast_types_identical(gives, &concrete_return_type);
184+
bool matches_return_type = ast_types_identical(gives, &concrete_return_type);
180185
ast_type_free(&concrete_return_type);
181186

182-
if(!meets_return_matching_requirement){
187+
if(!matches_return_type){
188+
compiler_panicf(builder->compiler, gives->source, "Unable to match requested return type with callee's return type");
183189
res = FAILURE;
184190
goto polymorphic_failure;
185191
}
186192
}
187193

188-
if(out_catalog) *out_catalog = catalog;
189-
else ast_poly_catalog_free(&catalog);
194+
if(out_catalog){
195+
*out_catalog = catalog;
196+
} else {
197+
ast_poly_catalog_free(&catalog);
198+
}
199+
190200
free(arg_value_list_unmodified);
191201
return SUCCESS;
192202

0 commit comments

Comments
 (0)