提交 6ab2391a 编写于 作者: V vsadov

Added more tests

上级 28b182d8
......@@ -4272,7 +4272,7 @@ private BoundExpression BindUnexpectedComplexElementInitializer(InitializerExpre
if (implicitReceiver.Type.IsDynamic())
{
var hasErrors = ReportBadDynamicArguments(elementInitializer, boundElementInitializerExpressions, default, diagnostics, queryClause: null);
var hasErrors = ReportBadDynamicArguments(elementInitializer, boundElementInitializerExpressions, refKinds: default, diagnostics, queryClause: null);
return new BoundDynamicCollectionElementInitializer(
elementInitializer,
......
......@@ -6182,6 +6182,44 @@ public void test()
expectedOutput: expectedOutput);
}
[Fact]
public void InArguments()
{
const string source = @"
using System;
using System.Linq.Expressions;
class C : TestBase
{
readonly static int x = 1;
readonly static int y = 2;
public static int TakesIn(in int x) => x;
public static void Main(string[] args)
{
// writeable field
Expression<Func<int>> e1 = () => TakesIn(x);
System.Console.Write(e1.Compile()());
// readonly field
Expression<Func<int>> e2 = () => TakesIn(in y);
System.Console.Write(e2.Compile()());
// constant
Expression<Func<int>> e3 = () => TakesIn(3);
Check<int>(e3, ""Call(null.[Int32 TakesIn(Int32 ByRef)](Constant(3 Type:System.Int32)) Type:System.Int32)"");
System.Console.Write(e3.Compile()());
}
}";
const string expectedOutput = @"123";
CompileAndVerify(
new[] { source, ExpressionTestLibrary },
new[] { ExpressionAssemblyRef },
expectedOutput: expectedOutput);
}
#endregion Regression Tests
#region helpers
......
......@@ -3954,5 +3954,113 @@ static void M1()
Diagnostic(ErrorCode.ERR_InDynamicMethodArg, "x").WithLocation(8, 31)
);
}
[WorkItem(22813, "https://github.com/dotnet/roslyn/issues/22813")]
[Fact]
public void InArgumentDynamicLocalFunction()
{
string source = @"
class C
{
private static void M1(in dynamic x, int y, in dynamic z) => System.Console.WriteLine(x == y);
static void Main()
{
dynamic d = 1;
// Produce an error. This cannot work correctly right now
M1(in d, d = 2, in d);
void M2(in dynamic x, int y, in dynamic z) => System.Console.WriteLine(x == y);
// NOTE: the following could work!!!
//
// Currently any kind of overloading that would require dynamic dispatch is not permitted
// for locals functions and dynamic dispatch is bypassed.
//
// We will still give an error for consistency with the case where the method is an ordinary private method.
// (and also in case if overloading restrictions are relaxed in the future and dispatch becomes necessary)
//
M2(in d, d = 3, in d);
}
}
";
var comp = CreateCompilationWithMscorlib45AndCSruntime(source, parseOptions: TestOptions.Regular7_2);
comp.VerifyEmitDiagnostics(
// (11,15): error CS8364: Arguments with 'in' modifier cannot be used in dynamically dispatched expessions.
// M1(in d, d = 2, in d);
Diagnostic(ErrorCode.ERR_InDynamicMethodArg, "d").WithLocation(11, 15),
// (11,28): error CS8364: Arguments with 'in' modifier cannot be used in dynamically dispatched expessions.
// M1(in d, d = 2, in d);
Diagnostic(ErrorCode.ERR_InDynamicMethodArg, "d").WithLocation(11, 28),
// (23,15): error CS8364: Arguments with 'in' modifier cannot be used in dynamically dispatched expessions.
// M2(in d, d = 3, in d);
Diagnostic(ErrorCode.ERR_InDynamicMethodArg, "d").WithLocation(23, 15),
// (23,28): error CS8364: Arguments with 'in' modifier cannot be used in dynamically dispatched expessions.
// M2(in d, d = 3, in d);
Diagnostic(ErrorCode.ERR_InDynamicMethodArg, "d").WithLocation(23, 28)
);
}
[WorkItem(22813, "https://github.com/dotnet/roslyn/issues/22813")]
[Fact]
public void InArgumentDynamicCtor()
{
string source = @"
class C
{
static void Main()
{
int x = 42;
dynamic d = 1;
var y = new M2(d, in x);
}
class M2
{
public M2(int a, in int d) => System.Console.Write(1);
public M2(int a, int d) => System.Console.Write(2);
}
}";
var comp = CreateCompilationWithMscorlib45AndCSruntime(source, parseOptions: TestOptions.Regular7_2);
comp.VerifyEmitDiagnostics(
// (8,30): error CS8364: Arguments with 'in' modifier cannot be used in dynamically dispatched expessions.
// var y = new M2(d, in x);
Diagnostic(ErrorCode.ERR_InDynamicMethodArg, "x").WithLocation(8, 30)
);
}
[WorkItem(22813, "https://github.com/dotnet/roslyn/issues/22813")]
[Fact]
public void InArgumentDynamicIndexer()
{
string source = @"
class C
{
static void Main()
{
int x = 42;
dynamic d = new C1();
System.Console.WriteLine(d[in x]);
}
class C1
{
public int this[in int x] => x;
}
}";
var comp = CreateCompilationWithMscorlib45AndCSruntime(source, parseOptions: TestOptions.Regular7_2);
comp.VerifyEmitDiagnostics(
// (8,39): error CS8364: Arguments with 'in' modifier cannot be used in dynamically dispatched expessions.
// System.Console.WriteLine(d[in x]);
Diagnostic(ErrorCode.ERR_InDynamicMethodArg, "x").WithLocation(8, 39)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册