Skip to content

Commit d121029

Browse files
authored
Revamp decimal literal tests (#17836)
1 parent 4fb264d commit d121029

File tree

1 file changed

+187
-39
lines changed
  • tests/FSharp.Compiler.ComponentTests/EmittedIL

1 file changed

+187
-39
lines changed

tests/FSharp.Compiler.ComponentTests/EmittedIL/Literals.fs

Lines changed: 187 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -176,37 +176,118 @@ let [<Literal>] x = System.Int32.MaxValue + 1
176176
}
177177

178178
[<Fact>]
179-
let ``Arithmetic can be used for constructing decimal literals``() =
179+
let ``Decimal literals are properly initialized``() =
180180
FSharp """
181-
module LiteralArithmetic
181+
module DecimalInit
182182
183183
[<Literal>]
184-
let x = 1m + 2m
184+
let x = 5.5m
185185
"""
186186
|> withLangVersion80
187187
|> compile
188188
|> shouldSucceed
189189
|> verifyIL [
190-
""".field public static initonly valuetype [runtime]System.Decimal x"""
191-
""".custom instance void [runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8,
190+
"""
191+
.class public abstract auto ansi sealed DecimalInit
192+
extends [runtime]System.Object
193+
{
194+
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationMappingAttribute::.ctor(valuetype [FSharp.Core]Microsoft.FSharp.Core.SourceConstructFlags) = ( 01 00 07 00 00 00 00 00 )
195+
.field public static initonly valuetype [runtime]System.Decimal x
196+
.custom instance void [runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8,
192197
uint8,
193198
int32,
194199
int32,
195-
int32) = ( 01 00 00 00 00 00 00 00 00 00 00 00 03 00 00 00
196-
00 00 )"""
197-
""".maxstack 8"""
198-
"""IL_0000: ldc.i4.3"""
199-
"""IL_0001: ldc.i4.0"""
200-
"""IL_0002: ldc.i4.0"""
201-
"""IL_0003: ldc.i4.0"""
202-
"""IL_0004: ldc.i4.0"""
203-
"""IL_0005: newobj instance void [runtime]System.Decimal::.ctor(int32,
200+
int32) = ( 01 00 01 00 00 00 00 00 00 00 00 00 37 00 00 00
201+
00 00 )
202+
.custom instance void [runtime]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [runtime]System.Diagnostics.DebuggerBrowsableState) = ( 01 00 00 00 00 00 00 00 )
203+
.method private specialname rtspecialname static void .cctor() cil managed
204+
{
205+
206+
.maxstack 8
207+
IL_0000: ldc.i4.0
208+
IL_0001: stsfld int32 '<StartupCode$assembly>'.$DecimalInit::init@
209+
IL_0006: ldsfld int32 '<StartupCode$assembly>'.$DecimalInit::init@
210+
IL_000b: pop
211+
IL_000c: ret
212+
}
213+
214+
.method assembly specialname static void staticInitialization@() cil managed
215+
{
216+
217+
.maxstack 8
218+
IL_0000: ldc.i4.s 55
219+
IL_0002: ldc.i4.0
220+
IL_0003: ldc.i4.0
221+
IL_0004: ldc.i4.0
222+
IL_0005: ldc.i4.1
223+
IL_0006: newobj instance void [runtime]System.Decimal::.ctor(int32,
204224
int32,
205225
int32,
206226
bool,
207-
uint8)"""
208-
"""IL_000a: stsfld valuetype [runtime]System.Decimal LiteralArithmetic::x"""
209-
"""IL_000f: ret"""
227+
uint8)
228+
IL_000b: stsfld valuetype [runtime]System.Decimal DecimalInit::x
229+
IL_0010: ret
230+
}
231+
232+
}
233+
234+
.class private abstract auto ansi sealed '<StartupCode$assembly>'.$DecimalInit
235+
extends [runtime]System.Object
236+
{
237+
.field static assembly int32 init@
238+
.custom instance void [runtime]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [runtime]System.Diagnostics.DebuggerBrowsableState) = ( 01 00 00 00 00 00 00 00 )
239+
.custom instance void [runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
240+
.custom instance void [runtime]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 )
241+
.method private specialname rtspecialname static void .cctor() cil managed
242+
{
243+
244+
.maxstack 8
245+
IL_0000: call void DecimalInit::staticInitialization@()
246+
IL_0005: ret
247+
}
248+
249+
}
250+
"""
251+
]
252+
253+
[<Fact>]
254+
let ``Arithmetic can be used for constructing decimal literals``() =
255+
FSharp """
256+
module LiteralArithmetic
257+
258+
[<Literal>]
259+
let x = 1m + 2m
260+
"""
261+
|> withLangVersion80
262+
|> compile
263+
|> shouldSucceed
264+
|> verifyIL [
265+
""".field public static initonly valuetype [runtime]System.Decimal x
266+
.custom instance void [runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8,
267+
uint8,
268+
int32,
269+
int32,
270+
int32) = ( 01 00 00 00 00 00 00 00 00 00 00 00 03 00 00 00
271+
00 00 )"""
272+
"""
273+
.method assembly specialname static void staticInitialization@() cil managed
274+
{
275+
276+
.maxstack 8
277+
IL_0000: ldc.i4.3
278+
IL_0001: ldc.i4.0
279+
IL_0002: ldc.i4.0
280+
IL_0003: ldc.i4.0
281+
IL_0004: ldc.i4.0
282+
IL_0005: newobj instance void [runtime]System.Decimal::.ctor(int32,
283+
int32,
284+
int32,
285+
bool,
286+
uint8)
287+
IL_000a: stsfld valuetype [runtime]System.Decimal LiteralArithmetic::x
288+
IL_000f: ret
289+
}
290+
"""
210291
]
211292

212293
[<Fact>]
@@ -226,28 +307,45 @@ let test () =
226307
|> compile
227308
|> shouldSucceed
228309
|> verifyIL [
229-
""".field public static initonly valuetype [runtime]System.Decimal x"""
230-
""" .custom instance void [runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8,
231-
uint8,
232-
int32,
233-
int32,
234-
int32) = ( 01 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00
235-
00 00 )"""
236-
"""IL_0016: call bool [netstandard]System.Decimal::op_Equality(valuetype [netstandard]System.Decimal,
237-
valuetype [netstandard]System.Decimal)"""
238-
""".maxstack 8"""
239-
"""IL_0000: ldc.i4.5"""
240-
"""IL_0001: ldc.i4.0"""
241-
"""IL_0002: ldc.i4.0"""
242-
"""IL_0003: ldc.i4.0"""
243-
"""IL_0004: ldc.i4.0"""
244-
"""IL_0005: newobj instance void [runtime]System.Decimal::.ctor(int32,
245-
int32,
246-
int32,
247-
bool,
248-
uint8)"""
249-
"""IL_000a: stsfld valuetype [runtime]System.Decimal PatternMatch::x"""
250-
"""IL_000f: ret"""
310+
"""
311+
.field public static initonly valuetype [runtime]System.Decimal x
312+
.custom instance void [runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8,
313+
uint8,
314+
int32,
315+
int32,
316+
int32) = ( 01 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00
317+
00 00 )"""
318+
"""
319+
.method public static int32 test() cil managed
320+
{
321+
322+
.maxstack 8
323+
.locals init (valuetype [runtime]System.Decimal V_0)
324+
IL_0000: ldc.i4.5
325+
IL_0001: ldc.i4.0
326+
IL_0002: ldc.i4.0
327+
IL_0003: ldc.i4.0
328+
IL_0004: ldc.i4.0
329+
IL_0005: newobj instance void [netstandard]System.Decimal::.ctor(int32,
330+
int32,
331+
int32,
332+
bool,
333+
uint8)
334+
IL_000a: stloc.0
335+
IL_000b: ldloc.0
336+
IL_000c: ldc.i4.5
337+
IL_000d: ldc.i4.0
338+
IL_000e: ldc.i4.0
339+
IL_000f: ldc.i4.0
340+
IL_0010: ldc.i4.0
341+
IL_0011: newobj instance void [netstandard]System.Decimal::.ctor(int32,
342+
int32,
343+
int32,
344+
bool,
345+
uint8)
346+
IL_0016: call bool [netstandard]System.Decimal::op_Equality(valuetype [netstandard]System.Decimal,
347+
valuetype [netstandard]System.Decimal)
348+
"""
251349
]
252350

253351
[<Fact>]
@@ -264,6 +362,56 @@ let y = 42m
264362
|> withLangVersion80
265363
|> compile
266364
|> shouldSucceed
365+
|> verifyIL [
366+
"""
367+
.field public static initonly valuetype [runtime]System.Decimal x
368+
.custom instance void [runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8,
369+
uint8,
370+
int32,
371+
int32,
372+
int32) = ( 01 00 00 00 00 00 00 00 00 00 00 00 29 00 00 00
373+
00 00 )
374+
"""
375+
"""
376+
.field public static initonly valuetype [runtime]System.Decimal y
377+
.custom instance void [runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8,
378+
uint8,
379+
int32,
380+
int32,
381+
int32) = ( 01 00 00 00 00 00 00 00 00 00 00 00 2A 00 00 00
382+
00 00 )
383+
"""
384+
"""
385+
.method assembly specialname static void staticInitialization@() cil managed
386+
{
387+
388+
.maxstack 8
389+
IL_0000: ldc.i4.s 41
390+
IL_0002: ldc.i4.0
391+
IL_0003: ldc.i4.0
392+
IL_0004: ldc.i4.0
393+
IL_0005: ldc.i4.0
394+
IL_0006: newobj instance void [runtime]System.Decimal::.ctor(int32,
395+
int32,
396+
int32,
397+
bool,
398+
uint8)
399+
IL_000b: stsfld valuetype [runtime]System.Decimal DecimalLiterals::x
400+
IL_0010: ldc.i4.s 42
401+
IL_0012: ldc.i4.0
402+
IL_0013: ldc.i4.0
403+
IL_0014: ldc.i4.0
404+
IL_0015: ldc.i4.0
405+
IL_0016: newobj instance void [runtime]System.Decimal::.ctor(int32,
406+
int32,
407+
int32,
408+
bool,
409+
uint8)
410+
IL_001b: stsfld valuetype [runtime]System.Decimal DecimalLiterals::y
411+
IL_0020: ret
412+
}
413+
"""
414+
]
267415

268416
[<Fact>]
269417
let ``Compilation fails when using arithmetic with a non-literal in literal``() =

0 commit comments

Comments
 (0)