xgo: Unsupported: Package level var MyStruct[GenericType] = {instantiation of struct}

Using go 1.24.4. To fix build: the instantiation has to be moved to an init() or a Setup func.

go tool xgo test -v ./providers/blockbook
# arculus.com/Arculus-Cloud/types
types/arculus-structs.go:205: cannot use generic type ReqBody[T any] without instantiation
types/arculus-structs.go:250: cannot use generic type ReqBody[T any] without instantiation
types/arculus-structs.go:342: cannot use generic type ReqBody[T any] without instantiation
# arculus.com/Arculus-Cloud/keydb
keydb/keydb.go:44: cannot use generic type task.Tasker[Client any] without instantiation
FAIL    arculus.com/Arculus-Cloud/providers/blockbook [build failed]
FAIL

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

@xhd2015 tested and working on my demo package! And confirmed working in our main codebase. Thanks so much for quickly addressing my issues, and thank you for your extremely useful project. Looking forward to what comes next!

nice work! I’ll check ASAP

@pspencer-arculus can you check the code in .xgo/gen/overlay/PROJECT/database/chainlist/chainlist.go:21, which is the rewritten content of the original file.

Can give a minimal reproducible example if possible, thanks.

Reproduce this with:

type Wrapper[T any] struct {
}

type Concrete struct {
}

var instance = Wrapper[Concrete]{}

And check the rewrite file inside ./.xgo:

var instance = Wrapper[Concrete]{}

func instance_xgo_get() Wrapper {
	__mock_res := instance
	__xgo_trap_var_0(__xgo_var_info_0_0, &instance, &__mock_res)
	return __mock_res
}
func instance_xgo_get_addr() *Wrapper {
	__mock_res := &instance
	__xgo_trap_varptr_0(__xgo_var_info_0_0, &instance, &__mock_res)
	return __mock_res
}

The return type of instance_xgo_get lacks generic param for Wrapper

Debugging command:

go run ./script/run-test -tags=dev --include go1.24.2 --debug-xgo ./runtime/test/mock/mock_var/mock_var_generic

Aha, a good point, I think it’s related to variable mocking, https://github.com/xhd2015/xgo/blob/master/runtime/test/mock/mock_var/mock_var_test.go

I’ll add a test for this and fix it, thanks for feedback!

I refactored my code to make xgo work and I think your project is amazing 😃