HaxeFoundation / HaxeFoundation/haxe

[dce] unification with an anon struct makes dce to keep unused class fields.

Open
#9,185 1 comment 2 reactions 1 assignee Claimed by @RealyUniqueName View on GitHub
bug feature-dce
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

```haxe
class Main {
static var tmp:Int;
static public function main() {
tmp = Cls.field;
}
}

abstract Abstr(Cls) to Iterable {}

class Cls {
static public var field:Int;

//this field is never used
public function iterator():Iterator {
return null;
}
}
```
Generated js:
```js
var Main = function() { };
Main.main = function() {
Main.tmp = Cls.field;
};
var Cls = function() { };
Cls.prototype = {
//this field is kept, but never used
iterator: function() {
return null;
}
};
```
Removing `to Iterable` makes `Cls.iterator` disappear from the output.

The issues is caused by these lines:
https://github.com/HaxeFoundation/haxe/blob/282df7714cd8fe82da70831ecdade098d3a3379e/src/core/tUnification.ml#L553-L555

I think `@:maybeUsed` should anly be added upon actual use of `to Iterable` in expressions. Currently it is triggered upon checking the compatibility of the underlying type with `to` types.

This issue is the reason why `haxe.iterator.ArrayIterator` is always kept even in a simple "hello world" sample:
https://github.com/HaxeFoundation/haxe/blob/282df7714cd8fe82da70831ecdade098d3a3379e/std/haxe/ds/ReadOnlyArray.hx#L34

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.