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

Cannot use len(s) (value of type int) as uintptr value in assignment #2046

Closed
StarpTech opened this issue Aug 14, 2021 · 2 comments
Closed

Comments

@StarpTech
Copy link

Hi, I see the following error when compiling a program with tinygo. Is it unsupported by design?

❯ tinygo build -o hello ./main.go
# github.com/cespare/xxhash
../../../go/pkg/mod/github.com/cespare/[email protected]/xxhash_unsafe.go:27:11: cannot use len(s) (value of type int) as uintptr value in assignment
../../../go/pkg/mod/github.com/cespare/[email protected]/xxhash_unsafe.go:28:11: cannot use len(s) (value of type int) as uintptr value in assignment

Source: https://github.com/cespare/xxhash/blob/569f7c8abf1f58d9043ab804d364483cb1c853b6/xxhash_unsafe.go#L27

I also saw similar issues in https://github.com/tidwall/gjson/blob/5c2e4b382486589dad7478130a364ee2fa6a068b/gjson_ngae.go#L74

@aykevl
Copy link
Member

aykevl commented Aug 14, 2021

This is a duplicate of #1284.

In short, the code is not portable. However, in this case it's easy to work around it. This slightly changed version also compiles under TinyGo, by using the fact that StringHeader and SliceHeader all use the same type for the Len and Cap fields:

func Sum64String(s string) uint64 {
	// See https://groups.google.com/d/msg/golang-nuts/dcjzJy-bSpw/tcZYBzQqAQAJ
	// for some discussion about this unsafe conversion.
	var b []byte
	bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
	sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
	bh.Data = sh.Data
	bh.Len = sh.Len
	bh.Cap = sh.Len
	return Sum64(b)
}

A similar trick can be used in gjson.

@aykevl
Copy link
Member

aykevl commented Aug 14, 2021

Closing because it is a duplicate.

@aykevl aykevl closed this as completed Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants