Visitar URL original
Fix intermittent FS0073 under parallel optimization: leave type definitions untouched in TLR pass 5 by majocha · Pull Request #20879 · dotnet/fsharp · GitHub
Skip to content

Fix intermittent FS0073 under parallel optimization: leave type definitions untouched in TLR pass 5 - #20879

Open
majocha wants to merge 3 commits into
dotnet:mainfrom
majocha:claude/fsharp-parallel-compilation-f0812e
Open

majocha wants to merge 3 commits into
dotnet:mainfrom
majocha:claude/fsharp-parallel-compilation-f0812e

Conversation

@majocha

@majocha majocha commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #20812.

Cause

Lambda lifting ends with pass 5, RecreateUniqueBounds = copyImplFile g OnlyCloneExprVals. In that mode copyTycon returns each tycon itself, yet copyAndRemapAndBindTyconsAndVals still rebound the tycon's typars and rewrote it in place: entity_typars first, then entity_tycon_repr, the augmentation, attributes, abbreviation and exception info in separate writes.

Under parallel optimization, node (file i, phase p) waits only for (i, p-1) and (i-1, p), so file i's TLR runs while file i+1's first optimization loop reads file i's types. A reader between the two writes sees SeqOps<'C#new> whose field still has type seq<int> -> 'C#old. tyOfExpr of ops.OfSeq cannot substitute 'C, the declaration's typar ends up in an Expr.App type of the member body, and IlxGen does not find it in the method's TypeReprEnv:

Under OnlyCloneExprVals the rewrite has no purpose, since tycons and member/module vals are not copied. Its only effect was to swap the declaration's typars for copies on an object every later file references.

Change

Under OnlyCloneExprVals the tycons are left untouched; only the module type is re-pointed at copied vals, as before. The other copy modes are unchanged.

As a side effect, FSharpCheckProjectResults.GetOptimizedAssemblyContents() with --optimize+ no longer rewrites the checked entities that FCS symbols hold.

Testing

  • New FCS test Issue 20812 - optimization leaves the type parameters of checked type definitions untouched (ExprTests): it runs single-threaded and needs no timing. It fails before the change and passes after.

  • A repro in the shape of the issue (a chain of 12 generic contract classes over a record of functions, --optimize+ --deterministic+), compiled in a loop on the same base commit:

    compiler parallel builds output differs from sequential
    main 80 10 (9 with FS3559)
    this PR 100 0

    Sequential output is unchanged.

  • With the SDK 10.0.403 compiler, isolating each parallel stage: graph checking alone failed 0 of 100 builds, parallel optimization alone failed 6 of 51.

  • After a clean Build.cmd -c Release: ComponentTests EmittedIL 1660/1660 (3 skipped), optimizer tests 36/36, DeterministicTests 26/26, ExprTests 114/114.

Not in this PR

This is one instance of later optimizer phases mutating typed-tree objects that other files' concurrent phases can read. Follow-ups worth considering:

🤖 Generated with Claude Code

… bounds

TLR pass 5 (RecreateUniqueBounds) runs copyImplFile with OnlyCloneExprVals.
In that mode copyTycon returns each tycon itself, yet
copyAndRemapAndBindTyconsAndVals still rebound its typars and rewrote
entity_typars, then entity_tycon_repr and the rest, in place.

Under parallel optimization, file i's TLR runs while file i+1's first
optimization loop reads file i's types. A reader between the two writes
saw new typars next to field types over the old ones, so tyOfExpr of a
field get could not substitute them and leaked the declaration's typar
into a member body. IlxGen then reported FS0073 "Undefined or unsolved
type variable", or, since the GenTypeAux fallback, silently emitted the
typar as obj.

Under OnlyCloneExprVals the tycons are no longer rewritten; only the
module type is re-pointed at copied vals.

Fixes dotnet#20812

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 🕵️ LGTM

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Nov 2, 2026
@T-Gro T-Gro added the AI-reviewed PR reviewed by AI review council label Nov 2, 2026
@T-Gro
T-Gro self-requested a review November 2, 2026 12:23

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-reviewed PR reviewed by AI review council

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

FS0073 internal error: Undefined or unsolved type variable: 'C, intermittent in Release

2 participants