Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when combining reordered arguments with params array #60590

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,18 @@ ImmutableArray<int> makeSourceIndices()
}
}

// After we do https://github.com/dotnet/roslyn/issues/49602, this assert can be
// simplified to `argsToParamsOpt.IsDefault || argsToParamsOpt == lengthAfterRewriting`.
Debug.Assert(argsToParamsOpt.IsDefault
|| argsToParamsOpt.Length == lengthAfterRewriting
// in expanded scenarios, lengthAfterRewriting can only be larger than argsToParamsOpt by 1--otherwise it will be the same size or smaller
|| (boundAttribute.ConstructorExpanded && lengthAfterRewriting - argsToParamsOpt.Length <= 1));

var constructorArgumentSourceIndices = ArrayBuilder<int>.GetInstance(lengthAfterRewriting);
constructorArgumentSourceIndices.Count = lengthAfterRewriting;
for (int argIndex = 0; argIndex < lengthAfterRewriting; argIndex++)
{
int paramIndex = argsToParamsOpt.IsDefault ? argIndex : argsToParamsOpt[argIndex];
int paramIndex = argsToParamsOpt.IsDefault || argIndex >= argsToParamsOpt.Length ? argIndex : argsToParamsOpt[argIndex];
constructorArgumentSourceIndices[paramIndex] = defaultArguments[argIndex] ? -1 : argIndex;
}
return constructorArgumentSourceIndices.ToImmutableAndFree();
Expand Down
70 changes: 70 additions & 0 deletions src/Compilers/CSharp/Test/Emit2/Attributes/AttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,76 @@ void M0()
.VerifyDiagnostics();
}

[Fact]
[WorkItem(60572, "https://github.com/dotnet/roslyn/issues/60572")]
public void TestParamArrayWithReorderedArguments_01()
{
var source = @"
using System;

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
class Attr : Attribute
{
public Attr(bool b, int i, params object?[] parameters) { }
}

[Attr(i: 1, b: true)]
[Attr(i: 1, b: true, parameters: ""a"")]
class Program { }
";
var comp = CreateCompilation(source, options: WithNullableEnable());
comp.VerifyDiagnostics();

var program = comp.GetMember<NamedTypeSymbol>("Program");
var attrs = program.GetAttributes();
Assert.Equal(2, attrs.Length);

var arguments0 = attrs[0].ConstructorArguments.ToArray();
Assert.Equal(3, arguments0.Length);
Assert.Equal(true, arguments0[0].Value);
Assert.Equal(1, arguments0[1].Value);
Assert.Empty(arguments0[2].Values);

var arguments1 = attrs[1].ConstructorArguments.ToArray();
Assert.Equal(3, arguments1.Length);
Assert.Equal(true, arguments1[0].Value);
Assert.Equal(1, arguments1[1].Value);
Assert.Equal("a", arguments1[2].Values.Single().Value);
}

[Fact]
[WorkItem(60572, "https://github.com/dotnet/roslyn/issues/60572")]
public void TestParamArrayWithReorderedArguments_02()
{
var source = @"
using System;

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
class Attr : Attribute
{
public Attr(bool b, int i, params object?[] parameters) { }
}

[Attr(i: 1, b: true, ""a"")]
[Attr(i: 1, b: true, ""a"", ""b"")]
class Program { }
";
var comp = CreateCompilation(source, options: WithNullableEnable());
comp.VerifyDiagnostics(
// (10,7): error CS8323: Named argument 'i' is used out-of-position but is followed by an unnamed argument
// [Attr(i: 1, b: true, "a")]
Diagnostic(ErrorCode.ERR_BadNonTrailingNamedArgument, "i").WithArguments("i").WithLocation(10, 7),
// (11,7): error CS8323: Named argument 'i' is used out-of-position but is followed by an unnamed argument
// [Attr(i: 1, b: true, "a", "b")]
Diagnostic(ErrorCode.ERR_BadNonTrailingNamedArgument, "i").WithArguments("i").WithLocation(11, 7));

var program = comp.GetMember<NamedTypeSymbol>("Program");
var attrs = program.GetAttributes();
Assert.Equal(2, attrs.Length);
Assert.Empty(attrs[0].ConstructorArguments);
Assert.Empty(attrs[1].ConstructorArguments);
}

[Fact]
[WorkItem(20741, "https://github.com/dotnet/roslyn/issues/20741")]
public void TestNamedArgumentOnObjectParamsArgument()
Expand Down