Skip to content

Fix returnCount arity for sum-returning HULL functions#486

Merged
rodrigogribeiro merged 1 commit into
argotorg:mainfrom
axic:hull-returncount-arity
Jun 29, 2026
Merged

Fix returnCount arity for sum-returning HULL functions#486
rodrigogribeiro merged 1 commit into
argotorg:mainfrom
axic:hull-returncount-arity

Conversation

@axic

@axic axic commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

returnCount in Language/Hull/TcEnv.hs collapsed every non-TFun/TPair type to a single word slot, so a sum-returning function called from a user asm block passed HULL's YAssign/YLet arity check (1 == 1). yule's sizeOf, however, lowers a TSum return as 1 + max (sizeOf t1) (sizeOf t2) words, emitting a multi-word Yul return into a single-slot LHS that solc later rejects.

Make returnCount mirror yule's sizeOf exactly by adding explicit TSum, TSumN and TBool cases (and an explicit TFun error), so the asm arity check agrees with the eventual Yul lowering.

Add a Hull type-checker test (11-err-asm-sum-return.hull) that assigns a word + word returning function into a single word in an assembly block; it now correctly fails the return-count check.

returnCount in Language/Hull/TcEnv.hs collapsed every non-TFun/TPair
type to a single word slot, so a sum-returning function called from a
user asm block passed HULL's YAssign/YLet arity check (1 == 1). yule's
sizeOf, however, lowers a TSum return as `1 + max (sizeOf t1) (sizeOf
t2)` words, emitting a multi-word Yul return into a single-slot LHS that
solc later rejects.

Make returnCount mirror yule's sizeOf exactly by adding explicit TSum,
TSumN and TBool cases (and an explicit TFun error), so the asm arity
check agrees with the eventual Yul lowering.

Add a Hull type-checker test (11-err-asm-sum-return.hull) that assigns a
`word + word` returning function into a single word in an assembly block;
it now correctly fails the return-count check.

Co-Authored-By: Alex Beregszaszi <alex@rtfs.hu>
returnCount TBool = 1
returnCount (TPair a b) = returnCount a + returnCount b
returnCount (TSum a b) = 1 + max (returnCount a) (returnCount b)
returnCount (TSumN ts) = 1 + maximum (map returnCount ts)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if these are correct?

I think the error below should be enough?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compressed sums can change the outcome and with the current pipeline we have no way of knowing whether the compression will be enabled or not.

See test/examples/spec/037dwarves.solc for a bigger sum example (compile with -s -g --pe-fuel 0 to see sums in the Hull output).

There is also the comment

-- Collapsing them to 1 here lets a sum-returning function be assigned into a
-- single-word asm LHS that yule then lowers to a multi-word Yul return.

(from the original code, but unchanged in this PR) which I do not understand, @rodrigogribeiro, can you help?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having thought about this for a while, I think a proper fix might be to remove unoptimised sums from yule and change the returnCount formula or TNamed appropriately.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I my view, the returnCount implementation for TSum and TSumN makes sense. Is there any problem that I was not able to perceive?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried that compressing sums may break this arithmetic, but compression actually runs before compression, so this is fine. Sorry.

@mbenke mbenke requested a review from rodrigogribeiro June 25, 2026 06:18

@rodrigogribeiro rodrigogribeiro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this change will fix a problem that I've found on implementing generic deriving. Nice catch!

@rodrigogribeiro rodrigogribeiro merged commit a8e5bfd into argotorg:main Jun 29, 2026
4 checks passed
@axic axic deleted the hull-returncount-arity branch June 29, 2026 11:29
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.

4 participants