KEMBAR78
Implement support for Inline Array conversions by AlekseyTs · Pull Request #68019 · dotnet/roslyn · GitHub
Skip to content

Conversation

@AlekseyTs
Copy link
Contributor

@AlekseyTs AlekseyTs requested review from cston and jjonescz April 28, 2023 18:02
@AlekseyTs AlekseyTs requested review from a team as code owners April 28, 2023 18:02
@ghost ghost added the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 28, 2023
@AlekseyTs
Copy link
Contributor Author

@cston, @jjonescz, @dotnet/roslyn-compiler Please review.


// Neither Span<T>, nor ReadOnlySpan<T> can be wrapped into a Nullable<T>, therefore, there is no point to check for an attempt to convert to Nullable types here.
if ((destination.OriginalDefinition.Equals(Compilation.GetWellKnownType(WellKnownType.System_Span_T), TypeCompareKind.AllIgnoreOptions) ||
destination.OriginalDefinition.Equals(Compilation.GetWellKnownType(WellKnownType.System_ReadOnlySpan_T), TypeCompareKind.AllIgnoreOptions)) &&
Copy link
Contributor

@cston cston May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition seems relatively expensive, at least compared to the next condition. Consider checking source?.HasInlineArrayAttribute(out _) == true first. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition seems relatively expensive, at least compared to the next condition. Consider checking source?.HasInlineArrayAttribute(out _) == true first.

The order is intentional. Checking source?.HasInlineArrayAttribute(out _) == true first causes a cycle. I do not plan to address it in context of this PR. Besides, symbols for well known types are cached and, since we are comparing definitions, this condition shouldn't have noticeable impact on performance.

createSpan = _factory.ModuleBuilderOpt.EnsureInlineArrayAsSpanExists(syntax, spanType.OriginalDefinition, _factory.SpecialType(SpecialType.System_Int32), _diagnostics.DiagnosticBag);
}

createSpan = createSpan.Construct(rewrittenOperand.Type, spanType.TypeArgumentsWithAnnotationsNoUseSiteDiagnostics.Single().Type);
Copy link
Contributor

@cston cston May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spanType.TypeArgumentsWithAnnotationsNoUseSiteDiagnostics.Single().Type

Perhaps just pass in the array spanType.TypeArgumentsWithAnnotationsNoUseSiteDiagnostics. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps just pass in the array

I do not think there is a Construct method that takes a TypeSymbol and an array.

Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "b4").WithArguments("Buffer10<string>", "System.Span<string?>").WithLocation(26, 34),
// (27,42): warning CS8619: Nullability of reference types in value of type 'Buffer10<string>' doesn't match target type 'ReadOnlySpan<string?>'.
// System.ReadOnlySpan<string?> y = b4;
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "b4").WithArguments("Buffer10<string>", "System.ReadOnlySpan<string?>").WithLocation(27, 42)
Copy link
Member

@jjonescz jjonescz May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test something similar separately for nullable value types (like int?)? #Resolved

Copy link
Contributor Author

@AlekseyTs AlekseyTs May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test something similar separately for nullable value types (like int?)?

A scenario like that wouldn't be any different than the one tested inConversion_ElementTypeMismatch. That is because int? and int are different types, whereas string? and string are the same type, but with different nullability.

}

private static Conversion GenerateConversion(Conversions conversions, BoundExpression? sourceExpression, TypeSymbol? sourceType, TypeSymbol destinationType, bool fromExplicitCast, bool extensionMethodThisArgument, bool isChecked)
private static Conversion GenerateConversion(Conversions conversions, BoundExpression? sourceExpression, TypeSymbol? sourceType, TypeSymbol destinationType, bool fromExplicitCast, bool extensionMethodThisArgument, bool isChecked, bool? fromExpression = null)
Copy link
Member

@jjonescz jjonescz May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does caller VisitArgumentConversionAndInboundAssignmentsAndPreConditions care if it doesn't set fromExpression: true for inline array conversions? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to a more robust approach that doesn't need the parameter.

@AlekseyTs
Copy link
Contributor Author

@cston, @jjonescz I think I addressed the feedback.

@AlekseyTs AlekseyTs merged commit e4d1884 into dotnet:features/InlineArrays May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers Feature - Inline Arrays untriaged Issues and PRs which have not yet been triaged by a lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants