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.03k stars 198 forks source link

unknown symbol in for loops within inflight methods (regression) #2353

Closed eladb closed 1 year ago

eladb commented 1 year ago

I tried this:

class Foo {
  init() {}
  inflight hello() {
    for p in ["hello"] {
      log(p);
    }
  }
}

This happened:

thread '<unnamed>' panicked at 'covered by type checking: TypeError { message: "Unknown symbol \"p\"", span: WingSpan { start: WingLocation { line: 6, col: 10 }, end: WingLocation { line: 6, col: 11 }, file_id: "repro.w" } }', libs/wingc/src/jsify.rs:1578:27
stack backtrace:
RuntimeError: unreachable
    at std::panicking::rust_panic_with_hook::h911ba172265603df (wasm://wasm/009a3c12:wasm-function[220]:0xca673)
    at core::panicking::panic_fmt::h6958314cbbe045b2 (wasm://wasm/009a3c12:wasm-function[1437]:0x17253d)
    at core::result::unwrap_failed::h5a65fd54aeec1613 (wasm://wasm/009a3c12:wasm-function[1080]:0x165834)
    at wingc::jsify::FieldReferenceVisitor::analyze_expr::h36a785df069f91f6 (wasm://wasm/009a3c12:wasm-function[86]:0x87e7e)
    at <wingc::jsify::FieldReferenceVisitor as wingc::visit::Visit>::visit_expr::hccd000dcdd336ea9 (wasm://wasm/009a3c12:wasm-function[58]:0x71e9b)
    at <wingc::jsify::FieldReferenceVisitor as wingc::visit::Visit>::visit_expr::hccd000dcdd336ea9 (wasm://wasm/009a3c12:wasm-function[58]:0x72bc2)
    at wingc::visit::visit_stmt::hc47e69e6d9fcb939 (wasm://wasm/009a3c12:wasm-function[212]:0xc8310)
    at wingc::visit::visit_stmt::hc47e69e6d9fcb939 (wasm://wasm/009a3c12:wasm-function[212]:0xc7d5f)
    at wingc::jsify::JSifier::jsify_statement::h217f18b8ad6ec044 (wasm://wasm/009a3c12:wasm-function[17]:0x8ffc)
    at wingc_compile.command_export (wasm://wasm/009a3c12:wasm-function[1582]:0x1794a7)
    at Object.invoke (/Users/eladb/.volta/tools/image/packages/winglang/lib/node_modules/winglang/dist/wingc.js:137:37)
    at compile (/Users/eladb/.volta/tools/image/packages/winglang/lib/node_modules/winglang/dist/commands/compile.js:135:38)

I expected this:

To work

Is there a workaround?

Not that I am aware of

Component

Compiler

Wing Version

No response

Wing Console Version

No response

Node.js Version

No response

Platform(s)

No response

Anything else?

I suspect this is related to #2211

Community Notes

eladb commented 1 year ago

I am looking into this.

eladb commented 1 year ago

@Chriscbr the culprit is indeed the code added to the FieldReferenceVisitor here.

In for loops, the iteration variable (p in the example above) is only defined within the symbol environment of the loop block and not the parent environment. This is why the lookup fails.

I'll leave it to you to pick up from here.

Chriscbr commented 1 year ago

Ick. I wish there was a way to arbitrarily find a symbol's parent environment / or just find the VariableInfo given a particular symbol.

In any case I've found a way to fix it for now since we can stop analyzing the expression as soon as we know it's a local/bound variable.

monadabot commented 1 year ago

Congrats! :rocket: This was released in Wing 0.15.2.

yoav-steinberg commented 1 year ago

Ick. I wish there was a way to arbitrarily find a symbol's parent environment / or just find the VariableInfo given a particular symbol.

You're right, that's what we need here. Added a fix as part of #2158.