@@ -13351,40 +13351,34 @@ and TcIncrementalLetRecGeneralization cenv scopem
1335113351
1335213352 // realsig+ fix: register each closure's owner typars so TLR (InnerLambdasToTopLevelFuncs
1335313353 // FlatEnvPacks.packEnv + createFHat) can subtract them from reqdTypars when re-hosting
13354- // the closure as a nested method.
13354+ // the closure as a nested method of a class .
1335513355 //
13356- // Case 1: closure is inside a class-family (envNonRec.eFamilyType is Some) — the
13357- // enclosing family's declared typars are the owner typars (the current behaviour).
13356+ // ONLY register when there is a genuine class owner (envNonRec.eFamilyType.IsSome). The
13357+ // re-hosted closure then lives as a nested method of that class, so the class supplies
13358+ // those typars via its own scope and the closure must not re-declare them.
1335813359 //
13359- // Case 2: closure is in module scope (envNonRec.eFamilyType is None) — the owner typars
13360- // are the typars free in the closure's generalized type but not declared by the closure
13361- // itself. These are the surrounding function's declared typars (e.g. 'T of outer<'T>) that
13362- // the closure captures from the environment via mutual- or self-recursion. Binding-declared
13363- // typars are subtracted because they are already bound by the closure's own signature and
13364- // must not be re-required.
13360+ // Do NOT register for module-scope closures (envNonRec.eFamilyType = None, e.g. a
13361+ // closure inside a generic top-level function such as binaryChop<'T> capturing 'T). The
13362+ // re-hosted version (go@N / loop@N) is a STANDALONE generic method whose OWN signature
13363+ // declares the captured typar at index 0..k-1; treating it as an owner typar reserves a
13364+ // leading ambient slot in the body's type-env and shifts every own typar index up by
13365+ // |ownTypars|, so the body emits !!1 / !!2.. where the method only declared !!0 / !!1,
13366+ // an out-of-range token -> PEVerify fails. There is no class to supply the typar at the
13367+ // call site, so it must remain the method's own declared typar.
1336513368 if g.realsig then
13366- let familyTypars =
13367- match envNonRec.eFamilyType with
13368- | Some tcref -> tcref.Typars
13369- | None ->
13370- let declaredTypars =
13371- newGeneralizableBindings
13372- |> List.collect (fun (b: PreGeneralizationRecursiveBinding) -> b.RecBindingInfo.DeclaredTypars)
13373- let allFree =
13374- newGeneralizableBindings
13375- |> List.collect (fun (b: PreGeneralizationRecursiveBinding) ->
13376- (freeInType CollectAllNoCaching b.RecBindingInfo.Val.TauType).FreeTypars
13377- |> Zset.elements)
13378- allFree
13379- |> List.filter (fun (t: Typar) ->
13380- not (List.exists (fun (d: Typar) -> d.Name = t.Name && d.Range = t.Range) declaredTypars))
13381- |> List.distinctBy (fun (t: Typar) -> (t.Name, t.Range))
13382- if not (List.isEmpty familyTypars) then
13383- let hostTycon = envNonRec.eFamilyType
13384- List.iter (fun (pgrbind: PreGeneralizationRecursiveBinding) ->
13385- let rbinfo = pgrbind.RecBindingInfo
13386- if rbinfo.DeclKind = DeclKind.ExpressionBinding then
13387- g.AddRealsigClosureParent(rbinfo.Val, familyTypars, hostTycon)) newGeneralizableBindings
13369+ match envNonRec.eFamilyType with
13370+ | Some hostTycon ->
13371+ let familyTypars = hostTycon.Typars
13372+ if not (List.isEmpty familyTypars) then
13373+ List.iter (fun (pgrbind: PreGeneralizationRecursiveBinding) ->
13374+ let rbinfo = pgrbind.RecBindingInfo
13375+ if rbinfo.DeclKind = DeclKind.ExpressionBinding then
13376+ g.AddRealsigClosureParent(rbinfo.Val, familyTypars, Some hostTycon)) newGeneralizableBindings
13377+ | None ->
13378+ // Module scope: no class owner. The captured typar, if any, is the closure's
13379+ // own generalized typar (declared on the re-hosted method's signature); leave
13380+ // it unregistered so the standalone method keeps it as its own declared typar.
13381+ ()
1338813382
1338913383 // Generalize the bindings.
1339013384 let newGeneralizedRecBinds = (generalizedTyparsL, newGeneralizableBindings) ||> List.map2 (TcLetrecGeneralizeBinding cenv denv )
0 commit comments