`allows ref struct` type parameters accept `'T byref` arguments: `int byref seq` compiles on net9.0+ and fails with `TypeLoadException`
Les mainteneurs répondent en général sous 2 jours
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 52/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- fsharp
- Domaine
- compilers
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- F#
- Étoiles
- 4.3k
- Forks
- 883
- Merge moyen
- 3 j 39 min
- PR mergées (30 j)
- 83
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de dotnet/fsharp
-
Needs-Triage
Difficulté 1/5 Moins d'une heure Accessibilité débutants 95/100
Les mainteneurs répondent en général sous 2 jours
-
Semantic classification cache for opened documents is never populated (written to the unopened-documents cache)Peut-être pris @xperiandri l’a pris il y a 65 jours. OuverteNeeds-Triage
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
Les mainteneurs répondent en général sous 2 jours
-
Needs-Triage
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
dotnet/fsharp#20265 · 1 commentaire ·
Les mainteneurs répondent en général sous 2 jours
-
Bug Needs-Triage
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 2 jours
-
Needs-Triage
Difficulté 4/5 3-5 jours Accessibilité débutants 42/100
Les mainteneurs répondent en général sous 2 jours
Toutes les issues de dotnet/fsharp
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
voidzero-dev/oxc-angular-compiler#686 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
godotengine/godot#125272 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 73/100
Les mainteneurs répondent en général sous 1 jour
-
bug compiler copilot good first issue help wanted
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
-
bug front-end
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour