From dfc654f84ea85e05b8bfffb45da22df31989602f Mon Sep 17 00:00:00 2001 From: Toby Webb Date: Mon, 21 Oct 2019 07:57:29 +0200 Subject: [PATCH] const declarations should be order independent --- vlib/compiler/parser.v | 15 +++++++++++++++ vlib/compiler/tests/const_test.v | 10 ++++++++++ 2 files changed, 25 insertions(+) create mode 100644 vlib/compiler/tests/const_test.v diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 9ab36462d4..c3153dfaa4 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -534,6 +534,21 @@ fn (p mut Parser) const_decl() { if p.first_pass() { p.table.register_const(name, typ, p.mod) } + // Check to see if this constant exists, and is void. If so, try and get the type again: + for { // TODO: Find out how the error handling, and use it here instead of the for/break hack... + my_const := p.v.table.find_const(name) or { + break + } + if my_const.typ == 'void' { + for i, v in p.v.table.consts { + if v.name == name { + p.v.table.consts[i].typ = typ + break + } + } + } + break + } if p.pass == .main && !p.cgen.nogen { // TODO hack // cur_line has const's value right now. if it's just a number, then optimize generation: diff --git a/vlib/compiler/tests/const_test.v b/vlib/compiler/tests/const_test.v new file mode 100644 index 0000000000..03a57594a4 --- /dev/null +++ b/vlib/compiler/tests/const_test.v @@ -0,0 +1,10 @@ +const ( + // c = a // TODO + a = b + b = 1 +) + +fn test_const() { + assert a == 1 + // assert c == 1 // TODO: This will not build yet +} \ No newline at end of file