Skip to content

fix(ir): derive internal function return types from declarations#56

Merged
brockelmore merged 2 commits into
mainfrom
nox/internal-return-type
Mar 9, 2026
Merged

fix(ir): derive internal function return types from declarations#56
brockelmore merged 2 commits into
mainfrom
nox/internal-return-type

Conversation

@N0xMare
Copy link
Copy Markdown
Collaborator

@N0xMare N0xMare commented Mar 8, 2026

Summary

Fixes #49 — Internal (non-pub) functions had their return type hardcoded to u256 in IR lowering, regardless of their actual declaration. Functions returning bool, addr, void, or tuples all got wrong IR type annotations.

The Bug

crates/ir/src/to_egglog/function.rs in lower_internal_function_body():

let out_ty = EvmType::Base(EvmBaseType::UIntT(256)); // TODO: derive from return type

This meant every internal function call site assumed a u256 return, which could silently corrupt values for functions returning bool, addr, or other types.

The Fix

Thread the declared return type through the call chain:

  1. ContractFunction type — extended from 3-tuple to 4-tuple, adding Vec<TypeSig> for returns
  2. emit_call — now accepts and forwards returns to lower_internal_function_body
  3. lower_internal_function_body — replaces hardcoded UIntT(256) with self.returns_to_type(returns)

This mirrors what lower_function() already does correctly for standalone free functions — returns_to_type() handles all cases: 0 returns → UnitT, 1 return → lower_type_sig, multiple → TupleT.

Files Changed

File Change
crates/ir/src/to_egglog/mod.rs ContractFunction type + population site
crates/ir/src/to_egglog/function.rs lower_internal_function_body signature + return type derivation
crates/ir/src/to_egglog/calls.rs emit_call signature + all call sites

Tests Added

3 e2e tests verifying internal function return types work correctly at runtime:

  1. Bool return — internal check_positive(x) -> (bool) called from pub fn; verifies both true and false cases
  2. U256 return (regression) — internal double(x) -> (u256) still works correctly
  3. Void/storage — internal function with storage side effects works correctly

All 656+ workspace tests pass.

@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 8, 2026

Deploy Preview for edgelang ready!

Name Link
🔨 Latest commit e66016d
🔍 Latest deploy log https://app.netlify.com/projects/edgelang/deploys/69ae47e7e505c00008e75a7a
😎 Deploy Preview https://deploy-preview-56--edgelang.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@brockelmore
Copy link
Copy Markdown
Collaborator

lgtm

@brockelmore brockelmore merged commit 096b488 into main Mar 9, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: internal function return type hardcoded to u256 in IR lowering

2 participants