Add suggestions for variables with the same name in imported modules when unknown variable #4810
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes #2697
As stated on this Discord message, this is my first contributions to OSS and this project. I would really appreciate any feedback and improvement on my code, this pull request or commits that I made !
Summary
This PR add more suggestions when an unknown variable is encounter by listing all variables / identifier in imported modules that match the same name. Also, when the variable / identifier is called such as functions, pipeline steps or type constructors, the suggestions are based on the name and number of arguments provided to the call. (ref: #2697 (comment))
Examples
For example, take this invalid program:
The error message reported by the compiler:
Also, it works with variables, type constructor, constant etc. Consider this custom module and this program:
With these changes we have new suggestions about variables / identifier in imported modules:
Implementations
The current implementation uses a new enumeration
VarUsage
. This is similar to the enumFieldAccessUsage
and it helps the functionreport_name_error
by giving a little more context about the usage of this variable: a function call or something else. This function can now report about imported modules containing a variable with the same name as the unknown variable and, in case of function call, the same arity.I tried to experiment another implementation where the function
report_name_error
was not provided with more context. The function was keeped as simple as possible by reporting imported modules containing a variable with the same name, without checking if it was a call or something else. Instead, we were filtering at the call side, insideinfer_call
function for example, if the variable from the imported module was of type "function-like".Honestly it does not feel right. We needed to check if an
UnknownVariable
error was emitted, apply a filtering on imported modules that were reported and re-emit the same error with the filter applied. Also, we had to perform this manipulation at every location where a function call was possible, which likely implies code duplication, errors and omissions.Shortcomings
Wording
First thing first, the wording. I am not sure about the error message wording:
Consider using one of these variables
For many developers,
io.println
is a function anduri.empty
is a constant, not really variables. I wanted to make the error message like:Consider using on of these identifiers
But the term
identifiers
is not used in the compiler, in error messages that I found or the language tour so it does not feel right. As I can see in the compiler,Var
refer to what I callidentifier
so I thought it would be best to writevariables
.No type checking on arguments
Because we only check name and arity in the case of calls, the suggestions may be wrong because the type of the function parameters does not match the type of arguments provided with the call. If we want that, we need to give more context, such as arguments types, to the
report_name_error
function in order to find the right functions to suggest.Suggestions for pipeline calls like
1 |> add(2)
Also, I had a problem with function call on pipeline. To better explain the situation, take this program:
It can be desugared in two ways:
I didn't know how to implement this case. For sure, the compiler can report function with arity of 2 and a function with arity 1 that has a return type that is also a function with arity 1. I just didn't know what was the right choice ...
Tests
To conclude, theses commits does not add tests for now, it might be cool to have some !
Ending
Thank you in advance for reading and helping me on my journey into OSS. Please feel free to give me feedback on all this work! 💛