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

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

Open
griesemer opened this issue Sep 22, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@griesemer
Copy link
Contributor

commented Sep 22, 2017

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.

@griesemer griesemer self-assigned this Sep 22, 2017

@gopherbot

This comment has been minimized.

Copy link

commented Sep 22, 2017

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

gopherbot pushed a commit that referenced this issue Sep 25, 2017

go/types: don't accept incorrect shift expression arguments
Under certain circumstances involving shifts, go/types didn't verify
that untyped constant values were representable by the relevant type,
leading to the acceptance of incorrect programs (see the issue).

Fixing this code exposed another problem with int-to-string conversions
which suddenly failed because now the type-checker complained that a
(constant) integer argument wasn't representable as a string. Fixed that
as well.

Added many additional tests covering the various scenarious.

Found two cmd/compile bugs in the process (#21979, #21981) and filed
a go/types TODO (#21982).

Fixes #21727.

Change-Id: If443ee0230979cd7d45d2fc669e623648caa70da
Reviewed-on: https://go-review.googlesource.com/65370
Reviewed-by: Alan Donovan <adonovan@google.com>

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Mar 29, 2018

@mdempsky

This comment has been minimized.

Copy link
Member

commented Apr 8, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.