diff --git a/tutorials/code/blog/blog.v b/tutorials/code/blog/blog.v index 75fc08cf5f..c17a34de16 100644 --- a/tutorials/code/blog/blog.v +++ b/tutorials/code/blog/blog.v @@ -33,11 +33,8 @@ pub fn (app &App) index() vweb.Result { pub fn (mut app App) init_once() { app.db = sqlite.connect('blog.db') or { panic(err) } - app.db.exec('create table if not exists article (' + - 'id integer primary key, ' + - "title text default ''," + - "text text default ''" + - ');') + app.db.exec('create table if not exists article (' + 'id integer primary key, ' + "title text default ''," + + "text text default ''" + ');') } pub fn (mut app App) init() { @@ -53,8 +50,7 @@ pub fn (mut app App) new_article() vweb.Result { title := app.form['title'] text := app.form['text'] if title == '' || text == '' { - app.text('Empty text/title') - return vweb.Result{} + return app.text('Empty text/title') } article := Article{ title: title diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index fc91ee7626..c7c40acae2 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -504,6 +504,13 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { type_sym.language != .c { c.error('type `$type_sym.name` is private', struct_init.pos) } + if type_sym.kind == .struct_ { + info := type_sym.info as table.Struct + if info.attrs.len > 0 && info.attrs[0].name == 'noinit' && type_sym.mod != c.mod { + c.error('struct `$type_sym.name` is declared with a `[noinit]` attribute, so ' + 'it cannot be initialized with `$type_sym.name{}`', + struct_init.pos) + } + } match type_sym.kind { .placeholder { c.error('unknown struct: $type_sym.name', struct_init.pos) diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index bc5c6e5098..5341dd0d24 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -295,6 +295,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { is_union: is_union is_ref_only: attrs.contains('ref_only') generic_types: generic_types + attrs: attrs } is_public: is_pub } diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index dc35814ab0..7c0b029451 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -603,6 +603,8 @@ pub fn (kinds []Kind) str() string { } pub struct Struct { +pub: + attrs []Attr pub mut: embeds []Type fields []Field diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 202aacabba..b3737cad35 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -72,6 +72,7 @@ pub struct Cookie { http_only bool } +[noinit] pub struct Result { }