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

Detect duplicate const literals in match #4712

Closed
medvednikov opened this issue May 4, 2020 · 7 comments
Closed

Detect duplicate const literals in match #4712

medvednikov opened this issue May 4, 2020 · 7 comments
Labels
Bug This tag is applied to issues which reports bugs. Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@medvednikov
Copy link
Member

Expected behavior:

a := 0
switch a {
case 0.0:
case 0:
}
./prog.go:12:6: duplicate case 0 in switch
@medvednikov medvednikov added the Bug This tag is applied to issues which reports bugs. label May 4, 2020
@medvednikov medvednikov added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label May 5, 2020
@Delta456
Copy link
Member

This now gives an error cannot match float literalwithint condition. I am not sure if 0.0 should implicitly cast to int.

@dumblob
Copy link
Contributor

dumblob commented Feb 22, 2021

I am not sure if 0.0 should implicitly cast to int.

It should not (because it's an approximate zero and thus not an integer exact zero).

@serkonda7
Copy link
Contributor

serkonda7 commented Feb 22, 2021

@dumblob if the value that is matched (here a) is an int, 0.0 should be cast to an int.

@dumblob
Copy link
Contributor

dumblob commented Feb 22, 2021

@dumblob if the value that is matched (here a) is an int, 0.0 should be cast to an int.

Generally yes, but only for compatible types. In V it must not be possible to do:

mut x := 1.5
x = 0.0
assert x == 0

The correct way would be:

mut x := 1.5
x = 0.0
assert int( x ) == 0

@serkonda7
Copy link
Contributor

Therefore we have rules for automatic type promotions. You can look them up in the docs

@dumblob
Copy link
Contributor

dumblob commented Feb 22, 2021

Therefore we have rules for automatic type promotions. You can look them up in the docs

Type promotions are in flux for quite some time already (#7801 (comment) , #8601 , #7692 , #6251 ) and they converge to guarantee no information loss which in this case is not possible (it's possible to silently promote int to f64, but not any float to int).

@medvednikov
Copy link
Member Author

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants