From 6c2b27dea69c16cc9de76e21c6f957c80a4fca37 Mon Sep 17 00:00:00 2001
From: Tomas Grosup <tomasgrosup@microsoft.com>
Date: Thu, 26 Sep 2024 15:17:58 +0200
Subject: [PATCH 1/6] Treat System.Private.CoreLib as a system lib for
 extension method search

---
 src/Compiler/AbstractIL/ilread.fs                |  1 +
 .../FSharpScriptTests.fs                         | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs
index 7ffa71feeee..9d174ec4965 100644
--- a/src/Compiler/AbstractIL/ilread.fs
+++ b/src/Compiler/AbstractIL/ilread.fs
@@ -2148,6 +2148,7 @@ and typeDefReader ctxtH : ILTypeDefStored =
                     ctxt.fileName.EndsWith("System.Runtime.dll")
                     || ctxt.fileName.EndsWith("mscorlib.dll")
                     || ctxt.fileName.EndsWith("netstandard.dll")
+                    || ctxt.fileName.EndsWith("System.Private.CoreLib.dll")
 
                 while attrIdx <= attrsEndIdx && not containsExtensionMethods do
                     let mutable addr = ctxt.rowAddr TableNames.CustomAttribute attrIdx
diff --git a/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs b/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs
index 50c354da629..fb8a0ad169e 100644
--- a/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs
+++ b/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs
@@ -352,6 +352,22 @@ tInput.Length
         let value = opt.Value
         Assert.Equal(4L, downcast value.ReflectionValue)
 
+    [<Fact>]
+    member _.``ML - use assembly with ref dependencies and without refing SMemory``() =
+        let code = """
+#r "nuget:Microsoft.ML.OnnxTransformer,1.4.0"
+
+open System
+open System.Numerics.Tensors
+let inputValues = [| 12.0; 10.0; 17.0; 5.0 |]
+let tInput = new DenseTensor<float>(inputValues.AsMemory(), new ReadOnlySpan<int>([|4|]))
+tInput.Length
+"""
+        use script = new FSharpScript(additionalArgs=[| "/usesdkrefs-" |])
+        let opt = script.Eval(code)  |> getValue
+        let value = opt.Value
+        Assert.Equal(4L, downcast value.ReflectionValue)
+
     [<Fact>]
     member _.``System.Device.Gpio - Ensure we reference the runtime version of the assembly``() =
         let code = """

From 3cd0cbbe594e14bb7d79e6c8f4c8706c555d1b6b Mon Sep 17 00:00:00 2001
From: Tomas Grosup <tomasgrosup@microsoft.com>
Date: Thu, 26 Sep 2024 17:47:10 +0200
Subject: [PATCH 2/6] usessdkrefs is not a valid option for desktop compiler

---
 .../FSharpScriptTests.fs                                        | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs b/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs
index fb8a0ad169e..08fa3ec324d 100644
--- a/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs
+++ b/tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharpScriptTests.fs
@@ -352,7 +352,7 @@ tInput.Length
         let value = opt.Value
         Assert.Equal(4L, downcast value.ReflectionValue)
 
-    [<Fact>]
+    [<FSharp.Test.FactForNETCOREAPP>] // usessdkrefs is not a valid option for desktop compiler
     member _.``ML - use assembly with ref dependencies and without refing SMemory``() =
         let code = """
 #r "nuget:Microsoft.ML.OnnxTransformer,1.4.0"

From dca40aee91a23be6726c4e2325bea34fba4c5cfe Mon Sep 17 00:00:00 2001
From: Tomas Grosup <tomasgrosup@microsoft.com>
Date: Thu, 26 Sep 2024 17:49:32 +0200
Subject: [PATCH 3/6] notes

---
 docs/release-notes/.FSharp.Compiler.Service/9.0.200.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
index 5ef93105656..f8806ea51ad 100644
--- a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
+++ b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
@@ -1,5 +1,6 @@
 ### Fixed
 
+* Fix extension methods support for non-reference system assemblies ([PR #17799](https://github.com/dotnet/fsharp/pull/17799))
 
 ### Added
 

From 46efff60e3962f265075970df91931f183ae9e09 Mon Sep 17 00:00:00 2001
From: Tomas Grosup <tomasgrosup@microsoft.com>
Date: Thu, 26 Sep 2024 19:20:53 +0200
Subject: [PATCH 4/6] Revert "notes"

This reverts commit dca40aee91a23be6726c4e2325bea34fba4c5cfe.
---
 docs/release-notes/.FSharp.Compiler.Service/9.0.200.md | 1 -
 1 file changed, 1 deletion(-)

diff --git a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
index f8806ea51ad..5ef93105656 100644
--- a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
+++ b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
@@ -1,6 +1,5 @@
 ### Fixed
 
-* Fix extension methods support for non-reference system assemblies ([PR #17799](https://github.com/dotnet/fsharp/pull/17799))
 
 ### Added
 

From 96903d357b4d06653b80dc6a41572f42d10eb2b5 Mon Sep 17 00:00:00 2001
From: Tomas Grosup <tomasgrosup@microsoft.com>
Date: Thu, 26 Sep 2024 19:24:03 +0200
Subject: [PATCH 5/6] Release notes back

---
 docs/release-notes/.FSharp.Compiler.Service/9.0.200.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
index 5ef93105656..a04aef1056f 100644
--- a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
+++ b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
@@ -1,5 +1,5 @@
 ### Fixed
-
+* Fix extension methods support for non-reference system assemblies ([PR #17799](https://github.com/dotnet/fsharp/pull/17799))
 
 ### Added
 

From f9e8a1c3882af1aa583a964fae9e448a52e9e049 Mon Sep 17 00:00:00 2001
From: Tomas Grosup <tomasgrosup@microsoft.com>
Date: Thu, 26 Sep 2024 21:49:49 +0200
Subject: [PATCH 6/6] get rid of looksLikeSystemAssembly  in ilread, it is not
 needed

---
 src/Compiler/AbstractIL/ilread.fs | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)

diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs
index 9d174ec4965..5bedfe05752 100644
--- a/src/Compiler/AbstractIL/ilread.fs
+++ b/src/Compiler/AbstractIL/ilread.fs
@@ -2144,12 +2144,6 @@ and typeDefReader ctxtH : ILTypeDefStored =
             else
                 let mutable attrIdx = attrsStartIdx
 
-                let looksLikeSystemAssembly =
-                    ctxt.fileName.EndsWith("System.Runtime.dll")
-                    || ctxt.fileName.EndsWith("mscorlib.dll")
-                    || ctxt.fileName.EndsWith("netstandard.dll")
-                    || ctxt.fileName.EndsWith("System.Private.CoreLib.dll")
-
                 while attrIdx <= attrsEndIdx && not containsExtensionMethods do
                     let mutable addr = ctxt.rowAddr TableNames.CustomAttribute attrIdx
                     // skip parentIndex to read typeIndex
@@ -2160,12 +2154,9 @@ and typeDefReader ctxtH : ILTypeDefStored =
                     let name =
                         if attrTypeIndex.tag = cat_MethodDef then
                             // the ExtensionAttribute constructor can be cat_MethodDef if the metadata is read from the assembly
-                            // in which the corresponding attribute is defined -- from the system library
-                            if not looksLikeSystemAssembly then
-                                ""
-                            else
-                                let _, (_, nameIdx, namespaceIdx, _, _, _) = seekMethodDefParent ctxt attrCtorIdx
-                                readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
+                            // in which the corresponding attribute is defined
+                            let _, (_, nameIdx, namespaceIdx, _, _, _) = seekMethodDefParent ctxt attrCtorIdx
+                            readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
                         else
                             let mutable addr = ctxt.rowAddr TableNames.MemberRef attrCtorIdx
                             let mrpTag = seekReadMemberRefParentIdx ctxt mdv &addr
