Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: compile an async fn #437

Open
FlyCloudC opened this issue Jan 25, 2025 · 6 comments
Open

error: compile an async fn #437

FlyCloudC opened this issue Jan 25, 2025 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@FlyCloudC
Copy link

running moon run --target wasm-gc bug.mbt

bug.mbt:

///|
fn main {
  ignore(f)
}

///|
async fn g() -> (() -> Unit)? {
  panic()
}

///|
async fn f() -> (Int, Int) {
  match g!!() {
    Some(f) => {
      ignore(f)
      panic()
    }
    None => panic()
  }
}

output:

CompileError: WebAssembly.Module(): Compiling function #4:"$moon/run/single.f.*async_driver.fn/1" failed: call_ref[1] expected type (ref 1), found i32.const of type i32 @+470
error: failed to run

moon version --all:

moon 0.1.20250121 (a825806 2025-01-21) ~\.moon\bin\moon.exe
moonc v0.1.20250121+7fc3467ab ~\.moon\bin\moonc.exe
moonrun 0.1.20250121 (a825806 2025-01-21) ~\.moon\bin\moonrun.exe

The error only occurs when compiling to wasm-gc.

@peter-jerry-ye
Copy link
Collaborator

I failed to reproduce on my side.

@Guest0x0
Copy link
Collaborator

Guest0x0 commented Jan 26, 2025

I cannot reproduce the issue neither. You may try the following:

  • do cd ~/.moon/lib/core && moon clean && moon bundle --target all and try again
  • if the issue persist, put bug.mbt in a MoonBit project with moon.mod.json and moon.pkg.json, then run moon build --target wam-gc --output-wat bug.mbt and post the following:
    • generated .wat file, located at target/wasm-gc/release/build/<path to package>/<package name>.wat
    • the output of mooninfo -format text target/wasm-gc/release/build/<path to package>/<package name>.core

@FlyCloudC
Copy link
Author

I moved it into the pkg directory, and then the compilation passed without errors. However, if I change fn main to test, and then run moon test --target wasm-gc -p username/project1 -f bug.mbt , the same error occurs again.

I don't know how to output a test in WAT format. When I run moon test --target wasm-gc -p username/project1 -f bug.mbt --output-wat, it just outputs:

Error: Unsupported file type
Failed to run the test: D:\Code\moonbit\bug\project1\target\wasm-gc\debug\test\project1.internal_test.wat

Total tests: 1, passed: 0, failed: 1.

@Guest0x0
Copy link
Collaborator

I still can't reproduce with your new setting. But your description may indicate that the bug only occur in debug mode. Have you tried cd ~/.moon/lib/core && moon clean && moon bundle --target all?

The .wat file is already generated at D:\Code\moonbit\bug\project1\target\wasm-gc\debug\test\project1.internal_test.wat . The error message you see is due to moonrun trying to execute the .wat file. Since the .wat file is quite big, it would be great if you could post only the f.*async_driver.fn/1 function in the .wat file.

@FlyCloudC
Copy link
Author

FlyCloudC commented Jan 26, 2025

I have ran cd ~/.moon/lib/core && moon clean && moon bundle --target all.
output:

Warning: [1029]
   ╭─[C:\Users\FlyCloudC\.moon\lib\core\env\moon.pkg.json:4:9]
   │
mitlang/core/array"
   │         ────────────┬───────────
   │                     ╰───────────── Warning: Unused package 'moonbitlang/core/array'
───╯
Finished. moon: ran 50 tasks, now up to date
Blocking waiting for file lock C:\Users\FlyCloudC\.moon\lib\core\target\.moon-lock ...
Finished. moon: ran 50 tasks, now up to date
Finished. moon: ran 50 tasks, now up to date
Finished. moon: ran 50 tasks, now up to date

After that, run again moon test --target wasm-gc -p username/project1 -f bug.mbt --output-wat. The function in .wat:

(func $$username/project1.f.*async_driver.fn/1
 (source_name "$username/project1.f.*async_driver.fn")
 (param $*env/311 (source_name "*env") (source_type unit) i32)
 (param $*state/230 (source_name "*state") (ref $moonbit.enum)) (result i32)
 (local $*state/231 (source_name "*state") (ref $moonbit.enum))
 (local $**arm/34/232 (source_name "**arm/34")
  (ref $$username/project1.f.State.*arm/34))
 (local $*cont/233 (source_name "*cont") (ref $<<Int*Int>>=>Unit))
 (local $ff/234 (source_name "ff") (ref $<>=>Unit))
 (local $*State_1/235 (source_name "*State_1")
  (ref $$username/project1.f.State.State_1))
 (local $*cont/236 (source_name "*cont") (ref $<<Int*Int>>=>Unit))
 (local $*cont_param/237 (source_name "*cont_param") (ref $moonbit.enum))
 (local $*bind/238 (source_name "*bind") (ref $moonbit.enum))
 (local $*Some/239 (source_name "*Some") (ref $Option<<>=>Unit>.Some))
 (local $*x/240 (source_name "*x") (ref $<>=>Unit))
 (local $tag/338 (source_name "tag") i32)
 (local $tag/339 (source_name "tag") i32) (prologue_end)
 (local.get $*state/230)
 (loop $loop:241 (param (ref $moonbit.enum)) (local.tee $*state/231)
  (struct.get $moonbit.enum 0) (local.set $tag/338)
  (if (i32.eq (local.get $tag/338) (i32.const 0))
   (then (local.get $*state/231)
    (ref.cast (ref $$username/project1.f.State.*arm/34))
    (local.tee $**arm/34/232)
    (struct.get $$username/project1.f.State.*arm/34 2) (local.set $*cont/233)
    (local.get $**arm/34/232)
    (struct.get $$username/project1.f.State.*arm/34 1) (local.set $ff/234)
    (source_pos username/project1 bug.mbt 15 6) (i32.const 0)
    (source_pos username/project1 bug.mbt 15 16) (drop)
    (local.get $*cont/233) (source_pos username/project1 bug.mbt 16 6)
    (call $throw) (unreachable)
    (struct.get $<<Int*Int>>=>Unit 0 (local.get $*cont/233))
    (call_ref $<<Int*Int>>=>Unit-sig) (drop))
   (else (local.get $*state/231)
    (ref.cast (ref $$username/project1.f.State.State_1))
    (local.tee $*State_1/235)
    (struct.get $$username/project1.f.State.State_1 2) (local.set $*cont/236)
    (local.get $*State_1/235)
    (struct.get $$username/project1.f.State.State_1 1)
    (local.set $*cont_param/237) (source_pos username/project1 bug.mbt 13 2)
    (local.get $*cont_param/237) (local.set $*bind/238)
    (local.get $*cont/236) (local.get $*bind/238)
    (struct.get $moonbit.enum 0) (local.set $tag/339)
    (if (i32.eq (local.get $tag/339) (i32.const 1))
     (then (local.get $*bind/238) (ref.cast (ref $Option<<>=>Unit>.Some))
      (local.tee $*Some/239) (struct.get $Option<<>=>Unit>.Some 1)
      (local.set $*x/240)
      (struct.new $$username/project1.f.State.*arm/34 (i32.const 0)
       (local.get $*x/240) (local.get $*cont/236))
      (br $loop:241))
     (else (source_pos username/project1 bug.mbt 18 12) (call $throw)
      (unreachable)))
    (i32.const 0) (struct.get $<<Int*Int>>=>Unit 0 (local.get $*cont/236))
    (call_ref $<<Int*Int>>=>Unit-sig)
    (source_pos username/project1 bug.mbt 19 3) (drop))))
 (i32.const 0))

At this point, when running moon test, the error occurs.

CompileError: WebAssembly.Module(): Compiling function 
#75:"$username/project1.f.*async_driver.fn/1" failed: call_ref[1] expected type (ref 12), found i32.const of type i32 @+6525
Failed to run the test: D:\Code\moonbit\bug\project1\target\wasm-gc\debug\test\project1.internal_test.wasm    

Total tests: 1, passed: 0, failed: 1.

@FlyCloudC
Copy link
Author

It is indeed related to the debug mode. When I change test to fn main and run moon run --debug ./src/bug.mbt, the error appears.

@peter-jerry-ye peter-jerry-ye added the bug Something isn't working label Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants