Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macro] no private field control for exprs generated with macro #3714

Closed
nadako opened this issue Dec 25, 2014 · 26 comments
Closed

[macro] no private field control for exprs generated with macro #3714

nadako opened this issue Dec 25, 2014 · 26 comments
Assignees
Labels
bug platform-macro Everything related to Haxe macros
Milestone

Comments

@nadako
Copy link
Member

nadako commented Dec 25, 2014

I feel like this should fail to compile with Cannot access private field id error, shouldn't it?

class A {
    var id:Int;

    public function new() {}
}

class Main {
    static function main() {
        var a = new A();
        f(a);
    }

    static macro function f(v) {
        return macro $v.id = 5;
    }
}
@nadako nadako added bug platform-macro Everything related to Haxe macros labels Dec 25, 2014
@Simn
Copy link
Member

Simn commented Dec 25, 2014

That's the @:privateAccess feature which was added for expressions returned by macros to deal with... some problem that I don't recall right now because it's christmas and you should stop opening issues. :P

@Simn Simn closed this as completed Dec 25, 2014
@nadako
Copy link
Member Author

nadako commented Dec 25, 2014

Haha, we celebrate christmas two weeks later here so I had no idea :-P Anyway, it looks quite sloppy to me having @:privateAccess by default in all macros, because when you actually need those checks (like in my case), you have to do them by hand with complex macro code. Maybe we could at least have @:nonPrivateAccess?

@ncannasse
Copy link
Member

That's indeed an issue, I think we should keep it open :)

@ncannasse ncannasse reopened this Dec 25, 2014
@Simn
Copy link
Member

Simn commented Dec 25, 2014

We designed it like this on purpose, but I really don't remember why. Chances are @back2dos was involved.

@back2dos
Copy link
Member

I think this came up because of how static extension macros worked, i.e. round tripping expressions through the typer, which meant that for example a field read access would become a call to the private getter, so when you returned that back, you got type errors from the private access. However because since 3.1.0 the expressions are handled differently I would speculate that this is now obsolete.

If this is was the only reason, then the @:privateAccess should only have been added for static extension macros and everything would function correctly. So there may be other causes.

Personally, I always add @:privateAccess by hand when required, because I do most of my twisted experiments in build macros anyway. So my suggestion is to get rid of it and see if anyone notices ;)

@Simn
Copy link
Member

Simn commented Dec 26, 2014

Static extensions were the only reason I could think of too, and I agree with your conclusion. Thanks for remembering!

@Simn
Copy link
Member

Simn commented Mar 26, 2015

I wonder if we should revert this. It seems to cause a bit of confusion because it's hard to tell where the problem comes from.

@Simn Simn reopened this Mar 26, 2015
@Simn Simn added this to the 3.2 milestone Mar 26, 2015
@Simn Simn self-assigned this Mar 26, 2015
@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

Could we then add @:noPrivateAccess or something to enable access checks explicitly?

@Simn
Copy link
Member

Simn commented Mar 26, 2015

I'm not sure what that would actually improve...

@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

I remember writing some syntax-sugar macros where I didn't want to loose access control to keep things error-proof, that's why I reported this issue. I don't remember the exact case, but I'm sure there are plenty of use cases.

@Simn
Copy link
Member

Simn commented Mar 26, 2015

Yeah but that shouldn't be about specific types. If anything we should have a -D flag for this.

We could go for a general -D transition flag which enables behavior that we are plan to make the default in the future. That could also be used for the is problem.

@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

Actually, I remember you writing a macro that allows to do:

myObj.with(
    a = 1,
    doStuff()
);

which is then translated to

myObj.a = 1;
myObj.doStuff();

It would make perfect sense to keep access restrictions in such case.

@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

I don't follow. Do you want to keep old "everything is public within a macro" behaviour, but hide it behind -D transition?

@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

(also I'm worried about libraries defining that flag with extraParams.hxml)

@Simn Simn assigned nadako and unassigned Simn Mar 26, 2015
@Simn
Copy link
Member

Simn commented Mar 26, 2015

I meant the opposite. Do I really have to worry for every single flag about libraries possibly defining them? There's always the option to tell such a library author that he's being an idiot.

Anyway, I have reverted the change, feel free to add whatever you feel is right but please keep this as the default.

@DanielUranga
Copy link
Contributor

Maybe improving the error report suggesting to use "@:privateAccess". Oh too late, not the default now.

@nadako nadako closed this as completed in f9ab409 Mar 26, 2015
@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

Just after I pushed the @:noPrivateAccess I thought about the awkward case like @:privateAccess someMacroThatDoesNoPrivateAccess(), but that's probably for macro authors to deal with.

@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

I still think it's weird to implicitly lose access control when calling a macro by default. Macros are supposed to be ast transformations and having that side effect is unexpected.

@nadako
Copy link
Member Author

nadako commented Mar 26, 2015

By the way, I noticed that @:privateAccess from macro doesn't get applied in interpreter mode.

E.g. this will fail with Cannot access private field a when run with --run Main

class A {
    static var a:Int;
}

class Main {
    static function main() {
        m();
    }

    static macro function m() return macro A.a;
}

@Simn
Copy link
Member

Simn commented Mar 26, 2015

I don't disagree but it still presents a problem that is really hard to track down, especially for mere users of a macro (library).

@back2dos
Copy link
Member

Firstly, I think removing this auto-generated @:privateAccess is the right
move. I was the one who requested it for the reasons already explained.
It's not like you couldn't work around it at the time either (this was
before EMeta):
https://github.com/back2dos/tinkerbell/blob/cf08e70d2f6c68f8d70fe1d1afd6f2ab306cf392/src/tink/macro/tools/ExprTools.hx#L37

Given the circumstances, I thought it would be the best thing we could
offer other library authors. The circumstances have changed, and now it
doesn't make sense to do this anymore.

Secondly, macro libraries are still very exotic and largely only used by
early adopters. Now is the best time to get it right, I think. Haxe 4 would
be the next best opportunity. But given how few people it would affect
right now, I think now is a better occasion.

From Haxe 3 on, every release broke something about macros. And I think it
was worth it. I don't think this will create more problems than the changes
to static extension macros did. That really made quite a lot of stuff blow
up in my face I can tell you. If you have
someObj.someField.bindExpr(theExpr) and you get an error around
someObj.someField telling you "field access expected", then THAT is what
I'd call a confusing error message ;)

The @:noPrivateAccess thing is still pretty useful I think, but
auto-@:privateAccess
should be discontinued, if you ask me. I think it's fair enough to expect
the library maintainers to deal with it.

On Thu, Mar 26, 2015 at 5:12 PM, Simon Krajewski notifications@github.com
wrote:

I don't disagree but it still presents a problem that is really hard to
track down, especially for mere users of a macro (library).


Reply to this email directly or view it on GitHub
#3714 (comment)
.

@ncannasse ncannasse reopened this Mar 28, 2015
@ncannasse
Copy link
Member

I don't agree with the revert of the change : I think that we should not add @:privateAccess and let the macro authors deal with the change, which was a "bug" IMHO to let accessing all privates as soon as you had an expr returned from macro.

@Simn
Copy link
Member

Simn commented Mar 28, 2015

It was not a bug, it was a deliberate implementation in the past and just because nobody remembers the exact reasoning doesn't mean that we can just discard it. There have been multiple reports of it in the RC phase and most users who are affected by this cannot fix it by themselves. This is very frustrating and adds to the perception of Haxe being unstable.

@ncannasse
Copy link
Member

Yes I understand how it was not a bug, but if we are planning to change that at some point (and we do), the the sooner the better. Haxe is growing, and the number of users/libraries as well. So let's make the change asap and help library authors to fix their code, or else it will be even harder and will affect even more users to do that for the next version.

Simn added a commit that referenced this issue Mar 28, 2015
@Simn
Copy link
Member

Simn commented Mar 28, 2015

I lack the energy to discuss this further. I have reverted the change.

@Simn Simn closed this as completed Mar 28, 2015
@nadako
Copy link
Member Author

nadako commented Mar 28, 2015

That was unexpected, but I'm certainly fine with it :-) I mentioned the change in https://github.com/HaxeFoundation/haxe/wiki/Breaking-changes-in-Haxe-3.2.0#macro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug platform-macro Everything related to Haxe macros
Projects
None yet
Development

No branches or pull requests

5 participants