HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.18k stars 656 forks source link

macro error not easy to debug... #2480

Closed delahee closed 10 years ago

delahee commented 10 years ago

Hi,

I often struggle with macro but that one is pretty often happening:


class Rsc 
{
    public function new() {

    }

    macro static public function hello() {
        return macro "Hello World" + haxe.Timer.stamp();
    }
}

class Main 
{

    static function main() 
    {
        var r = new Rsc();
        trace( Rsc.hello() );
        //#if !macro
        haxe.Timer.delay( function() trace("truc2"), 100);
        //#end
    }

}

Here the compiler yields : src/Main.hx:24: characters 2-18 : haxe.#Timer has no field delay

uncomment the #if macro does the trick but the error is a strong misleader in my opinion and shoul reads

src/Main.hx:24: characters 2-18 : haxe.#Timer has no field delay in the macro context.

Thanks !

Simn commented 10 years ago

That's not macro-specific, it comes from neko not having that function and macros running in neko mode. We can't easily improve the general error message either because conditional compilation happens during parsing, so there's no way to detect that the function would be available on other targets/with other compilation flags.

delahee commented 10 years ago

Thx for answering swiftly but I have to persevere.

Can't you detect the current compilation stage to hint the user ?

by the way

import haxe.io.Bytes;
import haxe.macro.Context;
import haxe.Resource;

class Rsc 
{
    public function new() {

    }

    macro static public function hello() {
        Context.addResource("arbre", Bytes.ofString("sapin"));
        return macro "Hello World" + haxe.Timer.stamp();
    }

    static public function get() {
        return Resource.getString("arbre");
    }
}

class Main 
{

    static function main() 
    {
        var r = new Rsc();
        trace( Rsc.hello() );

        #if !macro
        haxe.Timer.delay( function() trace("truc2"), 100);
        trace(Rsc.get());
        #end
    }

}

gives me the same error on hxcpp (latest haxe compiler from dev branch ) and I have the macro flag... :) (commenting the haxe.time.delay call allows compilation to finish) So on cpp I have an obscure error and a macro issue...

Simn commented 10 years ago

Unfortunately we cannot assume that the error comes from target/compilation flags because most of the time it doesn't. To the typer it's no different from trying to access a field which actually doesn't exist.

I don't understand your subsequent report, the delay method isn't defined for Cpp target either.

delahee commented 10 years ago

ok my bad, i am too used to openfl :)

2013/12/19 Simon Krajewski notifications@github.com

Unfortunately we cannot assume that the error comes from target/compilation flags because most of the time it doesn't. To the typer it's no different from trying to access a field which actually doesn't exist.

I don't understand your subsequent report, the delay method isn't defined for Cpp target either.

— Reply to this email directly or view it on GitHubhttps://github.com/HaxeFoundation/haxe/issues/2480#issuecomment-30928300 .

David Elahee