Release: `inline` function taking the address of a module-level `let mutable` emits a field reference to the wrong startup class when inlined into another assembly (TypeLoadException)
Maintainer thường phản hồi trong vòng 3 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 28/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- compilers
Hướng nghiên cứu
Reproduce with the two projects in the report: Lib/State.fs defines the inline read that takes &value, and App/Program.fs calls it. Build both in Release, then inspect App.dll for the value@3 MemberRef and compare its parent type with the startup classes in Lib.dll. Done means the Release app prints ok without a TypeLoadException, or the compiler stops inlining such a function across assemblies.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
When an inline function takes the address of a module-level let mutable value (&value, e.g. to call Volatile.Read) and that function is inlined into another assembly, the optimized (Release) build of the calling assembly emits a MemberRef to the backing static field on a type named after the assembly (<StartupCode$Lib>.$Lib) instead of the startup class of the file that defines the value (<StartupCode$Lib>.$State). At run time this fails with TypeLoadException. Debug builds (no cross-assembly inlining) work.
To Reproduce
Lib/Lib.fsproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="State.fs" />
</ItemGroup>
</Project>
Lib/State.fs:
module State
let mutable value : string = "ok"
let inline read () : string = System.Threading.Volatile.Read(&value)
App/App.fsproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lib\Lib.fsproj" />
</ItemGroup>
</Project>
App/Program.fs:
[<EntryPoint>]
let main _ =
printfn "%s" (State.read ())
0
dotnet run --project App -c Debug # prints: ok
dotnet run --project App -c Release # fails:
Unhandled exception. System.TypeLoadException: Could not load type '<StartupCode$Lib>.$Lib' from assembly 'Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at Program.main(String[] _arg1)
Metadata of the Release App.dll versus the types that actually exist in Lib.dll:
App TypeRef: ns=<StartupCode$Lib> name=$Lib
App MemberRef: value@3 on <StartupCode$Lib>.$Lib
Lib types: State
<StartupCode$Lib>.$State
<StartupCode$Lib>.$Lib.AssemblyInfo
Expected behavior
Either the inlined code references the correct, accessible storage for value, or the compiler does not inline such a function across assemblies (the backing field is assembly-internal, so even the correct type name would fail with FieldAccessException). It should not compile into a reference to a non-existent type.
Actual behavior
TypeLoadException at run time in Release only. In a larger project this surfaced as hundreds of failing tests in Release (and in a Microsoft.NET.Sdk.WebAssembly publish) while Debug was fully green. The assembly name does not matter: the same happens for CasAlgebra and Cas.Algebra (<StartupCode$Cas-Algebra>.$Cas.Algebra).
Known workarounds
- Do not take the address of a module-level
let mutablein aninlinefunction that may be inlined into another assembly: plain reads/assignments (optionally withThread.MemoryBarrier()) work. - Not inlining the function (
inlineremoved, or[<MethodImpl(MethodImplOptions.NoInlining)>]) also avoids the problem. --crossoptimize-on the consuming project did not help in our case.
Related information
- .NET SDK 10.0.400, F# 10.0 (
dotnet fsi --version: 15.2.400), FSharp.Core 10.1.400 - Windows 10 x64; also reproduced in a .NET WebAssembly (AOT) host
- Occurs with
Volatile.Read(&x),Volatile.Write(&x, v)on module-levellet mutablevalues used frominlinefunctions
- Ngôn ngữ chính
- F#
- Star
- 4.3k
- Fork
- 883
- Merge trung bình
- 3 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 110
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/fsharp
-
Needs-Triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Semantic classification cache for opened documents is never populated (written to the unopened-documents cache)Có thể đã có người làm @xperiandri đã nhận 61 ngày trước. Đang mởNeeds-Triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Needs-Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/fsharp#20265 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Bug Needs-Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 3 ngày
-
agentic-workflows
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của dotnet/fsharp
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 87/100
gleam-lang/gleam#6491 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
clang:frontend
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
llvm/llvm-project#238916 · 6 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
C ABI signature lowering drops explicit function alignmentCó thể đã có người làm @cpunion đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug Fortran low priority OOP
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100