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

Hex integer literal has unexpected value #18041

Open
inosik opened this issue Nov 21, 2024 · 6 comments
Open

Hex integer literal has unexpected value #18041

inosik opened this issue Nov 21, 2024 · 6 comments
Labels
Area-Compiler-Syntax lexfilter, indentation and parsing Bug Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code. Regression
Milestone

Comments

@inosik
Copy link

inosik commented Nov 21, 2024

Hex literals that would represent a negative number as a 32-bit value have an unexpected value when bound to a 64-bit value.

Repro steps

let a: int64 = 0xffff_ffff
let b: int64 = 0xffff_ffffL

Expected behavior

Not sure. It should either:

  • fail to compile like in VS 2019
  • a should be equal to b

Actual behavior

a has a value of -1, as if it was first parsed as a 32-bit signed integer and then converted to a 64-bit integer.

Known workarounds

  • add the L suffix to the literal
  • prepend a 0_ to force the value to be parsed as a 64-bit integer (i. e. 0x0_ffff_ffff)
@brianrourkeboll
Copy link
Contributor

a has a value of -1, as if it was first parsed as a 32-bit signed integer and then converted to a 64-bit integer.

I believe this is indeed more or less what happens.

@inosik
Copy link
Author

inosik commented Nov 21, 2024

I think you're right. I remember that I had to adapt the parsing code to support 64-bit numbers when I stole it from the F# compiler for my own purposes.

@brianrourkeboll
Copy link
Contributor

You can see here that 0xffff_ffff is parsed as SynConst.Int32 -1, while 0xffff_ffffL is parsed as SynConst.Int64 4294967295L.

@brianrourkeboll
Copy link
Contributor

I can't remember whether this widening conversion would always have compiled or was part of https://github.com/fsharp/fslang-design/blob/main/FSharp-6.0/FS-1093-additional-conversions.md.

@abonie abonie modified the milestones: Backlog, November-2024 Nov 25, 2024
@abonie
Copy link
Member

abonie commented Nov 25, 2024

Worth checking if this was introduced with implicit conversions

@abonie abonie added Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code. Area-Compiler-Syntax lexfilter, indentation and parsing labels Nov 25, 2024
@vzarytovskii
Copy link
Member

To add more context, as discussed before, it is likely that one scenario "goes" through literals "path", another through implicit (compile-time) conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-Syntax lexfilter, indentation and parsing Bug Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code. Regression
Projects
Status: New
Development

No branches or pull requests

4 participants