From a6595e5b49ffa94947a2e5767263f523cd827c44 Mon Sep 17 00:00:00 2001 From: metagn Date: Mon, 9 Sep 2024 12:29:30 +0300 Subject: open new scope for const values (#24084) fixes #5395 Previously values of `const` statements used the same scope as the `const` statement itself, meaning variables could be declared inside them and referred to in other statements in the same block. Now each `const` value opens its own scope, so any variable declared in the value of a constant can only be accessed for that constant. We could change this to open a new scope for the `const` *section* rather than each constant, so the variables can be used in other constants, but I'm not sure if this is sound. --- tests/vm/tconstscope1.nim | 5 +++++ tests/vm/tconstscope2.nim | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 tests/vm/tconstscope1.nim create mode 100644 tests/vm/tconstscope2.nim (limited to 'tests/vm') diff --git a/tests/vm/tconstscope1.nim b/tests/vm/tconstscope1.nim new file mode 100644 index 000000000..41c45a28f --- /dev/null +++ b/tests/vm/tconstscope1.nim @@ -0,0 +1,5 @@ +# issue #5395 + +const a = (var b = 3; b) +echo b #[tt.Error + ^ undeclared identifier: 'b']# diff --git a/tests/vm/tconstscope2.nim b/tests/vm/tconstscope2.nim new file mode 100644 index 000000000..d858e96c2 --- /dev/null +++ b/tests/vm/tconstscope2.nim @@ -0,0 +1,5 @@ +const + a = (var x = 3; x) + # should we allow this? + b = x #[tt.Error + ^ undeclared identifier: 'x']# -- cgit 1.4.1-2-gfad0