Skip to content

Commit ba0c208

Browse files
authored
Disallow ref like types as iterator element types. (#75593)
Fixes #75569. Fixes #75577.
1 parent 048a23f commit ba0c208

19 files changed

+253
-1
lines changed

src/Compilers/CSharp/Portable/Binder/ExecutableCodeBinder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ public static void ValidateIteratorMethod(CSharpCompilation compilation, MethodS
147147
Error(diagnostics, ErrorCode.ERR_BadIteratorReturn, errorLocation, iterator, returnType);
148148
}
149149
}
150+
else if (elementType.IsRefLikeOrAllowsRefLikeType())
151+
{
152+
Error(diagnostics, ErrorCode.ERR_IteratorRefLikeElementType, errorLocation);
153+
}
150154

151155
bool asyncInterface = InMethodBinder.IsAsyncStreamInterface(compilation, refKind, returnType);
152156
if (asyncInterface && !iterator.IsAsync)

src/Compilers/CSharp/Portable/CSharpResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8014,4 +8014,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
80148014
<data name="WRN_AccessorDoesNotUseBackingField_Title" xml:space="preserve">
80158015
<value>Property accessor should use 'field' because the other accessor is using it.</value>
80168016
</data>
8017+
<data name="ERR_IteratorRefLikeElementType" xml:space="preserve">
8018+
<value>Element type of an iterator may not be a ref struct or a type parameter allowing ref structs</value>
8019+
</data>
80178020
</root>

src/Compilers/CSharp/Portable/Errors/ErrorCode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,8 @@ internal enum ErrorCode
23492349
WRN_UnassignedInternalRefField = 9265,
23502350
WRN_AccessorDoesNotUseBackingField = 9266,
23512351

2352+
ERR_IteratorRefLikeElementType = 9267,
2353+
23522354
// Note: you will need to do the following after adding errors:
23532355
// 1) Update ErrorFacts.IsBuildOnlyDiagnostic (src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs)
23542356

src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,7 @@ or ErrorCode.ERR_PartialPropertyDuplicateInitializer
24662466
or ErrorCode.WRN_UninitializedNonNullableBackingField
24672467
or ErrorCode.WRN_UnassignedInternalRefField
24682468
or ErrorCode.WRN_AccessorDoesNotUseBackingField
2469+
or ErrorCode.ERR_IteratorRefLikeElementType
24692470
=> false,
24702471
};
24712472
#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.

src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)