未验证 提交 24c3b7bf 编写于 作者: D dotnet-automerge-bot 提交者: GitHub

Merge pull request #33789 from dotnet/merges/master-to-features/readonly-members

Merge master to features/readonly-members
......@@ -3299,38 +3299,70 @@ private static Symbol AsMemberOfType(TypeSymbol type, Symbol symbol)
}
var symbolDef = symbol.OriginalDefinition;
var symbolDefContainer = symbolDef.ContainingType;
while (true)
if (symbolDefContainer.IsTupleType)
{
return AsMemberOfTupleType((TupleTypeSymbol)containingType, symbol);
}
if (symbolDefContainer.IsAnonymousType)
{
if (containingType.OriginalDefinition.Equals(symbolDefContainer, TypeCompareKind.AllIgnoreOptions))
int? memberIndex = symbol.Kind == SymbolKind.Property ? symbol.MemberIndexOpt : null;
if (!memberIndex.HasValue)
{
if (symbolDefContainer.IsTupleType)
return symbol;
}
return AnonymousTypeManager.GetAnonymousTypeProperty(containingType, memberIndex.GetValueOrDefault());
}
if (!symbolDefContainer.IsGenericType)
{
Debug.Assert(symbol.ContainingType.IsDefinition);
return symbol;
}
if (symbolDefContainer.IsInterface)
{
if (tryAsMemberOfSingleType(containingType, out Symbol result))
{
return result;
}
foreach (var @interface in containingType.AllInterfacesNoUseSiteDiagnostics)
{
if (tryAsMemberOfSingleType(@interface, out result))
{
return AsMemberOfTupleType((TupleTypeSymbol)containingType, symbol);
return result;
}
if (symbolDefContainer.IsAnonymousType)
}
}
else
{
while (true)
{
if (tryAsMemberOfSingleType(containingType, out Symbol result))
{
int? memberIndex = symbol.Kind == SymbolKind.Property ? symbol.MemberIndexOpt : null;
if (!memberIndex.HasValue)
{
break;
}
return AnonymousTypeManager.GetAnonymousTypeProperty(containingType, memberIndex.GetValueOrDefault());
return result;
}
var result = symbolDef.SymbolAsMember(containingType);
if (result is MethodSymbol resultMethod && resultMethod.IsGenericMethod)
containingType = containingType.BaseTypeNoUseSiteDiagnostics;
if ((object)containingType == null)
{
return resultMethod.Construct(((MethodSymbol)symbol).TypeArguments);
break;
}
return result;
}
containingType = containingType.BaseTypeNoUseSiteDiagnostics;
if (containingType is null)
}
Debug.Assert(false); // If this assert fails, add an appropriate test.
return symbol;
bool tryAsMemberOfSingleType(NamedTypeSymbol singleType, out Symbol result)
{
if (!singleType.OriginalDefinition.Equals(symbolDefContainer, TypeCompareKind.AllIgnoreOptions))
{
break;
result = null;
return false;
}
result = symbolDef.SymbolAsMember(singleType);
if (result is MethodSymbol resultMethod && resultMethod.IsGenericMethod)
{
result = resultMethod.Construct(((MethodSymbol)symbol).TypeArguments);
}
return true;
}
// https://github.com/dotnet/roslyn/issues/29967 Handle other cases such as interfaces.
return symbol;
}
private static Symbol AsMemberOfTupleType(TupleTypeSymbol tupleType, Symbol symbol)
......
......@@ -80249,7 +80249,7 @@ class Program
[Fact]
[WorkItem(26628, "https://github.com/dotnet/roslyn/issues/26628")]
public void ImplicitConstructor_02()
public void ImplicitStaticConstructor_01()
{
var source =
@"#pragma warning disable 414
......@@ -80264,6 +80264,24 @@ class Program
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(4, 23));
}
[Fact]
[WorkItem(26628, "https://github.com/dotnet/roslyn/issues/26628")]
[WorkItem(33394, "https://github.com/dotnet/roslyn/issues/33394")]
public void ImplicitStaticConstructor_02()
{
var source =
@"class C
{
C(string s) { }
static C Empty = new C(null); // warning
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (4,28): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// static C Empty = new C(null); // warning
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(4, 28));
}
[Fact]
[WorkItem(25868, "https://github.com/dotnet/roslyn/issues/25868")]
public void ByRefTarget_01()
......@@ -81083,6 +81101,260 @@ static void F()
Diagnostic(ErrorCode.ERR_TaskRetNoObjectRequiredLambda, "return").WithLocation(9, 13));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceProperties_01()
{
var source =
@"interface IA<T>
{
T A { get; }
}
interface IB<T> : IA<T>
{
T B { get; }
}
class Program
{
static IB<T> CreateB<T>(T t)
{
throw null!;
}
static void F1<T>(T x1)
{
if (x1 == null) return;
var y1 = CreateB(x1);
y1.A.ToString(); // 1
y1.B.ToString(); // 2
}
static void F2<T>() where T : class
{
T x2 = null; // 3
var y2 = CreateB(x2);
y2.ToString();
y2.A.ToString(); // 4
y2.B.ToString(); // 5
}
static void F3<T>() where T : class, new()
{
T? x3 = new T();
var y3 = CreateB(x3);
y3.A.ToString();
y3.B.ToString();
}
static void F4<T>() where T : struct
{
T? x4 = null;
var y4 = CreateB(x4);
_ = y4.A.Value; // 6
_ = y4.B.Value; // 7
}
static void F5<T>() where T : struct
{
T? x5 = new T();
var y5 = CreateB(x5);
_ = y5.A.Value; // 8
_ = y5.B.Value; // 9
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (19,9): warning CS8602: Possible dereference of a null reference.
// y1.A.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y1.A").WithLocation(19, 9),
// (20,9): warning CS8602: Possible dereference of a null reference.
// y1.B.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y1.B").WithLocation(20, 9),
// (24,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// T x2 = null; // 3
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(24, 16),
// (27,9): warning CS8602: Possible dereference of a null reference.
// y2.A.ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y2.A").WithLocation(27, 9),
// (28,9): warning CS8602: Possible dereference of a null reference.
// y2.B.ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y2.B").WithLocation(28, 9),
// (41,13): warning CS8629: Nullable value type may be null.
// _ = y4.A.Value; // 6
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y4.A").WithLocation(41, 13),
// (42,13): warning CS8629: Nullable value type may be null.
// _ = y4.B.Value; // 7
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y4.B").WithLocation(42, 13),
// (48,13): warning CS8629: Nullable value type may be null.
// _ = y5.A.Value; // 8
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y5.A").WithLocation(48, 13),
// (49,13): warning CS8629: Nullable value type may be null.
// _ = y5.B.Value; // 9
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y5.B").WithLocation(49, 13));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceProperties_02()
{
var source =
@"interface IA<T>
{
T A { get; }
}
interface IB<T>
{
T B { get; }
}
interface IC<T, U> : IA<T>, IB<U>
{
}
class Program
{
static IC<T, U> CreateC<T, U>(T t, U u)
{
throw null!;
}
static void F<T, U>()
where T : class, new()
where U : class
{
T? x = new T();
T y = null; // 1
var xy = CreateC(x, y);
xy.A.ToString();
xy.B.ToString(); // 2
var yx = CreateC(y, x);
yx.A.ToString(); // 3
yx.B.ToString();
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (23,15): warning CS8600: Converting null literal or possible null value to non-nullable type.
// T y = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(23, 15),
// (26,9): warning CS8602: Possible dereference of a null reference.
// xy.B.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "xy.B").WithLocation(26, 9),
// (28,9): warning CS8602: Possible dereference of a null reference.
// yx.A.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "yx.A").WithLocation(28, 9));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceMethods_01()
{
var source =
@"interface IA<T>
{
void A(T t);
}
interface IB<T> : IA<T>
{
void B(T t);
}
class Program
{
static IB<T> CreateB<T>(T t)
{
throw null!;
}
static void F1<T>(T x1)
{
if (x1 == null) return;
T y1 = default; // 1
var ix1 = CreateB(x1);
var iy1 = CreateB(y1);
ix1.A(y1);
ix1.B(y1);
iy1.A(x1);
iy1.B(x1);
}
static void F2<T>() where T : class, new()
{
T x2 = null; // 2
T? y2 = new T();
var ix2 = CreateB(x2);
var iy2 = CreateB(y2);
ix2.A(y2);
ix2.B(y2);
iy2.A(x2); // 3
iy2.B(x2); // 4
}
static void F3<T>() where T : struct
{
T? x3 = null;
T? y3 = new T();
var ix3 = CreateB(x3);
var iy3 = CreateB(y3);
ix3.A(y3);
ix3.B(y3);
iy3.A(x3);
iy3.B(x3);
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (18,16): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type.
// T y1 = default; // 1
Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(18, 16),
// (28,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// T x2 = null; // 2
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(28, 16),
// (34,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IA<T>.A(T t)'.
// iy2.A(x2); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IA<T>.A(T t)").WithLocation(34, 15),
// (35,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IB<T>.B(T t)'.
// iy2.B(x2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IB<T>.B(T t)").WithLocation(35, 15));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceMethods_02()
{
var source =
@"interface IA
{
T A<T>(T u);
}
interface IB<T>
{
T B<U>(U u);
}
interface IC<T> : IA, IB<T>
{
}
class Program
{
static IC<T> CreateC<T>(T t)
{
throw null!;
}
static void F<T, U>()
where T : class, new()
where U : class
{
T? x = new T();
U y = null; // 1
var ix = CreateC(x);
var iy = CreateC(y);
ix.A(y).ToString(); // 2
ix.B(y).ToString();
iy.A(x).ToString();
iy.B(x).ToString(); // 3
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (23,15): warning CS8600: Converting null literal or possible null value to non-nullable type.
// U y = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(23, 15),
// (26,9): warning CS8602: Possible dereference of a null reference.
// ix.A(y).ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ix.A(y)").WithLocation(26, 9),
// (29,9): warning CS8602: Possible dereference of a null reference.
// iy.B(x).ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "iy.B(x)").WithLocation(29, 9));
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/33347")]
public void NestedNullConditionalAccess()
{
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册