Skip to content

Commit 9a658fa

Browse files
authored
Annotate more public syntax APIs (#44266)
1 parent 09db48d commit 9a658fa

File tree

94 files changed

+419
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+419
-182
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ private ImmutableArray<TypedConstant> GetRewrittenAttributeConstructorArguments(
666666
}
667667
else if (reorderedArgument.Kind == TypedConstantKind.Array &&
668668
parameter.Type.TypeKind == TypeKind.Array &&
669-
!((TypeSymbol)reorderedArgument.TypeInternal).Equals(parameter.Type, TypeCompareKind.AllIgnoreOptions))
669+
!((TypeSymbol)reorderedArgument.TypeInternal!).Equals(parameter.Type, TypeCompareKind.AllIgnoreOptions))
670670
{
671671
// NOTE: As in dev11, we don't allow array covariance conversions (presumably, we don't have a way to
672672
// represent the conversion in metadata).
@@ -922,6 +922,7 @@ private static bool TryGetNormalParamValue(ParameterSymbol parameter, ImmutableA
922922
}
923923

924924
HashSet<DiagnosticInfo>? useSiteDiagnostics = null; // ignoring, since already bound argument and parameter
925+
Debug.Assert(argument.TypeInternal is object);
925926
Conversion conversion = conversions.ClassifyBuiltInConversion((TypeSymbol)argument.TypeInternal, parameter.Type, ref useSiteDiagnostics);
926927

927928
// NOTE: Won't always succeed, even though we've performed overload resolution.

src/Compilers/CSharp/Portable/Symbols/Attributes/AttributeData.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ private DeclarativeSecurityAction DecodeSecurityAttributeAction(Symbol targetSym
370370
else
371371
{
372372
TypedConstant firstArg = ctorArgs.First();
373-
TypeSymbol firstArgType = (TypeSymbol)firstArg.TypeInternal;
374-
if ((object)firstArgType != null && firstArgType.Equals(compilation.GetWellKnownType(WellKnownType.System_Security_Permissions_SecurityAction)))
373+
var firstArgType = (TypeSymbol?)firstArg.TypeInternal;
374+
if (firstArgType is object && firstArgType.Equals(compilation.GetWellKnownType(WellKnownType.System_Security_Permissions_SecurityAction)))
375375
{
376376
return DecodeSecurityAction(firstArg, targetSymbol, nodeOpt, diagnostics, out hasErrors);
377377
}
@@ -387,6 +387,7 @@ private DeclarativeSecurityAction DecodeSecurityAction(TypedConstant typedValue,
387387
{
388388
Debug.Assert((object)targetSymbol != null);
389389
Debug.Assert(targetSymbol.Kind == SymbolKind.Assembly || targetSymbol.Kind == SymbolKind.NamedType || targetSymbol.Kind == SymbolKind.Method);
390+
Debug.Assert(typedValue.ValueInternal is object);
390391

391392
int securityAction = (int)typedValue.ValueInternal;
392393
bool isPermissionRequestAction;
@@ -527,15 +528,15 @@ private static Location GetSecurityAttributeActionSyntaxLocation(AttributeSyntax
527528
PermissionSetAttributeTypeHasRequiredProperty(attrType, filePropName))
528529
{
529530
// resolve file prop path
530-
var fileName = (string)namedArg.Value.ValueInternal;
531+
var fileName = (string?)namedArg.Value.ValueInternal;
531532
var resolver = compilation.Options.XmlReferenceResolver;
532533

533-
resolvedFilePath = (resolver != null) ? resolver.ResolveReference(fileName, baseFilePath: null) : null;
534+
resolvedFilePath = (resolver != null && fileName != null) ? resolver.ResolveReference(fileName, baseFilePath: null) : null;
534535

535536
if (resolvedFilePath == null)
536537
{
537538
// CS7053: Unable to resolve file path '{0}' specified for the named argument '{1}' for PermissionSet attribute
538-
Location argSyntaxLocation = nodeOpt != null ? nodeOpt.GetNamedArgumentSyntax(filePropName).Location : NoLocation.Singleton;
539+
Location argSyntaxLocation = nodeOpt?.GetNamedArgumentSyntax(filePropName)?.Location ?? NoLocation.Singleton;
539540
diagnostics.Add(ErrorCode.ERR_PermissionSetAttributeInvalidFile, argSyntaxLocation, fileName ?? "<null>", filePropName);
540541
}
541542
else if (!PermissionSetAttributeTypeHasRequiredProperty(attrType, hexPropName))
@@ -629,7 +630,7 @@ internal string DecodeGuidAttribute(AttributeSyntax? nodeOpt, DiagnosticBag diag
629630
{
630631
Debug.Assert(!this.HasErrors);
631632

632-
var guidString = (string)this.CommonConstructorArguments[0].ValueInternal;
633+
var guidString = (string?)this.CommonConstructorArguments[0].ValueInternal;
633634

634635
// Native compiler allows only a specific GUID format: "D" format (32 digits separated by hyphens)
635636
Guid guid;
@@ -641,7 +642,7 @@ internal string DecodeGuidAttribute(AttributeSyntax? nodeOpt, DiagnosticBag diag
641642
guidString = String.Empty;
642643
}
643644

644-
return guidString;
645+
return guidString!;
645646
}
646647

647648
private protected sealed override bool IsStringProperty(string memberName)

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Collections.Generic;
7-
using System.Collections.Immutable;
5+
#nullable enable
6+
87
using System.Diagnostics;
98
using System.Linq;
109
using System.Text;
11-
using Microsoft.CodeAnalysis.Collections;
12-
using Microsoft.CodeAnalysis.CSharp.Symbols;
13-
using Microsoft.CodeAnalysis.CSharp.Syntax;
1410
using Microsoft.CodeAnalysis.PooledObjects;
15-
using Microsoft.CodeAnalysis.Text;
16-
using Roslyn.Utilities;
1711

1812
namespace Microsoft.CodeAnalysis.CSharp
1913
{
@@ -35,8 +29,9 @@ public static string ToCSharpString(this TypedConstant constant)
3529
return "{" + string.Join(", ", constant.Values.Select(v => v.ToCSharpString())) + "}";
3630
}
3731

38-
if (constant.Kind == TypedConstantKind.Type || constant.TypeInternal.SpecialType == SpecialType.System_Object)
32+
if (constant.Kind == TypedConstantKind.Type || constant.TypeInternal!.SpecialType == SpecialType.System_Object)
3933
{
34+
Debug.Assert(constant.Value is object);
4035
return "typeof(" + constant.Value.ToString() + ")";
4136
}
4237

@@ -46,6 +41,7 @@ public static string ToCSharpString(this TypedConstant constant)
4641
return DisplayEnumConstant(constant);
4742
}
4843

44+
Debug.Assert(constant.ValueInternal is object);
4945
return SymbolDisplay.FormatPrimitive(constant.ValueInternal, quoteStrings: true, useHexadecimalNumbers: false);
5046
}
5147

@@ -55,7 +51,8 @@ private static string DisplayEnumConstant(TypedConstant constant)
5551
Debug.Assert(constant.Kind == TypedConstantKind.Enum);
5652

5753
// Create a ConstantValue of enum underlying type
58-
SpecialType splType = ((INamedTypeSymbol)constant.Type).EnumUnderlyingType.SpecialType;
54+
SpecialType splType = ((INamedTypeSymbol)constant.Type!).EnumUnderlyingType!.SpecialType;
55+
Debug.Assert(constant.ValueInternal is object);
5956
ConstantValue valueConstant = ConstantValue.Create(constant.ValueInternal, splType);
6057

6158
string typeName = constant.Type.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat);
@@ -71,25 +68,27 @@ private static string DisplayEnumConstant(TypedConstant constant)
7168

7269
private static string DisplayUnsignedEnumConstant(TypedConstant constant, SpecialType specialType, ulong constantToDecode, string typeName)
7370
{
71+
Debug.Assert(constant.Kind == TypedConstantKind.Enum);
72+
7473
// Specified valueConstant might have an exact matching enum field
7574
// or it might be a bitwise Or of multiple enum fields.
7675
// For the later case, we keep track of the current value of
7776
// bitwise Or of possible enum fields.
7877
ulong curValue = 0;
7978

8079
// Initialize the value string to empty
81-
PooledStringBuilder pooledBuilder = null;
82-
StringBuilder valueStringBuilder = null;
80+
PooledStringBuilder? pooledBuilder = null;
81+
StringBuilder? valueStringBuilder = null;
8382

8483
// Iterate through all the constant members in the enum type
85-
var members = constant.Type.GetMembers();
84+
var members = constant.Type!.GetMembers();
8685
foreach (var member in members)
8786
{
8887
var field = member as IFieldSymbol;
8988

90-
if ((object)field != null && field.HasConstantValue)
89+
if (field is object && field.HasConstantValue)
9190
{
92-
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType);
91+
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue!, specialType); // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964
9392
ulong memberValue = memberConstant.UInt64Value;
9493

9594
// Do we have an exact matching enum field
@@ -140,29 +139,34 @@ private static string DisplayUnsignedEnumConstant(TypedConstant constant, Specia
140139
}
141140

142141
// Unable to decode the enum constant, just display the integral value
143-
return constant.ValueInternal.ToString();
142+
Debug.Assert(constant.ValueInternal is object);
143+
var result = constant.ValueInternal.ToString();
144+
Debug.Assert(result is object);
145+
return result;
144146
}
145147

146148
private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialType specialType, long constantToDecode, string typeName)
147149
{
150+
Debug.Assert(constant.Kind == TypedConstantKind.Enum);
151+
148152
// Specified valueConstant might have an exact matching enum field
149153
// or it might be a bitwise Or of multiple enum fields.
150154
// For the later case, we keep track of the current value of
151155
// bitwise Or of possible enum fields.
152156
long curValue = 0;
153157

154158
// Initialize the value string to empty
155-
PooledStringBuilder pooledBuilder = null;
156-
StringBuilder valueStringBuilder = null;
159+
PooledStringBuilder? pooledBuilder = null;
160+
StringBuilder? valueStringBuilder = null;
157161

158162
// Iterate through all the constant members in the enum type
159-
var members = constant.Type.GetMembers();
163+
var members = constant.Type!.GetMembers();
160164
foreach (var member in members)
161165
{
162166
var field = member as IFieldSymbol;
163-
if ((object)field != null && field.HasConstantValue)
167+
if (field is object && field.HasConstantValue)
164168
{
165-
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType);
169+
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue!, specialType); // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964
166170
long memberValue = memberConstant.Int64Value;
167171

168172
// Do we have an exact matching enum field
@@ -213,7 +217,10 @@ private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialT
213217
}
214218

215219
// Unable to decode the enum constant, just display the integral value
216-
return constant.ValueInternal.ToString();
220+
Debug.Assert(constant.ValueInternal is object);
221+
var result = constant.ValueInternal.ToString();
222+
Debug.Assert(result is object);
223+
return result;
217224
}
218225
}
219226
}

