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: compiler does not reject overflowing type-parameter conversion #49247

Closed
ALTree opened this issue Oct 31, 2021 · 3 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@ALTree
Copy link
Member

ALTree commented Oct 31, 2021

$ gotip version
go version devel go1.18-8e3d5f0bb3 Sun Oct 31 08:29:02 2021 +0000 linux/amd64

According to my reading of the type parameters proposal, specifically this section:

Untyped constants

Some functions use untyped constants. An untyped constant is permitted with a value of a type parameter if it is permitted with every type in the type set of the type parameter's constraint.

// This function is INVALID.
func Add1024[T integer](s []T) {
  for i, v := range s {
  	s[i] = v + 1024 // INVALID: 1024 not permitted by int8/uint8
  }
}

This program should be rejected by the compiler:

package main

func f[T interface{ int8 }]() {
	println(T(1024))
}

func main() {
	f[int8]()
}

but it isn't, and it prints 0. If T is manually replaced with int8, the compiler errors with:

cannot convert 1024 (untyped int constant) to int8

cc @griesemer @ianlancetaylor

@ALTree ALTree added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker labels Oct 31, 2021
@ALTree ALTree added this to the Go1.18 milestone Oct 31, 2021
@griesemer griesemer self-assigned this Oct 31, 2021
@griesemer griesemer added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Oct 31, 2021
@griesemer
Copy link
Contributor

This is just a bug. Note that this code:

package p
func _[T ~byte]() {
	var _ T = 256 // ERROR: cannot use 256 (untyped int constant) as T value in variable declaration
}

is correctly rejected, but:

package p
func _[T ~byte]() {
	var _ T = T(256)
}

with an explicit conversion is accepted. The reason is that a value of type parameter type cannot be a constant value, so the conversion makes T(256) a non-constant value. But the switch from constant to non-constant happens too early, and then 256 is not correctly rejected.

@griesemer
Copy link
Contributor

Hm. This will require a bit of code shuffling to get right, after all. Marking as release blocker again.

@gopherbot
Copy link

Change https://golang.org/cl/360396 mentions this issue: cmd/compile/internal/types2: fix conversions of constants to type parameter

@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

3 participants