"Record member does not match positional parameter" diagnostic message is unclear
#67,869 opened on 2023年4月18日
Repository metrics
- Stars
- (20,414 stars)
- PR merge metrics
- (平均マージ 6d 17h) (30d で 256 merged PRs)
説明
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)