cgen: fix struct ref field with no ref structinit (#21932)

This commit is contained in:
Felipe Pena 2024-07-27 19:30:19 -03:00 committed by GitHub
parent 0bf8adb5e6
commit ac136c08e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 1 deletions

View File

@ -23,7 +23,7 @@ mut:
pub struct DownloaderParams { pub struct DownloaderParams {
FetchConfig FetchConfig
pub mut: pub mut:
downloader &Downloader = TerminalStreamingDownloader{} downloader &Downloader = &TerminalStreamingDownloader{}
} }
// download_file_with_progress will save the URL `url` to the filepath `path` . // download_file_with_progress will save the URL `url` to the filepath `path` .

View File

@ -111,6 +111,11 @@ 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
field.default_expr_typ = c.expr(mut field.default_expr) field.default_expr_typ = c.expr(mut field.default_expr)
if field.typ.is_ptr() && !field.default_expr_typ.is_ptr()
&& field.default_expr is ast.StructInit {
c.error('reference field must be initialized with reference', field.default_expr.pos())
}
// disallow map `mut a = b` // disallow map `mut a = b`
field_sym := c.table.sym(field.typ) field_sym := c.table.sym(field.typ)
expr_sym := c.table.sym(field.default_expr_typ) expr_sym := c.table.sym(field.default_expr_typ)

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/struct_ref_field_no_ptr_err.vv:2:13: error: reference field must be initialized with reference
1 | struct Foo {
2 | bar &Bar = Bar{}
| ~~~~~
3 | }
4 |

View File

@ -0,0 +1,7 @@
struct Foo {
bar &Bar = Bar{}
}
struct Bar {
field int
}