Skip to content

Fix internal error FS0073 w/nested inline SRTP and multiple overloads#19710

Open
gusty wants to merge 2 commits into
dotnet:mainfrom
gusty:fix/srtp-unsolved-typar-codegen
Open

Fix internal error FS0073 w/nested inline SRTP and multiple overloads#19710
gusty wants to merge 2 commits into
dotnet:mainfrom
gusty:fix/srtp-unsolved-typar-codegen

Conversation

@gusty
Copy link
Copy Markdown
Contributor

@gusty gusty commented May 9, 2026

Root cause: In CodegenWitnessExprForTraitConstraint, when SolveMemberConstraint resolves a trait call, FreshenMethInfo creates fresh type parameters for the method. During overload resolution, the fresh typar for a wildcard _ position gets unified with the trait call's anonymous typar, but neither gets solved to a concrete type. The solution's minst then carries these unsolved typars through GenWitnessExprBuildFSharpMethodCall → IlxGen, where they crash because the typar isn't in the TypeReprEnv.

Fix: After SolveMemberConstraint succeeds, identify any unsolved typars in the solution's minst that were freshly introduced during resolution (not present in the original trait constraint's types) and default them using ChooseTyparSolution. This ensures all type arguments in the generated expression are concrete before they reach IL codegen, without affecting typars from the calling context.

Fixes #19709

Checklist

  • Test cases added
  • Performance benchmarks added in case of performance changes
  • Release notes entry updated

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 9, 2026

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/11.0.100.md

@gusty gusty force-pushed the fix/srtp-unsolved-typar-codegen branch 3 times, most recently from f2c9225 to 6bb273f Compare May 10, 2026 20:51
Copy link
Copy Markdown
Member

@T-Gro T-Gro left a comment

Choose a reason for hiding this comment

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

Review Summary

The fix is sound — using ChooseTyparSolution to default unsolved typars in GenTypeAux is consistent with how the compiler handles this in other late-stage passes (optimizer for Expr.TyChoose, pattern match compilation, quotation translation). The regression test is comprehensive and correctly uses compileExeAndRun since this is a codegen bug.

Comment accuracy (minor)

IlxGen.fs line 718: The comment says "can arise in fsi for inline SRTP functions" but the bug also reproduces with fsc (compiled code), not just FSI. Suggest:
// Unsolved type variable not in the TypeReprEnv — can arise for inline SRTP // functions where constraint resolution leaves phantom typars unsolved.

Potential spurious warning 3559

ChooseTyparSolutionChooseTyparSolutionAndRange emits typrelNeverRefinedAwayFromTop (informational warning 3559) for unconstrained TyparKind.Type typars when DiagnosticForObjInference is enabled. Since the phantom typars being defaulted here are internal artifacts from FreshenMethInfo, not user-visible type variables, this would produce a confusing warning pointing at the method definition site. This is low priority since 3559 is opt-in, but worth noting — a future hardening could call ChooseTyparSolutionAndRange directly and skip the warning path.

Unrelated commit

The branch includes commit de1f4006362 (Array.exists2 XML doc fix from PR #19672) which is not in main and unrelated to this PR. Consider rebasing it out before merge to keep the PR focused.

Overall

The approach is correct and well-tested. The ContainsKey guard prevents the ICE while producing valid IL (the phantom typars genuinely don't affect runtime behavior — they're in wildcard _ positions). Approve once the comment is updated and the unrelated commit is cleaned up.

@gusty gusty force-pushed the fix/srtp-unsolved-typar-codegen branch from a68a775 to 9ff6dc6 Compare May 12, 2026 08:50
@gusty
Copy link
Copy Markdown
Contributor Author

gusty commented May 12, 2026

@T-Gro comment is fixed and PR is rebased, this is ready to squash-merge to main.

@T-Gro T-Gro added the AI-reviewed PR reviewed by AI review council label May 12, 2026
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: New

Development

Successfully merging this pull request may close these issues.

Internal error FS0073 "Undefined or unsolved type variable" with nested inline SRTP and multiple overloads

2 participants