-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Switch implicit span to conversion from type #74232
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
@@ -486,18 +487,63 @@ void checkConstraintLanguageVersionAndRuntimeSupportForConversion(SyntaxNode syn | |
} | ||
else if (conversion.IsSpan) | ||
{ | ||
Debug.Assert(destination.OriginalDefinition.IsSpan() || destination.OriginalDefinition.IsReadOnlySpan()); | ||
|
||
CheckFeatureAvailability(syntax, MessageID.IDS_FeatureFirstClassSpan, diagnostics); | ||
|
||
// PROTOTYPE: Check runtime APIs used for other span conversions once they are implemented. | ||
if (destination.OriginalDefinition.Equals(Compilation.GetWellKnownType(WellKnownType.System_ReadOnlySpan_T), TypeCompareKind.AllIgnoreOptions)) | ||
// NOTE: We cannot use well-known members because per the spec | ||
// the Span types involved in the Span conversions can be any that match the type name. | ||
if (TryFindImplicitOperatorFromArray(destination.OriginalDefinition) is { } method) | ||
{ | ||
_ = GetWellKnownTypeMember(WellKnownMember.System_ReadOnlySpan_T__op_Implicit_Array, diagnostics, syntax: syntax); | ||
diagnostics.ReportUseSite(method, syntax); | ||
} | ||
else | ||
{ | ||
Debug.Assert(destination.OriginalDefinition.Equals(Compilation.GetWellKnownType(WellKnownType.System_Span_T), TypeCompareKind.AllIgnoreOptions)); | ||
_ = GetWellKnownTypeMember(WellKnownMember.System_Span_T__op_Implicit_Array, diagnostics, syntax: syntax); | ||
Error(diagnostics, | ||
ErrorCode.ERR_MissingPredefinedMember, | ||
syntax, | ||
destination.OriginalDefinition, | ||
WellKnownMemberNames.ImplicitConversionName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal static MethodSymbol? TryFindImplicitOperatorFromArray(TypeSymbol type) | ||
{ | ||
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. |
||
Debug.Assert(type.IsSpan() || type.IsReadOnlySpan()); | ||
|
||
return TryFindSingleMember(type, WellKnownMemberNames.ImplicitConversionName, | ||
static (method) => method is | ||
{ | ||
ParameterCount: 1, | ||
Arity: 0, | ||
IsStatic: true, | ||
DeclaredAccessibility: Accessibility.Public, | ||
Parameters: [{ Type: ArrayTypeSymbol { IsSZArray: true, ElementType: TypeParameterSymbol } }] | ||
}); | ||
} | ||
|
||
private static MethodSymbol? TryFindSingleMember(TypeSymbol type, string name, Func<MethodSymbol, bool> predicate) | ||
{ | ||
var members = type.GetMembers(name); | ||
MethodSymbol? result = null; | ||
foreach (var member in members) | ||
{ | ||
if (member is MethodSymbol method && predicate(method)) | ||
{ | ||
if (result is not null) | ||
{ | ||
// Ambiguous member found. | ||
return null; | ||
} | ||
|
||
result = method; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static void CheckInlineArrayTypeIsSupported(SyntaxNode syntax, TypeSymbol inlineArrayType, TypeSymbol elementType, BindingDiagnosticBag diagnostics) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -619,28 +619,17 @@ private BoundExpression MakeConversionNodeCore( | |
{ | ||
var spanType = (NamedTypeSymbol)rewrittenType; | ||
|
||
WellKnownMember member; | ||
if (spanType.OriginalDefinition.Equals(_compilation.GetWellKnownType(WellKnownType.System_ReadOnlySpan_T), TypeCompareKind.AllIgnoreOptions)) | ||
{ | ||
member = WellKnownMember.System_ReadOnlySpan_T__op_Implicit_Array; | ||
} | ||
else | ||
{ | ||
Debug.Assert(spanType.OriginalDefinition.Equals(_compilation.GetWellKnownType(WellKnownType.System_Span_T), TypeCompareKind.AllIgnoreOptions)); | ||
member = WellKnownMember.System_Span_T__op_Implicit_Array; | ||
} | ||
|
||
if (!TryGetWellKnownTypeMember(rewrittenOperand.Syntax, member, out MethodSymbol? symbol)) | ||
if (Binder.TryFindImplicitOperatorFromArray(spanType.OriginalDefinition) is not { } methodDefinition) | ||
{ | ||
throw ExceptionUtilities.Unreachable(); | ||
} | ||
else | ||
{ | ||
MethodSymbol method = symbol.AsMember(spanType); | ||
MethodSymbol method = methodDefinition.AsMember(spanType); | ||
|
||
rewrittenOperand = _factory.Convert(method.ParameterTypesWithAnnotations[0].Type, rewrittenOperand); | ||
|
||
if (member == WellKnownMember.System_ReadOnlySpan_T__op_Implicit_Array) | ||
if (_compilation.IsReadOnlySpanType(spanType)) | ||
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. 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. That was my intent - I even have a test for it: |
||
{ | ||
return new BoundReadOnlySpanFromArray(syntax, rewrittenOperand, method, spanType) { WasCompilerGenerated = true }; | ||
} | ||
|
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.
It may be worth extracting this as a
TryFindSingleMember
method that takes a filter delegate, since I assume we'll need the same logic for looking up the ReadOnlySpan->ReadOnlySpan converter method. Up to you though. #Resolved