cxgo icon indicating copy to clipboard operation
cxgo copied to clipboard

Bug: Testing Boolean to Float Conversions in CxGo: Implicit Conversion Success, Explicit Conversion Error

Open Yeaseen opened this issue 6 months ago • 0 comments

Original C Code

int main() {
    float local_float = 0.0f;
    local_float += (4 < 4.5) + 3;
    local_float += (float)(4 < 4.5) + 3;
    return 0;
}

Converted Go Code

package main

import (
	"github.com/gotranspile/cxgo/runtime/libc"
	"os"
)

func main() {
	var local_float float32 = 0.0
	_ = local_float
	local_float += float32(int(libc.BoolToInt(4 < 4.5)) + 3)
	local_float += float32(4 < 4.5) + 3
	os.Exit(0)
}

Go compiler error

# command-line-arguments
./runner.go:12:25: cannot convert 4 < 4.5 (untyped bool constant true) to type float32

Root Cause

While Bool to Float implicit casting is okay, Bool to Float EXPLICIT casting is buggy. I modified the translated go code according to the rules for Bool to Float implicit casting, and it works fine.

Working Go Code

package main

import (
	"github.com/gotranspile/cxgo/runtime/libc"
	"os"
)

func main() {
	var local_float float32 = 0.0
	_ = local_float
	local_float += float32(int(libc.BoolToInt(4 < 4.5)) + 3)
	local_float += float32(int(libc.BoolToInt(4 < 4.5)) + 3)
	os.Exit(0)
}

This should be the expected Go code.

Yeaseen avatar Aug 24 '24 06:08 Yeaseen