dotnet/roslyn

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

Open

#67.869 aberto em 18 de abr. de 2023

Ver no GitHub
 (1 comment) (1 reaction) (0 assignees)C# (4.257 forks)batch import
Area-CompilersConcept-Diagnostic Clarityhelp wanted

Métricas do repositório

Stars
 (20.414 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (256 fundiu PRs em 30d)

Description

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)

Guia do colaborador