-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Support ref struct implementing interfaces in foreach
and using
statements
#72810
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1069,6 +1069,12 @@ private EnumeratorResult SatisfiesIEnumerableInterfaces(SyntaxNode collectionSyn | |
Debug.Assert((object)builder.CollectionType != null); | ||
|
||
NamedTypeSymbol collectionType = (NamedTypeSymbol)builder.CollectionType; | ||
|
||
if (unwrappedCollectionExprType.IsRefLikeType || unwrappedCollectionExprType is TypeParameterSymbol { AllowByRefLike: true }) | ||
{ | ||
builder.CollectionType = unwrappedCollectionExprType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels fragile. We change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I do not find this too fragile. Also, there are early returns from the |
||
} | ||
|
||
if (collectionType.IsGenericType) | ||
{ | ||
// If the type is generic, we have to search for the methods | ||
|
@@ -1188,14 +1194,12 @@ private void GetDisposalInfoForEnumerator(SyntaxNode syntax, ref ForEachEnumerat | |
// is potentially disposable. | ||
|
||
TypeSymbol enumeratorType = builder.GetEnumeratorInfo.Method.ReturnType; | ||
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics); | ||
|
||
MethodSymbol patternDisposeMethod = null; | ||
if (enumeratorType.IsRefLikeType || isAsync) | ||
{ | ||
// we throw away any binding diagnostics, and assume it's not disposable if we encounter errors | ||
var receiver = new BoundDisposableValuePlaceholder(syntax, enumeratorType); | ||
patternDisposeMethod = TryFindDisposePatternMethod(receiver, syntax, isAsync, BindingDiagnosticBag.Discarded, out bool expanded); | ||
MethodSymbol patternDisposeMethod = TryFindDisposePatternMethod(receiver, syntax, isAsync, BindingDiagnosticBag.Discarded, out bool expanded); | ||
if (patternDisposeMethod is object) | ||
{ | ||
Debug.Assert(!patternDisposeMethod.IsExtensionMethod); | ||
|
@@ -1226,22 +1230,44 @@ private void GetDisposalInfoForEnumerator(SyntaxNode syntax, ref ForEachEnumerat | |
// We already checked feature availability for async scenarios | ||
CheckFeatureAvailability(expr.Syntax, MessageID.IDS_FeatureDisposalPattern, diagnostics); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
if (!enumeratorType.IsRefLikeType && patternDisposeMethod is null) | ||
if (implementsInterface(enumeratorType, isAsync, diagnostics)) | ||
{ | ||
// If it wasn't pattern-disposable, see if it's directly convertable to IDisposable | ||
// For async foreach, we don't do the runtime check in unsealed case | ||
if ((!enumeratorType.IsSealed && !isAsync) || | ||
this.Conversions.ClassifyImplicitConversionFromType(enumeratorType, | ||
isAsync ? this.Compilation.GetWellKnownType(WellKnownType.System_IAsyncDisposable) : this.Compilation.GetSpecialType(SpecialType.System_IDisposable), | ||
ref useSiteInfo).IsImplicit) | ||
builder.NeedsDisposal = true; | ||
return; | ||
} | ||
|
||
if (!enumeratorType.IsSealed && !isAsync) // For async foreach, we don't do the runtime check in unsealed case | ||
{ | ||
Debug.Assert(!enumeratorType.IsRefLikeType); // Ref like types are supposed to be structs, therefore, sealed. | ||
|
||
if (enumeratorType is TypeParameterSymbol { AllowByRefLike: true }) | ||
{ | ||
Error(diagnostics, ErrorCode.ERR_BadAllowByRefLikeEnumerator, expr.Syntax, enumeratorType); | ||
} | ||
else | ||
{ | ||
builder.NeedsDisposal = true; | ||
} | ||
} | ||
|
||
bool implementsInterface(TypeSymbol enumeratorType, bool isAsync, BindingDiagnosticBag diagnostics) | ||
{ | ||
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics); | ||
|
||
NamedTypeSymbol targetInterface = isAsync ? this.Compilation.GetWellKnownType(WellKnownType.System_IAsyncDisposable) : this.Compilation.GetSpecialType(SpecialType.System_IDisposable); | ||
|
||
bool result = this.Conversions.HasImplicitConversionToOrImplementsVarianceCompatibleInterface(enumeratorType, | ||
targetInterface, | ||
ref useSiteInfo); | ||
|
||
diagnostics.Add(syntax, useSiteInfo); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
|
@@ -1736,8 +1762,9 @@ private bool AllInterfacesContainsIEnumerable( | |
var implementedNonGeneric = this.Compilation.GetSpecialType(SpecialType.System_Collections_IEnumerable); | ||
if ((object)implementedNonGeneric != null) | ||
{ | ||
var conversion = this.Conversions.ClassifyImplicitConversionFromType(type, implementedNonGeneric, ref useSiteInfo); | ||
if (conversion.IsImplicit) | ||
var implements = this.Conversions.HasImplicitConversionToOrImplementsVarianceCompatibleInterface(type, implementedNonGeneric, ref useSiteInfo); | ||
|
||
if (implements) | ||
{ | ||
implementedIEnumerable = implementedNonGeneric; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the spec-let update the foreach statement definition of collection type for
ref struct
andallows ref struct
cases?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate please? What are you suggesting to modify and where?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should dotnet/csharplang#8024 include a section that updates the foreach statement definition to indicate the expected collection type, etc. for
ref struct
cases and type parameters withallows ref struct
? Currently, the existing spec section seems to indicate the collection type will beIEnumerable<T>
orIEnumerable
. Are those correct, given how collection type, etc. is used in theforeach
expansion and perhaps elsewhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am satisfied with how dotnet/csharplang#8024 specifies the expected behavior. I will mention that foreach statement will have to be adjusted accordingly. However, I prefer leaving this exercise for later and quite possibly for the one changing the official spec.