提交 e36c388c 编写于 作者: G Gen Lu 提交者: GitHub

Merge pull request #19674 from genlu/arglistFix

Fix crash when creating IArgument for `__arglist` argument
......@@ -515,12 +515,31 @@ private static bool IsSafeForReordering(BoundExpression expression, RefKind kind
//
// If neither of those are the case then we can just take an early out.
if (CanSkipRewriting(arguments, methodOrIndexer, expanded, argsToParamsOpt, invokedAsExtensionMethod, out var isComReceiver))
{
if (CanSkipRewriting(arguments, methodOrIndexer, expanded, argsToParamsOpt, invokedAsExtensionMethod, out var _))
{
// In this case, the invocation is not in expanded form and there's no named argument provided.
// So we just return list of arguments as is.
return arguments.ZipAsArray(methodOrIndexer.GetParameters(), (a, p) => BoundCall.CreateArgumentOperation(ArgumentKind.Explicit, p, a));
ImmutableArray<ParameterSymbol> parameters = methodOrIndexer.GetParameters();
ArrayBuilder<IArgument> argumentsBuilder = ArrayBuilder<IArgument>.GetInstance(arguments.Length);
int i = 0;
for (; i < parameters.Length; ++i)
{
argumentsBuilder.Add(BoundCall.CreateArgumentOperation(ArgumentKind.Explicit, parameters[i], arguments[i]));
}
// TODO: In case of __arglist, we will have more arguments than parameters,
// set the parameter to null for __arglist argument for now.
// https://github.com/dotnet/roslyn/issues/19673
for (; i < arguments.Length; ++i)
{
argumentsBuilder.Add(BoundCall.CreateArgumentOperation(ArgumentKind.Explicit, null, arguments[i]));
}
Debug.Assert(methodOrIndexer.GetIsVararg() ^ parameters.Length == arguments.Length);
return argumentsBuilder.ToImmutableAndFree();
}
return BuildArgumentsInEvaluationOrder(syntax,
......@@ -558,7 +577,7 @@ private static bool IsSafeForReordering(BoundExpression expression, RefKind kind
((MethodSymbol)methodOrIndexer).Parameters[0].Type as NamedTypeSymbol :
methodOrIndexer.ContainingType;
isComReceiver = (object)receiverNamedType != null && receiverNamedType.IsComImport;
isComReceiver = (object)receiverNamedType != null && receiverNamedType.IsComImport;
return rewrittenArguments.Length == methodOrIndexer.GetParameterCount() &&
argsToParamsOpt.IsDefault &&
......
......@@ -990,6 +990,45 @@ void M2(string x )
VerifyOperationTreeAndDiagnosticsForTest<InvocationExpressionSyntax>(source, expectedOperationTree, expectedDiagnostics);
}
[Fact]
public void VarArgsCall()
{
string source = @"
using System;
public class P
{
void M()
{
/*<bind>*/Console.Write(""{0} {1} {2} {3} {4}"", 1, 2, 3, 4, __arglist(5))/*</bind>*/;
}
}
";
string expectedOperationTree = @"
IInvocationExpression (static void System.Console.Write(System.String format, System.Object arg0, System.Object arg1, System.Object arg2, System.Object arg3, __arglist)) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'Console.Wri ... arglist(5))')
Arguments(6): IArgument (ArgumentKind.Explicit, Matching Parameter: format) (OperationKind.Argument) (Syntax: '""{0} {1} {2} {3} {4}""')
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: ""{0} {1} {2} {3} {4}"") (Syntax: '""{0} {1} {2} {3} {4}""')
IArgument (ArgumentKind.Explicit, Matching Parameter: arg0) (OperationKind.Argument) (Syntax: '1')
IConversionExpression (ConversionKind.Cast, Implicit) (OperationKind.ConversionExpression, Type: System.Object) (Syntax: '1')
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1')
IArgument (ArgumentKind.Explicit, Matching Parameter: arg1) (OperationKind.Argument) (Syntax: '2')
IConversionExpression (ConversionKind.Cast, Implicit) (OperationKind.ConversionExpression, Type: System.Object) (Syntax: '2')
ILiteralExpression (Text: 2) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2) (Syntax: '2')
IArgument (ArgumentKind.Explicit, Matching Parameter: arg2) (OperationKind.Argument) (Syntax: '3')
IConversionExpression (ConversionKind.Cast, Implicit) (OperationKind.ConversionExpression, Type: System.Object) (Syntax: '3')
ILiteralExpression (Text: 3) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 3) (Syntax: '3')
IArgument (ArgumentKind.Explicit, Matching Parameter: arg3) (OperationKind.Argument) (Syntax: '4')
IConversionExpression (ConversionKind.Cast, Implicit) (OperationKind.ConversionExpression, Type: System.Object) (Syntax: '4')
ILiteralExpression (Text: 4) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 4) (Syntax: '4')
IArgument (ArgumentKind.Explicit, Matching Parameter: null) (OperationKind.Argument, IsInvalid) (Syntax: '__arglist(5)')
IOperation: (OperationKind.None) (Syntax: '__arglist(5)')
Children(1): ILiteralExpression (Text: 5) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 5) (Syntax: '5')
";
var expectedDiagnostics = DiagnosticDescription.None;
VerifyOperationTreeAndDiagnosticsForTest<InvocationExpressionSyntax>(source, expectedOperationTree, expectedDiagnostics);
}
[Fact]
public void InvalidConversionForDefaultArgument_InSource()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册