Open benmerckx opened 4 years ago
I guess the issue is that onGenerate
macros get executed before DCE (check the line numbers):
https://github.com/HaxeFoundation/haxe/blob/87ceafc689e36cec327923f24a709adbfd85c6cf/src/filters/filters.ml#L881
https://github.com/HaxeFoundation/haxe/blob/87ceafc689e36cec327923f24a709adbfd85c6cf/src/filters/filters.ml#L911
@Simn Do you know why do we have it before dce?
I know absolutely nothing about custom JS generators.
I guess the issue is that onGenerate macros get executed before DCE (check the line numbers):
The js generation is actually called at a later stage than onGenerate:
#if macro
import haxe.macro.Compiler;
import haxe.macro.JSGenApi;
import haxe.macro.Context;
#end
typedef Unused = UnusedConcrete;
class UnusedConcrete {}
class Main {
#if macro
static function generate(api: JSGenApi) {
trace('js generate');
for (type in api.types)
switch type {
case TInst(_.get() => {name: name}, _) | TType(_.get() => {name: name}, _):
if (name.substr(0, 2) == 'Un')
trace(name);
default:
}
}
public static function use() {
Context.onGenerate(_ -> trace('onGenerate'));
Compiler.setCustomJSGenerator(generate);
}
#end
static function main() {
trace('hello');
}
}
$ haxe --macro Main.use() --main Main --js out.js
Main.hx:24: onGenerate
Main.hx:14: js generate
Main.hx:19: Unused
Main.hx:19: UnusedConcrete
With dce the unused class UnusedConcrete
is not passed to the generate call, but the typedef referencing it is:
$ haxe --macro Main.use() --main Main --js out.js --dce full
Main.hx:24: onGenerate
Main.hx:14: js generate
Main.hx:19: Unused
When enabling DCE, a custom js generator receives only those concrete types that need to be generated. However it also receives every typedef included in the class path (even in unused modules). I'm generating Typescript definitions - and some of these typedefs reference concrete types that are not part of the generation (making Typescript upset). So I'm looking to either have unused typedefs not pass through to the
generate
call or for a way to check during generation if a typedef is in use?