`allows ref struct` type parameters accept `'T byref` arguments: `int byref seq` compiles on net9.0+ and fails with `TypeLoadException`
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 52/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- fsharp
- 领域
- compilers
调研方向
Read src/Compiler/Checking/PostInferenceChecks.fs, especially ByrefTypeCheck.VisitTyconRef and the PermitByRefType branches. Compare the byref and byref-like checks with the repro in the issue, then build the library targeting net9.0 and verify the expected FS3300 errors for f and g. Done when managed-pointer type arguments are rejected while byref-like structs remain permitted.
由索引模型根据 Issue 内容生成。
描述
Since F# 9 (#17706, consuming allows ref struct generic parameters) the post-inference byref checks skip a type argument entirely when its type parameter is allows ref struct. That lets managed-pointer types through (byref<T>, inref<T>, outref<T>), not only byref-like structs. On .NET 9 and later IEnumerable<T>, IEnumerator<T>, Func<> and Action<> carry AllowByRefLike, so seq<byref<int>> type-checks, IL is emitted, and the runtime rejects it at type load.
Repro steps
module Lib
let f xs = for x: int byref in xs do () // inferred xs: seq<byref<int>>, no error
let g (xs: seq<byref<int>>) = () // no error
let h (xs: byref<int> list) = () // FS3300, as expected
- Put the file in a library project with
<TargetFramework>net9.0</TargetFramework>(same withnet10.0andnet11.0) and rundotnet build. Onlyhreports an error. - Reflect over
Lib.gin the built assembly:
System.TypeLoadException: The generic type 'System.Collections.Generic.IEnumerable`1' was used with an invalid instantiation in assembly 'System.Private.CoreLib, Version=11.0.0.0'
- In
dotnet fsi,let f xs = for x: int byref in xs do ()printsval f: xs: byref<int> seq -> unit. Any use offthen fails:
error FS0193: internal error: Unable to load one or more of the requested types.
The generic type 'System.Collections.Generic.IEnumerable`1' was used with an invalid instantiation
Expected behavior
The same errors as with net8.0, where the file reports FS3300 for f, g and h and FS0412 at the loop. allows ref struct permits byref-like structs such as Span<T>; it never permits managed pointers (ECMA-335 II.9.4).
Actual behavior
f and g compile on net9.0, net10.0, net11.0 and in fsi. The output fails at type load.
Cause
src/Compiler/Checking/PostInferenceChecks.fs, ByrefTypeCheck.VisitTyconRef: the guard if not (ctx.TyparAllowsRefStruct()) then wraps all four PermitByRefType branches, including the isByrefTyconRef test in the SpanLike branch and the byref part of isByrefLikeTyconRef in the other branches. The anti-constraint should relax only the byref-like test; a byref type argument must still be an error. Introduced in bb027e1378.
Known workarounds
None needed: the code cannot run. Found while writing fslang-design#845 (RFC FS-1348), whose compatibility section assumed this form is always a compile-time error.
Related information
- Windows 11, .NET SDK 11.0.100-rc.1.26420.103 and its F# compiler;
net8.0build of the same file reports the errors. - On .NET 11:
typedefof<seq<_>>.GetGenericArguments().[0].GenericParameterAttributes=Covariant, AllowByRefLike.
- 主要语言
- F#
- 星标
- 4.3k
- 派生
- 883
- 平均合并
- 3 天 10 小时
- 30 天内合并 PR
- 95
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/fsharp 的其他 Issue
-
Needs-Triage
难度 1/5 1 小时以内 新手友好度 95/100
维护者通常 2 天内回复
-
Semantic classification cache for opened documents is never populated (written to the unopened-documents cache)可能已有人在做 @xperiandri 于 63 天前认领。 未关闭Needs-Triage
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 2 天内回复
-
Needs-Triage
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 2 天内回复
-
Bug Needs-Triage
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 2 天内回复
-
Bug Needs-Triage Regression
难度 4/5 3-5 天 新手友好度 48/100
维护者通常 2 天内回复
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
GaloisInc/llvm-pretty#211 ·
-
难度 2/5 1-3 小时 新手友好度 63/100
KhronosGroup/SPIRV-LLVM-Translator#4151 ·
维护者通常 2 天内回复
-
S3: friction & debt ws:emitter ws:wasm
难度 2/5 1-3 小时 新手友好度 78/100
MedakaLang/medaka#4913 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 74/100