Skip to content

Commit

Permalink
Update ManagedAddr binder checks to require CSharp8 for unmanaged gen…
Browse files Browse the repository at this point in the history
…eric structs
  • Loading branch information
RikkiGibson committed Dec 18, 2018
1 parent 1f66018 commit 80721b4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
49 changes: 41 additions & 8 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,10 +1132,23 @@ private BoundExpression BindSizeOf(SizeOfExpressionSyntax node, DiagnosticBag di

bool typeHasErrors = type.IsErrorType();

if (!typeHasErrors && type.IsManagedType)
if (!typeHasErrors)
{
diagnostics.Add(ErrorCode.ERR_ManagedAddr, node.Location, type);
typeHasErrors = true;
if (type.IsManagedType)
{
diagnostics.Add(ErrorCode.ERR_ManagedAddr, typeSyntax.Location, type);
typeHasErrors = true;
}
else if (type.GetArity() != 0)
{
var unsupported = Compilation.LanguageVersion < MessageID.IDS_FeatureUnmanagedGenericStructs.RequiredVersion();
if (unsupported)
{
// PROTOTYPE
MessageID.IDS_FeatureUnmanagedGenericStructs.CheckFeatureAvailability(Compilation.LanguageVersion, diagnostics, typeSyntax.Location);
typeHasErrors = true;
}
}
}

BoundTypeExpression boundType = new BoundTypeExpression(typeSyntax, alias, type, typeHasErrors);
Expand Down Expand Up @@ -2870,9 +2883,16 @@ private BoundExpression BindImplicitStackAllocArrayCreationExpression(ImplicitSt
bestType = CreateErrorType();
}

if (!bestType.IsErrorType() && bestType.IsManagedType)
if (!bestType.IsErrorType())
{
Error(diagnostics, ErrorCode.ERR_ManagedAddr, node, bestType);
if (bestType.IsManagedType)
{
Error(diagnostics, ErrorCode.ERR_ManagedAddr, node, bestType);
}
else if (bestType.GetArity() != 0)
{
MessageID.IDS_FeatureUnmanagedGenericStructs.CheckFeatureAvailability(Compilation.LanguageVersion, diagnostics, node.Location);
}
}

return BindStackAllocWithInitializer(
Expand Down Expand Up @@ -3223,10 +3243,23 @@ private BoundExpression BindStackAllocArrayCreationExpression(
var elementType = BindType(elementTypeSyntax, diagnostics);

TypeSymbol type = GetStackAllocType(node, elementType, diagnostics, out bool hasErrors);
if (!elementType.IsErrorType() && elementType.IsManagedType)
if (!elementType.IsErrorType())
{
Error(diagnostics, ErrorCode.ERR_ManagedAddr, elementTypeSyntax, elementType.TypeSymbol);
hasErrors = true;
if (elementType.IsManagedType)
{
Error(diagnostics, ErrorCode.ERR_ManagedAddr, elementTypeSyntax, elementType.TypeSymbol);
hasErrors = true;
}
else if (elementType.TypeSymbol.GetArity() != 0)
{
var unsupported = Compilation.LanguageVersion < MessageID.IDS_FeatureUnmanagedGenericStructs.RequiredVersion();
if (unsupported)
{
// PROTOTYPE
MessageID.IDS_FeatureUnmanagedGenericStructs.CheckFeatureAvailability(Compilation.LanguageVersion, diagnostics, elementTypeSyntax.Location);
hasErrors = true;
}
}
}

SyntaxList<ArrayRankSpecifierSyntax> rankSpecifiers = arrayTypeSyntax.RankSpecifiers;
Expand Down
19 changes: 16 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2131,10 +2131,23 @@ private BoundExpression BindAddressOfExpression(PrefixUnaryExpressionSyntax node
bool allowManagedAddressOf = Flags.Includes(BinderFlags.AllowManagedAddressOf);
if (!allowManagedAddressOf)
{
if (!hasErrors && isManagedType)
if (!hasErrors)
{
hasErrors = true;
Error(diagnostics, ErrorCode.ERR_ManagedAddr, node, operandType);
if (isManagedType)
{
hasErrors = true;
Error(diagnostics, ErrorCode.ERR_ManagedAddr, node, operandType);
}
else if (operandType.GetArity() != 0)
{
var supported = MessageID.IDS_FeatureUnmanagedGenericStructs.RequiredVersion() >= Compilation.LanguageVersion;
if (!supported)
{
// PROTOTYPE
MessageID.IDS_FeatureUnmanagedGenericStructs.CheckFeatureAvailability(Compilation.LanguageVersion, diagnostics, node.Location);
hasErrors = true;
}
}
}

if (!hasErrors)
Expand Down
10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,16 @@ private bool IsValidFixedVariableInitializer(TypeSymbol declType, SourceLocalSym
Error(diagnostics, ErrorCode.ERR_ManagedAddr, initializerSyntax, elementType);
hasErrors = true;
}
else if (elementType.GetArity() != 0)
{
var supported = MessageID.IDS_FeatureUnmanagedGenericStructs.RequiredVersion() >= Compilation.LanguageVersion;
if (!supported)
{
// PROTOTYPE
MessageID.IDS_FeatureUnmanagedGenericStructs.CheckFeatureAvailability(Compilation.LanguageVersion, diagnostics, initializerSyntax.Location);
hasErrors = true;
}
}

initializerOpt = GetFixedLocalCollectionInitializer(initializerOpt, elementType, declType, fixedPatternMethod, hasErrors, diagnostics);
return true;
Expand Down
13 changes: 10 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,17 @@ internal NamespaceOrTypeOrAliasSymbolWithAnnotations BindNamespaceOrTypeOrAliasS
// Invalid constraint type. A type used as a constraint must be an interface, a non-sealed class or a type parameter.
Error(diagnostics, ErrorCode.ERR_BadConstraintType, node);
}
else if (elementType.IsManagedType)
else
{
// "Cannot take the address of, get the size of, or declare a pointer to a managed type ('{0}')"
Error(diagnostics, ErrorCode.ERR_ManagedAddr, node, elementType.TypeSymbol);
if (elementType.IsManagedType)
{
// "Cannot take the address of, get the size of, or declare a pointer to a managed type ('{0}')"
Error(diagnostics, ErrorCode.ERR_ManagedAddr, node, elementType.TypeSymbol);
}
else if (elementType.TypeSymbol.GetArity() != 0)
{
MessageID.IDS_FeatureUnmanagedGenericStructs.CheckFeatureAvailability(Compilation.LanguageVersion, diagnostics, syntax.Location);
}
}

return TypeSymbolWithAnnotations.Create(new PointerTypeSymbol(elementType));
Expand Down

0 comments on commit 80721b4

Please sign in to comment.