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

Collection expressions - error when we can't emit good codegen for ImmutableArray #70384

Merged
merged 2 commits into from
Oct 16, 2023
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: 9 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,15 @@ private BoundExpression ConvertCollectionExpression(
ReportDiagnosticsIfUnmanagedCallersOnly(diagnostics, collectionBuilderMethod, syntax, isDelegateConversion: false);
}
break;

case CollectionExpressionTypeKind.ImplementsIEnumerableT:
case CollectionExpressionTypeKind.ImplementsIEnumerable:
if (targetType.OriginalDefinition.Equals(Compilation.GetWellKnownType(WellKnownType.System_Collections_Immutable_ImmutableArray_T), TypeCompareKind.ConsiderEverything))
{
diagnostics.Add(ErrorCode.ERR_CollectionExpressionImmutableArray, syntax, targetType.OriginalDefinition);
return BindCollectionExpressionForErrorRecovery(node, targetType, diagnostics);
}
break;
}

var elements = node.Elements;
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7821,4 +7821,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_ExpectedInterpolatedString" xml:space="preserve">
<value>Expected interpolated string</value>
</data>
<data name="ERR_CollectionExpressionImmutableArray" xml:space="preserve">
<value>This version of '{0}' cannot be used with collection expressions.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,7 @@ internal enum ErrorCode

WRN_CollectionExpressionRefStructMayAllocate = 9208,
WRN_CollectionExpressionRefStructSpreadMayAllocate = 9209,
ERR_CollectionExpressionImmutableArray = 9210,

#endregion

Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_InterceptorGlobalNamespace:
case ErrorCode.WRN_CollectionExpressionRefStructMayAllocate:
case ErrorCode.WRN_CollectionExpressionRefStructSpreadMayAllocate:
case ErrorCode.ERR_CollectionExpressionImmutableArray:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21999,6 +21999,94 @@ .locals init (System.Collections.Generic.List<int> V_0,
""");
}

[Fact]
public void ImmutableArray_06()
{
string sourceA = """
using System.Collections.Immutable;

class Program
{
static void Main()
{
ImmutableArray<int> arr = [1, 2, 3];
arr.Report();
}
}
""";

var comp = CreateCompilation(new[] { sourceA, s_collectionExtensions }, targetFramework: TargetFramework.Net60);
comp.VerifyEmitDiagnostics(
// 0.cs(7,35): error CS9210: This version of 'ImmutableArray<T>' cannot be used with collection expressions.
// ImmutableArray<int> arr = [1, 2, 3];
Diagnostic(ErrorCode.ERR_CollectionExpressionImmutableArray, "[1, 2, 3]").WithArguments("System.Collections.Immutable.ImmutableArray<T>").WithLocation(7, 35));

// Can work around this error in downlevel scenarios by defining ImmutableCollectionsMarshal.AsImmutableArray
string sourceB = """
using System.Collections.Immutable;

namespace System.Runtime.InteropServices
{
public static class ImmutableCollectionsMarshal
{
// nb: the real implementation of this would use an unsafe cast
public static ImmutableArray<T> AsImmutableArray<T>(T[] array) => ImmutableArray.Create(array);
}
}
""";

var verifier = CompileAndVerify(new[] { sourceA, sourceB, s_collectionExtensions }, targetFramework: TargetFramework.Net60, verify: Verification.Skipped, expectedOutput: IncludeExpectedOutput("[1, 2, 3],"));
verifier.VerifyDiagnostics();
verifier.VerifyIL("Program.Main", """
{
// Code size 34 (0x22)
.maxstack 3
IL_0000: ldc.i4.3
IL_0001: newarr "int"
IL_0006: dup
IL_0007: ldtoken "<PrivateImplementationDetails>.__StaticArrayInitTypeSize=12 <PrivateImplementationDetails>.4636993D3E1DA4E9D6B8F87B79E8F7C6D018580D52661950EABC3845C5897A4D"
IL_000c: call "void System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray(System.Array, System.RuntimeFieldHandle)"
IL_0011: call "System.Collections.Immutable.ImmutableArray<int> System.Runtime.InteropServices.ImmutableCollectionsMarshal.AsImmutableArray<int>(int[])"
IL_0016: box "System.Collections.Immutable.ImmutableArray<int>"
IL_001b: ldc.i4.0
IL_001c: call "void CollectionExtensions.Report(object, bool)"
IL_0021: ret
}
""");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Consider adding a test for the non-generic IEnumerable case, perhaps by compiling with TargetFramework.Mscorlib40 and

namespace System.Collections.Immutable
{
    struct ImmutableArray<T> : IEnumerable
    {
        public void Add(T t) { }
        IEnumerator IEnumerable.GetEnumerator() => null;
    }
}


[Fact]
public void ImmutableArray_07()
{
// Test an ImmutableArray<T> which implements only non-generic IEnumerable.
string sourceA = """
using System.Collections.Immutable;

class Program
{
static void Main()
{
ImmutableArray<int> arr = [1, 2, 3];
}
}

namespace System.Collections.Immutable
{
struct ImmutableArray<T> : IEnumerable
{
public void Add(T t) { }
IEnumerator IEnumerable.GetEnumerator() => null;
}
}
""";

var comp = CreateCompilation(sourceA, targetFramework: TargetFramework.Mscorlib40);
comp.VerifyEmitDiagnostics(
// 0.cs(7,35): error CS9210: This version of 'ImmutableArray<T>' cannot be used with collection expressions.
// ImmutableArray<int> arr = [1, 2, 3];
Diagnostic(ErrorCode.ERR_CollectionExpressionImmutableArray, "[1, 2, 3]").WithArguments("System.Collections.Immutable.ImmutableArray<T>").WithLocation(7, 35));
}

[Fact]
public void SpanImplicitAllocationWarning_01()
{
Expand Down