Skip to content

Commit e4eda12

Browse files
authored
fix 4715 (#12910)
1 parent e907eaa commit e4eda12

File tree

5 files changed

+99
-10
lines changed

5 files changed

+99
-10
lines changed

src/fsharp/IlxGen.fs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,16 +1250,12 @@ let ComputeFieldSpecForVal(optIntraAssemblyInfo: IlxGenIntraAssemblyInfo option,
12501250
match optIntraAssemblyInfo with
12511251
| None -> generate()
12521252
| Some intraAssemblyInfo ->
1253-
if vspec.IsMutable && vspec.IsCompiledAsTopLevel && isStructTy g vspec.Type then
1254-
let ok, res = intraAssemblyInfo.StaticFieldInfo.TryGetValue ilGetterMethRef
1255-
if ok then
1256-
res
1257-
else
1258-
let res = generate()
1259-
intraAssemblyInfo.StaticFieldInfo.[ilGetterMethRef] <- res
1260-
res
1261-
else
1262-
generate()
1253+
match intraAssemblyInfo.StaticFieldInfo.TryGetValue ilGetterMethRef with
1254+
| true, res -> res
1255+
| _ ->
1256+
let res = generate()
1257+
intraAssemblyInfo.StaticFieldInfo.[ilGetterMethRef] <- res
1258+
res
12631259

12641260
/// Compute the representation information for an F#-declared value (not a member nor a function).
12651261
/// Mutable and literal static fields must have stable names and live in the "public" location
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#light
2+
[<RequireQualifiedAccess>]
3+
module app.date
4+
5+
open System
6+
open System.Threading
7+
8+
type T = private T of DateTime
9+
10+
let mutable private _today = None
11+
12+
let set_today (dt:DateTime) = Interlocked.Exchange(&_today, Some (T dt.Date))
13+
14+
let today () =
15+
match _today with
16+
| Some d -> d
17+
| None -> T DateTime.Today.Date
18+
19+
let yesterday () =
20+
let today = DateTime.Today.Date
21+
T (today - TimeSpan(1, 0, 0, 0))
22+
23+
let inline private unwrap (T d) = d
24+
25+
let fromdt (dt:DateTime) = T dt.Date
26+
27+
let year d = (unwrap d).Year
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#light
2+
[<RequireQualifiedAccess>]
3+
module app.env
4+
5+
open System
6+
//open System.Runtime.CompilerServices
7+
open System.Threading
8+
9+
let mutable private _processing_date:date.T option = None
10+
let mutable private _irs_received_date:date.T option = None
11+
12+
//[<MethodImpl (MethodImplOptions.NoInlining)>]
13+
let set_processing_date d = Interlocked.Exchange(&_processing_date, Some d)
14+
15+
//[<MethodImpl (MethodImplOptions.NoInlining)>]
16+
let set_irs_received_dt d = Interlocked.Exchange(&_irs_received_date, Some d)
17+
18+
let processing_date () =
19+
match _processing_date with
20+
| Some d -> d
21+
| None -> date.fromdt DateTime.Today
22+
23+
let processing_year () =
24+
let d = processing_date ()
25+
date.year d
26+
27+
let tax_year () =
28+
let yr = processing_year ()
29+
yr - 1
30+
31+
let irs_received_date () = _irs_received_date
32+
33+
let irs_received_date_exn () =
34+
match _irs_received_date with
35+
| Some d -> d
36+
| None -> failwith "env.irs_received_date uninitialized via set_irs_received_dt"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#light
2+
module app.main
3+
4+
open System
5+
6+
let private init_dates rdate pdate =
7+
match rdate with
8+
| Some d -> env.set_irs_received_dt d |> ignore
9+
| None -> ()
10+
11+
match pdate with
12+
| Some d -> env.set_processing_date d |> ignore
13+
| None -> ()
14+
15+
[<EntryPoint>]
16+
let main argv =
17+
try
18+
19+
let rdate = Some (date.yesterday ())
20+
let pdate = Some (date.today () )
21+
init_dates rdate pdate
22+
with
23+
| ex -> eprintfn "Unhandled exception: %s" (ex.ToString()) ; exit 1
24+
25+
0

tests/fsharp/tests.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,11 @@ module RegressionTests =
21432143
[<Test >]
21442144
let ``12383-FSC_OPTIMIZED`` () = singleTestBuildAndRun "regression/12383" FSC_OPTIMIZED
21452145

2146+
[<Test >]
2147+
let ``4715-optimized`` () =
2148+
let cfg = testConfig "regression/4715"
2149+
fsc cfg "%s -o:test.exe --optimize+" cfg.fsc_flags ["date.fs"; "env.fs"; "main.fs"]
2150+
21462151
#if NETCOREAPP
21472152
[<Test >]
21482153
let ``Large inputs 12322 fsc.dll 64-bit fsc.dll .NET SDK generating optimized code`` () =

0 commit comments

Comments
 (0)