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

Failed to build with if + defer closuer #13

Closed
xhd2015 opened this issue Mar 30, 2024 · 2 comments
Closed

Failed to build with if + defer closuer #13

xhd2015 opened this issue Mar 30, 2024 · 2 comments
Labels
bug Something isn't working done

Comments

@xhd2015
Copy link
Owner

xhd2015 commented Mar 30, 2024

xgo version: v1.0.10
go version: go 1.21

code

package demo

import (
	"testing"
)

const condition = false

func TestClosuerWithIf(t *testing.T) {
	t.Logf("begin \n")
	if condition {
		defer func() {
				t.Logf("defer\n")
		}()
	}

	t.Logf("end\n")
}

error:

$ xgo test ./demo
sub.TestClosuerWithIf.func1·f: relocation target sub.TestClosuerWithIf.func1 not defined
FAIL   demo [build failed]

However, without the if condition, everything works fine.
With go1.22, the above code works fine.

@xhd2015 xhd2015 added the bug Something isn't working label Mar 30, 2024
@xhd2015
Copy link
Owner Author

xhd2015 commented Mar 30, 2024

This may be caused by go1.21 is removing the function due the if false condition being constant, though it should not be removed because it is registered some where else using IR.

@xhd2015
Copy link
Owner Author

xhd2015 commented Mar 30, 2024

Fixed in 1bc959a

Upgraded xgo to v1.0.11

Tests: https://github.com/xhd2015/xgo/blob/master/runtime/test/mock_closure/mock_closuer_with_if_test.go

package mock_closuer

import "testing"

// see https://github.com/xhd2015/xgo/issues/13
func TestClosuerWithIfFalse(t *testing.T) {
	// assert this function
	var a int
	if false {
		func() {
			a = 10
		}()
	}
	if a != 0 {
		t.Fatalf("expect a to be 0, actual: %d", a)
	}
}
func TestClosuerWithIfTrue(t *testing.T) {
	var a int
	if true {
		func() {
			a = 10
		}()
	}
	if a != 10 {
		t.Fatalf("expect a to be 10, actual: %d", a)
	}
}

@xhd2015 xhd2015 added the done label Mar 30, 2024
@xhd2015 xhd2015 closed this as completed Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working done
Projects
None yet
Development

No branches or pull requests

1 participant