dfinity / motoko

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

bug: possible compiler crash from let x = class ... #4210

Open crusso opened 1 year ago

crusso commented 1 year ago

Tentative repro extracted from:

https://github.com/sardariuss/MotokoStableBTree/pull/4/files#diff-cf7292a6830f875cef1c27b6375d4e4ce3575115ac20a60cbecf68426206d030R67

import Region "mo:base/Region";
import Blob "mo:base/Blob";

module {

  public let STABLE_MEMORY = class(r : Region)  { // Suspect class
    public func size() : Nat64 { 
      Region.size(r); 
    };
    public func grow(pages: Nat64) : Int64 {
      let old_size = Region.grow(r, pages);
      if (old_size == 0xFFFF_FFFF_FFFF_FFFF){
        return -1;
      };
      Int64.fromNat64(old_size);
    };
    public func write(address: Nat64, bytes: [Nat8]) {
      Region.storeBlob(r, address, Blob.fromArray(bytes));
    };
    public func read(address: Nat64, size: Nat) : [Nat8] {
      Blob.toArray(Region.loadBlob(r, address, size));
    };
  };

};

crashes the compiler.

crusso commented 1 year ago

Whittled down to:

actor {

  let c = class()  { // Suspect class
  };

};
[nix-shell:~/motoko/test/run-drun]$ moc issue-4210.mo
Ill-typed intermediate code after Desugaring (use -v to see dumped IR):
issue-4210.mo:3.11-4.4: IR type error [M0000], subtype violation:
  ()
  () -> anon-class-3.11

Raised at Ir_def__Check_ir.error.(fun) in file "ir_def/check_ir.ml", line 95, characters 30-92
Called from Ir_def__Check_ir.check_exp.(<:) in file "ir_def/check_ir.ml" (inlined), line 369, characters 6-32
Called from Ir_def__Check_ir.check_exp in file "ir_def/check_ir.ml", line 705, characters 4-17
Called from Ir_def__Check_ir.check_dec in file "ir_def/check_ir.ml", line 1100, characters 4-21
Called from Stdlib__list.iter in file "list.ml", line 110, characters 12-15
Called from Ir_def__Check_ir.check_comp_unit in file "ir_def/check_ir.ml", line 1159, characters 4-23
Called from Ir_def__Check_ir.check_prog in file "ir_def/check_ir.ml", line 1178, characters 6-28
matthewhammer commented 1 year ago

Thanks for chasing this down! 🎉