qsharp-compiler icon indicating copy to clipboard operation
qsharp-compiler copied to clipboard

Improve error message for how to give type annotations

Open bettinaheim opened this issue 3 years ago • 2 comments

For an operation

    operation Foo<'T> () : 'T {
        return (new 'T[0])[0];
    }

calling it in another operation

    operation Bar() : Unit {
        let _ = Foo();
    }

results in an error "The type annotation 'a is ambiguous. More type annotations or usage context may be needed."

This error message is only actionable if it is clear to people where to put the type annotation and what the syntax it. I suggest making it more actionable. The error message used to have an example: "Please provide explicit type arguments, e.g. Op<Int, Double>(arg)."

bettinaheim avatar Jul 26 '21 13:07 bettinaheim

The problem is that different situations need different actions... sometimes adding type arguments to an identifier is the best option, but other times it is not ideal (when simply using a variable in context could cause the type to be inferred), or not possible. For example, when [] is ambiguous it needs to be replaced with EmptyArray before a type can be annotated. Having the error message tell you to write F<T> when you have [] would be confusing, I think.

bamarsha avatar Jul 28 '21 22:07 bamarsha

I see your point. I think what would make sense here is to add a page to the Q# language docs and give a link to that in the error message.

bettinaheim avatar Mar 30 '22 22:03 bettinaheim