Skip to content
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

checker: check fixed array builtin method args mismatch #22626

Merged
merged 1 commit into from
Oct 23, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Oct 23, 2024

This PR check fixed array builtin method args mismatch.

  • Check fixed array builtin method args mismatch.
  • Add test.
fn main() {
	arr := [1, 2, 3]!

	_ := arr.index()
	_ := arr.index('hello')
	_ := arr.contains()
	_ := arr.contains('hello')
	_ := arr.any()
	_ := arr.any(22)
	_ := arr.all()
	_ := arr.all('hello')
}

PS D:\Test\v\tt1> v run .      
tt1.v:4:11: error: `.index()` expected 1 argument, but got 0
    2 |     arr := [1, 2, 3]!
    3 | 
    4 |     _ := arr.index()
      |              ~~~~~~~
    5 |     _ := arr.index('hello')
    6 |     _ := arr.contains()
tt1.v:4:4: error: assignment mismatch: 1 variable but `index()` returns 0 values
    2 |     arr := [1, 2, 3]!
    3 | 
    4 |     _ := arr.index()
      |       ~~
    5 |     _ := arr.index('hello')
    6 |     _ := arr.contains()
tt1.v:5:17: error: cannot use `string` as `int` in argument 1 to `.index()`
    3 | 
    4 |     _ := arr.index()
    5 |     _ := arr.index('hello')
      |                    ~~~~~~~
    6 |     _ := arr.contains()
    7 |     _ := arr.contains('hello')
tt1.v:5:4: error: assignment mismatch: 1 variable but `index()` returns 0 values
    3 | 
    4 |     _ := arr.index()
    5 |     _ := arr.index('hello')
      |       ~~
    6 |     _ := arr.contains()
    7 |     _ := arr.contains('hello')
tt1.v:6:11: error: `.contains()` expected 1 argument, but got 0
    4 |     _ := arr.index()
    5 |     _ := arr.index('hello')
    6 |     _ := arr.contains()
      |              ~~~~~~~~~~
    7 |     _ := arr.contains('hello')
    8 |     _ := arr.any()
tt1.v:6:4: error: assignment mismatch: 1 variable but `contains()` returns 0 values
    4 |     _ := arr.index()
    5 |     _ := arr.index('hello')
    6 |     _ := arr.contains()
      |       ~~
    7 |     _ := arr.contains('hello')
    8 |     _ := arr.any()
tt1.v:7:20: error: cannot use `string` as `int` in argument 1 to `.contains()`
    5 |     _ := arr.index('hello')
    6 |     _ := arr.contains()
    7 |     _ := arr.contains('hello')
      |                       ~~~~~~~
    8 |     _ := arr.any()
    9 |     _ := arr.any(22)
tt1.v:7:4: error: assignment mismatch: 1 variable but `contains()` returns 0 values
    5 |     _ := arr.index('hello')
    6 |     _ := arr.contains()
    7 |     _ := arr.contains('hello')
      |       ~~
    8 |     _ := arr.any()
    9 |     _ := arr.any(22)
tt1.v:8:11: error: `.any` expected 1 argument, but got 0
    6 |     _ := arr.contains()
    7 |     _ := arr.contains('hello')
    8 |     _ := arr.any()
      |              ~~~~~
    9 |     _ := arr.any(22)
   10 |     _ := arr.all()
tt1.v:8:4: error: assignment mismatch: 1 variable but `any()` returns 0 values
    6 |     _ := arr.contains()
    7 |     _ := arr.contains('hello')
    8 |     _ := arr.any()
      |       ~~
    9 |     _ := arr.any(22)
   10 |     _ := arr.all()
tt1.v:10:11: error: `.all` expected 1 argument, but got 0
    8 |     _ := arr.any()
    9 |     _ := arr.any(22)
   10 |     _ := arr.all()
      |              ~~~~~
   11 |     _ := arr.all('hello')
   12 | }
tt1.v:10:4: error: assignment mismatch: 1 variable but `all()` returns 0 values
    8 |     _ := arr.any()
    9 |     _ := arr.any(22)
   10 |     _ := arr.all()
      |       ~~
   11 |     _ := arr.all('hello')
   12 | }
tt1.v:11:15: error: type mismatch, should use e.g. `all(it > 2)`
    9 |     _ := arr.any(22)
   10 |     _ := arr.all()
   11 |     _ := arr.all('hello')
      |                  ~~~~~~~
   12 | }

Huly®: V_0.6-21075

@spytheman
Copy link
Member

error: assignment mismatch: 1 variable but index() returns 0 values

hm, index() should return 1 value, not 0

@spytheman
Copy link
Member

spytheman commented Oct 23, 2024

assignment mismatch: 1 variable but contains() returns 0 values

same - contains returns 1 boolean value

@spytheman
Copy link
Member

spytheman commented Oct 23, 2024

any and all also return 1 boolean value, not 0

@yuyi98
Copy link
Member Author

yuyi98 commented Oct 23, 2024

This means the function returned a void_type value.
Just like:

vlib/v/checker/tests/filter_on_non_arr_err.vv:2:14: error: unknown method or field: `string.filter`
    1 | fn main() {
    2 |     _ := 'test'.filter(it == `t`)
      |                 ~~~~~~~~~~~~~~~~~
    3 | }
vlib/v/checker/tests/filter_on_non_arr_err.vv:2:4: error: assignment mismatch: 1 variable but `filter()` returns 0 values
    1 | fn main() {
    2 |     _ := 'test'.filter(it == `t`)
      |       ~~
    3 | }

@spytheman
Copy link
Member

spytheman commented Oct 23, 2024

I understand that, but the difference, is that in the case of strings, there is no filter() method.

In the case of arrays, the methods do exist, and the compiler knows what they should return, and it can provide a more precise error message, based on that information.

It can also suppress the second message, since it is misleading, not just wrong.

@spytheman
Copy link
Member

Anyway, it is not directly related to the arity checks, that you implemented, and is out of scope of this PR.

Good work.

@yuyi98
Copy link
Member Author

yuyi98 commented Oct 23, 2024

Ok, I see.

@spytheman spytheman merged commit edbafcb into vlang:master Oct 23, 2024
78 checks passed
@yuyi98 yuyi98 deleted the check_fixed_array_method_args branch October 23, 2024 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants