toit icon indicating copy to clipboard operation
toit copied to clipboard

Type-inference tests fail with inheritance test

Open floitsch opened this issue 1 year ago • 5 comments

I get the following error:

1/2 Test  #738: tests/negative/inheritance-test.toit ...   Passed    0.07 sec
1137: fatal: didn't expect 24 at 3238

floitsch avatar Jul 19 '24 15:07 floitsch

The test fails here: https://github.com/toitlang/toit/blob/a8a5a92a23abbc9db6cd09df7959f1700e1c9237/tests/toitdoc/inheritance.toit#L46

Run with:

git checkout type-inference-test-failure
rm -rf build/host--ctp
LOCAL_CXXFLAGS="-O0" make HOST=host-ctp TOOLCHAIN=host TOIT_CHECK_PROPAGATED_TYPES=1 sdk
ctest --test-dir build/host-ctp --verbose -C slow -R toitdoc/inheritance-test

The LOCAL_CXXFLAGS="-O0" is optional.

floitsch avatar Jul 19 '24 15:07 floitsch

Once fixed please change the code (master/PR) back to using a block.

floitsch avatar Jul 19 '24 15:07 floitsch

Looking into this now.

Running:

$ build/host/sdk/bin/toit.compile -w snapshot -Xpropagate tests/toitdoc/inheritance-test.toit
$ build/host-ctp/sdk/bin/toit.run snapshot

leads to the fatal: didn't expect 24 at 3238 message. Digging into the snapshot, we see that class 24 is String_:

$ build/host/sdk/bin/toit.run tools/toitp.toit -c snapshot
Classes[63]:
  0: Object                          /Users/kasper/Toit/toitlang.org/toit/build/host/sdk/lib/core/objects.toit:14:7
... 
 24: String_                         /Users/kasper/Toit/toitlang.org/toit/build/host/sdk/lib/core/string.toit:1368:7
...

The bytecode at 3238 is loading an outer local in a block from within parse-module:

$ build/host/sdk/bin/toit.run tools/toitp.toit -bc snapshot
...
3233: [block] in parse-module tests/toitdoc/inheritance.toit:46:19
  0/3237 [016] - load local 2
  1/3238 [005] - load outer S4
  3/3240 [083] - branch if false T139
...

The corresponding source code is:

  finish-class := :
    if current-class-name:

So we infer that current-class-name cannot be a string, but yet it is. The propagated types confirm this:

$ build/host/sdk/bin/toit.compile -w snapshot -Xpropagate tests/toitdoc/inheritance-test.toit > types.txt
$ build/host/sdk/bin/toit.run tools/dump_types.toit --snapshot snapshot --types types.txt
...
[block] in parse-module tests/toitdoc/inheritance.toit
 - argument 0: [block]
  0[016] - load local 2
  1[005] - load outer S4 // {Null_}
  3[083] - branch if false T139
...

Now looking into what may cause us to not see the assignment to the local -- or to not reanalyze the block after the type information has been extended.

kasperl avatar Oct 07 '24 08:10 kasperl

This smaller example illustrates the problem:

main:
  x := null
  b := : if x: print "hello"
  x = 1
  b.call

The assignment to x does not force us to re-analyze the b block, but it really should.

kasperl avatar Oct 07 '24 08:10 kasperl

Addressed by #2597.

kasperl avatar Oct 22 '24 10:10 kasperl