dotnet/roslyn

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

开放

#67,869 创建于 2023年4月18日

 (1 条评论) (1 个反应) (0 位负责人)C# (4,257 个派生)batch import
Area-CompilersConcept-Diagnostic Clarityhelp wanted

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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)

贡献者指南