Skip to content

Commit

Permalink
checker: add a deprecation warning for const () groups (an error af…
Browse files Browse the repository at this point in the history
…ter 2025-01-01) (#22019)
  • Loading branch information
spytheman authored Aug 10, 2024
1 parent b802311 commit a741db4
Show file tree
Hide file tree
Showing 46 changed files with 268 additions and 322 deletions.
4 changes: 1 addition & 3 deletions cmd/tools/vcreate/project_model_web.v
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,7 @@ import vweb
import databases
import os
const (
port = 8082
)
const port = 8082
struct App {
vweb.Context
Expand Down
27 changes: 8 additions & 19 deletions vlib/regex/regex_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ struct TestItem {
}

// vfmt off
const(
match_test_suite = [
const match_test_suite = [
// minus in CC
TestItem{"d.def",r"abc.\.[\w\-]{,100}",-1,0},
TestItem{"abc12345.asd",r"abc.\.[\w\-]{,100}",-1,4},
Expand Down Expand Up @@ -203,10 +202,8 @@ match_test_suite = [
TestItem{"abcAAxyz", r"^abc\X4141xyz$", 0,8},
TestItem{"abcALxyz", r"^abc\X414cxyz$", 0,8},
TestItem{"abcALxyz", r"^abc\X414Cxyz$", 0,8},
TestItem{"abcBxyz", r"^abc\x41+xyz$", -1,3},

TestItem{"abcBxyz", r"^abc\x41+xyz$", -1,3},
]
)

struct TestItemRe {
src string
Expand All @@ -215,8 +212,7 @@ struct TestItemRe {
r string
}

const (
match_test_suite_replace = [
const match_test_suite_replace = [
// replace tests
TestItemRe{
"oggi pibao è andato a casa di pbababao ed ha trovato pibabababao",
Expand Down Expand Up @@ -250,7 +246,7 @@ match_test_suite_replace = [
},
]

match_test_suite_replace_simple = [
const match_test_suite_replace_simple = [
// replace tests
TestItemRe{
"oggi pibao è andato a casa di pbababao ed ha trovato pibabababao",
Expand All @@ -265,7 +261,6 @@ match_test_suite_replace_simple = [
"CIAO is a good day and CIAO will be for sure."
},
]
)

struct TestItemCGroup {
src string
Expand All @@ -276,8 +271,7 @@ struct TestItemCGroup {
cgn map[string]int
}

const (
cgroups_test_suite = [
const cgroups_test_suite = [
TestItemCGroup{
"http://www.ciao.mondo/hello/pippo12_/pera.html",
r"(?P<format>https?)|(?:ftps?)://(?P<token>[\w_]+[\.|/])+",0,42,
Expand Down Expand Up @@ -316,7 +310,6 @@ cgroups_test_suite = [
map[string]int{}
},
]
)

struct Test_find_all {
src string
Expand All @@ -325,8 +318,7 @@ struct Test_find_all {
res_str []string // ['find0','find1'...]
}

const (
find_all_test_suite = [
const find_all_test_suite = [
Test_find_all{
"abcd 1234 efgh 1234 ghkl1234 ab34546df",
r"\d+",
Expand Down Expand Up @@ -417,18 +409,16 @@ find_all_test_suite = [
[0, 2],
['ab']
}

]
)


struct Test_split {
src string
q string
res []string // ['abc','def',...]
}

const (
split_test_suite = [
const split_test_suite = [
Test_split{'abcd 1234 efgh 1234 ghkl1234 ab34546df', r'\d+', ['abcd ', ' efgh ', ' ghkl',
' ab', 'df']},
Test_split{'abcd 1234 efgh 1234 ghkl1234 ab34546df', r'\a+', ['', ' 1234 ', ' 1234 ', '1234 ',
Expand Down Expand Up @@ -457,7 +447,6 @@ const (
Test_split{'a-', r'-', ['a', '']},
Test_split{'-a', r'-', ['', 'a']},
]
)
// vfmt on

fn test_regex() {
Expand Down
5 changes: 2 additions & 3 deletions vlib/v/ast/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,7 @@ pub fn (node Stmt) str() string {
return node.str()
}
ConstDecl {
fields := node.fields.map(field_to_string)
return 'const (${fields.join(' ')})'
return node.fields.map(field_to_string).join('')
}
DeferStmt {
mut res := ''
Expand Down Expand Up @@ -870,7 +869,7 @@ pub fn (node Stmt) str() string {

fn field_to_string(f ConstField) string {
x := f.name.trim_string_left(f.mod + '.')
return '${x} = ${f.expr}'
return 'const ${x} = ${f.expr};'
}

pub fn (e ComptimeForKind) str() string {
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,10 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
if node.fields.len == 0 {
c.warn('const block must have at least 1 declaration', node.pos)
}
if node.is_block {
c.warn('const () groups will be an error after 2025-01-01 (`v fmt -w source.v` will fix that for you)',
node.pos)
}
for mut field in node.fields {
if checker.reserved_type_names_chk.matches(util.no_cur_mod(field.name, c.mod)) {
c.error('invalid use of reserved type `${field.name}` as a const name', field.pos)
Expand Down
14 changes: 7 additions & 7 deletions vlib/v/checker/tests/const_decl_multi_return_err.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vlib/v/checker/tests/const_decl_multi_return_err.vv:6:6: error: const declarations do not support multiple return values yet
4 |
5 | const (
6 | a = foo()
| ~~~~~
7 | )
8 |
vlib/v/checker/tests/const_decl_multi_return_err.vv:5:11: error: const declarations do not support multiple return values yet
3 | }
4 |
5 | const a = foo()
| ~~~~~
6 |
7 | fn main() {
7 changes: 2 additions & 5 deletions vlib/v/checker/tests/const_decl_multi_return_err.vv
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ fn foo() (int, int, int) {
return 1, 2, 3
}

const (
a = foo()
)

const a = foo()

fn main() {
println("$a")
println('${a}')
}
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/const_define_in_function_err.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vlib/v/checker/tests/const_define_in_function_err.vv:2:2: error: const can only be defined at the top level (outside of functions)
1 | fn main() {
2 | const (a = 1)
2 | const a = 1
| ~~~~~
3 | println(a)
4 | }
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/const_define_in_function_err.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
const (a = 1)
const a = 1
println(a)
}
10 changes: 5 additions & 5 deletions vlib/v/checker/tests/const_field_add_err.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vlib/v/checker/tests/const_field_add_err.vv:6:2: error: cannot modify constant `a`
4 |
5 | fn main() {
6 | a += 1
vlib/v/checker/tests/const_field_add_err.vv:4:2: error: cannot modify constant `a`
2 |
3 | fn main() {
4 | a += 1
| ^
7 | }
5 | }
4 changes: 1 addition & 3 deletions vlib/v/checker/tests/const_field_add_err.vv
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const (
a = 1
)
const a = 1

fn main() {
a += 1
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/checker/tests/const_field_dec_err.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vlib/v/checker/tests/const_field_dec_err.vv:6:2: error: cannot modify constant `a`
4 |
5 | fn main() {
6 | a--
vlib/v/checker/tests/const_field_dec_err.vv:4:2: error: cannot modify constant `a`
2 |
3 | fn main() {
4 | a--
| ^
7 | }
5 | }
4 changes: 1 addition & 3 deletions vlib/v/checker/tests/const_field_dec_err.vv
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const (
a = 1
)
const a = 1

fn main() {
a--
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/checker/tests/const_field_inc_err.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vlib/v/checker/tests/const_field_inc_err.vv:6:2: error: cannot modify constant `a`
4 |
5 | fn main() {
6 | a++
vlib/v/checker/tests/const_field_inc_err.vv:4:2: error: cannot modify constant `a`
2 |
3 | fn main() {
4 | a++
| ^
7 | }
5 | }
4 changes: 1 addition & 3 deletions vlib/v/checker/tests/const_field_inc_err.vv
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const (
a = 1
)
const a = 1

fn main() {
a++
Expand Down
13 changes: 6 additions & 7 deletions vlib/v/checker/tests/const_field_name_duplicate_err.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
vlib/v/checker/tests/const_field_name_duplicate_err.vv:3:2: error: duplicate const `aaa`
1 | const (
2 | aaa = 1
3 | aaa = 2
| ~~~
4 | )
5 | fn main() {
vlib/v/checker/tests/const_field_name_duplicate_err.vv:2:1: error: unexpected name `cosnt`
1 | const aaa = 1
2 | cosnt aaa = 2
| ~~~~~
3 |
4 | fn main() {
7 changes: 3 additions & 4 deletions vlib/v/checker/tests/const_field_name_duplicate_err.vv
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const (
aaa = 1
aaa = 2
)
const aaa = 1
cosnt aaa = 2

fn main() {
println(aaa)
}
11 changes: 5 additions & 6 deletions vlib/v/checker/tests/const_field_name_snake_case.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
vlib/v/checker/tests/const_field_name_snake_case.vv:2:2: error: const names cannot contain uppercase letters, use snake_case instead
1 | const (
2 | Red = 1
| ~~~
3 | )
4 | fn main() { println(Red) }
vlib/v/checker/tests/const_field_name_snake_case.vv:1:7: error: const names cannot contain uppercase letters, use snake_case instead
1 | const Red = 1
| ~~~
2 |
3 | fn main() { println(Red) }
5 changes: 2 additions & 3 deletions vlib/v/checker/tests/const_field_name_snake_case.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const (
Red = 1
)
const Red = 1

fn main() { println(Red) }
10 changes: 5 additions & 5 deletions vlib/v/checker/tests/const_field_sub_err.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vlib/v/checker/tests/const_field_sub_err.vv:6:2: error: cannot modify constant `a`
4 |
5 | fn main() {
6 | a -= 1
vlib/v/checker/tests/const_field_sub_err.vv:4:2: error: cannot modify constant `a`
2 |
3 | fn main() {
4 | a -= 1
| ^
7 | }
5 | }
4 changes: 1 addition & 3 deletions vlib/v/checker/tests/const_field_sub_err.vv
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const (
a = 1
)
const a = 1

fn main() {
a -= 1
Expand Down
11 changes: 5 additions & 6 deletions vlib/v/checker/tests/globals/name_conflict_with_const.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
vlib/v/checker/tests/globals/name_conflict_with_const.vv:6:2: error: duplicate global and const `foo`
4 |
5 | __global (
6 | foo = 123
| ~~~
7 | )
vlib/v/checker/tests/globals/name_conflict_with_const.vv:3:10: error: duplicate global and const `foo`
1 | const foo = 'abc'
2 |
3 | __global foo = 123
| ~~~
8 changes: 2 additions & 6 deletions vlib/v/checker/tests/globals/name_conflict_with_const.vv
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const (
foo = 'abc'
)
const foo = 'abc'

__global (
foo = 123
)
__global foo = 123
12 changes: 6 additions & 6 deletions vlib/v/checker/tests/if_diff_expected_type_err.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vlib/v/checker/tests/if_diff_expected_type_err.vv:7:11: error: mismatched types `&[]rune` and `[]rune`
5 |
6 | fn main() {
7 | runes := if true { &some_runes } else { some_other_runes }
vlib/v/checker/tests/if_diff_expected_type_err.vv:5:11: error: mismatched types `&[]rune` and `[]rune`
3 |
4 | fn main() {
5 | runes := if true { &some_runes } else { some_other_runes }
| ~~
8 | println(runes)
9 | }
6 | println(runes)
7 | }
8 changes: 3 additions & 5 deletions vlib/v/checker/tests/if_diff_expected_type_err.vv
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const (
some_runes = [`a`, `b`, `c`]
some_other_runes = [`c`, `b`, `a`]
)
const some_runes = [`a`, `b`, `c`]
const some_other_runes = [`c`, `b`, `a`]

fn main() {
runes := if true { &some_runes } else { some_other_runes }
println(runes)
}
}
4 changes: 1 addition & 3 deletions vlib/v/checker/tests/incorrect_name_const.vv
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const (
_my_const = 0
)
const _my_const = 0
12 changes: 6 additions & 6 deletions vlib/v/checker/tests/lock_const.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vlib/v/checker/tests/lock_const.vv:7:8: error: `a` must be declared as `shared` variable to be locked
5 | fn main() {
6 | mut c := 0
7 | rlock a {
vlib/v/checker/tests/lock_const.vv:5:8: error: `a` must be declared as `shared` variable to be locked
3 | fn main() {
4 | mut c := 0
5 | rlock a {
| ^
8 | c = a
9 | }
6 | c = a
7 | }
4 changes: 1 addition & 3 deletions vlib/v/checker/tests/lock_const.vv
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const (
a = 5
)
const a = 5

fn main() {
mut c := 0
Expand Down
Loading

0 comments on commit a741db4

Please sign in to comment.