nim-lang / RFCs

A repository for your Nim proposals.
136 stars 23 forks source link

Improving compiler's knowledge of "hidden" identifiers #337

Open dom96 opened 3 years ago

dom96 commented 3 years ago

Problem

Proposed solution

There are undoubtedly many other ways to solve this (suggestions welcome), but one that came to me yesterday seems like the least difficult to implement:

The compiler can then parse the code under any when branches with a userDefined call and have better knowledge of what procs are available with what -d flags. The docgen can make use of this information as well to list procs that are available under different -d flags. The compiler can use it for better error messages.

References

1 - I ran into a confusing error message, where compiling Nimble worked just fine, but compiling one of its specific modules failed because -d:ssl wasn't enabled (for Nimble it was enabled in its nim.cfg file which didn't apply to the specific module I was compiling). This lead to an "identifier not found: SslError" message which would have been much nicer as "SslError is not defined without -d:ssl" (or similar): https://irclogs.nim-lang.org/05-02-2021.html#22:41:06

Clyybber commented 3 years ago

See #269

mratsim commented 3 years ago

It's worse when when defined(foo): actually requires when defined(foo) or defined(nimdoc) or defined(nimsuggest): and maybe you also need when defined(nimcheck) or defined(drnim): nowadays.

It's not a hypothetical problem either, I have to deal with that in Arraymancer:

https://github.com/mratsim/Arraymancer/blob/71cf616/src/arraymancer/tensor.nim#L74-L80

Araq commented 3 years ago

When we have better nkError support the compiler could check all when branches, always, improving the tooling situation quite a bit...

treeform commented 3 years ago

This is even worse when you when type(x) == ... and it only checks branches for specific type only if you call the function. Otherwise a compiler error can live dormant in the code.

It would be nice if compiler could error check all when branches but then would it error out when win32 functions don't exist on linux? Is when checking even possible? Maybe platform branches are not done, while other do. Complex...