roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.46k stars 313 forks source link

Fix dbg expression desugaring and improve dbg error reporting #7054

Closed mulias closed 2 months ago

mulias commented 2 months ago

This PR builds on https://github.com/roc-lang/roc/pull/7038, fixing some issues with the initial implementation. The PR following this one will add support for dbg in pipelines.

Use module scope instead of var store to generate idents in dbg desugar

Fix a bug in dbg expression desugaring by using the module scope to generate unique identifiers instead of the variable store.

In the initial implementation of dbg expressions we used the VarStore to generate unique identifiers for new variables created during desugaring. We should have instead used the current module's Scope, which handles identifiers within the module. Each scope has its own incrementing variable count which is independent of the shared variable store. The scope is used to generate new identifiers at other points in canonicalization, such as when assigning a global identifier to closures and expects. It's possible that the identifier generated for dbg could conflict with an identifier generated by the scope, resulting in a confusing error.

Do not display generated symbol names in error messages

When an error message reports on a symbol that was generated during canonicalization, use text like "This value" instead of "This 123 value". Generated symbols use the identifier index as the symbol name, since valid Roc variables cannot begin with a number so there's no chance of collision. We don't want to display generated symbols to the user, so when building the error message we check if the symbol's name starts with a digit.