winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.02k stars 198 forks source link

ReferenceError: Cannot access variable before initialization #2266

Open Chriscbr opened 1 year ago

Chriscbr commented 1 year ago

I tried this

// wing code
log("");

let body = "hello";
log(body);
if true {
  let body = "${body}world";
  log(body);
}
log(body);

Instead, this happened

--------------------------------- STACK TRACE ---------------------------------
evalmachine.<anonymous>:12
      const body = `${body}world`;
                   ^

ReferenceError: Cannot access 'body' before initialization
    at new $Root (evalmachine.<anonymous>:12:20)
    at new $App (evalmachine.<anonymous>:22:7)
    at evalmachine.<anonymous>:33:1

I expected this:

It should be a compile time error (surfaced by the language server during type checking). We need to implement a check validating that in any let-statement that declares a new variable X, X may not appear as a subexpression on its right hand side.

Is there a workaround?

Use a different variable name

Component

Compiler

Wing Version

0.13.25

Wing Console Version

No response

Node.js Version

18.14.1

Platform(s)

MacOS

Anything else?

No response

Community Notes

github-actions[bot] commented 1 year ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] commented 11 months ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] commented 6 months ago

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] commented 2 months ago

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

Chriscbr commented 2 months ago

A straightforward way to implement this can be to use a visitor to explore the right-hand expression node and check for Identifier expressions that have the matching name. See here for an example of how the visitor pattern is used in the compiler currently.