`allows ref struct` type parameters accept `'T byref` arguments: `int byref seq` compiles on net9.0+ and fails with `TypeLoadException`

Offen
#20,936 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
52/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
fsharp
Bereich
compilers

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Bug Needs-Triage Regression

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
  1. Put the file in a library project with <TargetFramework>net9.0</TargetFramework> (same with net10.0 and net11.0) and run dotnet build. Only h reports an error.
  2. Reflect over Lib.g in 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'
  1. In dotnet fsi, let f xs = for x: int byref in xs do () prints val f: xs: byref<int> seq -> unit. Any use of f then 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.0 build of the same file reports the errors.
  • On .NET 11: typedefof<seq<_>>.GetGenericArguments().[0].GenericParameterAttributes = Covariant, AllowByRefLike.
Vorherrschende Sprache
F#
Sterne
4.3k
Forks
883
Ø Merge
2 T. 12 Std.
Gemergte PRs (30 T.)
80

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus dotnet/fsharp

Alle Issues in dotnet/fsharp

Ähnliche Issues

Weitere Issues zu Compilers

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.