Skip to content

Allow default values for ref readonly parameters#69032

Merged
jjonescz merged 5 commits intodotnet:features/RefReadonlyfrom
jjonescz:RefReadonly-11-DefaultParameters
Jul 19, 2023
Merged

Allow default values for ref readonly parameters#69032
jjonescz merged 5 commits intodotnet:features/RefReadonlyfrom
jjonescz:RefReadonly-11-DefaultParameters

Conversation

@jjonescz
Copy link
Copy Markdown
Member

@jjonescz jjonescz added the Feature - Ref Readonly Parameters `ref readonly` parameters label Jul 14, 2023
@ghost ghost added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 14, 2023
@jjonescz jjonescz force-pushed the RefReadonly-11-DefaultParameters branch 3 times, most recently from 0c6a54b to c66cf64 Compare July 14, 2023 14:04
@jjonescz jjonescz force-pushed the RefReadonly-11-DefaultParameters branch from c66cf64 to acb88a9 Compare July 14, 2023 14:06
@jjonescz jjonescz marked this pull request as ready for review July 14, 2023 17:39
@jjonescz jjonescz requested a review from a team as a code owner July 14, 2023 17:39
@jjonescz jjonescz requested review from AlekseyTs and jcouv and removed request for a team July 14, 2023 17:40
// (4,67): warning CS9521: A default value is specified for 'ref readonly' parameter 'i', but 'ref readonly' should be used only for references. Consider declaring the parameter as 'in'.
// static void M([DefaultParameterValue(1)] ref readonly int i = 1) => throw null;
Diagnostic(ErrorCode.WRN_RefReadonlyParameterDefaultValue, "1").WithArguments("i").WithLocation(4, 67),
// (4,67): error CS8017: The parameter has multiple distinct default values.
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Jul 17, 2023

Choose a reason for hiding this comment

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

// (4,67): error CS8017: The parameter has multiple distinct default values.

This is probably unrelated to the changes, but why is this error reported? #Closed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, that's weird (and you're right it's unrelated as it happens for in parameters today). It's reported because DecodeDefaultParameterValueAttribute returns a bad constant value if there's an equals value and hence it isn't equal to the one from the equals value checked by VerifyParamDefaultValueMatchesAttributeIfAny so it reports they are different (I guess the latter should not report if any of the values is bad).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess the latter should not report if any of the values is bad

Probably. Could you open an issue for this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

// (8,9): error CS7036: There is no argument given that corresponds to the required parameter 'd' of 'C.M(ref readonly DateTime)'
// M();
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "M").WithArguments("d", "C.M(ref readonly System.DateTime)").WithLocation(8, 9));
}
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Jul 17, 2023

Choose a reason for hiding this comment

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

}

It looks like we are not testing consumption of default values from metadata. I think we should cover at least all valid forms of encoding: attributes only, constant value, combination of both. #Closed

@AlekseyTs
Copy link
Copy Markdown
Contributor

Done with review pass (commit 1)

? CreateCompilation(source2, new[] { CreateCompilation(source1).ToMetadataReference() }, options: TestOptions.ReleaseExe)
: CreateCompilation(new[] { source1, source2 }, options: TestOptions.ReleaseExe);
var verifier = CompileAndVerify(comp, expectedOutput: "1222").VerifyDiagnostics(
// (3,47): warning CS9521: A default value is specified for 'ref readonly' parameter 'i', but 'ref readonly' should be used only for references. Consider declaring the parameter as 'in'.
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Jul 17, 2023

Choose a reason for hiding this comment

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

// (3,47): warning CS9521: A default value is specified for 'ref readonly' parameter 'i', but 'ref readonly' should be used only for references. Consider declaring the parameter as 'in'.

Is this warning expected when fromMetadata is true? And if its, then why? #Closed

Copy link
Copy Markdown
Member Author

@jjonescz jjonescz Jul 18, 2023

Choose a reason for hiding this comment

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

Yes, because ToMetadataReference() just wraps the compilation, so the warning is propagated from it (I think). I will change it to EmitToImageReference() and split the diagnostics, perhaps that's better.

// C.M(x);
Diagnostic(ErrorCode.WRN_ArgExpectedRefOrIn, "x").WithArguments("1").WithLocation(7, 13);
var comp = fromMetadata
? CreateCompilation(source2, new[] { CreateCompilation(source1).VerifyDiagnostics(warning1).EmitToImageReference() }, options: TestOptions.ReleaseExe).VerifyDiagnostics(warning2)
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Jul 18, 2023

Choose a reason for hiding this comment

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

VerifyDiagnostics(warning1)

I think verifying this diagnostics isn't that interesting. #Closed

Diagnostic(ErrorCode.WRN_ArgExpectedRefOrIn, "x").WithArguments("1").WithLocation(7, 13);
var comp = fromMetadata
? CreateCompilation(source2, new[] { CreateCompilation(source1).VerifyDiagnostics(warning1).EmitToImageReference() }, options: TestOptions.ReleaseExe).VerifyDiagnostics(warning2)
: CreateCompilation(new[] { source1, source2 }, options: TestOptions.ReleaseExe).VerifyDiagnostics(warning1, warning2);
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Jul 18, 2023

Choose a reason for hiding this comment

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

VerifyDiagnostics(warning1, warning2)

I think that we should verify diagnostics on CompileAndVerify instead. The set of diagnostics will be conditional on fromMetadata, but we want to verify complete set of diagnostics, including from emit phase for the compilation. #Closed

@jjonescz jjonescz requested a review from AlekseyTs July 18, 2023 15:14
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

System

Is this using necessary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, for Array.Empty for conditional diagnostics in one test

Copy link
Copy Markdown
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 4), assuming CI is passing

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 4)

@jcouv jcouv added this to the C# 12.0 milestone Jul 18, 2023
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Jul 18, 2023
@jcouv jcouv self-assigned this Jul 18, 2023
@jaredpar jaredpar modified the milestones: C# 12.0, 17.8 Jul 18, 2023
@jjonescz jjonescz enabled auto-merge (squash) July 19, 2023 06:31
@jjonescz jjonescz merged commit 0bed80f into dotnet:features/RefReadonly Jul 19, 2023
@jjonescz jjonescz deleted the RefReadonly-11-DefaultParameters branch July 19, 2023 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants