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, orm: add error for unchecked option multi return types, fix undefined behavior #21106

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions vlib/orm/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,13 @@ pub fn orm_table_gen(table string, q string, defaults bool, def_unique_len int,
return error("references attribute needs to be in the format [references], [references: 'tablename'], or [references: 'tablename(field_id)']")
}
if attr.arg.contains('(') {
ref_table, ref_field := attr.arg.split_once('(')
if !ref_field.ends_with(')') {
return error("explicit references attribute should be written as [references: 'tablename(field_id)']")
if ref_table, ref_field := attr.arg.split_once('(') {
spytheman marked this conversation as resolved.
Show resolved Hide resolved
if !ref_field.ends_with(')') {
return error("explicit references attribute should be written as [references: 'tablename(field_id)']")
}
references_table = ref_table
references_field = ref_field[..ref_field.len - 1]
}
references_table = ref_table
references_field = ref_field[..ref_field.len - 1]
} else {
references_table = attr.arg
references_field = 'id'
Expand Down
21 changes: 12 additions & 9 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1161,17 +1161,20 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
}
}
if expr_ret_type.has_option_or_result() {
if expr.or_block.kind == .absent && expr_ret_type.has_flag(.result) {
if expr.or_block.kind == .absent {
ret_sym := c.table.sym(expr_ret_type)
ret_typ_tok := if expr_ret_type.has_flag(.option) { '?' } else { '!' }
if c.inside_defer {
c.error('${expr.name}() returns `${ret_typ_tok}${ret_sym.name}`, so it should have an `or {}` block at the end',
expr.pos)
} else {
c.error('${expr.name}() returns `${ret_typ_tok}${ret_sym.name}`, so it should have either an `or {}` block, or `${ret_typ_tok}` at the end',
expr.pos)
if expr_ret_type.has_flag(.result)
|| (expr_ret_type.has_flag(.option) && ret_sym.kind == .multi_return) {
ret_typ_tok := if expr_ret_type.has_flag(.option) { '?' } else { '!' }
if c.inside_defer {
c.error('${expr.name}() returns `${ret_typ_tok}${ret_sym.name}`, so it should have an `or {}` block at the end',
expr.pos)
} else {
c.error('${expr.name}() returns `${ret_typ_tok}${ret_sym.name}`, so it should have either an `or {}` block, or `${ret_typ_tok}` at the end',
expr.pos)
}
}
} else if expr.or_block.kind != .absent {
} else {
c.check_or_expr(expr.or_block, ret_type, expr_ret_type, expr)
}
return ret_type.clear_flag(.result)
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/option_multi_return_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/option_multi_return_err.vv:6:14: error: split() returns `?(string, string)`, so it should have either an `or {}` block, or `?` at the end
4 |
5 | fn main() {
6 | foo, bar := split('foo/bar')
| ~~~~~~~~~~~~~~~~
7 | println('${foo}.${bar}')
8 | }
8 changes: 8 additions & 0 deletions vlib/v/checker/tests/option_multi_return_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn split(str string) ?(string, string) {
return none
}

fn main() {
foo, bar := split('foo/bar')
println('${foo}.${bar}')
}
Loading