未验证 提交 87dc5a0b 编写于 作者: R Rikki Gibson 提交者: GitHub

Nullable annotate TypeCompilationState and MessageID (#39449)

上级 73b57665
......@@ -1235,6 +1235,7 @@ private BoundExpression BindSizeOf(SizeOfExpressionSyntax node, DiagnosticBag di
this.GetSpecialType(SpecialType.System_Int32, diagnostics, node), hasErrors);
}
#nullable enable
/// <returns>true if managed type-related errors were found, otherwise false.</returns>
internal static bool CheckManagedAddr(CSharpCompilation compilation, TypeSymbol type, Location location, DiagnosticBag diagnostics)
{
......@@ -1243,13 +1244,14 @@ internal static bool CheckManagedAddr(CSharpCompilation compilation, TypeSymbol
case ManagedKind.Managed:
diagnostics.Add(ErrorCode.ERR_ManagedAddr, location, type);
return true;
case ManagedKind.UnmanagedWithGenerics when MessageID.IDS_FeatureUnmanagedConstructedTypes.GetFeatureAvailabilityDiagnosticInfoOpt(compilation) is CSDiagnosticInfo diagnosticInfo:
case ManagedKind.UnmanagedWithGenerics when MessageID.IDS_FeatureUnmanagedConstructedTypes.GetFeatureAvailabilityDiagnosticInfo(compilation) is CSDiagnosticInfo diagnosticInfo:
diagnostics.Add(diagnosticInfo, location);
return true;
default:
return false;
}
}
#nullable restore
internal static ConstantValue GetConstantSizeOf(TypeSymbol type)
{
......
......@@ -2309,14 +2309,15 @@ protected AssemblySymbol GetForwardedToAssembly(string name, int arity, ref Name
return null;
}
internal static bool CheckFeatureAvailability(SyntaxNode syntax, MessageID feature, DiagnosticBag diagnostics, Location locationOpt = null)
#nullable enable
internal static bool CheckFeatureAvailability(SyntaxNode syntax, MessageID feature, DiagnosticBag diagnostics, Location? location = null)
{
return CheckFeatureAvailability(syntax.SyntaxTree, feature, diagnostics, locationOpt ?? syntax.GetLocation());
return CheckFeatureAvailability(syntax.SyntaxTree, feature, diagnostics, location ?? syntax.GetLocation());
}
internal static bool CheckFeatureAvailability(SyntaxTree tree, MessageID feature, DiagnosticBag diagnostics, Location location)
{
if (feature.GetFeatureAvailabilityDiagnosticInfoOpt((CSharpParseOptions)tree.Options) is { } diagInfo)
if (feature.GetFeatureAvailabilityDiagnosticInfo((CSharpParseOptions)tree.Options) is { } diagInfo)
{
diagnostics.Add(diagInfo, location);
return false;
......
......@@ -687,7 +687,7 @@ private void CompileSynthesizedMethods(TypeCompilationState compilationState)
{
foreach (var methodWithBody in synthesizedMethods)
{
var importChain = methodWithBody.ImportChainOpt;
var importChain = methodWithBody.ImportChain;
compilationState.CurrentImportChain = importChain;
// We make sure that an asynchronous mutation to the diagnostic bag does not
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Emit;
......@@ -25,28 +27,28 @@ internal struct MethodWithBody
{
public readonly MethodSymbol Method;
public readonly BoundStatement Body;
public readonly ImportChain ImportChainOpt;
public readonly ImportChain? ImportChain;
internal MethodWithBody(MethodSymbol method, BoundStatement body, ImportChain importChainOpt)
internal MethodWithBody(MethodSymbol method, BoundStatement body, ImportChain? importChain)
{
Debug.Assert(method != null);
Debug.Assert(body != null);
RoslynDebug.Assert(method != null);
RoslynDebug.Assert(body != null);
this.Method = method;
this.Body = body;
this.ImportChainOpt = importChainOpt;
this.ImportChain = importChain;
}
}
/// <summary> Flat array of created methods, non-empty if not-null </summary>
private ArrayBuilder<MethodWithBody> _synthesizedMethods;
private ArrayBuilder<MethodWithBody>? _synthesizedMethods;
/// <summary>
/// Map of wrapper methods created for base access of base type virtual methods from
/// other classes (like those created for lambdas...); actually each method symbol will
/// only need one wrapper to call it non-virtually.
/// </summary>
private Dictionary<MethodSymbol, MethodSymbol> _wrappers;
private Dictionary<MethodSymbol, MethodSymbol>? _wrappers;
/// <summary>
/// Type symbol being compiled, or null if we compile a synthesized type that doesn't have a symbol (e.g. PrivateImplementationDetails).
......@@ -62,17 +64,17 @@ internal MethodWithBody(MethodSymbol method, BoundStatement body, ImportChain im
/// Any generated methods that don't suppress debug info will use this
/// list of debug imports.
/// </summary>
public ImportChain CurrentImportChain;
public ImportChain? CurrentImportChain;
public readonly CSharpCompilation Compilation;
public SynthesizedClosureEnvironment StaticLambdaFrame;
public SynthesizedClosureEnvironment? StaticLambdaFrame;
/// <summary>
/// A graph of method->method references for this(...) constructor initializers.
/// Used to detect and report initializer cycles.
/// </summary>
private SmallDictionary<MethodSymbol, MethodSymbol> _constructorInitializers;
private SmallDictionary<MethodSymbol, MethodSymbol>? _constructorInitializers;
public TypeCompilationState(NamedTypeSymbol typeOpt, CSharpCompilation compilation, PEModuleBuilder moduleBuilderOpt)
{
......@@ -89,7 +91,7 @@ public NamedTypeSymbol Type
get
{
// NOTE: currently it can be null if only private implementation type methods are compiled
Debug.Assert((object)_typeOpt != null);
RoslynDebug.Assert((object)_typeOpt != null);
return _typeOpt;
}
}
......@@ -97,7 +99,7 @@ public NamedTypeSymbol Type
/// <summary>
/// The type passed to the runtime binder as context.
/// </summary>
public NamedTypeSymbol DynamicOperationContextType
public NamedTypeSymbol? DynamicOperationContextType
{
get
{
......@@ -110,7 +112,7 @@ public bool Emitting
get { return ModuleBuilderOpt != null; }
}
public ArrayBuilder<MethodWithBody> SynthesizedMethods
public ArrayBuilder<MethodWithBody>? SynthesizedMethods
{
get { return _synthesizedMethods; }
set
......@@ -165,9 +167,9 @@ public int NextWrapperMethodIndex
/// Wrapper methods are created for base access of base type virtual methods from
/// other classes (like those created for lambdas...).
/// </remarks>
public MethodSymbol GetMethodWrapper(MethodSymbol method)
public MethodSymbol? GetMethodWrapper(MethodSymbol method)
{
MethodSymbol wrapper = null;
MethodSymbol? wrapper = null;
return _wrappers != null && _wrappers.TryGetValue(method, out wrapper) ? wrapper : null;
}
......@@ -217,7 +219,7 @@ internal void ReportCtorInitializerCycles(MethodSymbol method1, MethodSymbol met
{
if (_constructorInitializers.TryGetValue(next, out next))
{
Debug.Assert((object)next != null);
RoslynDebug.Assert((object)next != null);
if (method1 == next)
{
// We found a (new) cycle containing the edge (method1, method2). Report an
......
......@@ -32,10 +32,11 @@ public static void AddAll(bool isNullableEnabled, TypeWithAnnotations type, Loca
rawInfos.Free();
}
#nullable enable
private static void GetRawDiagnosticInfos(bool isNullableEnabled, CSharpSyntaxTree tree, ArrayBuilder<DiagnosticInfo> infos)
{
const MessageID featureId = MessageID.IDS_FeatureNullableReferenceTypes;
var info = featureId.GetFeatureAvailabilityDiagnosticInfoOpt(tree.Options);
var info = featureId.GetFeatureAvailabilityDiagnosticInfo(tree.Options);
if (info is object)
{
infos.Add(info);
......@@ -47,6 +48,7 @@ private static void GetRawDiagnosticInfos(bool isNullableEnabled, CSharpSyntaxTr
infos.Add(new CSDiagnosticInfo(code));
}
}
#nullable restore
private static bool IsNullableReference(TypeSymbol type)
=> type is null || !(type.IsValueType || type.IsErrorType());
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Diagnostics;
using Roslyn.Utilities;
......@@ -200,7 +202,7 @@ public override string ToString()
return ToString(null, null);
}
public string ToString(string format, IFormatProvider formatProvider)
public string ToString(string? format, IFormatProvider? formatProvider)
{
return ErrorFacts.GetMessage(_id, formatProvider as System.Globalization.CultureInfo);
}
......@@ -221,7 +223,7 @@ public static LocalizableErrorArgument Localize(this MessageID id)
// If this method returns non-null, use that.
// Features should be mutually exclusive between RequiredFeature and RequiredVersion.
// (hence the above rule - RequiredVersion throws when RequiredFeature returns non-null)
internal static string RequiredFeature(this MessageID feature)
internal static string? RequiredFeature(this MessageID feature)
{
// Check for current experimental features, if any, in the current branch.
switch (feature)
......@@ -235,9 +237,9 @@ internal static string RequiredFeature(this MessageID feature)
this MessageID feature,
DiagnosticBag diagnostics,
SyntaxNode syntax,
Location location = null)
Location? location = null)
{
var diag = GetFeatureAvailabilityDiagnosticInfoOpt(feature, (CSharpParseOptions)syntax.SyntaxTree.Options);
var diag = GetFeatureAvailabilityDiagnosticInfo(feature, (CSharpParseOptions)syntax.SyntaxTree.Options);
if (diag is object)
{
diagnostics.Add(diag, location ?? syntax.GetLocation());
......@@ -252,7 +254,7 @@ internal static string RequiredFeature(this MessageID feature)
Compilation compilation,
Location location)
{
if (GetFeatureAvailabilityDiagnosticInfoOpt(feature, (CSharpCompilation)compilation) is { } diagInfo)
if (GetFeatureAvailabilityDiagnosticInfo(feature, (CSharpCompilation)compilation) is { } diagInfo)
{
diagnostics.Add(diagInfo, location);
return false;
......@@ -260,15 +262,15 @@ internal static string RequiredFeature(this MessageID feature)
return true;
}
internal static CSDiagnosticInfo GetFeatureAvailabilityDiagnosticInfoOpt(this MessageID feature, CSharpParseOptions options)
internal static CSDiagnosticInfo? GetFeatureAvailabilityDiagnosticInfo(this MessageID feature, CSharpParseOptions options)
=> options.IsFeatureEnabled(feature) ? null : GetDisabledFeatureDiagnosticInfo(feature, options.LanguageVersion);
internal static CSDiagnosticInfo GetFeatureAvailabilityDiagnosticInfoOpt(this MessageID feature, CSharpCompilation compilation)
internal static CSDiagnosticInfo? GetFeatureAvailabilityDiagnosticInfo(this MessageID feature, CSharpCompilation compilation)
=> compilation.IsFeatureEnabled(feature) ? null : GetDisabledFeatureDiagnosticInfo(feature, compilation.LanguageVersion);
private static CSDiagnosticInfo GetDisabledFeatureDiagnosticInfo(MessageID feature, LanguageVersion availableVersion)
{
string requiredFeature = feature.RequiredFeature();
string? requiredFeature = feature.RequiredFeature();
if (requiredFeature != null)
{
return new CSDiagnosticInfo(ErrorCode.ERR_FeatureIsExperimental, feature.Localize(), requiredFeature);
......
......@@ -937,14 +937,16 @@ private void ScanSyntaxToken(ref TokenInfo info)
}
}
#nullable enable
private void CheckFeatureAvailability(MessageID feature)
{
var info = feature.GetFeatureAvailabilityDiagnosticInfoOpt(Options);
var info = feature.GetFeatureAvailabilityDiagnosticInfo(Options);
if (info != null)
{
AddError(info.Code, info.Arguments);
}
}
#nullable restore
private bool ScanInteger()
{
......
......@@ -1049,6 +1049,7 @@ internal DirectiveStack Directives
get { return lexer.Directives; }
}
#nullable enable
/// <remarks>
/// NOTE: we are specifically diverging from dev11 to improve the user experience.
/// Since treating the "async" keyword as an identifier in older language
......@@ -1077,7 +1078,7 @@ protected TNode CheckFeatureAvailability<TNode>(TNode node, MessageID feature, b
new CSharpRequiredLanguageVersion(requiredVersion));
}
var info = feature.GetFeatureAvailabilityDiagnosticInfoOpt(this.Options);
var info = feature.GetFeatureAvailabilityDiagnosticInfo(this.Options);
if (info != null)
{
if (forceWarning)
......@@ -1090,6 +1091,7 @@ protected TNode CheckFeatureAvailability<TNode>(TNode node, MessageID feature, b
return node;
}
#nullable restore
protected bool IsFeatureEnabled(MessageID feature)
{
......
......@@ -888,7 +888,7 @@ private static bool HasDuplicateInterfaces(NamedTypeSymbol type, ConsList<TypeSy
// extension method to be applicable, but then when you try to use it the IDE tells you to upgrade your language version.
if (!(currentCompilation is null))
{
var csDiagnosticInfo = MessageID.IDS_FeatureUnmanagedConstructedTypes.GetFeatureAvailabilityDiagnosticInfoOpt(currentCompilation);
var csDiagnosticInfo = MessageID.IDS_FeatureUnmanagedConstructedTypes.GetFeatureAvailabilityDiagnosticInfo(currentCompilation);
if (csDiagnosticInfo != null)
{
var typeParameterDiagnosticInfo = new TypeParameterDiagnosticInfo(typeParameter, csDiagnosticInfo);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册