Skip to content

Prettify type variables in missing constraint diagnostics - #20928

Open
ShryukGrandhi wants to merge 1 commit into
dotnet:mainfrom
ShryukGrandhi:fix/issue-6725-generics-error-message-typeid-shown-in-e
Open

ShryukGrandhi wants to merge 1 commit into
dotnet:mainfrom
ShryukGrandhi:fix/issue-6725-generics-error-message-typeid-shown-in-e

Conversation

@ShryukGrandhi

Copy link
Copy Markdown

Description

When a generic member needs a constraint it doesn't declare, FS0043 printed the constraint as-is. Unsolved inference variables then showed up as internal placeholders. For

type C() =
    static member M<'a,'b>(a: 'a, b: 'b) = a + b

the compiler reported

A type parameter is missing a constraint 'when (^a or ^b) : (static member (+) : ^a * ^b -> '``?``)'

In some of the duplicate diagnostics for the same expression, the support types were also printed as ^``?```. Older compilers showed the stamp instead (^?8018`).

richTextOfTyparConstraint, used only for this diagnostic, now goes through a new PrintTypes.prettyLayoutOfTyparConstraint. It attaches the constraint to a copy of the typar and runs PrettyTypes.PrettifyType, the same approach prettyLayoutOfTrait already uses. If a member constraint's support doesn't include the typar, the typar isn't printed, so the copy gets a placeholder name. That way it can't take a name from the support types and turn ^a into ^a0. The message now reads

A type parameter is missing a constraint 'when (^a or ^b) : (static member (+) : ^a * ^b -> 'c)'

Fixes #6725

Checklist

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

Testing

  • Added Issue 6725 - missing constraint message does not show unnamed inference variables to ErrorMessages/DiagnosticRegressionTests.fs. It fails on main (the messages contain '``?``` and ^``?```) and passes with this change.
  • Ran the ComponentTests classes that check "missing a constraint" text or SRTP/constraint printing: DiagnosticRegressionTests, MigratedTypeCheckTests (neg06/neg10/neg45/neg112 baselines), RecordSpreadsTests, TypeAbbreviations, CheckingOfImplementationFiles, TypeInference, TypeConstraints, IWSAM/SRTP. 486 passed. The 34 failures are all IL-baseline tests that couldn't start the x64-only ildasm package on my arm64 Mac (Bad CPU type in executable), so they're unrelated to this change.

FS0043 printed the missing constraint without prettifying it, so unsolved
inference variables showed up as '?' placeholders, e.g.
'when (^a or ^b) : (static member (+) : ^a * ^b -> '``?``)'.

Fixes dotnet#6725

Signed-off-by: ShryukGrandhi <85529990+ShryukGrandhi@users.noreply.github.com>
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

✅ Release notes checked


✅ Found changes and release notes in following paths:

Warning

No PR link found in some release notes, please consider adding it.

Change path Release notes path Description
`src/Compiler` docs/release-notes/.FSharp.Compiler.Service/11.0.200.md No current pull request URL (#20928) found, please consider adding it

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

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

[generics/error message] ?typeid shown in error message for missing type constraint

1 participant