kaleidawave / ezno

A JavaScript compiler and TypeScript checker written in Rust with a focus on static analysis and runtime performance
https://kaleidawave.github.io/posts/introducing-ezno/
MIT License
2.3k stars 42 forks source link

Print reasons for subtyping mismatches #167

Open kaleidawave opened 2 weeks ago

kaleidawave commented 2 weeks ago

When doing subtyping (initial declaration, assigning, passing argument as parameter, return type etc) the returned result is a SubTypeResult. The subtyping logic (in types/subtyping.rs) currently collects some of the reasons for mismatches when doing the checking and stores this as a NonEqualityReason.

However this reason is thrown away in many places and does not make it to diagnostics. This can be shown by running rg "SubTypeResult::IsNotSubType\(_".

It would be better to pass this via a field to many of the diagnostics. For example here

https://github.com/kaleidawave/ezno/blob/5191329edd448c364914917297bef2846527b91b/checker/src/context/mod.rs#L1115-L1120

There should be something like mismatch: MismatchExplanation. This can be turned into a user readable explanation of why the type error happened. This can be surfaced via adding labels in the Diagnostic::PositionWithAdditionalLabels.

The TypeStringRepresentation enum was designed to cover this, but I don't think it quite fits into this structure.