src/Compilers/CSharp/Portable/Syntax/AliasedQualifiedNameSyntax.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
namespace Microsoft.CodeAnalysis.CSharp.Syntax
68
{
79
public sealed partial class AliasQualifiedNameSyntax : NameSyntax

src/Compilers/CSharp/Portable/Syntax/AnonymousFunctionExpressionSyntax.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
namespace Microsoft.CodeAnalysis.CSharp.Syntax
68
{
79
public partial class AnonymousFunctionExpressionSyntax
@@ -10,7 +12,7 @@ public partial class AnonymousFunctionExpressionSyntax
1012
/// Either the <see cref="Block"/> if it is not <c>null</c> or the
1113
/// <see cref="ExpressionBody"/> otherwise.
1214
/// </summary>
13-
public CSharpSyntaxNode Body => Block ?? (CSharpSyntaxNode)ExpressionBody;
15+
public CSharpSyntaxNode Body => Block ?? (CSharpSyntaxNode)ExpressionBody!;
1416

1517
public AnonymousFunctionExpressionSyntax WithBody(CSharpSyntaxNode body)
1618
=> body is BlockSyntax block

src/Compilers/CSharp/Portable/Syntax/ArgumentSyntax.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System.ComponentModel;
68

79
namespace Microsoft.CodeAnalysis.CSharp.Syntax

src/Compilers/CSharp/Portable/Syntax/ArrayRankSpecifierSyntax.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
namespace Microsoft.CodeAnalysis.CSharp.Syntax
68
{
79
public partial class ArrayRankSpecifierSyntax

src/Compilers/CSharp/Portable/Syntax/AttributeSyntax.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Diagnostics;
79

@@ -20,7 +22,7 @@ internal string GetErrorDisplayName()
2022
return Name.ErrorDisplayName();
2123
}
2224

23-
internal AttributeArgumentSyntax GetNamedArgumentSyntax(string namedArgName)
25+
internal AttributeArgumentSyntax? GetNamedArgumentSyntax(string namedArgName)
2426
{
2527
Debug.Assert(!String.IsNullOrEmpty(namedArgName));
2628

src/Compilers/CSharp/Portable/Syntax/AttributeTargetSpecifierSyntax.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using Microsoft.CodeAnalysis.CSharp.Symbols;
68
using Microsoft.CodeAnalysis.CSharp.Syntax;
79
using Microsoft.CodeAnalysis.Text;

src/Compilers/CSharp/Portable/Syntax/BlockSyntax.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using Microsoft.CodeAnalysis.CSharp.Syntax;
68

79
namespace Microsoft.CodeAnalysis.CSharp.Syntax

0 commit comments

Comments
 (0)