binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

TypeBuilder Error Forward Child Reference. What condition will it trigger?

Open Guo-yyds opened this issue 1 year ago • 6 comments

When I write the test case, I want to test the type builder, but the above error occurs, and I don't understand it.

image image

Guo-yyds avatar Jul 09 '24 11:07 Guo-yyds

In general that means a type was not defined yet (not in a previous rec group, nor in the current one). But I can't tell from the screenshots why that happens in this case. If you can provide the source code of a full example that shows the issue that would help.

Another thing you can try is to make sure you are using a build of Binaryen with assertions enabled (as some internal error might be ignored, if assertions are off).

kripken avatar Jul 09 '24 17:07 kripken

c-api-ghn.txt The above is the source code for the test case.

Guo-yyds avatar Jul 10 '24 00:07 Guo-yyds

Hmm, I can't seem to find anything wrong here. The code seems reasonable and what it is doing is basically

(module
  (rec
    (type $struct (sub (struct (field (mut (ref null $struct)))
                               (field (mut (ref null $field))))))
    (type $field  (sub (struct (field (mut (ref null $field))))))
  )
)

Which is valid. But it does error as you said @Guo-yyds

@tlively is there some way to debug the TypeBuilder API? To get more detailed an error, or perhaps have it print out what it is building?

kripken avatar Jul 10 '24 23:07 kripken

There's a dump() method on TypeBuilder, but it's not currently exposed in the C API.

tlively avatar Jul 11 '24 01:07 tlively

So, this is Binaryen's fault? How should I handle this? Or what do you want the test cases to look like?

Guo-yyds avatar Jul 11 '24 04:07 Guo-yyds

Thanks @tlively !

I added a call to dump() in binaryen-c.cpp's TypeBuilderBuildAndDispose method, right before the build command. It prints

(type $0 (; temp ;) (sub (struct (field (mut (; temp ;) (ref null $0))) (field (mut (; temp ;) (ref null $1))))))
(type $1 (; temp ;) (sub (struct )))

Which is as expected. When I put it in a wat file surrounded by a rec group it validates.

But that reminded me that you do need to explicitly create rec groups, and that was the problem here. This fixes the testcase (before the build command):

TypeBuilderCreateRecGroup(builder, 0, 2);

kripken avatar Jul 11 '24 15:07 kripken