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

Annotate more public syntax APIs #44266

Merged
merged 9 commits into from
Jun 23, 2020
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
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ private ImmutableArray<TypedConstant> GetRewrittenAttributeConstructorArguments(
}
else if (reorderedArgument.Kind == TypedConstantKind.Array &&
parameter.Type.TypeKind == TypeKind.Array &&
!((TypeSymbol)reorderedArgument.TypeInternal).Equals(parameter.Type, TypeCompareKind.AllIgnoreOptions))
!((TypeSymbol)reorderedArgument.TypeInternal!).Equals(parameter.Type, TypeCompareKind.AllIgnoreOptions))
{
// NOTE: As in dev11, we don't allow array covariance conversions (presumably, we don't have a way to
// represent the conversion in metadata).
Expand Down Expand Up @@ -922,6 +922,7 @@ private static bool TryGetNormalParamValue(ParameterSymbol parameter, ImmutableA
}

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

// NOTE: Won't always succeed, even though we've performed overload resolution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ private DeclarativeSecurityAction DecodeSecurityAttributeAction(Symbol targetSym
else
{
TypedConstant firstArg = ctorArgs.First();
TypeSymbol firstArgType = (TypeSymbol)firstArg.TypeInternal;
if ((object)firstArgType != null && firstArgType.Equals(compilation.GetWellKnownType(WellKnownType.System_Security_Permissions_SecurityAction)))
var firstArgType = (TypeSymbol?)firstArg.TypeInternal;
if (firstArgType is object && firstArgType.Equals(compilation.GetWellKnownType(WellKnownType.System_Security_Permissions_SecurityAction)))
{
return DecodeSecurityAction(firstArg, targetSymbol, nodeOpt, diagnostics, out hasErrors);
}
Expand All @@ -387,6 +387,7 @@ private DeclarativeSecurityAction DecodeSecurityAction(TypedConstant typedValue,
{
Debug.Assert((object)targetSymbol != null);
Debug.Assert(targetSymbol.Kind == SymbolKind.Assembly || targetSymbol.Kind == SymbolKind.NamedType || targetSymbol.Kind == SymbolKind.Method);
Debug.Assert(typedValue.ValueInternal is object);

int securityAction = (int)typedValue.ValueInternal;
bool isPermissionRequestAction;
Expand Down Expand Up @@ -527,15 +528,15 @@ private static Location GetSecurityAttributeActionSyntaxLocation(AttributeSyntax
PermissionSetAttributeTypeHasRequiredProperty(attrType, filePropName))
{
// resolve file prop path
var fileName = (string)namedArg.Value.ValueInternal;
var fileName = (string?)namedArg.Value.ValueInternal;
var resolver = compilation.Options.XmlReferenceResolver;

resolvedFilePath = (resolver != null) ? resolver.ResolveReference(fileName, baseFilePath: null) : null;
resolvedFilePath = (resolver != null && fileName != null) ? resolver.ResolveReference(fileName, baseFilePath: null) : null;

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

var guidString = (string)this.CommonConstructorArguments[0].ValueInternal;
var guidString = (string?)this.CommonConstructorArguments[0].ValueInternal;

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

return guidString;
return guidString!;
}

private protected sealed override bool IsStringProperty(string memberName)
Expand Down
51 changes: 29 additions & 22 deletions src/Compilers/CSharp/Portable/Symbols/TypedConstantExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
#nullable enable

using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

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

if (constant.Kind == TypedConstantKind.Type || constant.TypeInternal.SpecialType == SpecialType.System_Object)
if (constant.Kind == TypedConstantKind.Type || constant.TypeInternal!.SpecialType == SpecialType.System_Object)
{
Debug.Assert(constant.Value is object);
return "typeof(" + constant.Value.ToString() + ")";
}

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

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

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

// Create a ConstantValue of enum underlying type
SpecialType splType = ((INamedTypeSymbol)constant.Type).EnumUnderlyingType.SpecialType;
SpecialType splType = ((INamedTypeSymbol)constant.Type!).EnumUnderlyingType!.SpecialType;
Debug.Assert(constant.ValueInternal is object);
ConstantValue valueConstant = ConstantValue.Create(constant.ValueInternal, splType);

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

private static string DisplayUnsignedEnumConstant(TypedConstant constant, SpecialType specialType, ulong constantToDecode, string typeName)
{
Debug.Assert(constant.Kind == TypedConstantKind.Enum);

// Specified valueConstant might have an exact matching enum field
// or it might be a bitwise Or of multiple enum fields.
// For the later case, we keep track of the current value of
// bitwise Or of possible enum fields.
ulong curValue = 0;

// Initialize the value string to empty
PooledStringBuilder pooledBuilder = null;
StringBuilder valueStringBuilder = null;
PooledStringBuilder? pooledBuilder = null;
StringBuilder? valueStringBuilder = null;

// Iterate through all the constant members in the enum type
var members = constant.Type.GetMembers();
var members = constant.Type!.GetMembers();
foreach (var member in members)
{
var field = member as IFieldSymbol;

if ((object)field != null && field.HasConstantValue)
if (field is object && field.HasConstantValue)
{
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType);
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue!, specialType); // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964
ulong memberValue = memberConstant.UInt64Value;

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

// Unable to decode the enum constant, just display the integral value
return constant.ValueInternal.ToString();
Debug.Assert(constant.ValueInternal is object);
Copy link
Member

@cston cston Jun 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug.Assert(constant.ValueInternal is object) [](start = 12, length = 46)

Why assert rather than using !? #ByDesign

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is the result of a check in a calling method (if (constant.IsNull) in ToCSharpString), but not in the present method.


In reply to: 443063994 [](ancestors = 443063994)

var result = constant.ValueInternal.ToString();
Debug.Assert(result is object);
return result;
}

private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialType specialType, long constantToDecode, string typeName)
{
Debug.Assert(constant.Kind == TypedConstantKind.Enum);

// Specified valueConstant might have an exact matching enum field
// or it might be a bitwise Or of multiple enum fields.
// For the later case, we keep track of the current value of
// bitwise Or of possible enum fields.
long curValue = 0;

// Initialize the value string to empty
PooledStringBuilder pooledBuilder = null;
StringBuilder valueStringBuilder = null;
PooledStringBuilder? pooledBuilder = null;
StringBuilder? valueStringBuilder = null;

// Iterate through all the constant members in the enum type
var members = constant.Type.GetMembers();
var members = constant.Type!.GetMembers();
foreach (var member in members)
{
var field = member as IFieldSymbol;
if ((object)field != null && field.HasConstantValue)
if (field is object && field.HasConstantValue)
{
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType);
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue!, specialType); // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964
long memberValue = memberConstant.Int64Value;

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

// Unable to decode the enum constant, just display the integral value
return constant.ValueInternal.ToString();
Debug.Assert(constant.ValueInternal is object);
Copy link
Member

@cston cston Jun 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug.Assert(constant.ValueInternal is object); [](start = 12, length = 47)

Why assert rather than using !? #ByDesign

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same rationale


In reply to: 443064067 [](ancestors = 443064067)

var result = constant.ValueInternal.ToString();
Debug.Assert(result is object);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

namespace Microsoft.CodeAnalysis.CSharp.Syntax
{
public sealed partial class AliasQualifiedNameSyntax : NameSyntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

namespace Microsoft.CodeAnalysis.CSharp.Syntax
{
public partial class AnonymousFunctionExpressionSyntax
Expand All @@ -10,7 +12,7 @@ public partial class AnonymousFunctionExpressionSyntax
/// Either the <see cref="Block"/> if it is not <c>null</c> or the
/// <see cref="ExpressionBody"/> otherwise.
/// </summary>
public CSharpSyntaxNode Body => Block ?? (CSharpSyntaxNode)ExpressionBody;
public CSharpSyntaxNode Body => Block ?? (CSharpSyntaxNode)ExpressionBody!;

public AnonymousFunctionExpressionSyntax WithBody(CSharpSyntaxNode body)
=> body is BlockSyntax block
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Syntax/ArgumentSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System.ComponentModel;

namespace Microsoft.CodeAnalysis.CSharp.Syntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

namespace Microsoft.CodeAnalysis.CSharp.Syntax
{
public partial class ArrayRankSpecifierSyntax
Expand Down
4 changes: 3 additions & 1 deletion src/Compilers/CSharp/Portable/Syntax/AttributeSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;
using System.Diagnostics;

Expand All @@ -20,7 +22,7 @@ internal string GetErrorDisplayName()
return Name.ErrorDisplayName();
}

internal AttributeArgumentSyntax GetNamedArgumentSyntax(string namedArgName)
internal AttributeArgumentSyntax? GetNamedArgumentSyntax(string namedArgName)
{
Debug.Assert(!String.IsNullOrEmpty(namedArgName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Syntax/BlockSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Microsoft.CodeAnalysis.CSharp.Syntax
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Syntax/BreakStatementSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Microsoft.CodeAnalysis.CSharp.Syntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
Expand Down Expand Up @@ -60,7 +62,7 @@ protected override LineMappingEntry GetEntry(DirectiveTriviaSyntax directiveNode
// skip both the mapped line and the filename if the line number is not valid
if (!lineToken.ContainsDiagnostics)
{
object value = lineToken.Value;
object? value = lineToken.Value;
if (value is int)
{
// convert one-based line number into zero-based line number
Expand All @@ -69,7 +71,7 @@ protected override LineMappingEntry GetEntry(DirectiveTriviaSyntax directiveNode

if (directive.File.Kind() == SyntaxKind.StringLiteralToken)
{
mappedPathOpt = (string)directive.File.Value;
mappedPathOpt = (string?)directive.File.Value;
}

state = PositionState.Remapped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand Down Expand Up @@ -125,7 +127,7 @@ private static WarningStateMapEntry[] CreatePragmaWarningStateEntries(ArrayBuild
if (currentErrorCode.Kind() == SyntaxKind.NumericLiteralExpression)
{
var token = ((LiteralExpressionSyntax)currentErrorCode).Token;
errorId = MessageProvider.Instance.GetIdForErrorCode((int)token.Value);
errorId = MessageProvider.Instance.GetIdForErrorCode((int)token.Value!);
}
else if (currentErrorCode.Kind() == SyntaxKind.IdentifierName)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ protected internal override SyntaxNode InsertTriviaInListCore(SyntaxTrivia origi
return SyntaxReplacer.InsertTriviaInList(this, originalTrivia, newTrivia, insertBefore).AsRootOfNewTreeWithOptionsFrom(this.SyntaxTree);
}

protected internal override SyntaxNode RemoveNodesCore(IEnumerable<SyntaxNode> nodes, SyntaxRemoveOptions options)
protected internal override SyntaxNode? RemoveNodesCore(IEnumerable<SyntaxNode> nodes, SyntaxRemoveOptions options)
{
return SyntaxNodeRemover.RemoveNodes(this, nodes.Cast<CSharpSyntaxNode>(), options).AsRootOfNewTreeWithOptionsFrom(this.SyntaxTree);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Syntax;

Expand All @@ -31,6 +32,7 @@ public virtual bool VisitIntoStructuredTrivia

private int _recursionDepth;

[return: NotNullIfNotNull("node")]
public override SyntaxNode? Visit(SyntaxNode? node)
{
if (node != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;
using System.Collections.Immutable;
using System.Text;
Expand Down
Loading