dotnet/roslyn

class? constraint combines poorly with non-nullable interface constraint

开放

#49,571 创建于 2020年11月23日

 (8 条评论) (0 个反应) (1 位负责人)C# (4,257 个派生)batch import
Area-CompilersBugFeature - Nullable Reference Typeshelp wanted

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

Version Used: dc6f316a5e4f8eb3a0d36ab356c5c12686db00a9

Steps to Reproduce:

#nullable enable
using System;
public class C {
    public void M<T>(T t) where T : class?, IEquatable<T> {
        t.ToString(); // no warning?
    }
}

Expected Behavior: Variable of type T has a maybe-null initial state

Actual Behavior: Variable has not-null initial state

Remarks: This may be fine and by-design, however the combination of these particular constraints seems to produce a contradictory/unintuitive result, so maybe there should be a warning to change class? to class, or to change IEquatable<T> to IEquatable<T>?.

We should report a warning under the following condition:

If the collection of interface, base class and the class constraints on a type parameter contain a single one which is "non-nullable" then we should warn if any of the others are "nullable".

贡献者指南