fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

A wrong type is reported in a type mismatch error

Open auduchinok opened this issue 6 months ago • 3 comments

Consider this code:

let a, b: int = ()

The error is produced on the unit expression, but it says that the type is int: Image

Error FS0001 : This expression was expected to have type
    ''a * 'b'    
but here has type
    'int'

This may be happening due to some expressions rewriting during the analysis.

auduchinok avatar Aug 14 '25 13:08 auduchinok

Right, if I remember correctly, the compiler basically uses the type annotation on a binding to wrap the expression being bound in a SynExpr.Typed.

AST

expr =
    SynExpr.Typed(
        expr = SynExpr.Const(constant = SynConst.Unit, range = R("(1,16--1,18)")),
        targetType = SynType.LongIdent(SynLongIdent.SynLongIdent(id = [ Ident("int", R("(1,10--1,13)")) ], dotRanges = [], trivia = [ None ])),
        range = R("(1,16--1,18)")
    ),

It gives the exact same error for this:

let x, y = () : int

But the SynBindingReturnInfo is still available in the AST after that transformation, so we could use that to improve the error message here.

brianrourkeboll avatar Aug 14 '25 17:08 brianrourkeboll

Is it alright if I try my hand at this?

travv0 avatar Oct 25 '25 15:10 travv0

Is it alright if I try my hand at this?

Absolutely, would be great!

vzarytovskii avatar Oct 25 '25 19:10 vzarytovskii