cmd/compile: disallow not-in-heap types to be used as type arguments #54765
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
FrozenDueToAge
NeedsDecision
Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone
I think we shouldn't allow not-in-heap types to be used as type arguments.
is a valid generic function declaration, but instantiating it as
f[nih]
will (or should) fail. (We're inconsistent about whether we catch this today: https://go.dev/play/p/bRMtJouiaR5)In theory, we could still allow instantiating
f[nih]
iff
doesn't containnew(T)
, or allocateT
on the stack, or any of the other problematic use cases; but this violates the generics principle that the type parameter constraints fully constrain what type arguments are valid.I think the most likely case where this could come up today is with cgo and incomplete types; a user might want to write
atomic.Pointer[C.some_incomplete_type]
, which would now fail to compile if we reject not-in-heap types as type arguments. However, internally atomic.Pointer is using an unsafe.Pointer to store the pointer, which may not be appropriate for a*C.some_incomplete_type
value, so arguably this is already potentially broken./cc @ianlancetaylor @randall77
The text was updated successfully, but these errors were encountered: