Allow default values for ref readonly parameters#69032
Allow default values for ref readonly parameters#69032jjonescz merged 5 commits intodotnet:features/RefReadonlyfrom
ref readonly parameters#69032Conversation
0c6a54b to
c66cf64
Compare
c66cf64 to
acb88a9
Compare
| // (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. |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I guess the latter should not report if any of the values is bad
Probably. Could you open an issue for this?
| // (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)); | ||
| } |
|
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'. |
There was a problem hiding this comment.
There was a problem hiding this comment.
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) |
| 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); |
There was a problem hiding this comment.
| // 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; |
There was a problem hiding this comment.
Yes, for Array.Empty for conditional diagnostics in one test
AlekseyTs
left a comment
There was a problem hiding this comment.
LGTM (commit 4), assuming CI is passing
Speclet: https://github.com/dotnet/csharplang/blob/05ed9dcfba84a038e02301a9b267516edfe14159/proposals/ref-readonly-parameters.md#default-parameter-values
Test plan: #68056