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: fix checking match branch call expr twice (fix #20709) #20910

Merged
merged 1 commit into from
Feb 26, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Feb 26, 2024

This PR fix checking match branch call expr twice (fix #20709).

  • Fix checking match branch call expr twice.
  • Add test.
struct Foo {
mut:
	x int
}

struct Bar {
mut:
	y int
}

type Foobar = Foo | Bar

fn mutate_foobar1(mut foobar Foobar) {
	match foobar {
		Foo {
			mutate_foo(mut foobar)
		}
		Bar {
			mutate_bar(mut foobar)
		}
	}
}

fn mutate_foobar2(mut foobar Foobar) int {
	return match foobar {
		Foo {
			mutate_foo(mut foobar)
		}
		Bar {
			mutate_bar(mut foobar)
		}
	}
}

fn mutate_foo(mut foo Foo) int {
	foo.x = 5
	return 5
}

fn mutate_bar(mut bar Bar) int {
	bar.y = 10
	return 10
}

fn main() {
	mut bar := Bar{y: 0}
	mutate_foobar1(mut bar)
	mutate_foobar2(mut bar)
}

PS D:\Test\v\tt1> v run .
tt1.v:16:19: error: cannot use `&Foobar` as `&Foo` in argument 1 to `mutate_foo`
   14 |     match foobar {
   15 |         Foo {
   16 |             mutate_foo(mut foobar)
      |                            ~~~~~~
   17 |         }
   18 |         Bar {
tt1.v:19:19: error: cannot use `&Foobar` as `&Bar` in argument 1 to `mutate_bar`
   17 |         }
   18 |         Bar {
   19 |             mutate_bar(mut foobar)
      |                            ~~~~~~
   20 |         }
   21 |     }
tt1.v:27:19: error: cannot use `&Foobar` as `&Foo` in argument 1 to `mutate_foo`
   25 |     return match foobar {
   26 |         Foo {
   27 |             mutate_foo(mut foobar)
      |                            ~~~~~~
   28 |         }
   29 |         Bar {
tt1.v:30:19: error: cannot use `&Foobar` as `&Bar` in argument 1 to `mutate_bar`
   28 |         }
   29 |         Bar {
   30 |             mutate_bar(mut foobar)
      |                            ~~~~~~
   31 |         }
   32 |     }

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 71bd94a into vlang:master Feb 26, 2024
54 checks passed
@yuyi98 yuyi98 deleted the fix_check_fn_arg_twice branch February 27, 2024 00:57
raw-bin pushed a commit to raw-bin/v that referenced this pull request Mar 9, 2024
raw-bin pushed a commit to raw-bin/v that referenced this pull request Mar 9, 2024
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.

The error "cannot use &T as &Y in argument 1 to func" is printed twice
2 participants