component-model icon indicating copy to clipboard operation
component-model copied to clipboard

Why does convert_int_to_bool convert?

Open sunfishcode opened this issue 2 years ago • 1 comments

The Canonical ABI says

Integer-to-boolean conversions treats 0 as false and all other bit-patterns as true

Why does it work this way, instead of doing trap_if(i >= 2) similar to what i32_to_char does?

(Aside: the names convert_int_to_bool and i32_to_char are a little inconsistent.)

Rust and many popular C and C++ ABIs use a convention of 0 meaning false and 1 meaning true, and could perform a no-op conversion of things like list<bool> into the source language if the Canonical ABI would guarantee that representation as well.

sunfishcode avatar Feb 18 '23 13:02 sunfishcode

It's a good general question of whether to try to have the ABI trap as much as possible or, rather, check only what's necessary. Initially, I had the rules try to trap as much as possible, but in a few cases, this ended up rather complicating things so, to avoid being inconsistent, we changed our default to only trap when necessary. Note that, while lifting allows the incoming value to be greater than 1, lowering guarantees you a 0 or 1, so I think you should have no-op conversions of list<bool> in both directions.

And yeah, I suppose i32_to_char should get a verb before it like all the other verb_x_to_y functions. Fixed

lukewagner avatar Feb 18 '23 18:02 lukewagner

The question has been answered!

sunfishcode avatar Aug 19 '24 15:08 sunfishcode