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

Disallow ref like types as iterator element types. #75593

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public static void ValidateIteratorMethod(CSharpCompilation compilation, MethodS
Error(diagnostics, ErrorCode.ERR_BadIteratorReturn, errorLocation, iterator, returnType);
}
}
else if (elementType.IsRefLikeOrAllowsRefLikeType())
{
Error(diagnostics, ErrorCode.ERR_IteratorRefLikeElementType, errorLocation);
}

bool asyncInterface = InMethodBinder.IsAsyncStreamInterface(compilation, refKind, returnType);
if (asyncInterface && !iterator.IsAsync)
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 @@ -8014,4 +8014,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="WRN_AccessorDoesNotUseBackingField_Title" xml:space="preserve">
<value>Property accessor should use 'field' because the other accessor is using it.</value>
</data>
<data name="ERR_IteratorRefLikeElementType" xml:space="preserve">
<value>Element type of an iterator may not be a ref struct or a type parameter allowing ref structs</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,8 @@ internal enum ErrorCode
WRN_UnassignedInternalRefField = 9265,
WRN_AccessorDoesNotUseBackingField = 9266,

ERR_IteratorRefLikeElementType = 9267,

// Note: you will need to do the following after adding errors:
// 1) Update ErrorFacts.IsBuildOnlyDiagnostic (src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs)

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 @@ -2466,6 +2466,7 @@ or ErrorCode.ERR_PartialPropertyDuplicateInitializer
or ErrorCode.WRN_UninitializedNonNullableBackingField
or ErrorCode.WRN_UnassignedInternalRefField
or ErrorCode.WRN_AccessorDoesNotUseBackingField
or ErrorCode.ERR_IteratorRefLikeElementType
=> false,
};
#pragma warning restore CS8524 // The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value.
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.

171 changes: 171 additions & 0 deletions src/Compilers/CSharp/Test/Emit3/RefStructInterfacesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28992,5 +28992,176 @@ public ReadOnlySpan<byte> GetDirectBuffer2()
Diagnostic(ErrorCode.ERR_RefReturnStructThis, "directBuffer").WithLocation(2000, 16)
);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75569")]
[WorkItem("https://github.com/dotnet/roslyn/issues/75577")]
public void Iterator_01()
{
var source =
@"
using System;
using System.Collections.Generic;

static class CSharpCompilerCrash
{
public static IEnumerable<ReadOnlySpan<char>> Lines(string data)
{
yield break;
}
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net90);
comp.VerifyEmitDiagnostics(
// (7,51): error CS9266: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
// public static IEnumerable<ReadOnlySpan<char>> Lines(string data)
Diagnostic(ErrorCode.ERR_IteratorRefLikeElementType, "Lines").WithLocation(7, 51)
);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75569")]
[WorkItem("https://github.com/dotnet/roslyn/issues/75577")]
public void Iterator_02()
{
var source =
@"
using System.Collections.Generic;

static class CSharpCompilerCrash
{
static IEnumerable<A> B()
{
yield break;
}

ref struct A;
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net90);
comp.VerifyEmitDiagnostics(
// (6,27): error CS9266: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
// static IEnumerable<A> B()
Diagnostic(ErrorCode.ERR_IteratorRefLikeElementType, "B").WithLocation(6, 27)
);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75569")]
[WorkItem("https://github.com/dotnet/roslyn/issues/75577")]
public void Iterator_03()
{
var source =
@"
using System.Collections.Generic;

static class CSharpCompilerCrash
{
static IEnumerator<A> B
{
get
{
yield break;
}
}

ref struct A;
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net90);
comp.VerifyEmitDiagnostics(
// (8,9): error CS9266: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
// get
Diagnostic(ErrorCode.ERR_IteratorRefLikeElementType, "get").WithLocation(8, 9)
);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75569")]
[WorkItem("https://github.com/dotnet/roslyn/issues/75577")]
public void Iterator_04()
{
var source =
@"
#pragma warning disable CS1998 // This async method lacks 'await' operators

using System.Collections.Generic;

static class CSharpCompilerCrash
{
static async IAsyncEnumerable<RefStructA> B()
{
yield return default;
yield break;
}

ref struct RefStructA;
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net90);
comp.VerifyDiagnostics(
// (8,47): error CS9266: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
// static async IAsyncEnumerable<RefStructA> B()
Diagnostic(ErrorCode.ERR_IteratorRefLikeElementType, "B").WithLocation(8, 47)
);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75569")]
[WorkItem("https://github.com/dotnet/roslyn/issues/75577")]
public void Iterator_05()
{
var source =
@"
#pragma warning disable CS1998 // This async method lacks 'await' operators

using System.Collections.Generic;

static class CSharpCompilerCrash
{
static async IAsyncEnumerator<RefStructA> B()
{
yield return default;
yield break;
}

ref struct RefStructA;
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net90);
comp.VerifyDiagnostics(
// (8,47): error CS9266: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
// static async IAsyncEnumerator<RefStructA> B()
Diagnostic(ErrorCode.ERR_IteratorRefLikeElementType, "B").WithLocation(8, 47)
);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75569")]
[WorkItem("https://github.com/dotnet/roslyn/issues/75577")]
public void Iterator_06()
{
var source =
@"
#pragma warning disable CS1998 // This async method lacks 'await' operators

using System.Collections.Generic;

static class CSharpCompilerCrash
{
static async IAsyncEnumerator<T> B<T>() where T : allows ref struct
{
yield return default;
yield break;
}
}
";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net90);
comp.VerifyDiagnostics(
// (8,38): error CS9266: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
// static async IAsyncEnumerator<T> B<T>() where T : allows ref struct
Diagnostic(ErrorCode.ERR_IteratorRefLikeElementType, "B").WithLocation(8, 38)
);
}
}
}
Loading