dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
516 stars 97 forks source link

bug: `Import expression found in unit body` #3539

Open tomijaga opened 2 years ago

tomijaga commented 2 years ago

OOPS! You've triggered a compiler bug. Please report this at https://github.com/dfinity/motoko/issues/new with the following details:

Motoko 0.7.3 (source qwdq7q2n-npg4rjz7-7s76jv69-kbwjwklg)

Fatal error: exception Invalid_argument("Import expression found in unit body: mo:base/Blob")
Raised at Lowering__Desugar.exp' in file "lowering/desugar.ml", line 227, characters 25-111
Called from Lowering__Desugar.typed_phrase' in file "lowering/desugar.ml", line 45, characters 16-30
Called from Lowering__Desugar.dec' in file "lowering/desugar.ml", line 688, characters 13-18
Called from Lowering__Desugar.phrase' in file "lowering/desugar.ml", line 41, characters 16-34
Called from Lowering__Desugar.dec in file "lowering/desugar.ml", line 682, characters 14-30
Called from Stdlib__list.map in file "list.ml", line 92, characters 20-23
Called from Stdlib__list.map in file "list.ml", line 92, characters 32-39
Called from Stdlib__list.map in file "list.ml", line 92, characters 32-39
Called from Stdlib__list.map in file "list.ml", line 92, characters 32-39
Called from Lowering__Desugar.transform_unit_body in file "lowering/desugar.ml", line 1068, characters 26-35
Called from Lowering__Desugar.transform_unit in file "lowering/desugar.ml", line 1107, characters 14-38
Called from Pipeline.desugar_unit in file "pipeline/pipeline.ml", line 584, characters 4-22
Called from Pipeline.compile_unit in file "pipeline/pipeline.ml", line 668, characters 16-43
Called from Pipeline.compile_files in file "pipeline/pipeline.ml", line 690, characters 19-56
Called from Diag.bind in file "lang_utils/diag.ml", line 32, characters 27-30
Called from Diag.bind in file "lang_utils/diag.ml", line 32, characters 27-30
Called from Dune__exe__Moc.process_files in file "exes/moc.ml", line 203, characters 49-94
Called from Dune__exe__Moc in file "exes/moc.ml", line 298, characters 4-23
make: *** [test1] Error 2
crusso commented 2 years ago

Thanks for reporting this!

If possible, can you provide the code (or a cut down version of it) that produces the bug, as well as the moc compiler arguments used to compile the code. I'm particularly interested in whether you are interpreting or compiling the code, and for which target.

tomijaga commented 2 years ago

I think this is the block of code that caused the issue: https://github.com/NatLabs/serde/blob/main/tests/Json.Test.mo#L32

I used this command in the makefile to compile the code: I don't know what you mean by target, but I'm using an m1 mac with Rosetta enabled.

tomijaga commented 2 years ago

@ggreif https://github.com/dfinity/motoko/issues/3540#issuecomment-1300015568

This shouldn't happen. Can you describe why the problem came and then went away?

After looking at it again. I think the issue is I am trying to compile more that one into an object file. It works when I remove the tests/*Test.mo command from the makefile and specify a single file. Is there a command for compiling multiple files?

tomijaga commented 2 years ago

I was able to write a bash script that compiles all the test files and runs them individually. https://github.com/NatLabs/serde/blob/main/test.sh

ggreif commented 2 years ago

Heh, this must be some old vestige in the moc compiler. Even I didn't know that you can use it to compile various files into one .wasm:

test1:
    $(shell vessel bin)/moc $(shell vessel sources) -wasi-system-api -o Test.wasm tests/*Test.mo && wasmtime test.wasm && rm -f Test.wasm

This pretty much explains the riddle. Thanks, I believe we can repro this now.

Btw. you can use (gnu-)make rules to run several tests. Something like

.PHONY: %.tested
%.tested: %Test.mo
    $(shell vessel bin)/moc $(shell vessel sources) -wasi-system-api -o $@.wasm $@ && wasmtime $@.wasm && rm -f $@.wasm

might work.

crusso commented 2 years ago

Thanks for the repo! This is indeed a left-over feature from the days when moc was just an interpreter. Maybe we should just refuse more gracefully to handle this when actually compiling...

ggreif commented 2 years ago

Yes, we should part from the gcc idea to compile several files at the same time. The list of sources should have at most one entry.