Skip to content

Unexpected type inference for Run method - generic constraints are checked too late #19349

Closed

Description

@Lanayx

Repro steps

open System.Runtime.CompilerServices

// -------------------------
// "View engine" bits
// -------------------------
type HtmlContainer() = class end

type HtmlContainerFun = HtmlContainer -> unit

[<Extension>]
type HtmlContainerExtensions =
    [<Extension>]
    static member inline Run(this: #HtmlContainer, [<InlineIfLambda>] runExpr: HtmlContainerFun) =
        runExpr this
        this

// -------------------------
// "AsyncSeq-like" CE builder bits
// -------------------------
type AsyncSeq<'T> = private AsyncSeq of 'T list

type AsyncSeqBuilder() =
    member _.Yield(x: 'T) : AsyncSeq<'T> =
        AsyncSeq [x]

let asyncSeq = AsyncSeqBuilder()

let xs : AsyncSeq<int> =
    asyncSeq {
        yield 1
    }

From Don's comment:

This could be onsidered an F# compiler bug but we'd have to check the spec - the compiler commits to using this Run before it knows the constraint check #HtmlContainer will fail.

Expected behavior

Code compiles just fine - Run method from HtmlContainerExtensions isn't used for asyncSeq

Actual behavior

error FS0193: The type 'AsyncSeqBuilder' is not compatible with the type 'HtmlContainer'

Known workarounds

No workarounds are known

Related information

Related issues:
Lanayx/Oxpecker#93
fsprojects/FSharp.Control.AsyncSeq#239

.NET SDK 10.0.102

Activity

  1. added theissue type on Feb 23, 2026
  2. added this to the Backlog milestone on Feb 23, 2026
  3. evgTSV commented on Feb 24, 2026

    @evgTSV
    Contributor

    I'm sustecting an invalid way of finding methods. For instance, in your case, the compiler tries to find a method with name "Run" in list of unindexed extensions methods (likewise this generic method). Therefore, the method is used for AsyncSeqBuilder as that method exists.

    To clarify, another example:

    open System.Runtime.CompilerServices
    open System
    
    // -------------------------
    // "View engine" bits
    // -------------------------
    type HtmlContainer() = class end
    
    type HtmlContainerFun = HtmlContainer -> unit
    
    [<Extension>]
    type HtmlContainerExtensions =
        [<Extension>]
        static member inline Dispose(this: #HtmlContainer, [<InlineIfLambda>] runExpr: HtmlContainerFun) =
            runExpr this
            this
    
    // -------------------------
    // "AsyncSeq-like" CE builder bits
    // -------------------------
    type AsyncSeq<'T> =
        private AsyncSeq of 'T list 
            interface IDisposable with 
                member _.Dispose() = ()
        
    let foo() =
        use a = AsyncSeq([1])
        1

    This code also doesn't compile (error FS0073: internal error: Couldn't find Dispose on IDisposable, or it was overloaded), because "Dispose" extension method is taken into account.

  4. Lanayx commented on Feb 25, 2026

    @Lanayx
    ContributorAuthor

    I'm sustecting an invalid way of finding methods. For instance, in your case, the compiler tries to find a method with name "Run" in list of unindexed extensions methods (likewise this generic method). Therefore, the method is used for AsyncSeqBuilder as that method exists.

    I'm not sure I understand the reason. I don't see any problem that compiler tries to find a method with name "Run" and finds it. In fact I want it to be found for the types that match generic restriction. The problem is that it decides to choose it even when types don't match.

  5. evgTSV commented on Feb 25, 2026

    @evgTSV
    Contributor

    Here's a code that tries to find a method called "Run" in a group that includes generic extension methods. Since there is no further checking, that type match generic restriction, a generic method is included into range of available methods. Identically, when the compiler gets method to find "Dispose", it's also getting generic method, dispite they aren't match generic restriction.

    let runExpr =
    match tryFindBuilderMethod ceenv mBuilderVal "Run" with
    | [] -> quotedSynExpr
    | _ -> mkSynCall "Run" mDelayOrQuoteOrRun [ quotedSynExpr ] builderValName

    Here's a function that gets extension methods. As you can see, there is a param nenv.eUnindexedExtensionMembers. So, it finds methods among unindexed extension members that include generic extension methods.

    let ExtensionMethInfosOfTypeInScope (collectionSettings: ResultCollectionSettings) (infoReader: InfoReader) (nenv: NameResolutionEnv) optFilter isInstanceFilter m ty =
    let extMemsDangling = SelectMethInfosFromExtMembers infoReader optFilter ty m nenv.eUnindexedExtensionMembers
    if collectionSettings = ResultCollectionSettings.AtMostOneResult && not (isNil extMemsDangling) then
    extMemsDangling

  6. added
    Area-ComputationExpressions
    Area-Compiler-Checking-Overloads
    and removed on Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-Checking-OverloadsArea-ComputationExpressionsBug

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions