Treat span-like types with a unit of measure as byref-like

Ouverte
#20,907 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

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
40/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Domaine
compilers

Piste de recherche

Start from isByrefLikeTyconRef and stripTyEqns in the compiler's type utilities, which the report says miss the representation of measure-annotated abbreviations. Run the repro with the TaggedSpan<kg> example and compare with the TaggedSpan<1> case, which already gives FS0406 and FS0412. Done means the kg version reports the same errors and existing byref-like tests still pass. The author already has a fix, so check with the maintainers before starting a parallel one.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Needs-Triage

A measure-annotated abbreviation of Span<int> is not treated as byref-like when its unit of measure is anything other than 1. The compiler then accepts code that captures such a value in a closure or uses its type as a type argument, and the program fails at run time.

open System
[<Measure>] type kg
[<MeasureAnnotatedAbbreviation>]
type TaggedSpan<[<Measure>] 'u> = Span<int>
let size (s: TaggedSpan<'u>) = 0

let capture (s: TaggedSpan<kg>) = let t = s in fun () -> size t
let instantiate (s: TaggedSpan<kg>) = id s

[<EntryPoint>]
let main args =
    let s = Unchecked.defaultof<TaggedSpan<kg>>
    if args.Length = 0 then capture s () else size (instantiate s)

Actual: this compiles with the .NET SDK 10.0.402 compiler and with main at fbc9be3. Compiled without optimization:

  • With no arguments, capture throws TypeLoadException: A ByRef or ByRef-like type cannot be used as the type for an instance field in a non-ByRef-like type.
  • With an argument, instantiate throws VerificationException: Method Microsoft.FSharp.Core.Operators.Identity: type argument 'System.Span`1[System.Int32]' violates the constraint of type parameter 'T'.

Expected: the errors the compiler reports when kg is replaced by 1: FS0406 on the closure in capture, and FS0412 on id s and on Unchecked.defaultof.

isByrefLikeTyconRef does not look at the representation of a measure-annotated type, and stripTyEqns reduces such a type to its representation only when the measure is dimensionless, so TaggedSpan<kg> is not recognised as byref-like. I found this while reviewing #20856, whose tests define TaggedSpan. I have a fix ready.

[!NOTE]
Prepared with AI assistance (Anthropic Claude, OpenAI Codex); Peter reviewed it.

Langage dominant
F#
Étoiles
4.3k
Forks
884
Merge moyen
2 j 11 h
PR mergées (30 j)
73

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de dotnet/fsharp

Toutes les issues de dotnet/fsharp

Issues similaires

Plus d'issues Compilers

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.