temperlang / temper

3 stars 0 forks source link

Bubbling breaks top-level sorting #104

Closed tjpalmer closed 8 months ago

tjpalmer commented 8 months ago

Here's a case I'm adding to TmpLBackendTest for now, although maybe the impact is in frontend stages:

let fuji = new Apple().maybe();
let gala = new Apple().maybe();
export class Apple {
  public maybe(): Apple | Bubble { this }
}

That currently renders to this TmpL:

//// cycles.temper => cycles.tmpl
@fail var fail#0: Boolean;
@fail var fail#1: Boolean;
let fuji__0: Apple;
module init {
  moduleExit#0: {
    fuji__0 = hs (fail#0, /*new*/ Apple().maybe());
    if (! fail#0) {
      let gala__0: Apple;
      gala__0 = hs (fail#1, /*new*/ Apple().maybe());
      if (! fail#1) {
        <garbage "CombinedTypeDefinition cannot convert to expression">;
        break moduleExit#0;
      }
    }
    abortLoad;
  }
}

Things are better in any of these cases, although some of these cases are better than others:

Oddly enough, some (all?) backends (also adding a LuaBackendTest case) seem to sort even a single apple instance to before the class definition, even though I'm not sure I see that in TmpLBackendTest. Here's the Lua case. This Temper:

let fuji = new Apple().maybe();
export class Apple {
  public maybe(): Apple | Bubble { this }
}

Goes to this Lua:

local temper = require('temper-core/prelude');
local fuji__0, Apple, exports;
fuji__0 = nil;
fuji__0 = Apple():maybe();
Apple = temper.type('Apple');
Apple.methods.maybe = function(this__0)
  return this__0;
end;
Apple.constructor = function(this__1)
  return nil;
end;
exports = {};
exports.Apple = Apple;
return exports;
tjpalmer commented 8 months ago

Turns out bubble doesn't really break it. Bubble just prevents auto fixing of it. We haven't included type definitions in module sorting, just the declarations. Because type definitions look like this rather than being assignments to LeftNameLeaf nodes like other things are:

  (Call
    (R class)
    (V \word)
    (V \Apple)
    (V \concrete)
    (V true)
    (Fun
      (V \typeDefined)
      (V Apple)
...