Skip to content

Constant fold toInt, toLong, toDouble, toFloat, toByte and toShort #13990

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

Closed
nicolasstucki opened this issue Nov 22, 2021 · 4 comments · Fixed by #17446
Closed

Constant fold toInt, toLong, toDouble, toFloat, toByte and toShort #13990

nicolasstucki opened this issue Nov 22, 2021 · 4 comments · Fixed by #17446
Assignees
Milestone

Comments

@nicolasstucki
Copy link
Contributor

Compiler version

3.1.0

Minimized code

inline val myIntMask = 1 << 4
inline val myLongMask = myIntMask.toLong

Output

2 |inline val myLongMask = myIntMask.toLong
  |                        ^^^^^^^^^^^^^^^^
  |                        inline value must have a literal constant type

Expectation

We should be able to constant fold toInt, toLong, toDouble, toFloat, toByte and toShort.

This would also alow us to write inline vals with Short and Byte types as shown bellow

inline val myByteMask = 2.toByte

This is currently impossible as we do not possess a byte or short numerical postfix in the syntax.

@mbovel
Copy link
Member

mbovel commented May 4, 2023

Seems like this should be relatively straightforward to extends foldUnop to support constant-folding these methods?

https://github.com/lampepfl/dotty/blob/d02ecd02b7acde957600649ad41a1a1b33572f4d/compiler/src/dotty/tools/dotc/typer/ConstFold.scala#L76-L90

Would that be appropriate for a spree?

@mbovel mbovel added area:typer area:inline Spree Suitable for a future Spree labels May 4, 2023
@scala-center-bot
Copy link

This issue was picked for the Issue Spree No. 30 of 09 May 2023 which takes place in a week from now. @KuceraMartin, @eugenefle, @TheElectronWill will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

@mbovel
Copy link
Member

mbovel commented May 9, 2023

@KuceraMartin, @eugenefle, @TheElectronWill: In case your team is super fast and manage to fix this issue in less than 1.5 hours, you can then try to tackle #17188 😉

TheElectronWill added a commit that referenced this issue May 24, 2023
Co-authored-by: Eugene Flesselle <[email protected]>
Co-authored-by: Martin Kucera <[email protected]>

Fixes #13990. Made during the Scala Spree :)
@scala scala deleted a comment from scala-center-bot May 28, 2023
@scala scala deleted a comment from scala-center-bot May 28, 2023
@mbovel
Copy link
Member

mbovel commented May 28, 2023

Please ignore the 2 previous message from the Scala Center bot. Teams for the next spree can be found here: https://airtable.com/shrSni1OItSuErosU/tblLEWc2ISeGHPbsc. Sorry for the spam!

@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants