From e3f8d448c18f8b9d0900b06e108d6edab4dc5ca6 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 8 Jan 2021 04:49:13 +0100 Subject: [PATCH] all: [noinit] struct attribute --- tutorials/code/blog/blog.v | 10 +++------- vlib/v/checker/checker.v | 7 +++++++ vlib/v/parser/struct.v | 1 + vlib/v/table/types.v | 2 ++ vlib/vweb/vweb.v | 1 + 5 files changed, 14 insertions(+), 7 deletions(-) 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 { }