golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.62k stars 17.49k forks source link

go/types: incorrect error message for string(1<<s + 1.1) #21982

Open griesemer opened 6 years ago

griesemer commented 6 years ago

Type-checking the program

package p
var s uint
var _ = string(1<<s + 1.0)

produces the error:

x.src:3:16: cannot convert 1 << s + 1.0 (untyped float value) to string

But the correct error would be:

cannot shift 1 (untyped float value)

See also the TODO in go/types/conversions.go.

gopherbot commented 6 years ago

Change https://golang.org/cl/65370 mentions this issue: go/types: don't accept incorrect shift expression arguments

mdempsky commented 6 years ago

I think the current error is fine.

"If the left operand of a non-constant shift expression is an untyped constant, it is first converted to the type it would assume if the shift expression were replaced by its left operand alone."

If we replace the shift expression, we get 1 + 1.0, which cannot be converted to string. Ergo, we can't convert the original expression to string either.

Also, we do allow shifting of untyped float constants (e.g., int(1.0 << s)), so I think the proposed error message (cannot shift 1 (untyped float value)) would be misleading. And the same expression is valid in some other contexts (e.g., int(1<<s + 1.0)), so I think mentioning the string conversion aspect is important.