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 asserts when emitting calls with optional byref parameters #72203

Merged
merged 5 commits into from
Feb 27, 2024

Conversation

jjonescz
Copy link
Member

Discovered while investigating #72004.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 21, 2024
@jjonescz jjonescz marked this pull request as ready for review February 21, 2024 17:48
@jjonescz jjonescz requested a review from a team as a code owner February 21, 2024 17:48
@@ -497,13 +497,13 @@ private BoundExpression MakeCallWithNoExplicitArgument(MethodArgumentInfo method
if (method.IsExtensionMethod)
{
Debug.Assert(expression == null);
Debug.Assert(method.Parameters.AsSpan()[1..].All(assertParametersAreOptional, (p, assertOptional) => (p.IsOptional || p.IsParams || !assertOptional) && p.RefKind == RefKind.None));
Debug.Assert(method.ParameterRefKinds.IsDefaultOrEmpty || method.ParameterRefKinds[0] is RefKind.In or RefKind.RefReadOnlyParameter or RefKind.None);
Debug.Assert(method.Parameters.AsSpan()[1..].All(assertParametersAreOptional, static (p, assertOptional) => (p.IsOptional || p.IsParams || !assertOptional) && p.RefKind is RefKind.None or RefKind.In or RefKind.RefReadOnlyParameter));
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 22, 2024

Choose a reason for hiding this comment

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

or RefKind.RefReadOnlyParameter

Do we really want to allow ref readonly parameters here? We will not be able to pass a reference to a "real" location to the call #Closed

Copy link
Member Author

@jjonescz jjonescz Feb 23, 2024

Choose a reason for hiding this comment

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

We will not be able to pass a reference to a "real" location to the call

We already warn at the declaration site if a ref readonly parameter has a default value. Also this PR is just fixing asserts, not changing behavior of the compiler in Release builds.

Debug.Assert(method.Parameters.AsSpan()[1..].All(assertParametersAreOptional, (p, assertOptional) => (p.IsOptional || p.IsParams || !assertOptional) && p.RefKind == RefKind.None));
Debug.Assert(method.ParameterRefKinds.IsDefaultOrEmpty || method.ParameterRefKinds[0] is RefKind.In or RefKind.RefReadOnlyParameter or RefKind.None);
Debug.Assert(method.Parameters.AsSpan()[1..].All(assertParametersAreOptional, static (p, assertOptional) => (p.IsOptional || p.IsParams || !assertOptional) && p.RefKind is RefKind.None or RefKind.In or RefKind.RefReadOnlyParameter));
Debug.Assert(method.ParameterRefKinds.IsDefaultOrEmpty || method.ParameterRefKinds.All(static refKind => refKind is RefKind.In or RefKind.RefReadOnlyParameter or RefKind.None));
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 22, 2024

Choose a reason for hiding this comment

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

Debug.Assert(method.ParameterRefKinds.IsDefaultOrEmpty || method.ParameterRefKinds.All(static refKind => refKind is RefKind.In or RefKind.RefReadOnlyParameter or RefKind.None));

It looks like this assert can be taken out of the if. #Closed

{
public int Current { get; private set; }
public bool MoveNext() => Current++ != 3;
public void Dispose({{modifier}} int x = 5) { Console.Write("D"); }
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 22, 2024

Choose a reason for hiding this comment

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

Console.Write("D");

It would be good to observe the value of x. #Closed

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 1)

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 5)

@jjonescz
Copy link
Member Author

@dotnet/roslyn-compiler for a second review, thanks

@jjonescz jjonescz merged commit b701a39 into dotnet:main Feb 27, 2024
24 checks passed
@jjonescz jjonescz deleted the RefOptionalPattern branch February 27, 2024 08:58
@jjonescz jjonescz added this to the 17.10 P2 milestone Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants