dotnet/roslyn

"Record member does not match positional parameter" diagnostic message is unclear

Open

#67.869 aperta il 18 apr 2023

Vedi su GitHub
 (1 commento) (1 reazione) (0 assegnatari)C# (4257 fork)batch import
Area-CompilersConcept-Diagnostic Clarityhelp wanted

Metriche repository

Star
 (20.414 star)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

Version Used: Compiler version: '4.4.0-6.22580.4 (d7a61210)'. Language version: 11.0.

Steps to Reproduce:

See this sharplab repro, or:

public abstract record Result()
{
    public static Result Error(Exception ex) => new ErrorResult(new ErrorModel(ex));
}

// CS8866: Record member 'Result.Error' must be a readable instance property or field of type 'ErrorModel' to match positional parameter 'Error'
public record ErrorResult(ErrorModel Error) : Result();

public record ErrorModel(Exception Exception);

Expected Behavior: A diagnostic message is emitted that makes it clear that the conflict is due to a static method or member with the same name as a positional property. A better message that was suggested to me in the area I discovered and questioned this issue was:

Record member 'Result.Error(Exception)' must be a readable instance property or field of type 'ErrorModel' to match positional parameter 'Error'

Actual Behavior: The actual diagnostic message, Record member 'Result.Error' must be a readable instance property or field of type 'ErrorModel' to match positional parameter 'Error', does not make it clear what the actual cause is. It implies the Error property in the "constructor" of the derived record is to blame, when it reality it is a static member on the base record.

*(Apologies if I filled the wrong template out, this seemed the most appropriate of the available options)

Guida contributor