Skip to content

Commit 85af456

Browse files
authored
Cherry pick Fix for #8827 --- Invalid floating point number for dotless exponential. (#8837)
* Fix dotnet/fsharp#8802 (#8827) * update mac os image
1 parent 6dc7819 commit 85af456

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ stages:
212212
# MacOS
213213
- job: MacOS
214214
pool:
215-
vmImage: macOS-10.13
215+
vmImage: macOS-latest
216216
variables:
217217
- name: _SignType
218218
value: Test
@@ -312,7 +312,7 @@ stages:
312312

313313
# - job: MacOS_FCS
314314
# pool:
315-
# vmImage: macOS-10.13
315+
# vmImage: macOS-latest
316316
# variables:
317317
# - name: _SignType
318318
# value: Test

src/fsharp/lex.fsl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ let evalIfDefExpression startPos isFeatureSupported args (lookup:string->bool) (
154154
let expr = FSharp.Compiler.PPParser.start tokenStream lexbuf
155155
LexerIfdefEval lookup expr
156156

157+
let evalFloat args lexbuf =
158+
try
159+
float32(removeUnderscores (lexemeTrimRight lexbuf 1))
160+
with _ ->
161+
fail args lexbuf (FSComp.SR.lexInvalidFloat()) 0.0f
162+
157163
}
158164
let letter = '\Lu' | '\Ll' | '\Lt' | '\Lm' | '\Lo' | '\Nl'
159165
let surrogateChar = '\Cs'
@@ -193,7 +199,11 @@ let floate = digit ((digit | separator)* digit)? ('.' (digit ((digit | separator
193199
let float = floatp | floate
194200
let bignum = integer ('I' | 'N' | 'Z' | 'Q' | 'R' | 'G')
195201
let ieee64 = float
196-
let ieee32 = (float | integer) ('f' | 'F')
202+
203+
let ieee32 = float ('f' | 'F')
204+
205+
let ieee32_dotless_no_exponent = integer ('f' | 'F')
206+
197207
let decimal = (float | integer) ('m' | 'M')
198208
let xieee32 = xinteger 'l' 'f'
199209
let xieee64 = xinteger 'L' 'F'
@@ -336,16 +346,16 @@ rule token args skip = parse
336346
with _ -> fail args lexbuf (FSComp.SR.lexOutsideNativeUnsigned()) (UNATIVEINT(0UL)) }
337347

338348
| ieee32
339-
{ let s = lexemeTrimRight lexbuf 1
340-
if lexbuf.SupportsFeature LanguageFeature.DotlessFloat32Literal || s.Contains "." then
341-
try
342-
IEEE32 (float32 (removeUnderscores s))
343-
with _ -> fail args lexbuf (FSComp.SR.lexInvalidFloat()) (IEEE32 0.0f)
349+
{ IEEE32 (evalFloat args lexbuf) }
350+
351+
| ieee32_dotless_no_exponent
352+
{ if lexbuf.SupportsFeature LanguageFeature.DotlessFloat32Literal then
353+
IEEE32 (evalFloat args lexbuf)
344354
else
345355
fail args lexbuf (FSComp.SR.lexInvalidFloat()) (IEEE32 0.0f)
346356
}
347357

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

351361
| decimal

tests/fsharp/Compiler/Conformance/BasicGrammarElements/BasicConstants.fs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,52 @@ if x14 <> 0o52 then failwith "Wrong parsing"
175175
printfn "%A" x14
176176
"""
177177
[<Test>]
178-
let ``float without dot``() =
178+
let ``dotless float``() =
179179
CompilerAssert.CompileExeWithOptions [|"--langversion:preview"|]
180180
"""
181181
let x = 42f
182182
printfn "%A" x
183183
"""
184-
184+
185185
[<Test>]
186-
let ``float with dot``() =
187-
CompilerAssert.CompileExeWithOptions [|"--langversion:preview"|]
186+
let ``dotted float``() =
187+
CompilerAssert.CompileExe
188188
"""
189189
let x = 42.f
190190
printfn "%A" x
191191
"""
192-
192+
193+
[<Test>]
194+
let ``dotted floats should be equal to dotless floats``() =
195+
CompilerAssert.CompileExeAndRunWithOptions [|"--langversion:preview"|]
196+
"""
197+
if 1.0f <> 1f then failwith "1.0f <> 1f"
198+
"""
199+
200+
[<Test>]
201+
let ``exponent dotted floats should be equal to dotted floats``() =
202+
CompilerAssert.CompileExeAndRun
203+
"""
204+
if 1.0e1f <> 10.f then failwith "1.0e1f <> 10.f"
205+
"""
206+
207+
[<Test>]
208+
let ``exponent dotless floats should be equal to dotted floats``() =
209+
CompilerAssert.CompileExeAndRun
210+
"""
211+
if 1e1f <> 10.f then failwith "1e1f <> 10.f"
212+
"""
213+
214+
[<Test>]
215+
let ``exponent dotted floats should be equal to dotless floats``() =
216+
CompilerAssert.CompileExeAndRunWithOptions [|"--langversion:preview"|]
217+
"""
218+
if 1.0e1f <> 10f then failwith "1.0e1f <> 10f"
219+
"""
220+
193221
[<Test>]
194-
let ``floats with dot should be equal to floats without dot``() =
222+
let ``exponent dotless floats should be equal to dotless floats``() =
195223
CompilerAssert.CompileExeAndRunWithOptions [|"--langversion:preview"|]
196224
"""
197-
if 1.0f <> 1f then failwith "1.0f is not equal to 1f"
225+
if 1e1f <> 10f then failwith "1e1f <> 10f"
198226
"""

0 commit comments

Comments
 (0)