diff --git a/vlib/v/fmt/tests/allow_const_with_decl_assign_expected.vv b/vlib/v/fmt/tests/allow_const_with_decl_assign_expected.vv new file mode 100644 index 0000000000..78aaa68b46 --- /dev/null +++ b/vlib/v/fmt/tests/allow_const_with_decl_assign_expected.vv @@ -0,0 +1,19 @@ +const a = 123 + +const b = 'abc' + +const c = rune(123) + +const d = u64(2 * c) + +const f = func() + +fn func() int { + return 42 +} + +dump(a) +dump(b) +dump(c) +dump(d) +dump(f) diff --git a/vlib/v/fmt/tests/allow_const_with_decl_assign_input.vv b/vlib/v/fmt/tests/allow_const_with_decl_assign_input.vv new file mode 100644 index 0000000000..167e44d89c --- /dev/null +++ b/vlib/v/fmt/tests/allow_const_with_decl_assign_input.vv @@ -0,0 +1,19 @@ +const a := 123 + +const b := 'abc' + +const c := rune(123) + +const d := u64(2 * c) + +const f := func() + +fn func() int { + return 42 +} + +dump(a) +dump(b) +dump(c) +dump(d) +dump(f) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d6db79abc2..4f60275807 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3713,10 +3713,13 @@ fn (mut p Parser) const_decl() ast.ConstDecl { if p.tok.kind == .comma { p.error_with_pos('const declaration do not support multiple assign yet', p.tok.pos()) } + // Allow for `const x := 123`, and for `const x = 123` too. + // Supporting `const x := 123` in addition to `const x = 123`, makes extracting local variables to constants much less annoying, while prototyping: if p.tok.kind == .decl_assign { - p.error_with_pos('cannot use `:=` to declare a const, use `=` instead', p.tok.pos()) + p.check(.decl_assign) + } else { + p.check(.assign) } - p.check(.assign) end_comments << p.eat_comments() if p.tok.kind == .key_fn { p.error('const initializer fn literal is not a constant') diff --git a/vlib/v/parser/tests/const_init_decl_assign_err.out b/vlib/v/parser/tests/const_init_decl_assign_err.out deleted file mode 100644 index 9099e5f7f4..0000000000 --- a/vlib/v/parser/tests/const_init_decl_assign_err.out +++ /dev/null @@ -1,5 +0,0 @@ -vlib/v/parser/tests/const_init_decl_assign_err.vv:2:4: error: cannot use `:=` to declare a const, use `=` instead - 1 | const ( - 2 | a := 43 - | ~~ - 3 | ) diff --git a/vlib/v/parser/tests/const_init_decl_assign_err.vv b/vlib/v/parser/tests/const_init_decl_assign_err.vv deleted file mode 100644 index 9b9cafba4a..0000000000 --- a/vlib/v/parser/tests/const_init_decl_assign_err.vv +++ /dev/null @@ -1,3 +0,0 @@ -const ( - a := 43 -)