mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
parent
83338e044a
commit
721dbec2e4
@ -87,7 +87,7 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
|||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
c.expected_type = field.typ
|
c.expected_type = field.typ
|
||||||
default_expr_type := c.expr(field.default_expr)
|
default_expr_type := c.expr(field.default_expr)
|
||||||
if !field.typ.has_flag(.optional) {
|
if !field.typ.has_flag(.optional) && !field.typ.has_flag(.result) {
|
||||||
c.check_expr_opt_call(field.default_expr, default_expr_type)
|
c.check_expr_opt_call(field.default_expr, default_expr_type)
|
||||||
}
|
}
|
||||||
struct_sym.info.fields[i].default_expr_typ = default_expr_type
|
struct_sym.info.fields[i].default_expr_typ = default_expr_type
|
||||||
@ -420,7 +420,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||||||
if expr_type == ast.void_type {
|
if expr_type == ast.void_type {
|
||||||
c.error('`$field.expr` (no value) used as value', field.pos)
|
c.error('`$field.expr` (no value) used as value', field.pos)
|
||||||
}
|
}
|
||||||
if !field_info.typ.has_flag(.optional) {
|
if !field_info.typ.has_flag(.optional) && !field.typ.has_flag(.result) {
|
||||||
expr_type = c.check_expr_opt_call(field.expr, expr_type)
|
expr_type = c.check_expr_opt_call(field.expr, expr_type)
|
||||||
}
|
}
|
||||||
expr_type_sym := c.table.sym(expr_type)
|
expr_type_sym := c.table.sym(expr_type)
|
||||||
@ -465,6 +465,10 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||||||
c.error('field `$field_info.name` is optional, but initialization of optional fields currently unsupported',
|
c.error('field `$field_info.name` is optional, but initialization of optional fields currently unsupported',
|
||||||
field.pos)
|
field.pos)
|
||||||
}
|
}
|
||||||
|
if field_info.typ.has_flag(.result) {
|
||||||
|
c.error('field `$field_info.name` is result, but initialization of result fields currently unsupported',
|
||||||
|
field.pos)
|
||||||
|
}
|
||||||
if expr_type.is_ptr() && expected_type.is_ptr() {
|
if expr_type.is_ptr() && expected_type.is_ptr() {
|
||||||
if mut field.expr is ast.Ident {
|
if mut field.expr is ast.Ident {
|
||||||
if mut field.expr.obj is ast.Var {
|
if mut field.expr.obj is ast.Var {
|
||||||
|
14
vlib/v/checker/tests/struct_field_optional_init_err.out
Normal file
14
vlib/v/checker/tests/struct_field_optional_init_err.out
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
vlib/v/checker/tests/struct_field_optional_init_err.vv:8:3: error: field `bar` is result, but initialization of result fields currently unsupported
|
||||||
|
6 | fn main() {
|
||||||
|
7 | _ := Foo{
|
||||||
|
8 | bar: 1
|
||||||
|
| ~~~~~~
|
||||||
|
9 | baz: 1
|
||||||
|
10 | }
|
||||||
|
vlib/v/checker/tests/struct_field_optional_init_err.vv:9:3: error: field `baz` is optional, but initialization of optional fields currently unsupported
|
||||||
|
7 | _ := Foo{
|
||||||
|
8 | bar: 1
|
||||||
|
9 | baz: 1
|
||||||
|
| ~~~~~~
|
||||||
|
10 | }
|
||||||
|
11 | }
|
11
vlib/v/checker/tests/struct_field_optional_init_err.vv
Normal file
11
vlib/v/checker/tests/struct_field_optional_init_err.vv
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
struct Foo {
|
||||||
|
bar !int
|
||||||
|
baz ?int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
_ := Foo{
|
||||||
|
bar: 1
|
||||||
|
baz: 1
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user