Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Explicit float to int casting for union member variable of int type #79

Open
Yeaseen opened this issue Aug 31, 2024 · 1 comment
Open
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@Yeaseen
Copy link
Contributor

Yeaseen commented Aug 31, 2024

Source C code:

#include <stdio.h>
union TypePunning {
    int intValue;
};
int main() {
    union TypePunning pun;
    pun.intValue = 1;
    pun.intValue =(int)1.23f;
    printf("Result: %d\n", pun.intValue);
    return 0;
}

Translated Go code:

package main

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

type TypePunning struct {
	// union
	IntValue int
}

func main() {
	var pun TypePunning
	pun.IntValue = 1
	pun.IntValue = int(1.23)
	stdio.Printf("Result: %d\n", pun.IntValue)
	os.Exit(0)
}

Go Build Failure at type casting

./runner.go:16:21: cannot convert 1.23 (untyped float constant) to type int

Root Cause:

Direct type conversion from float to int is illegal in Go. It should be an explicit conversion.

@dennwc
Copy link
Contributor

dennwc commented Aug 31, 2024

I don't think we should fix this, actually. It's one of the cases where Go compiler detects a legit mistake in the code (float part is silently dropped in C in this case).

@dennwc dennwc self-assigned this Aug 31, 2024
@dennwc dennwc added documentation Improvements or additions to documentation question Further information is requested labels Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants