X Tutup
The Wayback Machine - https://web.archive.org/web/20240501030207/https://github.com/PowerShell/PowerShell/pull/21331
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

Fix PowerShell class to support deriving from an abstract class with abstract properties #21331

Merged
merged 2 commits into from Mar 13, 2024

Conversation

daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented Mar 12, 2024

PR Summary

Fix PowerShell class to support deriving from an abstract class with abstract properties.
The fix is to detect that we are deriving from an abstract base type, and the property is an implementation of an abstract property from that base type.

PR Context

The following PowerShell class stops working in PowerShell v7.4.1, because .NET 8 enforces stricter validation on the dynamically emitted IL code. Previously, for a property that is supposed to be the implementation of an abstract property in base type, we don't set the Reflection.MethodAttributes.Virtual attribute when creating the property. That actually makes the created property have nothing to do with the abstract property in base type. Prior to .NET 8, the IL emitting library doesn't detect this error even though the abstract property is not implemented. However, starting from .NET 8, it detects the issue and throw exception in this case.

Running the following PowerShell class will result in an error in v7.4.1

class nxFileSystemInfo : System.IO.FileSystemInfo
{
    [string] $Name
    [bool] $Exists

    [void] Delete()
    {
    }
}
ParserError:
Line |
   1 |  class nxFileSystemInfo : System.IO.FileSystemInfo
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Error during creation of type "nxFileSystemInfo". Error message: Method 'get_Name' in type 'nxFileSystemInfo' from assembly 'PowerShell Class
     | Assembly, Version=1.0.0.2, Culture=neutral, PublicKeyToken=null' does not have an implementation.

PR Checklist

This PR has 31 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +26 -5
Percentile : 12.4%

Total files changed: 2

Change summary by file extension:
.cs : +9 -5
.ps1 : +17 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@jborean93
Copy link
Collaborator

jborean93 commented Mar 12, 2024

It would be great if you could look at #21062 and #21061 which is still pending a review and tried to fix somewhat related problems to this.

@daxian-dbw
Copy link
Member Author

@jborean93 Will do a review later today or tomorrow. Thanks for the reminder!

@daxian-dbw daxian-dbw self-assigned this Mar 12, 2024
@daxian-dbw daxian-dbw added CL-Engine Indicates that a PR should be marked as an engine change in the Change Log BackPort-7.4.x-Consider labels Mar 12, 2024
@@ -628,3 +628,33 @@ class Derived : Base
$sb.Invoke() | Should -Be 200
}
}

Describe 'Base type has abstract properties' -Tags "CI" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have validation which ensures that an error is produced if the abstract property is not defined? is it needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will add one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test for the error case was added.


if (_typeBuilder.BaseType.IsAbstract)
{
foreach (var property in _typeBuilder.BaseType.GetProperties())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will need to include BindingFlags.NonPublic as protected members should still be found right? Likewise GetAccessors below likely needs nonPublic: true.

Then a check like

accessor.IsFamilyOrAssembly || accessor.IsFamily || assessor.IsPublic

Interfaces may also actually need that check too since C# 8 added the ability to mark members as protected. Though that wouldn't be fixing a regression so likely fine to leave out of this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we don't create properties or methods with the "protected" (family) modifier, and we don't have a keyword in PowerShell language to declare a property as protected, we only create public (by default or with the hidden keyword) or static (static keyword) members. So, even if we check for protected abstract properties here, the public property we are going to create later won't be considered as an implementation of the protected property.

When a base type (MyBase) has a protected abstract property Name, the class definition class MySub : MyBase { [string]$Name } will fail on 7.2 and 7.3 as well today (see the screenshots below). So, I think we just don't support deriving from a class that has protected abstract properties/methods.

7.2.18
image

7.3.11
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah alright if it failed before then good enough for me!

Copy link
Collaborator

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

This PR has 40 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +35 -5
Percentile : 16%

Total files changed: 2

Change summary by file extension:
.cs : +9 -5
.ps1 : +26 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@daxian-dbw daxian-dbw merged commit 4e357f1 into PowerShell:master Mar 13, 2024
38 checks passed
@daxian-dbw daxian-dbw deleted the psclass branch March 13, 2024 21:45
Copy link
Contributor

microsoft-github-policy-service bot commented Mar 13, 2024

📣 Hey @daxian-dbw, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BackPort-7.4.x-Done CL-Engine Indicates that a PR should be marked as an engine change in the Change Log Extra Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants
X Tutup