-
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
Allow ref structs to implement interfaces, respect "allows ref struct" during constraints check. #72537
Allow ref structs to implement interfaces, respect "allows ref struct" during constraints check. #72537
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 |
---|---|---|
|
@@ -927,13 +927,37 @@ private static bool CheckBasicConstraints( | |
ArrayBuilder<TypeParameterDiagnosticInfo> nullabilityDiagnosticsBuilderOpt, | ||
ref ArrayBuilder<TypeParameterDiagnosticInfo> useSiteDiagnosticsBuilder) | ||
{ | ||
if (typeArgument.Type.IsPointerOrFunctionPointer() || typeArgument.IsRestrictedType() || typeArgument.IsVoidType()) | ||
if (typeArgument.Type.IsPointerOrFunctionPointer() || typeArgument.IsRestrictedType(ignoreSpanLikeTypes: true) || typeArgument.IsVoidType()) | ||
{ | ||
// "The type '{0}' may not be used as a type argument" | ||
diagnosticsBuilder.Add(new TypeParameterDiagnosticInfo(typeParameter, new UseSiteInfo<AssemblySymbol>(new CSDiagnosticInfo(ErrorCode.ERR_BadTypeArgument, typeArgument.Type)))); | ||
return false; | ||
} | ||
|
||
if (typeArgument.IsRefLikeType() || typeArgument.Type is TypeParameterSymbol { AllowByRefLike: true }) | ||
{ | ||
if (typeParameter.AllowByRefLike) | ||
{ | ||
if (args.CurrentCompilation.SourceModule != typeParameter.ContainingModule) | ||
{ | ||
if (MessageID.IDS_FeatureRefStructInterfaces.GetFeatureAvailabilityDiagnosticInfo(args.CurrentCompilation) is { } diagnosticInfo) | ||
{ | ||
diagnosticsBuilder.Add(new TypeParameterDiagnosticInfo(typeParameter, new UseSiteInfo<AssemblySymbol>(diagnosticInfo))); | ||
} | ||
|
||
if (!args.CurrentCompilation.Assembly.RuntimeSupportsByRefLikeGenerics) | ||
{ | ||
diagnosticsBuilder.Add(new TypeParameterDiagnosticInfo(typeParameter, new UseSiteInfo<AssemblySymbol>(new CSDiagnosticInfo(ErrorCode.ERR_RuntimeDoesNotSupportByRefLikeGenerics)))); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
diagnosticsBuilder.Add(new TypeParameterDiagnosticInfo(typeParameter, new UseSiteInfo<AssemblySymbol>(new CSDiagnosticInfo(ErrorCode.ERR_NotRefStructConstraintNotSatisfied, containingSymbol.ConstructedFrom(), typeParameter, typeArgument.Type)))); | ||
return false; | ||
} | ||
} | ||
|
||
if (typeArgument.IsStatic) | ||
{ | ||
// "'{0}': static types cannot be used as type arguments" | ||
|
@@ -1307,13 +1331,20 @@ private static bool SatisfiesConstraintType( | |
return true; | ||
} | ||
|
||
// "... A boxing conversion (6.1.7), provided that type A is a non-nullable value type. ..." | ||
// NOTE: we extend this to allow, for example, a conversion from Nullable<T> to object. | ||
if (typeArgument.Type.IsValueType && | ||
conversions.HasBoxingConversion(typeArgument.Type.IsNullableType() ? ((NamedTypeSymbol)typeArgument.Type).ConstructedFrom : typeArgument.Type, | ||
constraintType.Type, ref useSiteInfo)) | ||
if (typeArgument.Type.IsValueType) | ||
{ | ||
return true; | ||
// "... A boxing conversion (6.1.7), provided that type A is a non-nullable value type. ..." | ||
// NOTE: we extend this to allow, for example, a conversion from Nullable<T> to object. | ||
if (conversions.HasBoxingConversion(typeArgument.Type.IsNullableType() ? ((NamedTypeSymbol)typeArgument.Type).ConstructedFrom : typeArgument.Type, | ||
constraintType.Type, ref useSiteInfo)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (typeArgument.Type.IsRefLikeType && conversions.ImplementsVarianceCompatibleInterface(typeArgument.Type, constraintType.Type, ref useSiteInfo)) | ||
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. Why is this check needed specifically for ref struct type arguments - i.e., why they need different handling then normal structs for example? #Resolved 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 are no boxing conversion for ref structs, therefore, the |
||
{ | ||
return true; | ||
} | ||
} | ||
|
||
if (typeArgument.TypeKind == TypeKind.TypeParameter) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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: "The type ... must not be a ref struct ... in order to use it as type parameter ..." #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.
For "may not be a", I think I used the following message as a template:
For "in order to use it as parameter", there is a "prior art" too:
I can make the suggested change if you still think it is needed. Please let me know.