提交 251956f1 编写于 作者: L leppie

Fixes #7081 for SemanticAnalyzerTests.cs

上级 b9bb155b
......@@ -90,7 +90,7 @@ static void Main()
[Fact]
public void TestParenthesisErrors()
{
TestErrors(@"
string source = @"
namespace N { class D { public static int x; } }
class C
{
......@@ -101,16 +101,27 @@ void M()
int z = (N.D).x;
}
}
",
"'N' error CS0118: 'N' is a namespace but is used like a variable",
"'N.D' error CS0119: 'D' is a type, which is not valid in the given context");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (8,18): error CS0118: 'N' is a namespace but is used like a variable
// int y = (N).D.x;
Diagnostic(ErrorCode.ERR_BadSKknown, "N").WithArguments("N", "namespace", "variable").WithLocation(8, 18),
// (9,18): error CS0119: 'D' is a type, which is not valid in the given context
// int z = (N.D).x;
Diagnostic(ErrorCode.ERR_BadSKunknown, "N.D").WithArguments("N.D", "type").WithLocation(9, 18),
// (5,16): warning CS0169: The field 'C.x' is never used
// static int x;
Diagnostic(ErrorCode.WRN_UnreferencedField, "x").WithArguments("C.x").WithLocation(5, 16),
// (2,43): warning CS0649: Field 'D.x' is never assigned to, and will always have its default value 0
// namespace N { class D { public static int x; } }
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "x").WithArguments("N.D.x", "0").WithLocation(2, 43));
}
[Fact]
public void TestConstantFoldingErrors()
{
// UNDONE: integer overflows in checked contexts are not detected yet.
TestErrors(@"
string source = @"
class C
{
void M()
......@@ -120,15 +131,23 @@ void M()
decimal z = 10m % 0m;
}
}
",
"'10m % 0m' error CS0020: Division by constant zero",
"'10 / 0' error CS0020: Division by constant zero");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (6,17): error CS0020: Division by constant zero
// int a = 10 / 0;
Diagnostic(ErrorCode.ERR_IntDivByZero, "10 / 0").WithLocation(6, 17),
// (8,21): error CS0020: Division by constant zero
// decimal z = 10m % 0m;
Diagnostic(ErrorCode.ERR_IntDivByZero, "10m % 0m").WithLocation(8, 21),
// (7,16): warning CS0219: The variable 'y' is assigned but its value is never used
// double y = 10.0 % (-0.0); // not an error, makes an infinity
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y").WithArguments("y").WithLocation(7, 16));
}
[Fact]
public void TestStatementBindingErrors()
{
TestErrors(@"
string source = @"
class C
{
void M()
......@@ -136,9 +155,14 @@ void M()
x;
}
}
",
"'x' error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement",
"'x' error CS0103: The name 'x' does not exist in the current context");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (6,9): error CS0103: The name 'x' does not exist in the current context
// x;
Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(6, 9),
// (6,9): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
// x;
Diagnostic(ErrorCode.ERR_IllegalStatement, "x").WithLocation(6, 9));
}
[Fact]
......@@ -148,7 +172,7 @@ public void TestLocalDeclarationErrors()
// it is an illegal type for a local or type argument. This seems unnecessary
// and redundant. In Roslyn I've eliminated the second error.
TestErrors(@"
string source = @"
class C
{
class G<T> {}
......@@ -164,18 +188,38 @@ static class D1 { }
static class D2 { }
static class D3 { }
static class D4 { }
",
"'D1' error CS0723: Cannot declare a variable of static type 'D1'",
"'D2' error CS0719: 'D2': array elements cannot be of static type",
"'D3' error CS0718: 'D3': static types cannot be used as type arguments",
"'D4' error CS0719: 'D4': array elements cannot be of static type"
);
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (7,9): error CS0723: Cannot declare a variable of static type 'D1'
// D1 x1;
Diagnostic(ErrorCode.ERR_VarDeclIsStaticClass, "D1").WithArguments("D1").WithLocation(7, 9),
// (8,9): error CS0719: 'D2': array elements cannot be of static type
// D2[] x2;
Diagnostic(ErrorCode.ERR_ArrayOfStaticClass, "D2").WithArguments("D2").WithLocation(8, 9),
// (9,11): error CS0718: 'D3': static types cannot be used as type arguments
// G<D3> x3;
Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "D3").WithArguments("D3").WithLocation(9, 11),
// (10,11): error CS0719: 'D4': array elements cannot be of static type
// G<D4[]> x4;
Diagnostic(ErrorCode.ERR_ArrayOfStaticClass, "D4").WithArguments("D4").WithLocation(10, 11),
// (7,12): warning CS0168: The variable 'x1' is declared but never used
// D1 x1;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x1").WithArguments("x1").WithLocation(7, 12),
// (8,14): warning CS0168: The variable 'x2' is declared but never used
// D2[] x2;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x2").WithArguments("x2").WithLocation(8, 14),
// (9,15): warning CS0168: The variable 'x3' is declared but never used
// G<D3> x3;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x3").WithArguments("x3").WithLocation(9, 15),
// (10,17): warning CS0168: The variable 'x4' is declared but never used
// G<D4[]> x4;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x4").WithArguments("x4").WithLocation(10, 17));
}
[Fact]
public void TestOverloadResolutionErrors()
{
TestErrors(@"
string source = @"
using System;
using System.Collections;
......@@ -402,49 +446,87 @@ static void M()
// non-static field, method, or property 'C.InstanceMethod()'
}
}",
"'Ambiguous' error CS0121: The call is ambiguous between the following methods or properties: 'P.Ambiguous(string, object)' and 'P.Ambiguous(object, string)'",
"'Blah' error CS1061: 'P' does not contain a definition for 'Blah' and no extension method 'Blah' accepting a first argument of type 'P' could be found (are you missing a using directive or an assembly reference?)",
"'Generic' error CS0411: The type arguments for method 'P.Generic<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly.",
"'NotGeneric<int, int>' error CS0308: The non-generic method 'P.NotGeneric()' cannot be used with type arguments",
"'Generic<int, int>' error CS0305: Using the generic method 'P.Generic<T>()' requires 1 type arguments",
"'frob' error CS1739: The best overload for 'NoParameter' does not have a parameter named 'frob'",
"'NoParameter' error CS1501: No overload for method 'NoParameter' takes 2 arguments",
"'p.StaticMethod' error CS0176: Member 'P.StaticMethod()' cannot be accessed with an instance reference; qualify it with a type name instead",
"'P.NoParameter' error CS0120: An object reference is required for the non-static field, method, or property 'P.NoParameter()'",
//"'p.OneParameter' error CS1502: The best overloaded method match for 'P.OneParameter(int)' has some invalid arguments", //specifically omitted by roslyn
"'123.456' error CS1503: Argument 1: cannot convert from 'double' to 'int'",
//"'p.TwoRefParameters' error CS1502: The best overloaded method match for 'P.TwoRefParameters(ref int, ref int, int)' has some invalid arguments", //specifically omitted by roslyn
"'1234' error CS1620: Argument 1 must be passed with the 'ref' keyword",
"'4567' error CS1620: Argument 2 must be passed with the 'ref' keyword",
"'1234.4567' error CS1503: Argument 3: cannot convert from 'double' to 'int'",
//"'p.RefParameter' error CS1502: The best overloaded method match for 'P.RefParameter(ref int)' has some invalid arguments", //specifically omitted by roslyn
"'456' error CS1620: Argument 1 must be passed with the 'ref' keyword",
//"'p.OneParameter' error CS1502: The best overloaded method match for 'P.OneParameter(int)' has some invalid arguments", //specifically omitted by roslyn
"'local' error CS1615: Argument 1 should not be passed with the 'out' keyword",
//"'p.RefParameter' error CS1502: The best overloaded method match for 'P.RefParameter(ref int)' has some invalid arguments", //specifically omitted by roslyn
"'local' error CS1620: Argument 1 must be passed with the 'ref' keyword",
//"'p.ThreeRefParameters' error CS1502: The best overloaded method match for 'P.ThreeRefParameters(ref int, ref int, ref int)' has some invalid arguments", //specifically omitted by roslyn
"'null' error CS1503: Argument 1: cannot convert from '<null>' to 'ref int'",
"'p.ToString' error CS1503: Argument 2: cannot convert from 'method group' to 'ref int'",
"'12345.67' error CS1620: Argument 3 must be passed with the 'ref' keyword",
"'InstanceMethod' error CS0120: An object reference is required for the non-static field, method, or property 'C.InstanceMethod()'"
);
}";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (84,29): error CS1110: Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll?
// public static void PExt(this P p) {}
Diagnostic(ErrorCode.ERR_ExtensionAttrNotFound, "this").WithArguments("System.Runtime.CompilerServices.ExtensionAttribute").WithLocation(84, 29),
// (69,28): error CS1110: Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll?
// public static P Select(this P p, Func<P, P> projection)
Diagnostic(ErrorCode.ERR_ExtensionAttrNotFound, "this").WithArguments("System.Runtime.CompilerServices.ExtensionAttribute").WithLocation(69, 28),
// (80,33): error CS1110: Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll?
// public static object Select(this Q q, object projection)
Diagnostic(ErrorCode.ERR_ExtensionAttrNotFound, "this").WithArguments("System.Runtime.CompilerServices.ExtensionAttribute").WithLocation(80, 33),
// (76,28): error CS1110: Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll?
// public static P Select(this P p, Func<P, P> projection)
Diagnostic(ErrorCode.ERR_ExtensionAttrNotFound, "this").WithArguments("System.Runtime.CompilerServices.ExtensionAttribute").WithLocation(76, 28),
// (95,11): error CS0121: The call is ambiguous between the following methods or properties: 'P.Ambiguous(string, object)' and 'P.Ambiguous(object, string)'
// p.Ambiguous(null, null);
Diagnostic(ErrorCode.ERR_AmbigCall, "Ambiguous").WithArguments("P.Ambiguous(string, object)", "P.Ambiguous(object, string)").WithLocation(95, 11),
// (113,11): error CS1061: 'P' does not contain a definition for 'Blah' and no extension method 'Blah' accepting a first argument of type 'P' could be found (are you missing a using directive or an assembly reference?)
// p.Blah();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "Blah").WithArguments("P", "Blah").WithLocation(113, 11),
// (118,11): error CS0411: The type arguments for method 'P.Generic<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly.
// p.Generic();
Diagnostic(ErrorCode.ERR_CantInferMethTypeArgs, "Generic").WithArguments("P.Generic<T>()").WithLocation(118, 11),
// (122,11): error CS0305: Using the generic method 'P.Generic<T>()' requires 1 type arguments
// p.Generic<int, int>();
Diagnostic(ErrorCode.ERR_BadArity, "Generic<int, int>").WithArguments("P.Generic<T>()", "method", "1").WithLocation(122, 11),
// (125,11): error CS0308: The non-generic method 'P.NotGeneric()' cannot be used with type arguments
// p.NotGeneric<int, int>();
Diagnostic(ErrorCode.ERR_HasNoTypeVars, "NotGeneric<int, int>").WithArguments("P.NotGeneric()", "method").WithLocation(125, 11),
// (134,23): error CS1739: The best overload for 'NoParameter' does not have a parameter named 'frob'
// p.NoParameter(frob: null);
Diagnostic(ErrorCode.ERR_BadNamedArgument, "frob").WithArguments("NoParameter", "frob").WithLocation(134, 23),
// (160,11): error CS1501: No overload for method 'NoParameter' takes 2 arguments
// p.NoParameter(123, 456);
Diagnostic(ErrorCode.ERR_BadArgCount, "NoParameter").WithArguments("NoParameter", "2").WithLocation(160, 11),
// (165,24): error CS1503: Argument 1: cannot convert from 'double' to 'int'
// p.OneParameter(123.456);
Diagnostic(ErrorCode.ERR_BadArgType, "123.456").WithArguments("1", "double", "int").WithLocation(165, 24),
// (178,28): error CS1620: Argument 1 must be passed with the 'ref' keyword
// p.TwoRefParameters(1234, 4567, 1234.4567);
Diagnostic(ErrorCode.ERR_BadArgRef, "1234").WithArguments("1", "ref").WithLocation(178, 28),
// (178,34): error CS1620: Argument 2 must be passed with the 'ref' keyword
// p.TwoRefParameters(1234, 4567, 1234.4567);
Diagnostic(ErrorCode.ERR_BadArgRef, "4567").WithArguments("2", "ref").WithLocation(178, 34),
// (178,40): error CS1503: Argument 3: cannot convert from 'double' to 'int'
// p.TwoRefParameters(1234, 4567, 1234.4567);
Diagnostic(ErrorCode.ERR_BadArgType, "1234.4567").WithArguments("3", "double", "int").WithLocation(178, 40),
// (184,30): error CS1503: Argument 1: cannot convert from '<null>' to 'ref int'
// p.ThreeRefParameters(null, p.ToString, 12345.67);
Diagnostic(ErrorCode.ERR_BadArgType, "null").WithArguments("1", "<null>", "ref int").WithLocation(184, 30),
// (184,36): error CS1503: Argument 2: cannot convert from 'method group' to 'ref int'
// p.ThreeRefParameters(null, p.ToString, 12345.67);
Diagnostic(ErrorCode.ERR_BadArgType, "p.ToString").WithArguments("2", "method group", "ref int").WithLocation(184, 36),
// (184,48): error CS1620: Argument 3 must be passed with the 'ref' keyword
// p.ThreeRefParameters(null, p.ToString, 12345.67);
Diagnostic(ErrorCode.ERR_BadArgRef, "12345.67").WithArguments("3", "ref").WithLocation(184, 48),
// (190,24): error CS1620: Argument 1 must be passed with the 'ref' keyword
// p.RefParameter(456);
Diagnostic(ErrorCode.ERR_BadArgRef, "456").WithArguments("1", "ref").WithLocation(190, 24),
// (195,28): error CS1615: Argument 1 should not be passed with the 'out' keyword
// p.OneParameter(out local);
Diagnostic(ErrorCode.ERR_BadArgExtraRef, "local").WithArguments("1", "out").WithLocation(195, 28),
// (199,28): error CS1620: Argument 1 must be passed with the 'ref' keyword
// p.RefParameter(out local);
Diagnostic(ErrorCode.ERR_BadArgRef, "local").WithArguments("1", "ref").WithLocation(199, 28),
// (216,9): error CS0176: Member 'P.StaticMethod()' cannot be accessed with an instance reference; qualify it with a type name instead
// p.StaticMethod();
Diagnostic(ErrorCode.ERR_ObjectProhibited, "p.StaticMethod").WithArguments("P.StaticMethod()").WithLocation(216, 9),
// (219,9): error CS0120: An object reference is required for the non-static field, method, or property 'P.NoParameter()'
// P.NoParameter();
Diagnostic(ErrorCode.ERR_ObjectRequired, "P.NoParameter").WithArguments("P.NoParameter()").WithLocation(219, 9),
// (223,9): error CS0120: An object reference is required for the non-static field, method, or property 'C.InstanceMethod()'
// InstanceMethod(); // Verify that use of 'implicit this' is not legal in a static method.
Diagnostic(ErrorCode.ERR_ObjectRequired, "InstanceMethod").WithArguments("C.InstanceMethod()").WithLocation(223, 9));
}
[WorkItem(538651, "DevDiv")]
[Fact]
public void TestMemberResolutionWithHiding()
{
TestErrors(@"
string source = @"
class A
{
public static void P() { }
......@@ -502,25 +584,62 @@ static void M(B b)
b.V = 0; // CS1656
b.W = 0; // CS1656
}
}",
//"'F' error CS1502: The best overloaded method match for 'C.F(int)' has some invalid arguments", //specifically omitted by roslyn
"'B.T' error CS1503: Argument 1: cannot convert from 'method group' to 'int'",
//"'F' error CS1502: The best overloaded method match for 'C.F(int)' has some invalid arguments", //specifically omitted by roslyn
"'B.U' error CS1503: Argument 1: cannot convert from 'method group' to 'int'",
//"'F' error CS1502: The best overloaded method match for 'C.F(int)' has some invalid arguments", //specifically omitted by roslyn
"'b.V' error CS1503: Argument 1: cannot convert from 'method group' to 'int'",
//"'F' error CS1502: The best overloaded method match for 'C.F(int)' has some invalid arguments", //specifically omitted by roslyn
"'b.W' error CS1503: Argument 1: cannot convert from 'method group' to 'int'",
"'B.T' error CS1656: Cannot assign to 'T' because it is a 'method group'",
"'B.U' error CS1656: Cannot assign to 'U' because it is a 'method group'",
"'b.V' error CS1656: Cannot assign to 'V' because it is a 'method group'",
"'b.W' error CS1656: Cannot assign to 'W' because it is a 'method group'");
}";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (18,16): warning CS0108: 'B.Q' hides inherited member 'A.Q()'. Use the new keyword if hiding was intended.
// public int Q { get; set; } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "Q").WithArguments("B.Q", "A.Q()").WithLocation(18, 16),
// (19,23): warning CS0108: 'B.R' hides inherited member 'A.R()'. Use the new keyword if hiding was intended.
// public static int R { get; set; } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "R").WithArguments("B.R", "A.R()").WithLocation(19, 23),
// (20,16): warning CS0108: 'B.S' hides inherited member 'A.S()'. Use the new keyword if hiding was intended.
// public int S { get; set; } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "S").WithArguments("B.S", "A.S()").WithLocation(20, 16),
// (21,24): warning CS0108: 'B.T()' hides inherited member 'A.T'. Use the new keyword if hiding was intended.
// public static void T() { } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "T").WithArguments("B.T()", "A.T").WithLocation(21, 24),
// (22,17): warning CS0108: 'B.U()' hides inherited member 'A.U'. Use the new keyword if hiding was intended.
// public void U() { } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "U").WithArguments("B.U()", "A.U").WithLocation(22, 17),
// (23,24): warning CS0108: 'B.V()' hides inherited member 'A.V'. Use the new keyword if hiding was intended.
// public static void V() { } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "V").WithArguments("B.V()", "A.V").WithLocation(23, 24),
// (24,17): warning CS0108: 'B.W()' hides inherited member 'A.W'. Use the new keyword if hiding was intended.
// public void W() { } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "W").WithArguments("B.W()", "A.W").WithLocation(24, 17),
// (17,23): warning CS0108: 'B.P' hides inherited member 'A.P()'. Use the new keyword if hiding was intended.
// public static int P { get; set; } // CS0108
Diagnostic(ErrorCode.WRN_NewRequired, "P").WithArguments("B.P", "A.P()").WithLocation(17, 23),
// (45,11): error CS1503: Argument 1: cannot convert from 'method group' to 'int'
// F(B.T); // CS1503
Diagnostic(ErrorCode.ERR_BadArgType, "B.T").WithArguments("1", "method group", "int").WithLocation(45, 11),
// (46,11): error CS1503: Argument 1: cannot convert from 'method group' to 'int'
// F(B.U); // CS1503
Diagnostic(ErrorCode.ERR_BadArgType, "B.U").WithArguments("1", "method group", "int").WithLocation(46, 11),
// (47,11): error CS1503: Argument 1: cannot convert from 'method group' to 'int'
// F(b.V); // CS1503
Diagnostic(ErrorCode.ERR_BadArgType, "b.V").WithArguments("1", "method group", "int").WithLocation(47, 11),
// (48,11): error CS1503: Argument 1: cannot convert from 'method group' to 'int'
// F(b.W); // CS1503
Diagnostic(ErrorCode.ERR_BadArgType, "b.W").WithArguments("1", "method group", "int").WithLocation(48, 11),
// (54,9): error CS1656: Cannot assign to 'T' because it is a 'method group'
// B.T = 0; // CS1656
Diagnostic(ErrorCode.ERR_AssgReadonlyLocalCause, "B.T").WithArguments("T", "method group").WithLocation(54, 9),
// (55,9): error CS1656: Cannot assign to 'U' because it is a 'method group'
// B.U = 0; // CS1656
Diagnostic(ErrorCode.ERR_AssgReadonlyLocalCause, "B.U").WithArguments("U", "method group").WithLocation(55, 9),
// (56,9): error CS1656: Cannot assign to 'V' because it is a 'method group'
// b.V = 0; // CS1656
Diagnostic(ErrorCode.ERR_AssgReadonlyLocalCause, "b.V").WithArguments("V", "method group").WithLocation(56, 9),
// (57,9): error CS1656: Cannot assign to 'W' because it is a 'method group'
// b.W = 0; // CS1656
Diagnostic(ErrorCode.ERR_AssgReadonlyLocalCause, "b.W").WithArguments("W", "method group").WithLocation(57, 9));
}
[Fact]
public void TestAssignmentErrors01()
{
TestErrors(@"
string source = @"
namespace N { struct Q<T,U> {} }
struct S { public int z; }
class C
......@@ -558,27 +677,62 @@ void M()
this = null;
}
}
",
"'null' error CS0131: The left-hand side of an assignment must be a variable, property or indexer",
"'M' error CS1656: Cannot assign to 'M' because it is a 'method group'",
"'x' error CS0131: The left-hand side of an assignment must be a variable, property or indexer",
"'N' error CS0118: 'N' is a namespace but is used like a variable",
"'C' error CS0118: 'C' is a type but is used like a variable",
"'static_readonly.z' error CS1650: Fields of static readonly field 'C.static_readonly' cannot be assigned to (except in a static constructor or a variable initializer)",
"'GetS(null, null)' error CS1612: Cannot modify the return value of 'C.GetS(Q<string, double>?[][*,*][*,*,*], int?)' because it is not a variable",
"'instance_readonly' error CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer)",
"'M()' error CS0029: Cannot implicitly convert type 'void' to 'int'",
"'null' error CS0037: Cannot convert null to 'int' because it is a non-nullable value type",
"'l1' error CS0266: Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)",
"'M' error CS0428: Cannot convert method group 'M' to non-delegate type 'int'. Did you intend to invoke the method?",
"'this' error CS1604: Cannot assign to 'this' because it is read-only"
);
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (7,7): error CS0161: 'C.GetS(Q<string, double>?[][*,*][*,*,*], int?)': not all code paths return a value
// S GetS(N.Q<string, double>?[][,][,,] q, int? x) { }
Diagnostic(ErrorCode.ERR_ReturnExpected, "GetS").WithArguments("C.GetS(N.Q<string, double>?[][*,*][*,*,*], int?)").WithLocation(7, 7),
// (12,9): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
// null = 123;
Diagnostic(ErrorCode.ERR_AssgLvalueExpected, "null").WithLocation(12, 9),
// (13,9): error CS1656: Cannot assign to 'M' because it is a 'method group'
// M = 123;
Diagnostic(ErrorCode.ERR_AssgReadonlyLocalCause, "M").WithArguments("M", "method group").WithLocation(13, 9),
// (14,9): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
// x = 123;
Diagnostic(ErrorCode.ERR_AssgLvalueExpected, "x").WithLocation(14, 9),
// (15,9): error CS0118: 'N' is a namespace but is used like a variable
// N = 123;
Diagnostic(ErrorCode.ERR_BadSKknown, "N").WithArguments("N", "namespace", "variable").WithLocation(15, 9),
// (16,9): error CS0118: 'C' is a type but is used like a variable
// C = 123;
Diagnostic(ErrorCode.ERR_BadSKknown, "C").WithArguments("C", "type", "variable").WithLocation(16, 9),
// (17,9): error CS1650: Fields of static readonly field 'C.static_readonly' cannot be assigned to (except in a static constructor or a variable initializer)
// static_readonly.z = 123;
Diagnostic(ErrorCode.ERR_AssgReadonlyStatic2, "static_readonly.z").WithArguments("C.static_readonly").WithLocation(17, 9),
// (18,9): error CS1612: Cannot modify the return value of 'C.GetS(Q<string, double>?[][*,*][*,*,*], int?)' because it is not a variable
// GetS(null, null).z = 123;
Diagnostic(ErrorCode.ERR_ReturnNotLValue, "GetS(null, null)").WithArguments("C.GetS(N.Q<string, double>?[][*,*][*,*,*], int?)").WithLocation(18, 9),
// (24,9): error CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer)
// instance_readonly = 123;
Diagnostic(ErrorCode.ERR_AssgReadonly, "instance_readonly").WithLocation(24, 9),
// (31,18): error CS0029: Cannot implicitly convert type 'void' to 'int'
// int m1 = M();
Diagnostic(ErrorCode.ERR_NoImplicitConv, "M()").WithArguments("void", "int").WithLocation(31, 18),
// (32,18): error CS0037: Cannot convert null to 'int' because it is a non-nullable value type
// int m2 = null;
Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("int").WithLocation(32, 18),
// (34,18): error CS0266: Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
// int m3 = l1;
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "l1").WithArguments("long", "int").WithLocation(34, 18),
// (35,18): error CS0428: Cannot convert method group 'M' to non-delegate type 'int'. Did you intend to invoke the method?
// int m4 = M;
Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "M").WithArguments("M", "int").WithLocation(35, 18),
// (36,9): error CS1604: Cannot assign to 'this' because it is read-only
// this = null;
Diagnostic(ErrorCode.ERR_AssgReadonlyLocal, "this").WithArguments("this").WithLocation(36, 9),
// (6,23): warning CS0414: The field 'C.static_readonly' is assigned but its value is never used
// static readonly S static_readonly;
Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "static_readonly").WithArguments("C.static_readonly").WithLocation(6, 23),
// (8,18): warning CS0414: The field 'C.instance_readonly' is assigned but its value is never used
// readonly int instance_readonly;
Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "instance_readonly").WithArguments("C.instance_readonly").WithLocation(8, 18));
}
[Fact]
public void TestAssignmentErrors02()
{
TestErrors(
string source =
@"struct S { public int Field; }
class C
{
......@@ -590,15 +744,20 @@ void M()
Get().Field = 1;
}
}
",
"'Property' error CS1612: Cannot modify the return value of 'C.Property' because it is not a variable",
"'Get()' error CS1612: Cannot modify the return value of 'C.Get()' because it is not a variable");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (8,9): error CS1612: Cannot modify the return value of 'C.Property' because it is not a variable
// Property.Field = 0;
Diagnostic(ErrorCode.ERR_ReturnNotLValue, "Property").WithArguments("C.Property").WithLocation(8, 9),
// (9,9): error CS1612: Cannot modify the return value of 'C.Get()' because it is not a variable
// Get().Field = 1;
Diagnostic(ErrorCode.ERR_ReturnNotLValue, "Get()").WithArguments("C.Get()").WithLocation(9, 9));
}
[Fact]
public void TestRefErrors()
{
TestErrors(@"
string source = @"
class C
{
struct S { public int z; }
......@@ -643,29 +802,38 @@ void M()
// UNDONE: event, property, indexer, lambda, anonymous method, using, fixed, foreach, lock, inaccessible field, range variables
}
}
",
// (Note that in these three cases overload resolution succeeds. See above.)
"'123' error CS1510: A ref or out argument must be an assignable variable",
"'x' error CS1510: A ref or out argument must be an assignable variable",
"'y + y' error CS1510: A ref or out argument must be an assignable variable",
"'null' error CS1510: A ref or out argument must be an assignable variable",
//"'N3' error CS1502: The best overloaded method match for 'C.N3(ref int)' has some invalid arguments",
"'M' error CS1657: Cannot pass 'M' as a ref or out argument because it is a 'method group'",
//"'N4' error CS1502: The best overloaded method match for 'C.N4(ref int)' has some invalid arguments",
"'C' error CS0118: 'C' is a type but is used like a variable"
// UNDONE: Note that the native compiler gives a slightly different error message; it says
// UNDONE: cannot convert from 'ref C' to 'ref int'. Do we want to replicate that error message?
//"'N5' error CS1502: The best overloaded method match for 'C.N5(ref int)' has some invalid arguments",
);
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (27,24): error CS1510: A ref or out argument must be an assignable variable
// N1(ref 123);
Diagnostic(ErrorCode.ERR_RefLvalueExpected, "123").WithLocation(27, 24),
// (28,24): error CS1510: A ref or out argument must be an assignable variable
// N1(ref x);
Diagnostic(ErrorCode.ERR_RefLvalueExpected, "x").WithLocation(28, 24),
// (33,24): error CS1510: A ref or out argument must be an assignable variable
// N2(ref y + y);
Diagnostic(ErrorCode.ERR_RefLvalueExpected, "y + y").WithLocation(33, 24),
// (35,24): error CS1510: A ref or out argument must be an assignable variable
// N3(ref null);
Diagnostic(ErrorCode.ERR_RefLvalueExpected, "null").WithLocation(35, 24),
// (36,24): error CS1657: Cannot pass 'M' as a ref or out argument because it is a 'method group'
// N4(ref M);
Diagnostic(ErrorCode.ERR_RefReadonlyLocalCause, "M").WithArguments("M", "method group").WithLocation(36, 24),
// (37,24): error CS0118: 'C' is a type but is used like a variable
// N5(ref C);
Diagnostic(ErrorCode.ERR_BadSKknown, "C").WithArguments("C", "type", "variable").WithLocation(37, 24),
// (12,33): warning CS0169: The field 'C.static_readonly' is never used
// static readonly int static_readonly;
Diagnostic(ErrorCode.WRN_UnreferencedField, "static_readonly").WithArguments("C.static_readonly").WithLocation(12, 33),
// (4,35): warning CS0649: Field 'C.S.z' is never assigned to, and will always have its default value 0
// struct S { public int z; }
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "z").WithArguments("C.S.z", "0").WithLocation(4, 35));
}
[Fact]
public void TestStaticErrors()
{
TestErrors(@"
string source = @"
class C
{
int instanceField;
......@@ -675,10 +843,17 @@ static void M()
x = this.instanceField;
}
}
",
"'this' error CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer",
"'instanceField' error CS0120: An object reference is required for the non-static field, method, or property 'C.instanceField'"
);
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (7,17): error CS0120: An object reference is required for the non-static field, method, or property 'C.instanceField'
// int x = instanceField;
Diagnostic(ErrorCode.ERR_ObjectRequired, "instanceField").WithArguments("C.instanceField").WithLocation(7, 17),
// (8,13): error CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer
// x = this.instanceField;
Diagnostic(ErrorCode.ERR_ThisInStaticMeth, "this").WithLocation(8, 13),
// (4,9): warning CS0649: Field 'C.instanceField' is never assigned to, and will always have its default value 0
// int instanceField;
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "instanceField").WithArguments("C.instanceField", "0").WithLocation(4, 9));
}
[Fact]
......@@ -739,7 +914,7 @@ void foo(dynamic d)
[Fact]
public void TestDuplicateLocalDeclaration()
{
TestErrors(@"
string source = @"
class C
{
void M()
......@@ -751,14 +926,32 @@ void M()
}
}
static class D { }
",
"'i' error CS0128: A local variable named 'i' is already defined in this scope");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (9,17): error CS0128: A local variable named 'i' is already defined in this scope
// long k, i;
Diagnostic(ErrorCode.ERR_LocalDuplicate, "i").WithArguments("i").WithLocation(9, 17),
// (6,16): warning CS0219: The variable 'q' is assigned but its value is never used
// string q = "hello";
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "q").WithArguments("q").WithLocation(6, 16),
// (7,13): warning CS0168: The variable 'i' is declared but never used
// int i;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "i").WithArguments("i").WithLocation(7, 13),
// (8,13): warning CS0168: The variable 'j' is declared but never used
// int j;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "j").WithArguments("j").WithLocation(8, 13),
// (9,14): warning CS0168: The variable 'k' is declared but never used
// long k, i;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "k").WithArguments("k").WithLocation(9, 14),
// (9,17): warning CS0168: The variable 'i' is declared but never used
// long k, i;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "i").WithArguments("i").WithLocation(9, 17));
}
[Fact]
public void TestEnumZeroArg()
{
TestErrors(
string source =
@"enum E { Zero, One }
enum F { Zero, One }
class C
......@@ -810,59 +1003,83 @@ static void M(E e)
M(F.One); // error
}
}
",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'false' error CS1503: Argument 1: cannot convert from 'bool' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(char)(One - 1)' error CS1503: Argument 1: cannot convert from 'char' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(F)(One - 1)' error CS1503: Argument 1: cannot convert from 'F' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'Null' error CS1503: Argument 1: cannot convert from 'object' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'null' error CS1503: Argument 1: cannot convert from '<null>' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'F.Zero' error CS1503: Argument 1: cannot convert from 'F' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'true' error CS1503: Argument 1: cannot convert from 'bool' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(char)One' error CS1503: Argument 1: cannot convert from 'char' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(sbyte)One' error CS1503: Argument 1: cannot convert from 'sbyte' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(byte)One' error CS1503: Argument 1: cannot convert from 'byte' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(short)One' error CS1503: Argument 1: cannot convert from 'short' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(ushort)One' error CS1503: Argument 1: cannot convert from 'ushort' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(int)One' error CS1503: Argument 1: cannot convert from 'int' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(uint)One' error CS1503: Argument 1: cannot convert from 'uint' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(long)One' error CS1503: Argument 1: cannot convert from 'long' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(ulong)One' error CS1503: Argument 1: cannot convert from 'ulong' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(decimal)One' error CS1503: Argument 1: cannot convert from 'decimal' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(float)One' error CS1503: Argument 1: cannot convert from 'float' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(double)One' error CS1503: Argument 1: cannot convert from 'double' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'(F)One' error CS1503: Argument 1: cannot convert from 'F' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'new object()' error CS1503: Argument 1: cannot convert from 'object' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'1' error CS1503: Argument 1: cannot convert from 'int' to 'E'",
//"'M' error CS1502: The best overloaded method match for 'C.M(E)' has some invalid arguments", //specifically omitted by roslyn
"'F.One' error CS1503: Argument 1: cannot convert from 'F' to 'E'");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (10,11): error CS1503: Argument 1: cannot convert from 'bool' to 'E'
// M(false); // error
Diagnostic(ErrorCode.ERR_BadArgType, "false").WithArguments("1", "bool", "E").WithLocation(10, 11),
// (11,11): error CS1503: Argument 1: cannot convert from 'char' to 'E'
// M((char)(One - 1)); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(char)(One - 1)").WithArguments("1", "char", "E").WithLocation(11, 11),
// (24,11): error CS1503: Argument 1: cannot convert from 'F' to 'E'
// M((F)(One - 1)); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(F)(One - 1)").WithArguments("1", "F", "E").WithLocation(24, 11),
// (25,11): error CS1503: Argument 1: cannot convert from 'object' to 'E'
// M(Null); // error
Diagnostic(ErrorCode.ERR_BadArgType, "Null").WithArguments("1", "object", "E").WithLocation(25, 11),
// (26,11): error CS1503: Argument 1: cannot convert from '<null>' to 'E'
// M(null); // error
Diagnostic(ErrorCode.ERR_BadArgType, "null").WithArguments("1", "<null>", "E").WithLocation(26, 11),
// (29,11): error CS1503: Argument 1: cannot convert from 'F' to 'E'
// M(F.Zero); // error
Diagnostic(ErrorCode.ERR_BadArgType, "F.Zero").WithArguments("1", "F", "E").WithLocation(29, 11),
// (31,11): error CS1503: Argument 1: cannot convert from 'bool' to 'E'
// M(true); // error
Diagnostic(ErrorCode.ERR_BadArgType, "true").WithArguments("1", "bool", "E").WithLocation(31, 11),
// (32,11): error CS1503: Argument 1: cannot convert from 'char' to 'E'
// M((char)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(char)One").WithArguments("1", "char", "E").WithLocation(32, 11),
// (33,11): error CS1503: Argument 1: cannot convert from 'sbyte' to 'E'
// M((sbyte)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(sbyte)One").WithArguments("1", "sbyte", "E").WithLocation(33, 11),
// (34,11): error CS1503: Argument 1: cannot convert from 'byte' to 'E'
// M((byte)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(byte)One").WithArguments("1", "byte", "E").WithLocation(34, 11),
// (35,11): error CS1503: Argument 1: cannot convert from 'short' to 'E'
// M((short)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(short)One").WithArguments("1", "short", "E").WithLocation(35, 11),
// (36,11): error CS1503: Argument 1: cannot convert from 'ushort' to 'E'
// M((ushort)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(ushort)One").WithArguments("1", "ushort", "E").WithLocation(36, 11),
// (37,11): error CS1503: Argument 1: cannot convert from 'int' to 'E'
// M((int)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(int)One").WithArguments("1", "int", "E").WithLocation(37, 11),
// (38,11): error CS1503: Argument 1: cannot convert from 'uint' to 'E'
// M((uint)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(uint)One").WithArguments("1", "uint", "E").WithLocation(38, 11),
// (39,11): error CS1503: Argument 1: cannot convert from 'long' to 'E'
// M((long)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(long)One").WithArguments("1", "long", "E").WithLocation(39, 11),
// (40,11): error CS1503: Argument 1: cannot convert from 'ulong' to 'E'
// M((ulong)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(ulong)One").WithArguments("1", "ulong", "E").WithLocation(40, 11),
// (41,11): error CS1503: Argument 1: cannot convert from 'decimal' to 'E'
// M((decimal)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(decimal)One").WithArguments("1", "decimal", "E").WithLocation(41, 11),
// (42,11): error CS1503: Argument 1: cannot convert from 'float' to 'E'
// M((float)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(float)One").WithArguments("1", "float", "E").WithLocation(42, 11),
// (43,11): error CS1503: Argument 1: cannot convert from 'double' to 'E'
// M((double)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(double)One").WithArguments("1", "double", "E").WithLocation(43, 11),
// (45,11): error CS1503: Argument 1: cannot convert from 'F' to 'E'
// M((F)One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "(F)One").WithArguments("1", "F", "E").WithLocation(45, 11),
// (46,11): error CS1503: Argument 1: cannot convert from 'object' to 'E'
// M(new object()); // error
Diagnostic(ErrorCode.ERR_BadArgType, "new object()").WithArguments("1", "object", "E").WithLocation(46, 11),
// (47,11): error CS1503: Argument 1: cannot convert from 'int' to 'E'
// M(1); // error
Diagnostic(ErrorCode.ERR_BadArgType, "1").WithArguments("1", "int", "E").WithLocation(47, 11),
// (49,11): error CS1503: Argument 1: cannot convert from 'F' to 'E'
// M(F.One); // error
Diagnostic(ErrorCode.ERR_BadArgType, "F.One").WithArguments("1", "F", "E").WithLocation(49, 11));
}
[Fact]
public void TestEnumZeroAssign()
{
TestErrors(
string source =
@"enum E { Zero, One }
enum F { Zero, One }
class C
......@@ -915,36 +1132,83 @@ static void M()
e = F.One; // error
}
}
",
"'false' error CS0029: Cannot implicitly convert type 'bool' to 'E'",
"'(char)(One - 1)' error CS0266: Cannot implicitly convert type 'char' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(F)(One - 1)' error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'Null' error CS0266: Cannot implicitly convert type 'object' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'null' error CS0037: Cannot convert null to 'E' because it is a non-nullable value type",
"'F.Zero' error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'true' error CS0029: Cannot implicitly convert type 'bool' to 'E'",
"'(char)One' error CS0266: Cannot implicitly convert type 'char' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(sbyte)One' error CS0266: Cannot implicitly convert type 'sbyte' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(byte)One' error CS0266: Cannot implicitly convert type 'byte' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(short)One' error CS0266: Cannot implicitly convert type 'short' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(ushort)One' error CS0266: Cannot implicitly convert type 'ushort' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(int)One' error CS0266: Cannot implicitly convert type 'int' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(uint)One' error CS0266: Cannot implicitly convert type 'uint' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(long)One' error CS0266: Cannot implicitly convert type 'long' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(ulong)One' error CS0266: Cannot implicitly convert type 'ulong' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(decimal)One' error CS0266: Cannot implicitly convert type 'decimal' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(float)One' error CS0266: Cannot implicitly convert type 'float' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(double)One' error CS0266: Cannot implicitly convert type 'double' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'(F)One' error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'new object()' error CS0266: Cannot implicitly convert type 'object' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'1' error CS0266: Cannot implicitly convert type 'int' to 'E'. An explicit conversion exists (are you missing a cast?)",
"'F.One' error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (11,13): error CS0029: Cannot implicitly convert type 'bool' to 'E'
// e = false; // error
Diagnostic(ErrorCode.ERR_NoImplicitConv, "false").WithArguments("bool", "E").WithLocation(11, 13),
// (12,13): error CS0266: Cannot implicitly convert type 'char' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (char)(One - 1); // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(char)(One - 1)").WithArguments("char", "E").WithLocation(12, 13),
// (25,13): error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (F)(One - 1); // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(F)(One - 1)").WithArguments("F", "E").WithLocation(25, 13),
// (26,13): error CS0266: Cannot implicitly convert type 'object' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = Null; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "Null").WithArguments("object", "E").WithLocation(26, 13),
// (27,13): error CS0037: Cannot convert null to 'E' because it is a non-nullable value type
// e = null; // error
Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("E").WithLocation(27, 13),
// (30,13): error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = F.Zero; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "F.Zero").WithArguments("F", "E").WithLocation(30, 13),
// (32,13): error CS0029: Cannot implicitly convert type 'bool' to 'E'
// e = true; // error
Diagnostic(ErrorCode.ERR_NoImplicitConv, "true").WithArguments("bool", "E").WithLocation(32, 13),
// (33,13): error CS0266: Cannot implicitly convert type 'char' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (char)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(char)One").WithArguments("char", "E").WithLocation(33, 13),
// (34,13): error CS0266: Cannot implicitly convert type 'sbyte' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (sbyte)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(sbyte)One").WithArguments("sbyte", "E").WithLocation(34, 13),
// (35,13): error CS0266: Cannot implicitly convert type 'byte' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (byte)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(byte)One").WithArguments("byte", "E").WithLocation(35, 13),
// (36,13): error CS0266: Cannot implicitly convert type 'short' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (short)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(short)One").WithArguments("short", "E").WithLocation(36, 13),
// (37,13): error CS0266: Cannot implicitly convert type 'ushort' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (ushort)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(ushort)One").WithArguments("ushort", "E").WithLocation(37, 13),
// (38,13): error CS0266: Cannot implicitly convert type 'int' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (int)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(int)One").WithArguments("int", "E").WithLocation(38, 13),
// (39,13): error CS0266: Cannot implicitly convert type 'uint' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (uint)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(uint)One").WithArguments("uint", "E").WithLocation(39, 13),
// (40,13): error CS0266: Cannot implicitly convert type 'long' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (long)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(long)One").WithArguments("long", "E").WithLocation(40, 13),
// (41,13): error CS0266: Cannot implicitly convert type 'ulong' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (ulong)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(ulong)One").WithArguments("ulong", "E").WithLocation(41, 13),
// (42,13): error CS0266: Cannot implicitly convert type 'decimal' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (decimal)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(decimal)One").WithArguments("decimal", "E").WithLocation(42, 13),
// (43,13): error CS0266: Cannot implicitly convert type 'float' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (float)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(float)One").WithArguments("float", "E").WithLocation(43, 13),
// (44,13): error CS0266: Cannot implicitly convert type 'double' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (double)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(double)One").WithArguments("double", "E").WithLocation(44, 13),
// (46,13): error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = (F)One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(F)One").WithArguments("F", "E").WithLocation(46, 13),
// (47,13): error CS0266: Cannot implicitly convert type 'object' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = new object(); // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "new object()").WithArguments("object", "E").WithLocation(47, 13),
// (48,13): error CS0266: Cannot implicitly convert type 'int' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = 1; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "1").WithArguments("int", "E").WithLocation(48, 13),
// (50,13): error CS0266: Cannot implicitly convert type 'F' to 'E'. An explicit conversion exists (are you missing a cast?)
// e = F.One; // error
Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "F.One").WithArguments("F", "E").WithLocation(50, 13));
}
[Fact]
public void TestControlFlowErrors()
{
TestErrors(@"
string source = @"
class C
{
bool N() { return false; }
......@@ -954,8 +1218,11 @@ void M()
break;
}
}
",
"'break;' error CS0139: No enclosing loop out of which to break or continue");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (8,13): error CS0139: No enclosing loop out of which to break or continue
// break;
Diagnostic(ErrorCode.ERR_NoBreakOrCont, "break;").WithLocation(8, 13));
}
[Fact]
......@@ -984,7 +1251,7 @@ void M()
[Fact]
public void TestArrayCreationErrors()
{
TestErrors(@"
string source = @"
class C
{
int P { set { } }
......@@ -1007,19 +1274,44 @@ void M()
int[] intArray11 = new int[] { P }; // write-only property
}
}
",
"'{ 1, 2, 3 }' error CS0847: An array initializer of length '2' is expected",
"'4' error CS0846: A nested array initializer is expected",
"'5' error CS0846: A nested array initializer is expected",
"'6' error CS0846: A nested array initializer is expected",
"'{ 9 }' error CS0847: An array initializer of length '2' is expected",
"'x' error CS0150: A constant value is expected",
"'{ 13, 14 }' error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.",
"'{ 15, 16 }' error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.",
"'System' error CS0118: 'System' is a namespace but is used like a variable",
"'System.Int64' error CS0119: 'long' is a type, which is not valid in the given context",
"'{ 17, 18, 19 }' error CS0622: Can only use array initializer expressions to assign to array types. Try using a new expression instead.",
"'P' error CS0154: The property or indexer 'C.P' cannot be used in this context because it lacks the get accessor");
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (8,38): error CS0847: An array initializer of length '2' is expected
// int[] intArray1 = new int[2] { 1, 2, 3 }; // count mismatch
Diagnostic(ErrorCode.ERR_ArrayInitializerIncorrectLength, "{ 1, 2, 3 }").WithArguments("2").WithLocation(8, 38),
// (9,41): error CS0846: A nested array initializer is expected
// int[,] intArray2 = new int[,] { 4, 5, 6 }; // missing initializer
Diagnostic(ErrorCode.ERR_ArrayInitializerExpected, "4").WithLocation(9, 41),
// (9,44): error CS0846: A nested array initializer is expected
// int[,] intArray2 = new int[,] { 4, 5, 6 }; // missing initializer
Diagnostic(ErrorCode.ERR_ArrayInitializerExpected, "5").WithLocation(9, 44),
// (9,47): error CS0846: A nested array initializer is expected
// int[,] intArray2 = new int[,] { 4, 5, 6 }; // missing initializer
Diagnostic(ErrorCode.ERR_ArrayInitializerExpected, "6").WithLocation(9, 47),
// (10,51): error CS0847: An array initializer of length '2' is expected
// int[,] intArray3 = new int[,] { { 7, 8 }, { 9 } }; // inconsistent size
Diagnostic(ErrorCode.ERR_ArrayInitializerIncorrectLength, "{ 9 }").WithArguments("2").WithLocation(10, 51),
// (14,35): error CS0150: A constant value is expected
// int[] intArray5 = new int[x] { 10, 11, 12 }; // non-constant size with initializer
Diagnostic(ErrorCode.ERR_ConstantExpected, "x").WithLocation(14, 35),
// (15,40): error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
// int[] intArray6 = new int[2] { { 13, 14 }, { 15, 16 } }; // unexpected initializer
Diagnostic(ErrorCode.ERR_ArrayInitInBadPlace, "{ 13, 14 }").WithLocation(15, 40),
// (15,52): error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
// int[] intArray6 = new int[2] { { 13, 14 }, { 15, 16 } }; // unexpected initializer
Diagnostic(ErrorCode.ERR_ArrayInitInBadPlace, "{ 15, 16 }").WithLocation(15, 52),
// (17,35): error CS0118: 'System' is a namespace but is used like a variable
// int[] intArray8 = new int[System]; // expected value, not namespace
Diagnostic(ErrorCode.ERR_BadSKknown, "System").WithArguments("System", "namespace", "variable").WithLocation(17, 35),
// (18,39): error CS0119: 'long' is a type, which is not valid in the given context
// int[] intArray9 = new int[] { System.Int64 }; // expected value, not type
Diagnostic(ErrorCode.ERR_BadSKunknown, "System.Int64").WithArguments("long", "type").WithLocation(18, 39),
// (19,26): error CS0622: Can only use array initializer expressions to assign to array types. Try using a new expression instead.
// int intArray10 = { 17, 18, 19 }; // int is not an array type
Diagnostic(ErrorCode.ERR_ArrayInitToNonArrayType, "{ 17, 18, 19 }").WithLocation(19, 26),
// (21,40): error CS0154: The property or indexer 'C.P' cannot be used in this context because it lacks the get accessor
// int[] intArray11 = new int[] { P }; // write-only property
Diagnostic(ErrorCode.ERR_PropertyLacksGet, "P").WithArguments("C.P").WithLocation(21, 40));
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册