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

cmd/compile: compiler does not reject overflowing type-parameter conversion #49247

ALTree opened this issue Oct 31, 2021 · 3 comments
Labels
NeedsFix release-blocker
Milestone

Comments

@ALTree
Copy link
Member

@ALTree 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 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 NeedsFix and removed NeedsInvestigation release-blocker NeedsDecision labels Oct 31, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 31, 2021

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

@griesemer griesemer commented Oct 31, 2021

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

@gopherbot
Copy link

@gopherbot gopherbot commented Nov 1, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix release-blocker
Projects
None yet
Development

No branches or pull requests

3 participants