From 70dc0c15fd244bdc4a769b0d91b50ecc210bca65 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 13:43:35 +0100 Subject: [PATCH] Improve type hint for #load to allow for string types --- src/check_builtin.cpp | 8 ++++++-- src/check_expr.cpp | 14 ++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index b0c7f8d4b..122d3a461 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1196,10 +1196,14 @@ LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, As GB_ASSERT(o.value.kind == ExactValue_String); + operand->type = t_u8_slice; + if (type_hint && is_type_string(type_hint)) { + operand->type = type_hint; + } + operand->mode = Addressing_Constant; + LoadFileCache *cache = nullptr; if (cache_load_file_directive(c, call, o.value.value_string, err_on_not_found, &cache)) { - operand->type = t_u8_slice; - operand->mode = Addressing_Constant; operand->value = exact_value_string(cache->data); return LoadDirective_Success; } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index ae459574c..076eaba73 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7435,10 +7435,6 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type // NOTE(bill, 2022-08-11): edge case to handle #load(path) or_else default if (is_load_directive_call(arg)) { LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false); - if (res == LoadDirective_Success) { - *o = x; - return Expr_Expr; - } bool y_is_diverging = false; check_expr_base(c, &y, default_value, x.type); @@ -7468,10 +7464,16 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type if (!y_is_diverging) { check_assignment(c, &y, x.type, name); + if (y.mode != Addressing_Constant) { + error(y.expr, "expected a constant expression on the right-hand side of 'or_else' in conjuction with '#load'"); + } } - o->mode = y.mode; - o->type = y.type; + if (res == LoadDirective_Success) { + *o = x; + } else { + *o = y; + } o->expr = node; return Expr_Expr;