Skip to content

Commit 23ecea2

Browse files
authored
Make remaining managed pointer type scenarios warnings (#64294)
1 parent 258dc62 commit 23ecea2

25 files changed

+512
-342
lines changed

src/Compilers/CSharp/Portable/Binder/BinderFlags.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ internal enum BinderFlags : uint
112112
/// </summary>
113113
InExpressionTree = 1 << 30,
114114

115-
/// <summary>
116-
/// Indicates whether the current context allows pointer types to managed types,
117-
/// assuming we're already in an unsafe context (otherwise all pointer types are errors anyways).
118-
/// </summary>
119-
AllowManagedPointer = 1u << 31,
120-
121115
// Groups
122116

123117
AllClearedAtExecutableCodeBoundary = InLockBody | InCatchBlock | InCatchFilter | InFinallyBlock | InTryBlockOfTryCatch | InNestedFinallyBlock,

src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ private BoundExpression BindSizeOf(SizeOfExpressionSyntax node, BindingDiagnosti
13931393
TypeWithAnnotations typeWithAnnotations = this.BindType(typeSyntax, diagnostics, out alias);
13941394
TypeSymbol type = typeWithAnnotations.Type;
13951395

1396-
bool typeHasErrors = type.IsErrorType() || CheckManagedAddr(Compilation, type, warnForManaged: false, node.Location, diagnostics);
1396+
bool typeHasErrors = type.IsErrorType() || CheckManagedAddr(Compilation, type, node.Location, diagnostics);
13971397

13981398
BoundTypeExpression boundType = new BoundTypeExpression(typeSyntax, alias, typeWithAnnotations, typeHasErrors);
13991399
ConstantValue constantValue = GetConstantSizeOf(type);
@@ -1403,31 +1403,29 @@ private BoundExpression BindSizeOf(SizeOfExpressionSyntax node, BindingDiagnosti
14031403
}
14041404

14051405
/// <returns>true if managed type-related errors were found, otherwise false.</returns>
1406-
internal static bool CheckManagedAddr(CSharpCompilation compilation, TypeSymbol type, bool warnForManaged, Location location, BindingDiagnosticBag diagnostics)
1406+
internal static bool CheckManagedAddr(CSharpCompilation compilation, TypeSymbol type, Location location, BindingDiagnosticBag diagnostics, bool errorForManaged = false)
14071407
{
14081408
var useSiteInfo = new CompoundUseSiteInfo<AssemblySymbol>(diagnostics, compilation.Assembly);
14091409
var managedKind = type.GetManagedKind(ref useSiteInfo);
14101410
diagnostics.Add(location, useSiteInfo);
14111411

1412-
return CheckManagedAddr(compilation, type, managedKind, warnForManaged, location, diagnostics);
1412+
return CheckManagedAddr(compilation, type, managedKind, location, diagnostics, errorForManaged);
14131413
}
14141414

14151415
/// <returns>true if managed type-related errors were found, otherwise false.</returns>
1416-
internal static bool CheckManagedAddr(CSharpCompilation compilation, TypeSymbol type, ManagedKind managedKind, bool warnForManaged, Location location, BindingDiagnosticBag diagnostics)
1416+
internal static bool CheckManagedAddr(CSharpCompilation compilation, TypeSymbol type, ManagedKind managedKind, Location location, BindingDiagnosticBag diagnostics, bool errorForManaged = false)
14171417
{
14181418
switch (managedKind)
14191419
{
14201420
case ManagedKind.Managed:
1421-
if (warnForManaged)
1422-
{
1423-
diagnostics.Add(ErrorCode.WRN_ManagedAddr, location, type);
1424-
return false;
1425-
}
1426-
else
1421+
if (errorForManaged)
14271422
{
14281423
diagnostics.Add(ErrorCode.ERR_ManagedAddr, location, type);
14291424
return true;
14301425
}
1426+
1427+
diagnostics.Add(ErrorCode.WRN_ManagedAddr, location, type);
1428+
return false;
14311429
case ManagedKind.UnmanagedWithGenerics when MessageID.IDS_FeatureUnmanagedConstructedTypes.GetFeatureAvailabilityDiagnosticInfo(compilation) is CSDiagnosticInfo diagnosticInfo:
14321430
diagnostics.Add(diagnosticInfo, location);
14331431
return true;
@@ -2233,7 +2231,7 @@ private BoundBaseReference BindBase(BaseExpressionSyntax node, BindingDiagnostic
22332231
private BoundExpression BindCast(CastExpressionSyntax node, BindingDiagnosticBag diagnostics)
22342232
{
22352233
BoundExpression operand = this.BindValue(node.Expression, diagnostics, BindValueKind.RValue);
2236-
TypeWithAnnotations targetTypeWithAnnotations = this.WithAdditionalFlags(BinderFlags.AllowManagedPointer).BindType(node.Type, diagnostics);
2234+
TypeWithAnnotations targetTypeWithAnnotations = this.BindType(node.Type, diagnostics);
22372235
TypeSymbol targetType = targetTypeWithAnnotations.Type;
22382236

22392237
if (targetType.IsNullableType() &&
@@ -3313,7 +3311,7 @@ private BoundExpression BindImplicitStackAllocArrayCreationExpression(ImplicitSt
33133311

33143312
if (!bestType.IsErrorType())
33153313
{
3316-
CheckManagedAddr(Compilation, bestType, warnForManaged: false, node.Location, diagnostics);
3314+
CheckManagedAddr(Compilation, bestType, node.Location, diagnostics, errorForManaged: true);
33173315
}
33183316

33193317
return BindStackAllocWithInitializer(
@@ -3670,7 +3668,7 @@ private BoundExpression BindStackAllocArrayCreationExpression(
36703668
TypeSymbol type = GetStackAllocType(node, elementType, diagnostics, out bool hasErrors);
36713669
if (!elementType.Type.IsErrorType())
36723670
{
3673-
hasErrors = hasErrors || CheckManagedAddr(Compilation, elementType.Type, warnForManaged: false, elementTypeSyntax.Location, diagnostics);
3671+
hasErrors = hasErrors || CheckManagedAddr(Compilation, elementType.Type, elementTypeSyntax.Location, diagnostics, errorForManaged: true);
36743672
}
36753673

36763674
SyntaxList<ArrayRankSpecifierSyntax> rankSpecifiers = arrayTypeSyntax.RankSpecifiers;

src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,7 @@ private BoundExpression BindAddressOfExpression(PrefixUnaryExpressionSyntax node
25042504

25052505
if (!hasErrors)
25062506
{
2507-
hasErrors = CheckManagedAddr(Compilation, operandType, managedKind, warnForManaged: true, node.Location, diagnostics);
2507+
hasErrors = CheckManagedAddr(Compilation, operandType, managedKind, node.Location, diagnostics);
25082508
}
25092509

25102510
bool allowManagedAddressOf = Flags.Includes(BinderFlags.AllowMoveableAddressOf);

src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ private bool IsValidFixedVariableInitializer(TypeSymbol declType, SourceLocalSym
13341334
}
13351335
}
13361336

1337-
if (CheckManagedAddr(Compilation, elementType, warnForManaged: false, initializerSyntax.Location, diagnostics))
1337+
if (CheckManagedAddr(Compilation, elementType, initializerSyntax.Location, diagnostics))
13381338
{
13391339
hasErrors = true;
13401340
}

src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ NamespaceOrTypeOrAliasSymbolWithAnnotations bindPointer()
575575

576576
if (!Flags.HasFlag(BinderFlags.SuppressConstraintChecks))
577577
{
578-
CheckManagedAddr(Compilation, elementType.Type, warnForManaged: Flags.HasFlag(BinderFlags.AllowManagedPointer), node.Location, diagnostics);
578+
CheckManagedAddr(Compilation, elementType.Type, node.Location, diagnostics);
579579
}
580580

581581
return TypeWithAnnotations.Create(new PointerTypeSymbol(elementType));

src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_FixedStatement.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public override BoundNode VisitFixedStatement(BoundFixedStatement node)
4040
}
4141
else
4242
{
43-
Debug.Assert(!pinnedTemp.Type.IsManagedTypeNoUseSiteDiagnostics);
44-
4543
// temp = ref *default(T*);
4644
cleanup[i] = _factory.Assignment(_factory.Local(pinnedTemp), _factory.NullRef(pinnedTemp.TypeWithAnnotations), isRef: true);
4745
}

src/Compilers/CSharp/Portable/Symbols/ConstraintsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ private static bool CheckConstraintsSingleType(TypeSymbol type, in CheckConstrai
545545
}
546546
else if (type.Kind == SymbolKind.PointerType)
547547
{
548-
Binder.CheckManagedAddr(args.CurrentCompilation, ((PointerTypeSymbol)type).PointedAtType, warnForManaged: false, args.Location, args.Diagnostics);
548+
Binder.CheckManagedAddr(args.CurrentCompilation, ((PointerTypeSymbol)type).PointedAtType, args.Location, args.Diagnostics);
549549
}
550550
return false; // continue walking types
551551
}

src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenTupleTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14341,15 +14341,15 @@ interface ITest2<T> : ValueTuple<int, int, int, int, int, int, int, T> where T :
1434114341
options: TestOptions.ReleaseExe.WithAllowUnsafe(true));
1434214342

1434314343
comp.VerifyDiagnostics(
14344-
// (31,18): error CS0509: 'Test1<T>': cannot derive from sealed type 'ValueTuple<int, int, int, int, int, int, int, T>'
14345-
// class Test1<T> : ValueTuple<int, int, int, int, int, int, int, T> where T : struct
14346-
Diagnostic(ErrorCode.ERR_CantDeriveFromSealedType, "ValueTuple<int, int, int, int, int, int, int, T>").WithArguments("Test1<T>", "System.ValueTuple<int, int, int, int, int, int, int, T>").WithLocation(31, 18),
1434714344
// (35,23): error CS0527: Type 'ValueTuple<int, int, int, int, int, int, int, T>' in interface list is not an interface
1434814345
// interface ITest2<T> : ValueTuple<int, int, int, int, int, int, int, T> where T : struct
1434914346
Diagnostic(ErrorCode.ERR_NonInterfaceInInterfaceList, "ValueTuple<int, int, int, int, int, int, int, T>").WithArguments("System.ValueTuple<int, int, int, int, int, int, int, T>").WithLocation(35, 23),
14350-
// (25,69): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('ValueTuple<int, int, int, int, int, int, int, T>')
14347+
// (31,18): error CS0509: 'Test1<T>': cannot derive from sealed type 'ValueTuple<int, int, int, int, int, int, int, T>'
14348+
// class Test1<T> : ValueTuple<int, int, int, int, int, int, int, T> where T : struct
14349+
Diagnostic(ErrorCode.ERR_CantDeriveFromSealedType, "ValueTuple<int, int, int, int, int, int, int, T>").WithArguments("Test1<T>", "System.ValueTuple<int, int, int, int, int, int, int, T>").WithLocation(31, 18),
14350+
// (25,69): warning CS8500: This takes the address of, gets the size of, or declares a pointer to a managed type ('ValueTuple<int, int, int, int, int, int, int, T>')
1435114351
// public static ValueTuple<int, int, int, int, int, int, int, T>* M5()
14352-
Diagnostic(ErrorCode.ERR_ManagedAddr, "M5").WithArguments("System.ValueTuple<int, int, int, int, int, int, int, T>").WithLocation(25, 69),
14352+
Diagnostic(ErrorCode.WRN_ManagedAddr, "M5").WithArguments("System.ValueTuple<int, int, int, int, int, int, int, T>").WithLocation(25, 69),
1435314353
// (23,74): error CS0066: 'Test<T>.E1': event must be of a delegate type
1435414354
// public static event ValueTuple<int, int, int, int, int, int, int, T> E1;
1435514355
Diagnostic(ErrorCode.ERR_EventNotDelegate, "E1").WithArguments("Test<T>.E1").WithLocation(23, 74),

src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,9 +1407,9 @@ void M()
14071407
}
14081408
}",
14091409
comp => comp.VerifyDiagnostics(
1410-
// (7,45): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('S')
1410+
// (7,45): warning CS8500: This takes the address of, gets the size of, or declares a pointer to a managed type ('S')
14111411
// System.Console.WriteLine(sizeof(S*));
1412-
Diagnostic(ErrorCode.ERR_ManagedAddr, "S*").WithArguments("S").WithLocation(7, 45)
1412+
Diagnostic(ErrorCode.WRN_ManagedAddr, "S*").WithArguments("S").WithLocation(7, 45)
14131413
));
14141414
}
14151415

src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4471,12 +4471,12 @@ public static void Main(int* a, var* c, Typ* e)
44714471
// (13,31): error CS1525: Invalid expression term 'int'
44724472
// switch (a) { case int* b: break; }
44734473
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(13, 31),
4474-
// (5,42): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('var')
4474+
// (5,42): warning CS8500: This takes the address of, gets the size of, or declares a pointer to a managed type ('var')
44754475
// public static void Main(int* a, var* c, Typ* e)
4476-
Diagnostic(ErrorCode.ERR_ManagedAddr, "c").WithArguments("var").WithLocation(5, 42),
4477-
// (5,50): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('Typ')
4476+
Diagnostic(ErrorCode.WRN_ManagedAddr, "c").WithArguments("var").WithLocation(5, 42),
4477+
// (5,50): warning CS8500: This takes the address of, gets the size of, or declares a pointer to a managed type ('Typ')
44784478
// public static void Main(int* a, var* c, Typ* e)
4479-
Diagnostic(ErrorCode.ERR_ManagedAddr, "e").WithArguments("Typ").WithLocation(5, 50),
4479+
Diagnostic(ErrorCode.WRN_ManagedAddr, "e").WithArguments("Typ").WithLocation(5, 50),
44804480
// (8,27): error CS0103: The name 'b' does not exist in the current context
44814481
// if (a is int* b) {}
44824482
Diagnostic(ErrorCode.ERR_NameNotInContext, "b").WithArguments("b").WithLocation(8, 27),

0 commit comments

Comments
 (0)