Skip to content

Commit ff7b8ff

Browse files
KevinRansomTIHan
authored andcommitted
Improve netcore reference selection (#7263)
* Improve netcore reference selection * Update baselines
1 parent c70ead8 commit ff7b8ff

2 files changed

Lines changed: 136 additions & 75 deletions

File tree

tests/fsharp/Compiler/CompilerAssert.fs

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,76 @@ module CompilerAssert =
3232

3333
let private config = TestFramework.initializeSuite ()
3434

35+
36+
// Do a one time dotnet sdk build to compute the proper set of reference assemblies to pass to the compiler
37+
#if !NETCOREAPP
38+
#else
39+
let projectFile = """
40+
<Project Sdk="Microsoft.NET.Sdk">
41+
42+
<PropertyGroup>
43+
<OutputType>Exe</OutputType>
44+
<TargetFramework>netcoreapp2.1</TargetFramework>
45+
</PropertyGroup>
46+
47+
<ItemGroup><Compile Include="Program.fs" /></ItemGroup>
48+
49+
<Target Name="WriteFrameworkReferences" AfterTargets="AfterBuild">
50+
<WriteLinesToFile File="FrameworkReferences.txt" Lines="@(ReferencePath)" Overwrite="true" WriteOnlyWhenDifferent="true" />
51+
</Target>
52+
53+
</Project>"""
54+
55+
let programFs = """
56+
open System
57+
58+
[<EntryPoint>]
59+
let main argv = 0"""
60+
61+
let getNetCoreAppReferences =
62+
let mutable output = ""
63+
let mutable errors = ""
64+
let mutable cleanUp = true
65+
let projectDirectory = Path.Combine(Path.GetTempPath(), "netcoreapp2.1", Path.GetRandomFileName())
66+
try
67+
try
68+
Directory.CreateDirectory(projectDirectory) |> ignore
69+
let projectFileName = Path.Combine(projectDirectory, "ProjectFile.fsproj")
70+
let programFsFileName = Path.Combine(projectDirectory, "Program.fs")
71+
let frameworkReferencesFileName = Path.Combine(projectDirectory, "FrameworkReferences.txt")
72+
73+
File.WriteAllText(projectFileName, projectFile)
74+
File.WriteAllText(programFsFileName, programFs)
75+
76+
let pInfo = ProcessStartInfo ()
77+
78+
pInfo.FileName <- config.DotNetExe
79+
pInfo.Arguments <- "build"
80+
pInfo.WorkingDirectory <- projectDirectory
81+
pInfo.RedirectStandardOutput <- true
82+
pInfo.RedirectStandardError <- true
83+
pInfo.UseShellExecute <- false
84+
85+
let p = Process.Start(pInfo)
86+
p.WaitForExit()
87+
88+
output <- p.StandardOutput.ReadToEnd ()
89+
errors <- p.StandardError.ReadToEnd ()
90+
if not (String.IsNullOrWhiteSpace errors) then Assert.Fail errors
91+
92+
if p.ExitCode <> 0 then Assert.Fail(sprintf "Program exited with exit code %d" p.ExitCode)
93+
94+
File.ReadLines(frameworkReferencesFileName) |> Seq.toArray
95+
with | e ->
96+
cleanUp <- false
97+
printfn "%s" output
98+
printfn "%s" errors
99+
raise (new Exception (sprintf "An error occured getting netcoreapp references: %A" e))
100+
finally
101+
if cleanUp then
102+
try Directory.Delete(projectDirectory) with | _ -> ()
103+
#endif
104+
35105
let private defaultProjectOptions =
36106
{
37107
ProjectFileName = "Z:\\test.fsproj"
@@ -41,14 +111,7 @@ module CompilerAssert =
41111
OtherOptions = [|"--preferreduilang:en-US";|]
42112
#else
43113
OtherOptions =
44-
// Hack: Currently a hack to get the runtime assemblies for netcore in order to compile.
45-
let assemblies =
46-
typeof<obj>.Assembly.Location
47-
|> Path.GetDirectoryName
48-
|> Directory.EnumerateFiles
49-
|> Seq.toArray
50-
|> Array.filter (fun x -> x.ToLowerInvariant().Contains("system."))
51-
|> Array.map (fun x -> sprintf "-r:%s" x)
114+
let assemblies = getNetCoreAppReferences |> Array.map (fun x -> sprintf "-r:%s" x)
52115
Array.append [|"--preferreduilang:en-US"; "--targetprofile:netcore"; "--noframework"|] assemblies
53116
#endif
54117
ReferencedProjects = [||]
@@ -60,7 +123,7 @@ module CompilerAssert =
60123
ExtraProjectInfo = None
61124
Stamp = None
62125
}
63-
126+
64127
let private gate = obj ()
65128

66129
let private compile isExe source f =
@@ -110,8 +173,6 @@ module CompilerAssert =
110173

111174
Assert.IsEmpty(typeCheckResults.Errors, sprintf "Type Check errors: %A" typeCheckResults.Errors)
112175

113-
114-
115176
let TypeCheckWithErrors (source: string) expectedTypeErrors =
116177
lock gate <| fun () ->
117178
let parseResults, fileAnswer = checker.ParseAndCheckFileInProject("test.fs", 0, SourceText.ofString source, defaultProjectOptions) |> Async.RunSynchronously
@@ -141,7 +202,7 @@ module CompilerAssert =
141202
TypeCheckWithErrors (source: string) [| expectedServerity, expectedErrorNumber, expectedErrorRange, expectedErrorMsg |]
142203

143204
let CompileExe (source: string) =
144-
compile true source (fun (errors, _) ->
205+
compile true source (fun (errors, _) ->
145206
if errors.Length > 0 then
146207
Assert.Fail (sprintf "Compile had warnings and/or errors: %A" errors))
147208

@@ -216,7 +277,7 @@ module CompilerAssert =
216277
||> Seq.iter2 (fun expectedErrorMessage errorMessage ->
217278
Assert.AreEqual(expectedErrorMessage, errorMessage)
218279
)
219-
280+
220281
let ParseWithErrors (source: string) expectedParseErrors =
221282
let parseResults = checker.ParseFile("test.fs", SourceText.ofString source, FSharpParsingOptions.Default) |> Async.RunSynchronously
222283

tests/fsharp/Compiler/Language/SpanOptimizationTests.fs

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,45 @@ let test () =
2727
verifier.VerifyIL
2828
[
2929
""".method public static void test() cil managed
30-
{
31-
32-
.maxstack 5
33-
.locals init (valuetype [System.Private.CoreLib]System.Span`1<class [System.Private.CoreLib]System.Object> V_0,
34-
int32 V_1,
35-
valuetype [System.Private.CoreLib]System.Int32 V_2,
36-
class [System.Private.CoreLib]System.Object& V_3)
37-
IL_0000: call valuetype [System.Private.CoreLib]System.Span`1<!0> valuetype [System.Private.CoreLib]System.Span`1<class [System.Private.CoreLib]System.Object>::get_Empty()
38-
IL_0005: stloc.0
39-
IL_0006: ldc.i4.0
40-
IL_0007: stloc.2
41-
IL_0008: ldloca.s V_0
42-
IL_000a: call instance int32 valuetype [System.Private.CoreLib]System.Span`1<class [System.Private.CoreLib]System.Object>::get_Length()
43-
IL_000f: ldc.i4.1
44-
IL_0010: sub
45-
IL_0011: stloc.1
46-
IL_0012: ldloc.1
47-
IL_0013: ldloc.2
48-
IL_0014: blt.s IL_0034
49-
50-
IL_0016: ldloca.s V_0
51-
IL_0018: ldloc.2
52-
IL_0019: call instance !0& valuetype [System.Private.CoreLib]System.Span`1<class [System.Private.CoreLib]System.Object>::get_Item(int32)
53-
IL_001e: stloc.3
54-
IL_001f: ldloc.3
55-
IL_0020: ldobj [System.Private.CoreLib]System.Object
56-
IL_0025: call void [System.Console]System.Console::WriteLine(object)
57-
IL_002a: ldloc.2
58-
IL_002b: ldc.i4.1
59-
IL_002c: add
60-
IL_002d: stloc.2
61-
IL_002e: ldloc.2
62-
IL_002f: ldloc.1
63-
IL_0030: ldc.i4.1
64-
IL_0031: add
65-
IL_0032: bne.un.s IL_0016
66-
67-
IL_0034: ret
68-
} """
30+
{
31+
32+
.maxstack 5
33+
.locals init (valuetype [System.Runtime]System.Span`1<object> V_0,
34+
int32 V_1,
35+
int32 V_2,
36+
object& V_3)
37+
IL_0000: call valuetype [System.Runtime]System.Span`1<!0> valuetype [System.Runtime]System.Span`1<object>::get_Empty()
38+
IL_0005: stloc.0
39+
IL_0006: ldc.i4.0
40+
IL_0007: stloc.2
41+
IL_0008: ldloca.s V_0
42+
IL_000a: call instance int32 valuetype [System.Runtime]System.Span`1<object>::get_Length()
43+
IL_000f: ldc.i4.1
44+
IL_0010: sub
45+
IL_0011: stloc.1
46+
IL_0012: ldloc.1
47+
IL_0013: ldloc.2
48+
IL_0014: blt.s IL_0034
49+
50+
IL_0016: ldloca.s V_0
51+
IL_0018: ldloc.2
52+
IL_0019: call instance !0& valuetype [System.Runtime]System.Span`1<object>::get_Item(int32)
53+
IL_001e: stloc.3
54+
IL_001f: ldloc.3
55+
IL_0020: ldobj [System.Runtime]System.Object
56+
IL_0025: call void [System.Console]System.Console::WriteLine(object)
57+
IL_002a: ldloc.2
58+
IL_002b: ldc.i4.1
59+
IL_002c: add
60+
IL_002d: stloc.2
61+
IL_002e: ldloc.2
62+
IL_002f: ldloc.1
63+
IL_0030: ldc.i4.1
64+
IL_0031: add
65+
IL_0032: bne.un.s IL_0016
66+
67+
IL_0034: ret
68+
}"""
6969
])
7070

7171
[<Test>]
@@ -88,18 +88,18 @@ let test () =
8888
[
8989
""".method public static void test() cil managed
9090
{
91-
91+
9292
.maxstack 5
93-
.locals init (valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<class [System.Private.CoreLib]System.Object> V_0,
93+
.locals init (valuetype [System.Runtime]System.ReadOnlySpan`1<object> V_0,
9494
int32 V_1,
95-
valuetype [System.Private.CoreLib]System.Int32 V_2,
96-
class [System.Private.CoreLib]System.Object& V_3)
97-
IL_0000: call valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<!0> valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<class [System.Private.CoreLib]System.Object>::get_Empty()
95+
int32 V_2,
96+
object& V_3)
97+
IL_0000: call valuetype [System.Runtime]System.ReadOnlySpan`1<!0> valuetype [System.Runtime]System.ReadOnlySpan`1<object>::get_Empty()
9898
IL_0005: stloc.0
9999
IL_0006: ldc.i4.0
100100
IL_0007: stloc.2
101101
IL_0008: ldloca.s V_0
102-
IL_000a: call instance int32 valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<class [System.Private.CoreLib]System.Object>::get_Length()
102+
IL_000a: call instance int32 valuetype [System.Runtime]System.ReadOnlySpan`1<object>::get_Length()
103103
IL_000f: ldc.i4.1
104104
IL_0010: sub
105105
IL_0011: stloc.1
@@ -109,10 +109,10 @@ let test () =
109109
110110
IL_0016: ldloca.s V_0
111111
IL_0018: ldloc.2
112-
IL_0019: call instance !0& modreq([System.Private.CoreLib]System.Runtime.InteropServices.InAttribute) valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<class [System.Private.CoreLib]System.Object>::get_Item(int32)
112+
IL_0019: call instance !0& modreq([System.Runtime]System.Runtime.InteropServices.InAttribute) valuetype [System.Runtime]System.ReadOnlySpan`1<object>::get_Item(int32)
113113
IL_001e: stloc.3
114114
IL_001f: ldloc.3
115-
IL_0020: ldobj [System.Private.CoreLib]System.Object
115+
IL_0020: ldobj [System.Runtime]System.Object
116116
IL_0025: call void [System.Console]System.Console::WriteLine(object)
117117
IL_002a: ldloc.2
118118
IL_002b: ldc.i4.1
@@ -176,54 +176,54 @@ module Test =
176176
[
177177
""".method public static void test() cil managed
178178
{
179-
179+
180180
.maxstack 3
181-
.locals init (valuetype System.Span`1<class [System.Private.CoreLib]System.Object> V_0,
182-
class [System.Private.CoreLib]System.Collections.IEnumerator V_1,
181+
.locals init (valuetype System.Span`1<object> V_0,
182+
class [System.Runtime]System.Collections.IEnumerator V_1,
183183
class [FSharp.Core]Microsoft.FSharp.Core.Unit V_2,
184-
class [System.Private.CoreLib]System.IDisposable V_3)
184+
class [System.Runtime]System.IDisposable V_3)
185185
IL_0000: ldc.i4.0
186-
IL_0001: newarr [System.Private.CoreLib]System.Object
187-
IL_0006: newobj instance void valuetype System.Span`1<class [System.Private.CoreLib]System.Object>::.ctor(!0[])
186+
IL_0001: newarr [System.Runtime]System.Object
187+
IL_0006: newobj instance void valuetype System.Span`1<object>::.ctor(!0[])
188188
IL_000b: stloc.0
189189
IL_000c: ldloc.0
190-
IL_000d: box valuetype System.Span`1<class [System.Private.CoreLib]System.Object>
191-
IL_0012: unbox.any [System.Private.CoreLib]System.Collections.IEnumerable
192-
IL_0017: callvirt instance class [System.Private.CoreLib]System.Collections.IEnumerator [System.Private.CoreLib]System.Collections.IEnumerable::GetEnumerator()
190+
IL_000d: box valuetype System.Span`1<object>
191+
IL_0012: unbox.any [System.Runtime]System.Collections.IEnumerable
192+
IL_0017: callvirt instance class [System.Runtime]System.Collections.IEnumerator [System.Runtime]System.Collections.IEnumerable::GetEnumerator()
193193
IL_001c: stloc.1
194194
.try
195195
{
196196
IL_001d: ldloc.1
197-
IL_001e: callvirt instance bool [System.Private.CoreLib]System.Collections.IEnumerator::MoveNext()
197+
IL_001e: callvirt instance bool [System.Runtime]System.Collections.IEnumerator::MoveNext()
198198
IL_0023: brfalse.s IL_0032
199199
200200
IL_0025: ldloc.1
201-
IL_0026: callvirt instance object [System.Private.CoreLib]System.Collections.IEnumerator::get_Current()
201+
IL_0026: callvirt instance object [System.Runtime]System.Collections.IEnumerator::get_Current()
202202
IL_002b: call void [System.Console]System.Console::WriteLine(object)
203203
IL_0030: br.s IL_001d
204204
205205
IL_0032: ldnull
206206
IL_0033: stloc.2
207207
IL_0034: leave.s IL_004c
208208
209-
}
209+
}
210210
finally
211211
{
212212
IL_0036: ldloc.1
213-
IL_0037: isinst [System.Private.CoreLib]System.IDisposable
213+
IL_0037: isinst [System.Runtime]System.IDisposable
214214
IL_003c: stloc.3
215215
IL_003d: ldloc.3
216216
IL_003e: brfalse.s IL_0049
217217
218218
IL_0040: ldloc.3
219-
IL_0041: callvirt instance void [System.Private.CoreLib]System.IDisposable::Dispose()
219+
IL_0041: callvirt instance void [System.Runtime]System.IDisposable::Dispose()
220220
IL_0046: ldnull
221221
IL_0047: pop
222222
IL_0048: endfinally
223223
IL_0049: ldnull
224224
IL_004a: pop
225225
IL_004b: endfinally
226-
}
226+
}
227227
IL_004c: ldloc.2
228228
IL_004d: pop
229229
IL_004e: ret

0 commit comments

Comments
 (0)