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 option mismatch checking on array initializations(fix #20409) #20415

Merged
merged 1 commit into from
Jan 6, 2024

Conversation

shove70
Copy link
Contributor

@shove70 shove70 commented Jan 6, 2024

  1. Fixed C error when using an ?string member of a struct as a parameter to pg exec function #20409
  2. Add tests.

When the array is initialized, V takes the type of the first element as the type of the array elements, so when the first type is not option, the following elements should not have option either.

import db.pg

@[table: 'demo']
struct Demo {
	id int @[primary; sql: serial]
	a string
	b ?string
}

fn run() ! {
	conn := '' // insert own connection string
	db := pg.connect_with_conninfo(conn)!
	sql db { create table Demo }!
	demo := Demo{0, 'a', none}
	_ := db.exec_param_many('INSERT INTO demo(a, b) VALUES($1, $2)', [demo.a, demo.b])!
	db.close()
}

run() or { panic(err.msg()) }

outputs:

a.v:15:81: error: cannot use `?string` as `string`, it must be unwrapped first
   13 |     sql db { create table Demo }!
   14 |     demo := Demo{0, 'a', none}
   15 |     _ := db.exec_param_many('INSERT INTO demo(a, b) VALUES($1, $2)', [demo.a, demo.b])!
      |                                                                                    ^
   16 |     db.close()
   17 | }

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 2d97f16 into vlang:master Jan 6, 2024
54 checks passed
@shove70 shove70 deleted the opt_field branch January 6, 2024 22:37
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.

C error when using an ?string member of a struct as a parameter to pg exec function
2 participants