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: missing/bad error for "s"+bool #46749
Comments
Compiling this with
which seems pretty good. So this will certainly be fixed for Go 1.18. Haven't looked into the compiler's type checker yet. @mdempsky ? |
The -G=3 output has the same problem as the current regular output. It should say instead:
|
@rsc You want to make it specific to string+bool or general? The "invalid operation" seems unclear to me, as the op "+" is defined for both string and bool. |
@cuonglm I would expect that such a change should work in general (e.g. for |
Ack, the second error there was more helpful. I don't think it was specifically removed. I refactored a lot of the untyped constant handling in typecheck a while back (I think in that time period) to behave more consistently, and in particular to avoid printing multiple errors.
The invalid operation is trying to add two mismatched types. (Also, "+" isn't applicable to bool, but the point still stands if it was int or something.) I think this basically similar to when we made Edit: I suspect we may just want defaultlit2 to never report errors on its own. Instead, leave it for the caller to detect that their types are still mismatched and report a better error. |
Related: #41500 |
Yeah, my bad. I mean the op can |
Change https://golang.org/cl/328050 mentions this issue: |
Change https://golang.org/cl/328053 mentions this issue: |
…alid untyped operation This ports the fix in CL 328050 for typecheck to types2. The fix is not identical, due to code structure differences between typecheck and types2, but the idea is the same. We only do the untyped conversion when both operands can be mixed. Updates #46749 Change-Id: Ib2c63ba0d5dd8bf02318b1bfdfe51dcaeeeb7f82 Reviewed-on: https://go-review.googlesource.com/c/go/+/328053 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Change https://golang.org/cl/328869 mentions this issue: |
CL 328050 looks great. Thanks so much! |
…eration This is port of CL 328053 for types2 to go/type. The change is identical, but for some tweaks to the error positions in tests. Updates #46749 Change-Id: I8d34c5b1669e59e4ec7d91f81dcf655b2bfd89a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/328869 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org>
The Go 1.15 output is what is still printed today. Having two errors was definitely a bit annoying, but the fix should have been to delete the first one, not the second. I got this in a larger program as part of a larger expression, like:
The error was cannot use "blah blah" (type untyped string) as bool.
I spent quite a while puzzling through the problem of why my string would need to be a bool.
The answer is that validHosts is a map[string]bool so host is a bool.
If the error had reported string+bool, the problem would have been far more obvious.
We should restore the second error and probably drop the first.
I think we could drop the first by making the implicit conversion of strings
just return plain string if the suggestion is bool.
Then the invalid operation or whatever other error will happen will be reported with more context.
Would be nice to fix for Go 1.17 but not a release blocker.
/cc @griesemer @mdempsky
The text was updated successfully, but these errors were encountered: