-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shadowing function declarations with variables shouldn't be allowed #22685
Comments
I don't know what the best solution is, but the less ambiguity allowed, the better. |
V has a check specifically for That's what's going on here. |
There is already a handle for this, if it is used only as a variable and does not report an error, it will report an error if it is also called as a function. fn foo1(foo1 int) {
foo1(foo1 + 1)
}
fn foo2() {
foo2 := 1
foo2(foo2)
}
fn main() {
foo1(5)
foo2()
}
PS D:\Test\v\tt1> v run .
tt1.v:2:2: error: ambiguous call to: `foo1`, may refer to fn `foo1` or variable `foo1`
1 | fn foo1(foo1 int) {
2 | foo1(foo1 + 1)
| ~~~~~~~~~~~~~~
3 | }
4 |
tt1.v:7:2: error: ambiguous call to: `foo2`, may refer to fn `foo2` or variable `foo2`
5 | fn foo2() {
6 | foo2 := 1
7 | foo2(foo2)
| ~~~~~~~~~~
8 | }
9 |
tt1.v:7:7: error: expected 0 arguments, but got 1
5 | fn foo2() {
6 | foo2 := 1
7 | foo2(foo2)
| ~~~~
8 | }
9 |
Details: have (int)
want () |
The error message could be improved... it isn't that it is ambiguous, it is that it simply isn't allowed. Perhaps something like tt1.v:2:2: error: no name shadowing is allowed: either fn `foo1` or variable `foo1` must be renamed the error message could also come out for the fn foo1(foo1 int) { line, since that is where it first occurs. |
I think the compiler shouldn't allow shadowing the function at all, like here: const x = 12345
fn main() {
x := 5
println(x)
} produces an error like this:
|
This situation is very easy to encounter, and using only variables in functions without using function calls is not a problem. |
I think it is a problem from a readability standpoint, take this code for example: fn x() {}
fn main() {
x := 5
println(x)
} It is not immediately obvious if this code will print |
Variable shadowing is explicitly not allowed. This case (below) is different. The function "x()" is outside the scope of function "main", thus arguably should be legal (as is).
If the user places the function with the same name of a variable in the same scope, then there will be an error. However, I do agree with JalonSolov, that the error should be more clear. Possibly, "No name shadowing within the same function or within the same parent scope."
|
What about this then? const x = 12345
fn main() {
x := 5
println(x)
} This errors out even though |
Functions have their own separate scopes. There isn't a visible function of global scope that all other functions are within the scope of. The conflict with functions, would be them having the same name, in the same module. In both cases, where you are correctly getting the error, you are placing something of the same name inside the scope of a function that is already using that name for a variable. |
V doctor:
What did you do?
./v -g -o vdbg cmd/v && ./vdbg test.v
What did you expect to see?
An error message about
x
being redefinedWhat did you see instead?
Huly®: V_0.6-21134
The text was updated successfully, but these errors were encountered: