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 array builtin method call immutable errors (fix #22850) #22853

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Nov 14, 2024

This PR check array builtin method call immutable errors (fix #22850).

  • Check array builtin method call immutable errors.
  • Add tests.
fn main() {
	a0 := [1, 2]
	a1 := [3, 4]
	a2 := [5, 6]
	a3 := ['aa', 'bb']

	a2.prepend(a1)
	a1.insert(0, a0)
	a3.sort_with_compare(fn (a &string, b &string) int {
		if a < b {
			return -1
		}
		if a > b {
			return 1
		}
		return 0
	})
	a3.delete(0)
	a2.pop()
	a3.sort()

	b0 := ['aa', 'bb']!
	b0.sort_with_compare(fn (a &string, b &string) int {
		if a < b {
			return -1
		}
		if a > b {
			return 1
		}
		return 0
	})
	b0.sort()
}

PS D:\Test\v\tt1> v run .    
tt1.v:7:2: error: `a2` is immutable, declare it with `mut` to make it mutable
    5 |     a3 := ['aa', 'bb']
    6 | 
    7 |     a2.prepend(a1)
      |     ~~
    8 |     a1.insert(0, a0)
    9 |     a3.sort_with_compare(fn (a &string, b &string) int {
tt1.v:8:2: error: `a1` is immutable, declare it with `mut` to make it mutable
    6 | 
    7 |     a2.prepend(a1)
    8 |     a1.insert(0, a0)
      |     ~~
    9 |     a3.sort_with_compare(fn (a &string, b &string) int {
   10 |         if a < b {
tt1.v:9:2: error: `a3` is immutable, declare it with `mut` to make it mutable
    7 |     a2.prepend(a1)
    8 |     a1.insert(0, a0)
    9 |     a3.sort_with_compare(fn (a &string, b &string) int {
      |     ~~
   10 |         if a < b {
   11 |             return -1
tt1.v:18:2: error: `a3` is immutable, declare it with `mut` to make it mutable
   16 |         return 0
   17 |     })
   18 |     a3.delete(0)
      |     ~~
   19 |     a2.pop()
   20 |     a3.sort()
tt1.v:19:2: error: `a2` is immutable, declare it with `mut` to make it mutable
   17 |     })
   18 |     a3.delete(0)
   19 |     a2.pop()
      |     ~~
   20 |     a3.sort()
   21 |
tt1.v:20:2: error: `a3` is immutable, declare it with `mut` to make it mutable
   18 |     a3.delete(0)
   19 |     a2.pop()
   20 |     a3.sort()
      |     ~~
   21 |
   22 |     b0 := ['aa', 'bb']!
tt1.v:23:2: error: `b0` is immutable, declare it with `mut` to make it mutable
   21 |
   22 |     b0 := ['aa', 'bb']!
   23 |     b0.sort_with_compare(fn (a &string, b &string) int {
      |     ~~
   24 |         if a < b {
   25 |             return -1
tt1.v:32:2: error: `b0` is immutable, declare it with `mut` to make it mutable
   30 |         return 0
   31 |     })
   32 |     b0.sort()
      |     ~~
   33 | }

Huly®: V_0.6-21298

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 409f6fb into vlang:master Nov 14, 2024
71 checks passed
@yuyi98 yuyi98 deleted the check_array_method_call branch November 14, 2024 12:05
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.

Mutating array methods like insert and prepend work without mut declaration
2 participants