Skip to content

Conversation

@xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Oct 28, 2020

@ghost ghost assigned rjmholt Oct 28, 2020
@xtqqczze xtqqczze force-pushed the GH13915 branch 2 times, most recently from f49168e to a739002 Compare October 29, 2020 00:20
@xtqqczze xtqqczze changed the title Mark non-mutable structs readonly Mark applicable structs as readonly Oct 29, 2020
@xtqqczze xtqqczze changed the title Mark applicable structs as readonly Mark applicable structs as readonly and use in-modifier Oct 30, 2020
@xtqqczze xtqqczze marked this pull request as ready for review October 30, 2020 00:28
@ghost ghost added the Review - Needed The PR is being reviewed label Nov 6, 2020
@ghost
Copy link

ghost commented Nov 6, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

/// can only be used as a switch.
/// </summary>
public struct SwitchParameter
public readonly struct SwitchParameter
Copy link
Collaborator

Choose a reason for hiding this comment

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

This one could be an issue -- SwitchParameter is a very widely used public API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But SwitchParameter is immutable by design, and adding the readonly-modifier doesn't cause any overload resolution changes [1], so I think this is not a breaking change.

[1] https://blog.paranoidcoding.com/2019/03/27/readonly-struct-breaking-change.html

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok I'm fairly convinced by this

{
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct EventDescriptor
public readonly struct EventDescriptor
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about changing public structs to readonly -- existing code that relies on the previous form may be affected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This struct is immutable by design.

/// <param name="eventPayload">
/// </param>
public bool WriteEvent(ref EventDescriptor eventDescriptor, params object[] eventPayload)
public bool WriteEvent(in EventDescriptor eventDescriptor, params object[] eventPayload)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Changing the signature on a public method like this is a breaking change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The compiler doesn't permit overloading on ref/out/in differences, so this change is non-breaking.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically the issue isn't with the compiler, but with the runtime. Modules compiled against the old API surface must continue to work after any changes are made to the API.

But I tested it out with a toy project (an exe and a lib, where I built an in version of the lib and then changed it so the exe is compiled against a ref version) and it seems to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah that's still C# compiler rules, where the compiler statically decides which overload to resolve for a call. My concern was that the resolved method call in the IL might not match what the library was originally compiled against when its loaded at runtime (so even though the original program isn't broken it still requires recompilation, which is a dealbreaker for modules). It makes sense that that's not the case though.

@rjmholt
Copy link
Collaborator

rjmholt commented Nov 11, 2020

@daxian-dbw can you review please?

@ghost ghost removed the Review - Needed The PR is being reviewed label Nov 11, 2020
@ghost ghost added the Review - Needed The PR is being reviewed label Nov 19, 2020
@ghost
Copy link

ghost commented Nov 19, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@rjmholt rjmholt merged commit 41ab20c into PowerShell:master Dec 9, 2020
@ghost ghost removed the Review - Needed The PR is being reviewed label Dec 9, 2020
@rjmholt rjmholt added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Dec 9, 2020
@xtqqczze xtqqczze deleted the GH13915 branch December 9, 2020 20:35
@ghost
Copy link

ghost commented Dec 15, 2020

🎉v7.2.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants