Core: Return through the same value path as every other consumer

A pointer used where a value is expected is dereferenced to it, with a
null check, in helper arguments, printk arguments, binary-operation
operands and comparisons, all through deref_to_depth. return was the
one consumer that never joined that convention, so `return p` on a map
lookup emitted `ret i64*` and left it to llc to reject. handle_return
now goes through get_typed_operand, which is that path, and converts
the result to the declared return type like the augmented-assignment
store does.

convert() no longer passes a non-integer through silently: every
caller either checks both sides or is on an integer-only path, so what
reaches that branch is a type error, and it is raised with both types
named instead of surfacing from llc with an IR line number. Returning
a struct value is the case that now fails at compile time.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-18 17:52:23 +05:30
parent 359ddbd031
commit 8e802e8532
2 changed files with 14 additions and 9 deletions

View File

@ -53,7 +53,12 @@ def convert(builder, val, from_ty, to_ty):
value itself, which may already be wider than its descriptor says.
"""
if not (isinstance(to_ty, ir.IntType) and isinstance(val.type, ir.IntType)):
return val
# Every caller either checks both sides are integers or sits on a path
# that only carries integers, so reaching here is a type error that
# would otherwise surface as an llc rejection with no Python line.
raise TypeError(
f"integer conversion requested for a {val.type} value to {to_ty}"
)
if val.type.width > to_ty.width:
return builder.trunc(val, to_ty)
if val.type.width < to_ty.width:

View File

@ -333,7 +333,9 @@ def handle_if(func, compilation_context, builder, stmt, local_sym_tab):
builder.position_at_end(merge_block)
def handle_return(builder, stmt, local_sym_tab, ret_type, compilation_context=None):
def handle_return(
func, builder, stmt, local_sym_tab, ret_type, compilation_context=None
):
logger.info(f"Handling return statement: {ast.dump(stmt)}")
if stmt.value is None:
return handle_none_return(builder)
@ -358,12 +360,10 @@ def handle_return(builder, stmt, local_sym_tab, ret_type, compilation_context=No
"CompilationContext required for return statement evaluation"
)
val = eval_expr(
func=None,
compilation_context=compilation_context,
builder=builder,
expr=stmt.value,
local_sym_tab=local_sym_tab,
# A pointer to a value is dereferenced to it (null-checked), the way
# every other consumer of a value does; get_typed_operand is that path.
val = get_typed_operand(
func, compilation_context, stmt.value, builder, local_sym_tab
)
logger.info(f"Evaluated return expression to {val}")
# The declared return type is the LHS of an implicit assignment:
@ -403,7 +403,7 @@ def process_stmt(
handle_if(func, compilation_context, builder, stmt, local_sym_tab)
elif isinstance(stmt, ast.Return):
did_return = handle_return(
builder, stmt, local_sym_tab, ret_type, compilation_context
func, builder, stmt, local_sym_tab, ret_type, compilation_context
)
else:
# Silently dropping a statement makes the program mean something other