-
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
Switch implicit span to conversion from type #74232
Conversation
// PROTOTYPE: Is it fine that this conversion does not exists when Compilation is null? | ||
if (Compilation?.IsFeatureEnabled(MessageID.IDS_FeatureFirstClassSpan) != true) | ||
// Note: when Compilation is null, we assume latest LangVersion. | ||
if (Compilation?.IsFeatureEnabled(MessageID.IDS_FeatureFirstClassSpan) == false) |
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.
A little worried about this. Do we have an understanding of when Compilation
is null
, such that we can test those scenarios with lower language versions? Will they get different behavior, or new language version errors? #Resolved
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.
So far I've been able to hit only one scenario with Compilation being null - from IMethodSymbol.ReduceExtensionMethod
public API - see e.g., test Conversion_Array_Span_ExtensionMethodReceiver_Implicit_Reduced_01
. And the documentation says we should consider the latest language version in that case (it talks about constraints but it seems consistent to do for other features as well):
/// <param name="compilation">The compilation in which constraints should be checked.
/// Should not be null, but if it is null we treat constraints as we would in the latest
/// language version.</param>
Maybe other scenarios (not just reduced extension methods) should behave similarly? But there might be no more scenarios, I have investigated callsites for a while and haven't found any that I could hit.
Will they get different behavior, or new language version errors?
If they end up in the binder, they will get a language version error. Otherwise, they will get different behavior - like the ReduceExtensionMethod example mentioned above - the call will consider the implicit span conversion even in older lang version.
// 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: Span conversions do not use well-known types because they are "conversions from type" and hence don't have access to Compilation. |
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.
Not quite how I'd word this comment; the lack of a Compilation
in ConversionsBase
is just an implementation detail. The reason is that we explicitly specified that we match (ReadOnly)Span
by type name, so we need to use that same (ReadOnly)Span
to look up APIs in. #Resolved
} | ||
} | ||
} | ||
} | ||
|
||
internal static MethodSymbol? TryFindImplicitOperatorFromArray(TypeSymbol type) |
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
comp.VerifyDiagnostics( | ||
// (2,15): error CS0656: Missing compiler required member 'System.Span`1.op_Implicit' | ||
[Fact] | ||
public void Conversion_Array_Span_Implicit_DifferentOperator() |
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.
Perhaps a version where the operator is explicit
? #Resolved
} | ||
|
||
[Fact] | ||
public void Conversion_Array_Span_Implicit_UnrecognizedModreq() |
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.
Let's add a test with an unrecognized modopt
as well. #Resolved
@cston for the second review, thanks |
} | ||
|
||
internal static MethodSymbol? TryFindImplicitOperatorFromArray(TypeSymbol type) | ||
{ |
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.
} method ? method : null); | ||
} | ||
|
||
private static T? TryFindSingleMember<T>(TypeSymbol type, string name, Func<Symbol, T?> predicate) where T : class |
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.
(ignoreUserDefinedSpanConversionsInOneDirection(Compilation, source, target) || | ||
ignoreUserDefinedSpanConversionsInOneDirection(Compilation, target, source)); | ||
// Note: when Compilation is null, we assume latest LangVersion. | ||
return Compilation?.IsFeatureEnabled(MessageID.IDS_FeatureFirstClassSpan) != false && |
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.
This check is repeated several times, with the same comment. Consider creating a private IsFirstClassSpansEnabled
property so this is captured in one location. #Closed
|
||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
That was my intent - I even have a test for it: Conversion_Array_Span_Implicit_ConstantData_NotWellKnownSpan
. The emit of constant data for ReadOnlySpan (i.e., emit of BoundReadOnlySpanFromArray
) expects the well-known span type and I don't think changing that is worth the effort.
[This comment is not specific to this PR, so it can be addressed separately.]
Refers to: src/Compilers/CSharp/Test/Emit3/FirstClassSpanTests.cs:58 in 4161bd1. [](commit_id = 4161bd1, deletion_comment = False) |
Sounds good, thanks! Let me do that after I merge main into the feature branch so I get #74281 in. In reply to: 2215489010 Refers to: src/Compilers/CSharp/Test/Emit3/FirstClassSpanTests.cs:58 in 4161bd1. [](commit_id = 4161bd1, deletion_comment = False) |
@333fred for another look, thanks |
Test plan: #73445
Corresponding speclet update: dotnet/csharplang#8241