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

[FS-1080] Add new syntax option for Float32. #7839

Merged
2 changes: 2 additions & 0 deletions src/fsharp/LanguageFeatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type LanguageFeature =
| NameOf
| ImplicitYield
| OpenStaticClasses
| DotlessFloat32Literal
| PackageManagement
| FromEndSlicing
| FixedIndexSlice3d4d
Expand Down Expand Up @@ -54,6 +55,7 @@ type LanguageVersion (specifiedVersionAsString) =
// F# preview
LanguageFeature.NameOf, previewVersion
LanguageFeature.OpenStaticClasses, previewVersion
LanguageFeature.DotlessFloat32Literal, languageVersion50
LanguageFeature.PackageManagement, previewVersion
LanguageFeature.FixedIndexSlice3d4d, previewVersion
LanguageFeature.FromEndSlicing, previewVersion
Expand Down
1 change: 1 addition & 0 deletions src/fsharp/LanguageFeatures.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type LanguageFeature =
| NameOf
| ImplicitYield
| OpenStaticClasses
| DotlessFloat32Literal
| PackageManagement
| FromEndSlicing
| FixedIndexSlice3d4d
Expand Down
16 changes: 12 additions & 4 deletions src/fsharp/lex.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ let floate = digit ((digit | separator)* digit)? ('.' (digit ((digit | separator
let float = floatp | floate
let bignum = integer ('I' | 'N' | 'Z' | 'Q' | 'R' | 'G')
let ieee64 = float
let ieee32 = float ('f' | 'F')
let decimal = (float | integer) ('m' | 'M')
let ieee32 = (float | integer) ('f' | 'F')
let decimal = (float | integer) ('m' | 'M')
let xieee32 = xinteger 'l' 'f'
let xieee64 = xinteger 'L' 'F'
let escape_char = ('\\' ( '\\' | "\"" | '\'' | 'a' | 'f' | 'v' | 'n' | 't' | 'b' | 'r'))
Expand Down Expand Up @@ -335,8 +335,16 @@ rule token args skip = parse
UNATIVEINT(uint64 (removeUnderscores (lexemeTrimRight lexbuf 2)))
with _ -> fail args lexbuf (FSComp.SR.lexOutsideNativeUnsigned()) (UNATIVEINT(0UL)) }

| ieee32
{ IEEE32 (try float32(removeUnderscores (lexemeTrimRight lexbuf 1)) with _ -> fail args lexbuf (FSComp.SR.lexInvalidFloat()) 0.0f) }
| ieee32
{ let s = lexemeTrimRight lexbuf 1
if lexbuf.SupportsFeature LanguageFeature.DotlessFloat32Literal || s.Contains "." then
try
IEEE32 (float32 (removeUnderscores s))
with _ -> fail args lexbuf (FSComp.SR.lexInvalidFloat()) (IEEE32 0.0f)
else
fail args lexbuf (FSComp.SR.lexInvalidFloat()) (IEEE32 0.0f)
}

| ieee64
{ IEEE64 (try float(lexeme lexbuf) with _ -> fail args lexbuf (FSComp.SR.lexInvalidFloat()) 0.0) }

Expand Down
10 changes: 5 additions & 5 deletions tests/FSharp.Core.UnitTests/FSharp.Core.UnitTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</PropertyGroup>

<PropertyGroup>
<DefineConstants Condition="'$(UnitTestType)' == 'xunit'">$(DefineConstants);XUNIT</DefineConstants>
<DefineConstants Condition="'$(UnitTestType)' != 'xunit'">$(DefineConstants);NUNIT</DefineConstants>
<DefineConstants Condition="'$(UnitTestType)' == 'xunit'">$(DefineConstants);XUNIT</DefineConstants>
<DefineConstants Condition="'$(UnitTestType)' != 'xunit'">$(DefineConstants);NUNIT</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -88,7 +88,7 @@
</ItemGroup>

<ItemGroup>
<Content Include="**/*" Exclude="**/*.bak;Directory.Build.Props;Directory.Build.targets" CopyToOutputDirectory="never" />
<Content Include="**/*" Exclude="**/*.bak;Directory.Build.Props;Directory.Build.targets;FSharp.Core.UnitTests.fsproj" CopyToOutputDirectory="never" />
cartermp marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup Condition="'$(BUILD_IN_FSHARP_REPOSITORY)' == 'true'">
Expand All @@ -102,8 +102,8 @@
</ItemGroup>

<ItemGroup Condition="'$(UnitTestType)' == 'xunit'">
<PackageReference Include="xunit" Version="2.4.1" PrivateAssets="All" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="All" />
<PackageReference Include="xunit" Version="2.4.1" PrivateAssets="All" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="All" />
</ItemGroup>

</Project>
17 changes: 10 additions & 7 deletions tests/fsharp/Compiler/CompilerAssert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,22 @@ let main argv = 0"""
let TypeCheckSingleError (source: string) (expectedServerity: FSharpErrorSeverity) (expectedErrorNumber: int) (expectedErrorRange: int * int * int * int) (expectedErrorMsg: string) =
TypeCheckWithErrors source [| expectedServerity, expectedErrorNumber, expectedErrorRange, expectedErrorMsg |]

let CompileExe (source: string) =
compile true [||] source (fun (errors, _) ->
let CompileExeWithOptions options (source: string) =
compile true options source (fun (errors, _) ->
if errors.Length > 0 then
Assert.Fail (sprintf "Compile had warnings and/or errors: %A" errors))

let CompileExe (source: string) =
CompileExeWithOptions [||] source

let CompileExeAndRunWithOptions options (source: string) =
compile true options source (fun (errors, outputExe) ->
compile true options source (fun (errors, outputExe) ->

if errors.Length > 0 then
Assert.Fail (sprintf "Compile had warnings and/or errors: %A" errors)
if errors.Length > 0 then
Assert.Fail (sprintf "Compile had warnings and/or errors: %A" errors)

executeBuiltApp outputExe
)
executeBuiltApp outputExe
)

let CompileExeAndRun (source: string) =
CompileExeAndRunWithOptions [||] source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open FSharp.Compiler.SourceCodeServices
module ``Basic Grammar Element Constants`` =

[<Test>]
let `` Basic constants compile `` () =
let ``Basic constants compile `` () =
CompilerAssert.Pass
"""
let sbyteConst = 1y
Expand All @@ -25,6 +25,8 @@ let uint64Const = 1uL
let ieee32Const1 = 1.0f
let ieee32Const2 = 1.0F
let ieee32Const3 = 0x0000000000000001lf
let ieee32Const4 = 1F
let ieee32Const5 = 1f

let ieee64Const1 = 1.0
let ieee64Const2 = 0x0000000000000001LF
Expand Down Expand Up @@ -172,3 +174,25 @@ let x14 = 0o5_2
if x14 <> 0o52 then failwith "Wrong parsing"
printfn "%A" x14
"""
[<Test>]
let ``float without dot``() =
CompilerAssert.CompileExeWithOptions [|"--langversion:preview"|]
"""
let x = 42f
printfn "%A" x
"""

[<Test>]
let ``float with dot``() =
CompilerAssert.CompileExeWithOptions [|"--langversion:preview"|]
"""
let x = 42.f
printfn "%A" x
"""

[<Test>]
let ``floats with dot should be equal to floats without dot``() =
CompilerAssert.CompileExeAndRunWithOptions [|"--langversion:preview"|]
"""
if 1.0f <> 1f then failwith "1.0f is not equal to 1f"
"""
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//<Expects id="FS1156" span="(18,11-18,13)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(18,14-18,19)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS0599" span="(18,13-18,14)" status="error">Missing qualification after '.'</Expects>
//<Expects id="FS1156" span="(19,11-19,19)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(20,29-20,42)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(22,10-22,13)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(16,11-16,13)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(17,11-17,19)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(18,29-18,42)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(20,10-20,13)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(21,10-21,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(22,10-22,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(23,10-23,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(24,10-24,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(24,10-24,14)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(25,10-25,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(26,10-26,14)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(26,10-26,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(27,10-27,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(28,10-28,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(29,10-29,15)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(30,11-30,18)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(31,11-31,18)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(32,11-32,16)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(28,11-28,18)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(29,11-29,18)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>
//<Expects id="FS1156" span="(30,11-30,16)" status="error">This is not a valid numeric literal. Valid numeric literals include</Expects>

let pi1 = 3_.1415F
let pi2 = 3._1415F
Expand Down