Skip to content

Commit

Permalink
checker: add a deprecation warning for const () groups
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Aug 10, 2024
1 parent a02913a commit 19d6e2b
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 46 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
8 changes: 3 additions & 5 deletions vlib/v/slow_tests/inout/printing_const_array.vv
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const (
dat = 'Data tag ,No data'.split(',')
dd = []Info{len: 4, init: Info{if index in tag { dat[0] + index.str() } else { dat[1] }}}
tag = [1, 2]
)
const dat = 'Data tag ,No data'.split(',')
const dd = []Info{len: 4, init: Info{if index in tag { dat[0] + index.str() } else { dat[1] }}}
const tag = [1, 2]

struct Info {
val string
Expand Down
11 changes: 4 additions & 7 deletions vlib/v2/gen/v/gen.v
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,16 @@ fn (mut g Gen) stmt(stmt ast.Stmt) {
g.writeln('}')
}
ast.ConstDecl {
if stmt.is_public {
g.write('pub ')
}
g.writeln('const (')
g.indent++
for field in stmt.fields {
if stmt.is_public {
g.write('pub ')
}
g.writeln('const ')
g.write(field.name)
g.write(' = ')
g.expr(field.value)
g.writeln('')
}
g.indent--
g.writeln(')')
}
ast.ComptimeStmt {
g.write('$')
Expand Down
17 changes: 8 additions & 9 deletions vlib/vweb/csrf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ import net.http
import vweb
import vweb.csrf
const (
// the configuration moved here
csrf_config = csrf.CsrfConfig{
// change the secret
secret: 'my-64bytes-secret'
// change to which domains you want to allow
allowed_hosts: ['*']
}
)
// the configuration moved here
const csrf_config = csrf.CsrfConfig{
// change the secret
secret: 'my-64bytes-secret'
// change to which domains you want to allow
allowed_hosts: ['*']
}
struct App {
vweb.Context
Expand Down

0 comments on commit 19d6e2b

Please sign in to comment.