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/go: compiler does not report error on mismatched integer types #37518
Comments
I don't understand the bug you're seeing. Can you provide a full program?
You might also want to read about untyped constants. |
I added in interface to my simple code. Now it fails. If I run the example I sent originally, it works. `package main import ( type junk interface { type stuff struct{} func (s stuff) getkey() string { func (s stuff) getvalue() int64 { type stuffFactory func() junk func createStuff() junk { type item struct { func (i item) getkey() string { func (i item) getvalue() interface{} { func main() {
}` Running this program gives this output:
|
I don't see a bug here, so closing. You may want to take a look at https://blog.golang.org/constants. |
I can send code tomorrow.
Get BlueMail for Android<http://www.bluemail.me/r?b=15824>
On Feb 27, 2020, at 4:58 PM, Keith Randall <notifications@github.com<mailto:notifications@github.com>> wrote:
I don't understand the bug you're seeing. Can you provide a full program?
This program prints true twice:
package main
func main() {
println(test1())
println(test2())
}
func test1() bool {
var isFound bool
var value int64 = 1
if value == 1 {
isFound = true
} else {
isFound = false
}
return isFound
}
func test2() bool {
var isFound bool
var value int64 = 1
if value == int64(1) {
isFound = true
} else {
isFound = false
}
return isFound
}
You might also want to read about untyped constants<https://golang.org/ref/spec#Constants>. value == 1 is allowed by the language spec because 1 is an untyped constant and is allowed to be used in comparisons with any numeric type.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#37518?email_source=notifications&email_token=AJ657S4UU4HTV5QUKYCCWNDRFAZRXA5CNFSM4K5DMVPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENGDR4Q#issuecomment-592197874>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJ657S4BQNT5VEZTMGHTTDDRFAZRXANCNFSM4K5DMVPA>.
|
I read the blog. I still don't understand why the if statement works for a int64 variable, but not for an int64 variable returned from an interface. Can you please explain? In both cases, you can use a 'fmt.Printf("%T\n", value) and the printed result is int64. The simple value int64 =1 will be equal to a literal integer 1, but the int64 returned from the interface will not be equal to a literal of 1.
I look forward to your explanation.
Regards,
Martin Rothberg
Get BlueMail for Android<http://www.bluemail.me/r?b=15824>
On Feb 28, 2020, at 2:18 PM, Ian Lance Taylor <notifications@github.com<mailto:notifications@github.com>> wrote:
I don't see a bug here, so closing. You may want to take a look at https://blog.golang.org/constants.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#37518?email_source=notifications&email_token=AJ657SZXN2BI6GO3C3QEZ6DRFFPRDA5CNFSM4K5DMVPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENJ2HZA#issuecomment-592684004>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJ657S7U22EVP6XC7YY37QLRFFPRDANCNFSM4K5DMVPA>.
|
The first comparison compares the values of the variables When you do
That 1 on the RHS must be converted to an interface before the equality check is done. Because |
Thanks for making it clear. I will share this with my co-workers on Monday.
Best regards,
Martin Rothberg
Get BlueMail for Android<http://www.bluemail.me/r?b=15824>
On Feb 28, 2020, at 6:11 PM, Keith Randall <notifications@github.com<mailto:notifications@github.com>> wrote:
var x, y int64
var i, j interface{}
a := x == y
b := i == j
c := i == x
The first comparison compares the values of the variables x and y, as expected.
The second comparison compares both the types and the values of i and j. Both the type and the value have to match, for the equality to be true.
The third comparison is really this: i == (interface{}((x). That is, the concrete type is promoted to an interface before the equality check. Thus, the third comparison compares both the types and values. In this case, the type of x is int64, so an int64 must be in i for the equality to be true.
When you do
var x interface{} = int64(1)
a := x == 1
That 1 on the RHS must be converted to an interface before the equality check is done. Because 1 is a Go constant, it gets the default type for integer constants, which is int. Then when the comparison happens, it returns false because the types don't match (int64 and int are not the same type).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#37518?email_source=notifications&email_token=AJ657SYQUX73HGEZCYMIMXDRFGKZBA5CNFSM4K5DMVPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENKPRVA#issuecomment-592771284>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJ657S2QBYRCG373TRFYSILRFGKZBANCNFSM4K5DMVPA>.
|
Shouldn't the compiler at least flag this as a type mismatch? |
I'm not sure what you think the compiler should do. There are several different test cases in this issue. Can you provide a specific example where you think the compiler should behave differently? |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
The following code will silently fail to make the correct decision:
var isFound bool
var value int64 = 1
if value == 1 {
isFound = true
} else {
isFound = false
}
This will always evaluate to false unless the code is modified to:
if value == int64(1) {
isFound = true
} else {
isFound = false
}
This should be flagged as an error since the integer types do not match.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I added a cast to an int64 to fix the problem.
What did you expect to see?
I expected the compiler to give an error due to the mismatch of the integer types.
What did you see instead?
The program compiled without error and ran, but never saw the literal of 1 as a match for the value of type int64.
The text was updated successfully, but these errors were encountered: