You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Enginecore PowerShell engine, interpreter, and runtime
There was supposed to be a DefaultVisit type of method added to ICustomAstVisitor2 (with a default implementation) and AstVisitor2. It looks like that was missed.
Yeah sorry I guess I didn't make that clear enough. The big draw of it is that it lets the implementer decide how to deal with new AST types instead of blowing up. If the implementer can't implement that method then it's the same as having the default implementation be => null.
I think one question in making it public is, should it be the default implementation of all methods? I think it will be confusing if there's a public DefaultVisit() method that only works for some methods
Reacted by Patrick Meinecke and Rain Sallow (/u/ta11ow)
I think one question in making it public is, should it be the default implementation of all methods? I think it will be confusing if there's a public DefaultVisit() method that only works for some methods
I agree the DefaultVisit should be public and it should be added to AstVisitor2 as well (but, should a public version of it added to DefaultCustomAstVisitor and DefaultCustomAstVisitor2 instead?).
But I'm concerns about having all methods in ICustomAstVisitor2 and ICustomAstVisitor have default implementations.
In my opinion, interface is supposed to be as explicit as possible, and the abstract classes DefaultCustomAstVisitor and DefaultCustomAstVisitor2 are provided in case you only want to implement a few selected methods. If we choose to do this then DefaultCustomAstVisitor and DefaultCustomAstVisitor2 will become redundant and needless.
Comparing to DefaultCustomAstVisitor and DefaultCustomAstVisitor2. The benefit, I guess, is that a user can derive from ICustomAstVisitor2 and another class, and only implements a few selected methods. The drawback is, in my opinion, the compiler will not notify you when you miss implementations for any of those methods in ICustomAstVisitor2 and ICustomAstVisitor because you basically make the interface an abstract class.
But I'm concerns about having all methods in ICustomAstVisitor2 and ICustomAstVisitor have default implementations.
(...)
The drawback is, in my opinion, the compiler will not notify you when you miss implementations for any of those methods in ICustomAstVisitor2 and ICustomAstVisitor because you basically make the interface an abstract class.
Yeah that's fair. There's pros and cons to each and I don't really have a strong opinion either way, so whichever one y'all want to go with is fine with me.
Interfaces already have members that don't appear everywhere, and
Interfaces can be used as mixins (makes me think a different declaration would be nice to unlock these behaviours to force our hand)
We've already lost the compiler check on implementations; for that we need ICustomAstVisitor3 with no default implementations.
So we've lost the important distinguishing property between the interface and the abstract class there, and we seem to be investing in the interface (because that's where breaking changes will occur if we do nothing), so we should just make all its methods use the default.
Otherwise we end up with an API that ages more and more badly as we add new AST types, since there will be more and more methods that happen to have a default because the defaults were added at some language version; the nice thing about ICustomAstVisitor[n] is that we know there are n versions of the AST API, but with an interface it will one day just look like some methods use the default and others don't.
Reacted by Patrick Meinecke and Rain Sallow (/u/ta11ow)
The PR is out #13258. I decided to not mark DefaultCustomAstVisitor and DefaultCustomAstVisitor2 obsolete for 3 reasons:
Even if we use error: false, it's a breaking change if a build is configured to be "treat warning as error"
Update code that use DefaultCustomAstVisitor to ICustomAstVisitor is not simply change the type to be derived, but you also need to change each method to remove the override keyword.
Default implementation of an interface is treated as explicit implementation, meaning that for A : ICustomAstVisitor, unless A explicitly implement a method, that method is hidden from an instance of A (unless casting the instance to ICustomAstVisitor). Usually such a visitor will be passed to Ast.Accept which will be cast to ICustomAstVisitor anyway, so I'm uncertain how many uses will directly call the methods on the instance of A. But it's better to not break that.
Thinking on it a bit more, I'm leaning on the side of adding the obsolete attribute:
It's a dev-time thing, so it's not going to suddenly appear for lots of people in the middle of doing something unrelated
It's a warning, and developers deliberately choose whether that breaks their build or not, they can choose to suppress it too
It's pretty easy to address this warning; migrating to the preferred API is not a major cross-cutting shift, just a move from one implementing class to another that still gets used and passed around the same way
We need to take every opportunity to notify developers about what we expect from them, otherwise we may cause much subtler, nastier things down the track; we must be cruel to be kind
Developers targeting 7.1 are targeting a higher-cadence release, and are probably set up to manage changes like this. And I think part of the reason for targeting a newer, non-LTS release is to pick up changes and adapt to them quickly
Reacted by Patrick Meinecke and Rain Sallow (/u/ta11ow)
Default implementation of an interface is treated as explicit implementation, meaning that for A : ICustomAstVisitor, unless A explicitly implement a method, that method is hidden from an instance of A (unless casting the instance to ICustomAstVisitor). Usually such a visitor will be passed to Ast.Accept which will be cast to ICustomAstVisitor anyway, so I'm uncertain how many uses will directly call the methods on the instance of A. But it's better to not break that.
^ I want to call out again this behavior change MAY affect user's code.
Today, for A: DefaultCustomAstVisitor, the user can run objOfA.VisitSwitchStatement(xxx) even if A doesn't override/implement VisitSwitchStatement, but with A : ICustomAstVisitor the user won't be able to do so.
I see the work to add DefaultVisit an enhancement to allow a developer to do things more easily, no matter the developer is using ICustomAstVisitor or DefaultCustomAstVisitor.
Personally, I still think that adding the obsolete attribute is a breaking change with little or maybe no value.
I don't see using the interface offers you any extra benefits over the other, except for allowing you to derive from another class at the same time, which is not needed anyway for code that is using DefaultCustomAstVisitor today.
You may argue that having 2 types doing similar things is not optimal. I agree it's not optimal, but they have been there for a long time and I don't think it really confuses anyone. For a user that uses DefaultCustomAstVisitor today, there is nothing bad for them to continue using it.
The only benefit of deprecating DefaultCustomVisitor/DefaultCustomVisitor2 that I can see is we don't need to update DefaultCustomVisitor2 when adding a new AST type. But it's trivial to keep that type update-to-date -- just adding a new template-like virtual method and that's all.
In base library we could define only interfaces to avoid complication in support and confusing consumers. Helper classes like DefaultCustomAstVisitor developers can define in their projects if they need.
I realize I'm late to the party, but basically due to (not actually so non-)breaking modifications made to ICustomAstVisitor2 between 6 and 7, my ICustomAstVisitor2 (written for S.M.A 5.x/6.x) compiles against S.M.A 7.x without error - but then fails with contextually nonsensical exceptions at runtime whenever an AST contains any of the new conditional operators because I haven't provided an implementation (because that would make the entire class implementation invalid pre-7, since the injected AST types don't exist) 😒
In an ideal world we would have extended the interfaces and base classes with AstVisitor3 and ICustomAstVisitor3, so that I could have simply done:
# Root module always imports this one
class MyVisitor2 : ICustomAstVisitor2
{
<# all 61 ICustomAstVisitor + ICustomAstVisitor2 declared Visit methods implemented here #>
}
# Root module only imports this one when major version -ge 7
class MyVisitor3 : MyVisitor2, ICustomAstVisitor3
{
<# only need 2 additional method implementations here #>
}
... and then instantiate the version-appropriate visitor type in my commands at runtime - fairly easy to maintain.
But now that we've apparently decided to ignore the O in SOLID, I have to maintain:
# Root module only imports this one when major version -lt 7
class MyVisitor2 : ICustomAstVisitor2
{
<# all 61 ICustomAstVisitor + ICustomAstVisitor2 declared Visit methods implemented here #>
}
# Root module only imports this one when major version -ge 7
class MyVisitor3 : MyVisitor2, ICustomAstVisitor3
{
<# all 61 ICustomAstVisitor + ICustomAstVisitor2 declared Visit methods implemented here, again #>
<# 2 additional ICustomVisitor2 method implementations here #>
}
... lest I want to turn the entire type definition into a web of code generation - in both cases not very maintainable anymore.
I appreciate that the visitor model as architected here isn't really super fit for incrementally evolving the language, and I don't expect this particular design decision to be reverted, but please keep the above in mind for the next time we need to expand the language grammar, because this sucks
In an ideal world we would have extended the interfaces and base classes with AstVisitor3 and ICustomAstVisitor3, so that I could have simply done:
Honestly I think the prospect of adding more of these versioned visitors is the reason we didn't get any new AST types for so long. DIM makes adding new ones much more palatable imo.
re: the issue with non-sensical exceptions, that's what DefaultVisit was added for (granted, not yet helpful). That will allow us to choose how we want to handle new AST types. In the future we can polyfill AST types to older versions, ignore, throw a custom exception, or use reflection to find child nodes.
... and then instantiate the version-appropriate visitor type in my commands at runtime - fairly easy to maintain.
If you swap ICustomAstVisitor2 for DefaultCustomAstVisitor2 you can still do that since the DIM's will be virtual, e.g.
Sorry I did it that way for brevity, but I meant put it in a different file and conditionally dot source it. The same way you'd have to do it if it were ICustomAstVisitor3.
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
iSazonov commentedon Mar 9, 2020
For reference #10859.
vexx32 commentedon Jul 22, 2020
Rob Holt (@rjmholt) Joey Aiello (@joeyaiello) Dongbo Wang (@daxian-dbw) this should probably be prioritised for 7.1 if not earlier; it will significantly complicate solutions that need to be compatible with more than one version, and the problem will get worse over time. (see also: https://twitter.com/IISResetMe/status/1286005066311372800?s=20)
rjmholt commentedon Jul 22, 2020
Original discussion: #9849 (comment)
If you've got links to other discussions on this, please feel free to add them
rjmholt commentedon Jul 22, 2020
This has been in the code since #10367:
PowerShell/src/System.Management.Automation/engine/parser/AstVisitor.cs
Lines 176 to 180 in a6bd47f
SeeminglyScience commentedon Jul 22, 2020
Oh it's...private?
PowerShell/src/System.Management.Automation/engine/parser/AstVisitor.cs
Line 153 in a6bd47f
daxian-dbw commentedon Jul 22, 2020
Patrick Meinecke (@SeeminglyScience) Do you think it should be
public?SeeminglyScience commentedon Jul 22, 2020
Yeah sorry I guess I didn't make that clear enough. The big draw of it is that it lets the implementer decide how to deal with new AST types instead of blowing up. If the implementer can't implement that method then it's the same as having the default implementation be
=> null.rjmholt commentedon Jul 22, 2020
I think one question in making it public is, should it be the default implementation of all methods? I think it will be confusing if there's a public
DefaultVisit()method that only works for some methodsSeeminglyScience commentedon Jul 22, 2020
Good idea, yeah that would be great.
daxian-dbw commentedon Jul 22, 2020
I agree the
DefaultVisitshould be public and it should be added toAstVisitor2as well (but, should a public version of it added toDefaultCustomAstVisitorandDefaultCustomAstVisitor2instead?).But I'm concerns about having all methods in
ICustomAstVisitor2andICustomAstVisitorhave default implementations.In my opinion, interface is supposed to be as explicit as possible, and the abstract classes
DefaultCustomAstVisitorandDefaultCustomAstVisitor2are provided in case you only want to implement a few selected methods. If we choose to do this thenDefaultCustomAstVisitorandDefaultCustomAstVisitor2will become redundant and needless.Comparing to
DefaultCustomAstVisitorandDefaultCustomAstVisitor2. The benefit, I guess, is that a user can derive fromICustomAstVisitor2and another class, and only implements a few selected methods. The drawback is, in my opinion, the compiler will not notify you when you miss implementations for any of those methods inICustomAstVisitor2andICustomAstVisitorbecause you basically make the interface an abstract class.SeeminglyScience commentedon Jul 22, 2020
Yeah that's fair. There's pros and cons to each and I don't really have a strong opinion either way, so whichever one y'all want to go with is fine with me.
5 remaining items
rjmholt commentedon Jul 23, 2020
Yeah I think in a world where:
We've already lost the compiler check on implementations; for that we need
ICustomAstVisitor3with no default implementations.So we've lost the important distinguishing property between the interface and the abstract class there, and we seem to be investing in the interface (because that's where breaking changes will occur if we do nothing), so we should just make all its methods use the default.
Otherwise we end up with an API that ages more and more badly as we add new AST types, since there will be more and more methods that happen to have a default because the defaults were added at some language version; the nice thing about
ICustomAstVisitor[n]is that we know there arenversions of the AST API, but with an interface it will one day just look like some methods use the default and others don't.daxian-dbw commentedon Jul 23, 2020
OK, it sounds there's a consensus :) I will go submit a PR that
DefaultVisitpublic inICustomAstVisitorICustomAstVisitorandICustomAstVisitor2have default implementation=> DefaultVisit(ast)AstVisitorandAstVisitor2.MarkDefaultCustomAstVisitorandDefaultCustomAstVisitor2obsolete.daxian-dbw commentedon Jul 23, 2020
The PR is out #13258. I decided to not mark
DefaultCustomAstVisitorandDefaultCustomAstVisitor2obsolete for 3 reasons:error: false, it's a breaking change if a build is configured to be "treat warning as error"DefaultCustomAstVisitortoICustomAstVisitoris not simply change the type to be derived, but you also need to change each method to remove theoverridekeyword.A : ICustomAstVisitor, unless A explicitly implement a method, that method is hidden from an instance of A (unless casting the instance toICustomAstVisitor). Usually such a visitor will be passed toAst.Acceptwhich will be cast toICustomAstVisitoranyway, so I'm uncertain how many uses will directly call the methods on the instance of A. But it's better to not break that.iSazonov commentedon Jul 24, 2020
Developers can suppress the warning. Notification is better because developers can skip the warning in docs.
Although we have a separate discussion, I think we should take the next steps in time:
rjmholt commentedon Jul 24, 2020
Thinking on it a bit more, I'm leaning on the side of adding the obsolete attribute:
daxian-dbw commentedon Jul 25, 2020
^ I want to call out again this behavior change MAY affect user's code.
Today, for
A: DefaultCustomAstVisitor, the user can runobjOfA.VisitSwitchStatement(xxx)even ifAdoesn't override/implementVisitSwitchStatement, but withA : ICustomAstVisitorthe user won't be able to do so.I see the work to add
DefaultVisitan enhancement to allow a developer to do things more easily, no matter the developer is usingICustomAstVisitororDefaultCustomAstVisitor.Personally, I still think that adding the obsolete attribute is a breaking change with little or maybe no value.
I don't see using the interface offers you any extra benefits over the other, except for allowing you to derive from another class at the same time, which is not needed anyway for code that is using
DefaultCustomAstVisitortoday.You may argue that having 2 types doing similar things is not optimal. I agree it's not optimal, but they have been there for a long time and I don't think it really confuses anyone. For a user that uses
DefaultCustomAstVisitortoday, there is nothing bad for them to continue using it.The only benefit of deprecating
DefaultCustomVisitor/DefaultCustomVisitor2that I can see is we don't need to updateDefaultCustomVisitor2when adding a new AST type. But it's trivial to keep that type update-to-date -- just adding a new template-like virtual method and that's all.rjmholt commentedon Jul 26, 2020
Ok, I'm happy with there being two types
iSazonov commentedon Jul 27, 2020
In base library we could define only interfaces to avoid complication in support and confusing consumers. Helper classes like
DefaultCustomAstVisitordevelopers can define in their projects if they need.I think we will come to this in the future.
IISResetMe commentedon Aug 15, 2020
I realize I'm late to the party, but basically due to (not actually so non-)breaking modifications made to
ICustomAstVisitor2between 6 and 7, myICustomAstVisitor2(written for S.M.A 5.x/6.x) compiles against S.M.A 7.x without error - but then fails with contextually nonsensical exceptions at runtime whenever an AST contains any of the new conditional operators because I haven't provided an implementation (because that would make the entire class implementation invalid pre-7, since the injected AST types don't exist) 😒In an ideal world we would have extended the interfaces and base classes with
AstVisitor3andICustomAstVisitor3, so that I could have simply done:... and then instantiate the version-appropriate visitor type in my commands at runtime - fairly easy to maintain.
But now that we've apparently decided to ignore the O in SOLID, I have to maintain:
... lest I want to turn the entire type definition into a web of code generation - in both cases not very maintainable anymore.
I appreciate that the visitor model as architected here isn't really super fit for incrementally evolving the language, and I don't expect this particular design decision to be reverted, but please keep the above in mind for the next time we need to expand the language grammar, because this sucks
SeeminglyScience commentedon Aug 15, 2020
Honestly I think the prospect of adding more of these versioned visitors is the reason we didn't get any new AST types for so long. DIM makes adding new ones much more palatable imo.
re: the issue with non-sensical exceptions, that's what
DefaultVisitwas added for (granted, not yet helpful). That will allow us to choose how we want to handle new AST types. In the future we can polyfill AST types to older versions, ignore, throw a custom exception, or use reflection to find child nodes.If you swap
ICustomAstVisitor2forDefaultCustomAstVisitor2you can still do that since the DIM's will be virtual, e.g.Ideally PowerShell classes would implement all DIM's as virtual methods that call their default impl, then you wouldn't need that.
Also in C# you can do the same or use compiler directives/partial classes.
IISResetMe commentedon Aug 16, 2020
Patrick Meinecke (@SeeminglyScience) That would be great, but unfortunately not viable - we generate and emit all type definitions and
usingbindings as one of the first compilation step, so dot-sourcing a script simply containing a class definition with a non-existing parameter type will fail long before we can evaluateif ($PSVersionTable.PSVersion.Major -ge 7)SeeminglyScience commentedon Aug 16, 2020
Sorry I did it that way for brevity, but I meant put it in a different file and conditionally dot source it. The same way you'd have to do it if it were
ICustomAstVisitor3.daxian-dbw commentedon Aug 16, 2020
The polyfill idea is pretty neat 👍