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 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -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,6 +528,7 @@ private static Location GetSecurityAttributeActionSyntaxLocation(AttributeSyntax
PermissionSetAttributeTypeHasRequiredProperty(attrType, filePropName))
{
// resolve file prop path
Debug.Assert(namedArg.Value.ValueInternal is object);
var fileName = (string)namedArg.Value.ValueInternal;
var resolver = compilation.Options.XmlReferenceResolver;

Expand All @@ -535,7 +537,7 @@ private static Location GetSecurityAttributeActionSyntaxLocation(AttributeSyntax
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 +631,8 @@ internal string DecodeGuidAttribute(AttributeSyntax? nodeOpt, DiagnosticBag diag
{
Debug.Assert(!this.HasErrors);

var guidString = (string)this.CommonConstructorArguments[0].ValueInternal;
Debug.Assert(this.CommonConstructorArguments[0].ValueInternal is object);
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 Down
42 changes: 23 additions & 19 deletions src/Compilers/CSharp/Portable/Symbols/TypedConstantExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@
// 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
{
public static class TypedConstantExtensions
{
/// <summary>
/// Returns the System.String that represents the current TypedConstant.
/// For testing and debugging only
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.

For testing and debugging only [](start = 12, length = 30)

This method is part of the public API so I don't think we can state how this method is used. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks


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

/// </summary>
/// <returns>A System.String that represents the current TypedConstant.</returns>
public static string ToCSharpString(this TypedConstant constant)
Expand All @@ -37,6 +32,7 @@ public static string ToCSharpString(this TypedConstant constant)

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 +42,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 +52,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 @@ -78,18 +76,18 @@ private static string DisplayUnsignedEnumConstant(TypedConstant constant, Specia
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();
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,7 +138,10 @@ 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)
Expand All @@ -152,17 +153,17 @@ private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialT
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();
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 +214,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// 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.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading;
Expand All @@ -22,20 +25,20 @@ private class ParsedSyntaxTree : CSharpSyntaxTree
private readonly string _path;
private readonly CSharpSyntaxNode _root;
private readonly bool _hasCompilationUnitRoot;
private readonly Encoding _encodingOpt;
private readonly Encoding? _encodingOpt;
private readonly SourceHashAlgorithm _checksumAlgorithm;
private readonly ImmutableDictionary<string, ReportDiagnostic> _diagnosticOptions;
private SourceText _lazyText;
private SourceText? _lazyText;

internal ParsedSyntaxTree(
SourceText textOpt,
Encoding encodingOpt,
SourceText? textOpt,
Encoding? encodingOpt,
SourceHashAlgorithm checksumAlgorithm,
string path,
CSharpParseOptions options,
CSharpSyntaxNode root,
Syntax.InternalSyntax.DirectiveStack directives,
ImmutableDictionary<string, ReportDiagnostic> diagnosticOptions,
ImmutableDictionary<string, ReportDiagnostic>? diagnosticOptions,
bool? isGeneratedCode,
bool cloneRoot)
{
Expand Down Expand Up @@ -75,13 +78,13 @@ public override SourceText GetText(CancellationToken cancellationToken)
return _lazyText;
}

public override bool TryGetText(out SourceText text)
public override bool TryGetText([NotNullWhen(true)] out SourceText? text)
{
text = _lazyText;
return text != null;
}

public override Encoding Encoding
public override Encoding? Encoding
{
get { return _encodingOpt; }
}
Expand Down Expand Up @@ -133,7 +136,7 @@ public override SyntaxTree WithRootAndOptions(SyntaxNode root, ParseOptions opti
}

return new ParsedSyntaxTree(
null,
textOpt: null,
_encodingOpt,
_checksumAlgorithm,
_path,
Expand Down
Loading