Skip to content
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

Block typeof(delegate*<void>) in attributes #50903

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,7 @@ private BoundExpression BindParenthesizedExpression(ExpressionSyntax innerExpres
return result;
}

#nullable enable
private BoundExpression BindTypeOf(TypeOfExpressionSyntax node, DiagnosticBag diagnostics)
{
ExpressionSyntax typeSyntax = node.Type;
Expand All @@ -1294,10 +1295,19 @@ private BoundExpression BindTypeOf(TypeOfExpressionSyntax node, DiagnosticBag di
diagnostics.Add(ErrorCode.ERR_BadNullableTypeof, node.Location);
hasError = true;
}
else if (this.InAttributeArgument && type.ContainsFunctionPointer())
{
// https://github.com/dotnet/roslyn/issues/48765 tracks removing this error and properly supporting function
// pointers in attribute types. Until then, we don't know how serialize them, so error instead of crashing
// during emit.
diagnostics.Add(ErrorCode.ERR_FunctionPointerTypesInAttributeNotSupported, node.Location);
hasError = true;
}

BoundTypeExpression boundType = new BoundTypeExpression(typeSyntax, alias, typeWithAnnotations, type.IsErrorType());
return new BoundTypeOfOperator(node, boundType, null, this.GetWellKnownType(WellKnownType.System_Type, diagnostics, node), hasError);
}
#nullable disable

private BoundExpression BindSizeOf(SizeOfExpressionSyntax node, DiagnosticBag diagnostics)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6600,4 +6600,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="WRN_DoNotCompareFunctionPointers_Title" xml:space="preserve">
<value>Do not compare function pointer values</value>
</data>
<data name="ERR_FunctionPointerTypesInAttributeNotSupported" xml:space="preserve">
<value>Using a function pointer type in a 'typeof' in an attribute is not supported.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,7 @@ internal enum ErrorCode

WRN_DoNotCompareFunctionPointers = 8909,
ERR_RecordAmbigCtor = 8910,
ERR_FunctionPointerTypesInAttributeNotSupported = 8911,

#endregion diagnostics introduced for C# 9.0

Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,12 @@ internal static bool ContainsTuple(this TypeSymbol type) =>
internal static bool ContainsTupleNames(this TypeSymbol type) =>
type.VisitType((TypeSymbol t, object? _1, bool _2) => !t.TupleElementNames.IsDefault, null) is object;

/// <summary>
/// Return true if the type contains any function pointer types.
/// </summary>
internal static bool ContainsFunctionPointer(this TypeSymbol type) =>
type.VisitType((TypeSymbol t, object? _, bool _) => t.IsFunctionPointer(), null) is object;

/// <summary>
/// Guess the non-error type that the given type was intended to represent.
/// If the type itself is not an error type, then it will be returned.
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11263,6 +11263,38 @@ .maxstack 2
");
}

[Fact, WorkItem(48765, "https://github.com/dotnet/roslyn/issues/48765")]
public void TypeOfFunctionPointerInAttribute()
{
var comp = CreateCompilationWithFunctionPointers(@"
using System;
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
[Attr(typeof(delegate*<void>))]
[Attr(typeof(delegate*<void>[]))]
[Attr(typeof(C<delegate*<void>[]>))]
unsafe class Attr : System.Attribute
{
public Attr(System.Type type) {}
}

class C<T> {}
");

// https://github.com/dotnet/roslyn/issues/48765 tracks enabling support for this scenario. Currently, we don't know how to
// encode these in metadata, and may need to work with the runtime team to define a new format.
comp.VerifyDiagnostics(
// (4,7): error CS8911: Using a function pointer type in a 'typeof' in an attribute is not supported.
// [Attr(typeof(delegate*<void>))]
Diagnostic(ErrorCode.ERR_FunctionPointerTypesInAttributeNotSupported, "typeof(delegate*<void>)").WithLocation(4, 7),
// (5,7): error CS8911: Using a function pointer type in a 'typeof' in an attribute is not supported.
// [Attr(typeof(delegate*<void>[]))]
Diagnostic(ErrorCode.ERR_FunctionPointerTypesInAttributeNotSupported, "typeof(delegate*<void>[])").WithLocation(5, 7),
// (6,7): error CS8911: Using a function pointer type in a 'typeof' in an attribute is not supported.
// [Attr(typeof(C<delegate*<void>[]>))]
Diagnostic(ErrorCode.ERR_FunctionPointerTypesInAttributeNotSupported, "typeof(C<delegate*<void>[]>)").WithLocation(6, 7)
);
}

private static readonly Guid s_guid = new Guid("97F4DBD4-F6D1-4FAD-91B3-1001F92068E5");
private static readonly BlobContentId s_contentId = new BlobContentId(s_guid, 0x04030201);

Expand Down