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: incorrect and duplicate error for invalid string conversion #21979
Comments
Change https://golang.org/cl/65370 mentions this issue: |
I am interested in contributing to Go - is this a good issue for a first-time contributor? |
Yes and no. Yes because I suspect the actual fix might be small (a missing check somewhere); no because it requires digging into the compiler internals which can be daunting. That said, why don't you give it a shot and see how far you get. Send any CLs to me (gri) or mdempsky, please. Thanks. |
Duplicate of #20812? Does this reproduce on tip? |
Thanks, I will take a look at this over the weekend/early next week. |
I can conform that @kshvmdn's CL https://go-review.googlesource.com/46912 fixed the double error printing issue However, there is a change in error message, it now says |
Thanks for the confirmation, @odeke-em! Unfortunately, as discussed in the CL, I couldn't seem to find a way of adding more relevant type information to the first error message. Maybe @frikkasoft, @griesemer, or someone else will be able to find a better solution. |
@kshvmdn thanks for the CL and fix btw. So in regards to relevant information to that type, perhaps this helper that I added in https://go-review.googlesource.com/c/25156/ could help out with that message go/src/cmd/compile/internal/gc/typecheck.go Lines 2752 to 2771 in 6872a8e
|
@odeke-em, cool, I wasn't aware of this function! So this change: --- a/src/cmd/compile/internal/gc/const.go
+++ b/src/cmd/compile/internal/gc/const.go
@@ -408,7 +408,7 @@ func convlit1(n *Node, t *types.Type, explicit bool, reuse canReuseNode) *Node {
bad:
if !n.Diag() {
if !t.Broke() {
- yyerror("cannot convert %L to type %v", n, t)
+ yyerror("cannot convert %v (type %v) to type %v", n, sigrepr(n.Typ()), t)
}
n.SetDiag(true)
} results in the message saying |
Thanks for trying it out @kshvmdn, in this case, I'll defer to @mdempsky and @griesemer but IMHO I'd fly with |
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>
My apologies for not checking at tip myself before filing this. That said, the error is still misleading: |
Inaccurate error message, but we do get an error message. Not urgent. |
Change https://golang.org/cl/191079 mentions this issue: |
Reopening this, because I reverted it with CL 193977 (e6ba19f). To summarize the current standing of the issue, given
we currently report:
Like @griesemer pointed out, this is a vague and somewhat misleading error message: it's okay to convert untyped integers to string, but not untyped floats. Unfortunately, the error message doesn't disambiguate the two. CL 191079 attempted to fix this by changing it to report:
However, this is technically incorrect: 1's type is not float64, but "untyped float constant". (Admittedly though, the difference is probably irrelevant to many Go programmers.) go/types correctly reports "untyped float constant", and cmd/compile should do the same. |
Change https://golang.org/cl/194019 mentions this issue: |
https://play.golang.org/p/nqGlIR42-9
produces:
tmp/sandbox179252628/main.go:4:12: cannot convert 1 to type string
tmp/sandbox179252628/main.go:4:12: cannot convert 1 (type float64) to type string
The first error is incorrect (1 can be converted to string, but not 1.0); it's also not necessary. The 2nd error would be sufficient.
The text was updated successfully, but these errors were encountered: