From 1e3d38255f5b39f35958749b60d27c565d89ad6b Mon Sep 17 00:00:00 2001 From: Joe C Date: Wed, 15 Nov 2023 22:07:59 +1100 Subject: [PATCH] parser: deprecate old attribute syntax & update remaining (missed) attributes (#19879) --- vlib/net/http/request.v | 2 +- .../tests/generic_interface_missing_type_names_err.vv | 4 ++-- vlib/v/checker/tests/struct_field_type_err.vv | 4 ++-- vlib/v/checker/tests/unknown_type_in_anon_fn.out | 2 +- vlib/v/checker/tests/unknown_type_in_anon_fn.vv | 2 +- vlib/v/parser/parse_type.v | 5 ++++- vlib/v/parser/parser.v | 10 ++++++---- vlib/v/parser/tests/fn_attributes_empty_err.out | 4 ++-- vlib/v/parser/tests/fn_attributes_empty_err.vv | 4 ++-- .../tests/struct_field_required_fn_option_type.vv | 2 +- .../tests/struct_field_required_fn_result_type.vv | 2 +- vlib/vweb/vweb.v | 2 +- 12 files changed, 24 insertions(+), 19 deletions(-) diff --git a/vlib/net/http/request.v b/vlib/net/http/request.v index f38f1d6c715ec6..aedaa41fff5c87 100644 --- a/vlib/net/http/request.v +++ b/vlib/net/http/request.v @@ -24,7 +24,7 @@ pub mut: method Method = .get header Header host string - cookies map[string]string [deprecated: 'use req.cookie(name) and req.add_cookie(name) instead'] + cookies map[string]string @[deprecated: 'use req.cookie(name) and req.add_cookie(name) instead'] data string url string user_agent string = 'v.http' diff --git a/vlib/v/checker/tests/generic_interface_missing_type_names_err.vv b/vlib/v/checker/tests/generic_interface_missing_type_names_err.vv index bfc68013ffa9d4..123160f5c79ea7 100644 --- a/vlib/v/checker/tests/generic_interface_missing_type_names_err.vv +++ b/vlib/v/checker/tests/generic_interface_missing_type_names_err.vv @@ -5,11 +5,11 @@ interface Output[T] { struct Coil { pub mut: val int - pub: name string [required] + pub: name string @[required] } struct Light { pub mut: val int - pub: name string [required] + pub: name string @[required] } fn main() { diff --git a/vlib/v/checker/tests/struct_field_type_err.vv b/vlib/v/checker/tests/struct_field_type_err.vv index 9a022d48ca71c4..5e13c30317f81b 100644 --- a/vlib/v/checker/tests/struct_field_type_err.vv +++ b/vlib/v/checker/tests/struct_field_type_err.vv @@ -2,8 +2,8 @@ struct Data { mut: n int b bool - f1 fn (voidptr) [required] - f2 fn (...voidptr) [required] + f1 fn (voidptr) @[required] + f2 fn (...voidptr) @[required] data &Data } diff --git a/vlib/v/checker/tests/unknown_type_in_anon_fn.out b/vlib/v/checker/tests/unknown_type_in_anon_fn.out index 6fea7a8a9083a3..e23316dd201f40 100644 --- a/vlib/v/checker/tests/unknown_type_in_anon_fn.out +++ b/vlib/v/checker/tests/unknown_type_in_anon_fn.out @@ -1,7 +1,7 @@ vlib/v/checker/tests/unknown_type_in_anon_fn.vv:5:10: error: unknown type `Another` 3 | struct Struc{ 4 | mut: - 5 | f fn (s Another, i int)? [required] + 5 | f fn (s Another, i int)? @[required] | ~~~~~~~ 6 | } 7 | diff --git a/vlib/v/checker/tests/unknown_type_in_anon_fn.vv b/vlib/v/checker/tests/unknown_type_in_anon_fn.vv index 742675d6d791e5..6e68684b68d59d 100644 --- a/vlib/v/checker/tests/unknown_type_in_anon_fn.vv +++ b/vlib/v/checker/tests/unknown_type_in_anon_fn.vv @@ -2,7 +2,7 @@ module main struct Struc{ mut: - f fn (s Another, i int)? [required] + f fn (s Another, i int)? @[required] } fn main() {} diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index e29a1b53535e95..3fc146beafdfca 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -445,10 +445,13 @@ fn (mut p Parser) parse_type() ast.Type { if is_option || is_result { // maybe the '[' is the start of the field attribute + // TODO: remove once old syntax dropped is_required_field := p.inside_struct_field_decl && p.tok.kind == .lsbr && p.peek_tok.kind == .name && p.peek_tok.lit == 'required' + is_attr := p.tok.kind == .at - if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar, .assign] || is_required_field { + if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar, .assign] + || (is_attr || is_required_field) { mut typ := ast.void_type if is_option { typ = typ.set_flag(.option) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 86d383c6922589..8c526126bd0ac4 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1812,8 +1812,10 @@ fn (mut p Parser) is_attributes() bool { // when is_top_stmt is true attrs are added to p.attrs fn (mut p Parser) attributes() { + start_pos := p.tok.pos() mut is_at := false if p.tok.kind == .lsbr { + p.note('`[attr]` has been deprecated, use `@[attr]` instead') // [attr] p.check(.lsbr) } else if p.tok.kind == .at { @@ -1824,16 +1826,16 @@ fn (mut p Parser) attributes() { } mut has_ctdefine := false for p.tok.kind != .rsbr { - start_pos := p.tok.pos() + attr_start_pos := p.tok.pos() attr := p.parse_attr(is_at) if p.attrs.contains(attr.name) && attr.name != 'wasm_export' { - p.error_with_pos('duplicate attribute `${attr.name}`', start_pos.extend(p.prev_tok.pos())) + p.error_with_pos('duplicate attribute `${attr.name}`', attr_start_pos.extend(p.prev_tok.pos())) return } if attr.kind == .comptime_define { if has_ctdefine { p.error_with_pos('only one `[if flag]` may be applied at a time `${attr.name}`', - start_pos.extend(p.prev_tok.pos())) + attr_start_pos.extend(p.prev_tok.pos())) return } else { has_ctdefine = true @@ -1851,7 +1853,7 @@ fn (mut p Parser) attributes() { p.next() } if p.attrs.len == 0 { - p.error_with_pos('attributes cannot be empty', p.prev_tok.pos().extend(p.tok.pos())) + p.error_with_pos('attributes cannot be empty', start_pos.extend(p.tok.pos())) return } // TODO: remove when old attr syntax is removed diff --git a/vlib/v/parser/tests/fn_attributes_empty_err.out b/vlib/v/parser/tests/fn_attributes_empty_err.out index d021aee7e113a6..99c85786232eb4 100644 --- a/vlib/v/parser/tests/fn_attributes_empty_err.out +++ b/vlib/v/parser/tests/fn_attributes_empty_err.out @@ -1,5 +1,5 @@ vlib/v/parser/tests/fn_attributes_empty_err.vv:1:1: error: attributes cannot be empty - 1 | [] fn tt() { - | ~~ + 1 | @[] fn tt() { + | ~~~ 2 | println('text') 3 | } diff --git a/vlib/v/parser/tests/fn_attributes_empty_err.vv b/vlib/v/parser/tests/fn_attributes_empty_err.vv index fd584c2972820f..e71f63d496a5b0 100644 --- a/vlib/v/parser/tests/fn_attributes_empty_err.vv +++ b/vlib/v/parser/tests/fn_attributes_empty_err.vv @@ -1,6 +1,6 @@ -[] fn tt() { +@[] fn tt() { println('text') } fn main() { tt() -} \ No newline at end of file +} diff --git a/vlib/v/parser/tests/struct_field_required_fn_option_type.vv b/vlib/v/parser/tests/struct_field_required_fn_option_type.vv index d33fdb44136204..6adde85cd58a19 100644 --- a/vlib/v/parser/tests/struct_field_required_fn_option_type.vv +++ b/vlib/v/parser/tests/struct_field_required_fn_option_type.vv @@ -1,5 +1,5 @@ struct Foo { - foo fn () ? [required] + foo fn () ? @[required] } fn main() { diff --git a/vlib/v/parser/tests/struct_field_required_fn_result_type.vv b/vlib/v/parser/tests/struct_field_required_fn_result_type.vv index 3892fb63bf4bf3..cf017f42b4b0ae 100644 --- a/vlib/v/parser/tests/struct_field_required_fn_result_type.vv +++ b/vlib/v/parser/tests/struct_field_required_fn_result_type.vv @@ -1,5 +1,5 @@ struct Foo { - foo fn() ! [required] + foo fn() ! @[required] } fn main() { diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 4f39de161c19f2..f82654a3941788 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -1181,7 +1181,7 @@ fn (mut w Worker[T]) process_incoming_requests() { @[params] pub struct PoolParams[T] { - handler fn () T [required] = unsafe { nil } + handler fn () T = unsafe { nil } @[required] nr_workers int = runtime.nr_jobs() }