parser: deprecate old attribute syntax & update remaining (missed) attributes (#19879)

This commit is contained in:
Joe C 2023-11-15 22:07:59 +11:00 committed by GitHub
parent bc24683608
commit 1e3d38255f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 24 additions and 19 deletions

View File

@ -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'

View File

@ -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() {

View File

@ -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
}

View File

@ -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 |

View File

@ -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() {}

View File

@ -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)

View File

@ -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

View File

@ -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 | }

View File

@ -1,6 +1,6 @@
[] fn tt() {
@[] fn tt() {
println('text')
}
fn main() {
tt()
}
}

View File

@ -1,5 +1,5 @@
struct Foo {
foo fn () ? [required]
foo fn () ? @[required]
}
fn main() {

View File

@ -1,5 +1,5 @@
struct Foo {
foo fn() ! [required]
foo fn() ! @[required]
}
fn main() {

View File

@ -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()
}