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: strengthen the judgment of generic type parameters replacement #13460

Merged
merged 1 commit into from Feb 14, 2022

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Feb 13, 2022

This PR strengthen the judgment of generic type parameters replacement and merge two tests into one.

struct Response<U> {
	result U
}

fn send_1<A, B>(res A, b B) string {
	msg := Response<U>{
		result: res
	}
	println(b)
	return '$msg'
}

fn send_2<A, B>(res A, b B) string {
	msg := Response<A, B>{
		result: res
	}
	println(b)
	return '$msg'
}

fn main() {
	mut ret := send_1(123, 22.2)
	println(ret)

	ret = send_2(123, 22.2)
	println(ret)
}

PS D:\Test\v\tt1> v run .
./tt1.v:6:9: error: generic struct init type parameter `U` must be within the parameters `(A,B)` of the current generic function
    4 |
    5 | fn send_1<A, B>(res A, b B) string {
    6 |     msg := Response<U>{
      |            ~~~~~~~~~~~~
    7 |         result: res
    8 |     }
./tt1.v:7:3: error: cannot assign to field `result`: expected `U`, not `int`
    5 | fn send_1<A, B>(res A, b B) string {
    6 |     msg := Response<U>{
    7 |         result: res
      |         ~~~~~~~~~~~
    8 |     }
    9 |     println(b)
./tt1.v:14:9: error: generic struct init expects 1 generic parameter, but got 2
   12 |
   13 | fn send_2<A, B>(res A, b B) string {
   14 |     msg := Response<A, B>{
      |            ~~~~~~~~~~~~~~~
   15 |         result: res
   16 |     }
./tt1.v:15:3: error: cannot assign to field `result`: expected `U`, not `int`
   13 | fn send_2<A, B>(res A, b B) string {
   14 |     msg := Response<A, B>{
   15 |         result: res
      |         ~~~~~~~~~~~
   16 |     }
   17 |     println(b)

@spytheman spytheman merged commit b2f9842 into vlang:master Feb 14, 2022
@yuyi98 yuyi98 deleted the fix_generic_struct_init branch February 14, 2022 05:29
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.

None yet

2 participants