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

cmd/compile: spurious not a slice error when calling unsafe.SliceData on a type-parameterized slice #64406

Closed
bcmills opened this issue Nov 27, 2023 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@bcmills
Copy link
Contributor

bcmills commented Nov 27, 2023

Go version

Go 1.21 (playground)

What operating system and processor architecture are you using (go env)?

playground

What did you do?

https://go.dev/play/p/1onZLUkD_VX

What did you expect to see?

successful compilation

What did you see instead?

./prog.go:8:26: invalid argument: s (variable of type S constrained by ~[]E) is not a slice

But it clearly is a slice, because (according to the same error message) it is constrained by ~[]E.

(attn @griesemer)

@bcmills bcmills added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. compiler/runtime Issues related to the Go compiler and/or runtime. labels Nov 27, 2023
@mdempsky
Copy link
Contributor

Confirmed with -gcflags=-h this is a types2 error.

@mdempsky
Copy link
Contributor

/cc @findleyr @adonovan @cuonglm

@mdempsky mdempsky added this to the Go1.23 milestone Nov 27, 2023
@cuonglm
Copy link
Member

cuonglm commented Nov 28, 2023

This happens with unsafe.Slice, too:

package main

import (
	"unsafe"
)

func sliceData[E any, S ~[]E](s S) *E {
	return unsafe.SliceData(s)
}

func slice[E any, S ~*E](s S) []E {
	return unsafe.Slice(s, 0)
}

func main() {
	s := []uint32{0}
	_ = sliceData(s)
	_ = slice(&s)
}

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/545395 mentions this issue: types2, go/types: fix type checking of ~[]E passing to unsafe builtins

@griesemer
Copy link
Contributor

I just point out that the spec doesn't allow type parameter arguments for these functions.
It probably should, so this is fine to fix, but we also need to update the spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants