Skip to content

Commit

Permalink
parser: deprecate old attribute syntax & update remaining (missed) at…
Browse files Browse the repository at this point in the history
…tributes (#19879)
  • Loading branch information
joe-conigliaro committed Nov 15, 2023
1 parent bc24683 commit 1e3d382
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion vlib/net/http/request.v
Expand Up @@ -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'
Expand Down
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/tests/struct_field_type_err.vv
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion 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 |
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/unknown_type_in_anon_fn.vv
Expand Up @@ -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() {}
5 changes: 4 additions & 1 deletion vlib/v/parser/parse_type.v
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions vlib/v/parser/parser.v
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 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 | }
4 changes: 2 additions & 2 deletions vlib/v/parser/tests/fn_attributes_empty_err.vv
@@ -1,6 +1,6 @@
[] fn tt() {
@[] fn tt() {
println('text')
}
fn main() {
tt()
}
}
@@ -1,5 +1,5 @@
struct Foo {
foo fn () ? [required]
foo fn () ? @[required]
}

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

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion vlib/vweb/vweb.v
Expand Up @@ -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()
}

Expand Down

0 comments on commit 1e3d382

Please sign in to comment.