Pauan/rollup-plugin-purs

assumePureVars breaks Data.StrMap.insert (via Data.StrMap.mutate)

FrigoEU opened this issue · 1 comments

Hey, I'm loving the plugin and all the impressive optimizations. When running with the default configuration, I found a problem in Data.StrMap, namely when trying to insert something. underlying, this uses the function mutate, which is actually causing the problem. The PS code is quite complicated imo, but that's because it needs to be useful for STStrMaps aswell. Anyway here is the PS code:

-- | Insert or replace a key/value pair in a map
insert :: forall a. String -> a -> StrMap a -> StrMap a
insert k v = mutate (\s -> void $ SM.poke s k v)

mutate :: forall a b. (forall h e. SM.STStrMap h a -> Eff (st :: ST.ST h | e) b) -> StrMap a -> StrMap a
mutate f m = pureST do
  s <- thawST m
  _ <- f s
  pure s

This gets compiled to:

var mutate = function (f) {
    return function (m) {
        return pureST(function __do() {
            var v = thawST(m)();
            var v1 = f(v)();
            return v;
        });
    };
};

With assumePureVars set to false, mutate becomes this: (thawST gets inlined with _copyEff)

  var _mutate_uncurried = function (f, m) {
    return pureST$1(function __do() {
      var v = _copyEff(m)();
      f(v)();
      return v;
    });
  };

When assumePureVars is try, the f(v)(); gets stripped. I'm not sure if this is a bug or intene, because I don't really understand (yet?) what assumePureVars means. I thought I'd mention it, since it's an often used function and there is not FFI stuff going on in/around it.

I think assumePureVars should maybe make an exception when we're "in Eff"? Though I'm not sure how we can detect that.

My bad, this is a duplicate of #16 .