Skip to content

Commit f0dd73a

Browse files
committed
Fix module scope closures
1 parent bfdae01 commit f0dd73a

2 files changed

Lines changed: 36 additions & 42 deletions

File tree

‎src/Compiler/Checking/Expressions/CheckExpressions.fs‎

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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 )

‎src/Compiler/CodeGen/IlxGen.fs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10401,17 +10401,17 @@ and GenMethodForBinding
1040110401
(match cenv.g.RealsigClosureParentTryGet(v) with
1040210402
| Some info -> info.ownerTypars
1040310403
| None ->
10404-
// Re-hosted TLR closures are compiled as top-level methods that are
10405-
// not member/module bindings. Only those inherit the host class's
10406-
// typars; ordinary module functions (e.g. MyCollections.IEnumerator.check)
10407-
// must not, or a non-generic module gets spurious generic args.
10408-
if
10409-
(v.IsCompiledAsTopLevel || v.IsCompilerGenerated)
10410-
&& not (v.IsMember || v.IsMemberOrModuleBinding)
10411-
then
10412-
Zset.elements (eenv.tyenv.AsUserProvidedTypars())
10413-
else
10414-
[])
10404+
// [realsig+] A TLR'd top-level method (IsCompiledAsTopLevel /
10405+
// IsCompilerGenerated, NOT a member or module binding) carries its
10406+
// own typars in methLambdaTypars. Returning eenv.tyenv's
10407+
// user-provided typars as ambientTypars here would duplicate them
10408+
// (ambient=own, methLambda=own), shifting the body's own typars by
10409+
// |own| and making body (il-index |own|..2|own|-1) disagree with the
10410+
// signature (il-index 0..|own|-1) -> out-of-range ldelem/callvirt
10411+
// element types for loop@1[+T1,T2] / go@10412[+T]. An *unregistered*
10412+
// top-level re-hosted closure's own typars are the full set and
10413+
// must not be re-added as ambient.
10414+
[])
1041510415

1041610416
| _ -> []
1041710417

0 commit comments

Comments
 (0)