diff --git a/src/Compilers/CSharp/Portable/CSharpCompilationOptions.cs b/src/Compilers/CSharp/Portable/CSharpCompilationOptions.cs index 3cbb4c019fec4282a1a05d5a938363d899743bc5..66d508d5b62b81840326f1acbfb97d3fd084f150 100644 --- a/src/Compilers/CSharp/Portable/CSharpCompilationOptions.cs +++ b/src/Compilers/CSharp/Portable/CSharpCompilationOptions.cs @@ -36,7 +36,7 @@ public sealed class CSharpCompilationOptions : CompilationOptions, IEquatable /// Global Nullable context options. /// - public NullableContextOptions NullableContextOptions { get; private set; } + public override NullableContextOptions NullableContextOptions { get; protected set; } // Defaults correspond to the compiler's defaults or indicate that the user did not specify when that is significant. // That's significant when one option depends on another's setting. SubsystemVersion depends on Platform and Target. diff --git a/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs b/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs index 376fbc5b8abff403c47d83c304d874808e8a0fb4..da0d262648d8e83147f5ccca020c2883e81e7742 100644 --- a/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs +++ b/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs @@ -5024,6 +5024,27 @@ protected sealed override bool IsEventUsableAsFieldCore(int position, IEventSymb return this.IsEventUsableAsField(position, symbol.EnsureCSharpSymbolOrNull(nameof(symbol))); } + public sealed override NullableContext GetNullableContext(int position) + { + var syntaxTree = (CSharpSyntaxTree)Root.SyntaxTree; + NullableContextState contextState = syntaxTree.GetNullableContextState(position); + var defaultState = syntaxTree.IsGeneratedCode() ? NullableContextOptions.Disable : Compilation.Options.NullableContextOptions; + + NullableContext context = getFlag(contextState.AnnotationsState, defaultState.AnnotationsEnabled(), NullableContext.AnnotationsContextInherited, NullableContext.AnnotationsEnabled); + context |= getFlag(contextState.WarningsState, defaultState.WarningsEnabled(), NullableContext.WarningsContextInherited, NullableContext.WarningsEnabled); + + return context; + + static NullableContext getFlag(bool? contextState, bool defaultEnableState, NullableContext inheritedFlag, NullableContext enableFlag) => + contextState switch + { + null when defaultEnableState => (inheritedFlag | enableFlag), + null => inheritedFlag, + true => enableFlag, + false => NullableContext.Disabled + }; + } + #endregion } } diff --git a/src/Compilers/CSharp/Portable/NullableContextOptions.cs b/src/Compilers/CSharp/Portable/NullableContextOptions.cs deleted file mode 100644 index eee1b25af0922b50d1edcf695256d45dabc5a0e6..0000000000000000000000000000000000000000 --- a/src/Compilers/CSharp/Portable/NullableContextOptions.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.CodeAnalysis.CSharp -{ - /// - /// Specifies the nullable context. - /// - public enum NullableContextOptions : byte - { - /// - /// Nullable annotation and warning contexts are disabled. - /// - Disable, - - /// - /// Nullable annotation and warning contexts are enabled. - /// - Enable, - - /// - /// Nullable annotation context is disabled and the nullable warning context is enabled. - /// - Warnings, - - /// - /// Nullable annotation context is enabled and the nullable warning context is disabled. - /// - Annotations - } -} diff --git a/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt b/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt index b8300fd3508b015cc22065919cdbb4f3b5d9b49f..b4b24024ff47b9868ae1c892ca76a9a1ccce1c5e 100644 --- a/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt +++ b/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt @@ -17,7 +17,8 @@ *REMOVED*Microsoft.CodeAnalysis.CSharp.Syntax.EnumMemberDeclarationSyntax.AttributeLists.get -> Microsoft.CodeAnalysis.SyntaxList *REMOVED*Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax.AttributeLists.get -> Microsoft.CodeAnalysis.SyntaxList *REMOVED*Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax.Modifiers.get -> Microsoft.CodeAnalysis.SyntaxTokenList -Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Annotations = 3 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions +Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind outputKind, bool reportSuppressedDiagnostics = false, string moduleName = null, string mainTypeName = null, string scriptClassName = null, System.Collections.Generic.IEnumerable usings = null, Microsoft.CodeAnalysis.OptimizationLevel optimizationLevel = Microsoft.CodeAnalysis.OptimizationLevel.Debug, bool checkOverflow = false, bool allowUnsafe = false, string cryptoKeyContainer = null, string cryptoKeyFile = null, System.Collections.Immutable.ImmutableArray cryptoPublicKey = default(System.Collections.Immutable.ImmutableArray), bool? delaySign = null, Microsoft.CodeAnalysis.Platform platform = Microsoft.CodeAnalysis.Platform.AnyCpu, Microsoft.CodeAnalysis.ReportDiagnostic generalDiagnosticOption = Microsoft.CodeAnalysis.ReportDiagnostic.Default, int warningLevel = 4, System.Collections.Generic.IEnumerable> specificDiagnosticOptions = null, bool concurrentBuild = true, bool deterministic = false, Microsoft.CodeAnalysis.XmlReferenceResolver xmlReferenceResolver = null, Microsoft.CodeAnalysis.SourceReferenceResolver sourceReferenceResolver = null, Microsoft.CodeAnalysis.MetadataReferenceResolver metadataReferenceResolver = null, Microsoft.CodeAnalysis.AssemblyIdentityComparer assemblyIdentityComparer = null, Microsoft.CodeAnalysis.StrongNameProvider strongNameProvider = null, bool publicSign = false, Microsoft.CodeAnalysis.MetadataImportOptions metadataImportOptions = Microsoft.CodeAnalysis.MetadataImportOptions.Public, Microsoft.CodeAnalysis.NullableContextOptions nullableContextOptions = Microsoft.CodeAnalysis.NullableContextOptions.Disable) -> void +Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.WithNullableContextOptions(Microsoft.CodeAnalysis.NullableContextOptions options) -> Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions Microsoft.CodeAnalysis.CSharp.Syntax.EventDeclarationSyntax.SemicolonToken.get -> Microsoft.CodeAnalysis.SyntaxToken Microsoft.CodeAnalysis.CSharp.Syntax.EventDeclarationSyntax.Update(Microsoft.CodeAnalysis.SyntaxList attributeLists, Microsoft.CodeAnalysis.SyntaxTokenList modifiers, Microsoft.CodeAnalysis.SyntaxToken eventKeyword, Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax type, Microsoft.CodeAnalysis.CSharp.Syntax.ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier, Microsoft.CodeAnalysis.SyntaxToken identifier, Microsoft.CodeAnalysis.CSharp.Syntax.AccessorListSyntax accessorList, Microsoft.CodeAnalysis.SyntaxToken semicolonToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.EventDeclarationSyntax Microsoft.CodeAnalysis.CSharp.Syntax.EventDeclarationSyntax.Update(Microsoft.CodeAnalysis.SyntaxList attributeLists, Microsoft.CodeAnalysis.SyntaxTokenList modifiers, Microsoft.CodeAnalysis.SyntaxToken eventKeyword, Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax type, Microsoft.CodeAnalysis.CSharp.Syntax.ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier, Microsoft.CodeAnalysis.SyntaxToken identifier, Microsoft.CodeAnalysis.SyntaxToken semicolonToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.EventDeclarationSyntax @@ -30,17 +31,10 @@ Microsoft.CodeAnalysis.CSharp.SyntaxKind.AnnotationsKeyword = 8489 -> Microsoft. Microsoft.CodeAnalysis.CSharp.SyntaxKind.WarningsKeyword = 8488 -> Microsoft.CodeAnalysis.CSharp.SyntaxKind abstract Microsoft.CodeAnalysis.CSharp.Syntax.CommonForEachStatementSyntax.AwaitKeyword.get -> Microsoft.CodeAnalysis.SyntaxToken Microsoft.CodeAnalysis.CSharp.Conversion.IsSwitchExpression.get -> bool -Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind outputKind, bool reportSuppressedDiagnostics = false, string moduleName = null, string mainTypeName = null, string scriptClassName = null, System.Collections.Generic.IEnumerable usings = null, Microsoft.CodeAnalysis.OptimizationLevel optimizationLevel = Microsoft.CodeAnalysis.OptimizationLevel.Debug, bool checkOverflow = false, bool allowUnsafe = false, string cryptoKeyContainer = null, string cryptoKeyFile = null, System.Collections.Immutable.ImmutableArray cryptoPublicKey = default(System.Collections.Immutable.ImmutableArray), bool? delaySign = null, Microsoft.CodeAnalysis.Platform platform = Microsoft.CodeAnalysis.Platform.AnyCpu, Microsoft.CodeAnalysis.ReportDiagnostic generalDiagnosticOption = Microsoft.CodeAnalysis.ReportDiagnostic.Default, int warningLevel = 4, System.Collections.Generic.IEnumerable> specificDiagnosticOptions = null, bool concurrentBuild = true, bool deterministic = false, Microsoft.CodeAnalysis.XmlReferenceResolver xmlReferenceResolver = null, Microsoft.CodeAnalysis.SourceReferenceResolver sourceReferenceResolver = null, Microsoft.CodeAnalysis.MetadataReferenceResolver metadataReferenceResolver = null, Microsoft.CodeAnalysis.AssemblyIdentityComparer assemblyIdentityComparer = null, Microsoft.CodeAnalysis.StrongNameProvider strongNameProvider = null, bool publicSign = false, Microsoft.CodeAnalysis.MetadataImportOptions metadataImportOptions = Microsoft.CodeAnalysis.MetadataImportOptions.Public, Microsoft.CodeAnalysis.CSharp.NullableContextOptions nullableContextOptions = Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Disable) -> void Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind outputKind, bool reportSuppressedDiagnostics, string moduleName, string mainTypeName, string scriptClassName, System.Collections.Generic.IEnumerable usings, Microsoft.CodeAnalysis.OptimizationLevel optimizationLevel, bool checkOverflow, bool allowUnsafe, string cryptoKeyContainer, string cryptoKeyFile, System.Collections.Immutable.ImmutableArray cryptoPublicKey, bool? delaySign, Microsoft.CodeAnalysis.Platform platform, Microsoft.CodeAnalysis.ReportDiagnostic generalDiagnosticOption, int warningLevel, System.Collections.Generic.IEnumerable> specificDiagnosticOptions, bool concurrentBuild, bool deterministic, Microsoft.CodeAnalysis.XmlReferenceResolver xmlReferenceResolver, Microsoft.CodeAnalysis.SourceReferenceResolver sourceReferenceResolver, Microsoft.CodeAnalysis.MetadataReferenceResolver metadataReferenceResolver, Microsoft.CodeAnalysis.AssemblyIdentityComparer assemblyIdentityComparer, Microsoft.CodeAnalysis.StrongNameProvider strongNameProvider, bool publicSign, Microsoft.CodeAnalysis.MetadataImportOptions metadataImportOptions) -> void -Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.NullableContextOptions.get -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions -Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.WithNullableContextOptions(Microsoft.CodeAnalysis.CSharp.NullableContextOptions options) -> Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8 = 800 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion Microsoft.CodeAnalysis.CSharp.LanguageVersion.LatestMajor = 2147483645 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion Microsoft.CodeAnalysis.CSharp.LanguageVersion.Preview = 2147483646 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion -Microsoft.CodeAnalysis.CSharp.NullableContextOptions -Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Disable = 0 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions -Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Enable = 1 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions -Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Warnings = 2 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions Microsoft.CodeAnalysis.CSharp.Syntax.AnonymousFunctionExpressionSyntax.WithAsyncKeyword(Microsoft.CodeAnalysis.SyntaxToken asyncKeyword) -> Microsoft.CodeAnalysis.CSharp.Syntax.AnonymousFunctionExpressionSyntax Microsoft.CodeAnalysis.CSharp.Syntax.AnonymousFunctionExpressionSyntax.WithBody(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode body) -> Microsoft.CodeAnalysis.CSharp.Syntax.AnonymousFunctionExpressionSyntax Microsoft.CodeAnalysis.CSharp.Syntax.BaseArgumentListSyntax.AddArguments(params Microsoft.CodeAnalysis.CSharp.Syntax.ArgumentSyntax[] items) -> Microsoft.CodeAnalysis.CSharp.Syntax.BaseArgumentListSyntax @@ -245,6 +239,7 @@ Microsoft.CodeAnalysis.CSharp.SyntaxKind.VarPattern = 9027 -> Microsoft.CodeAnal override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ClassifyCommonConversion(Microsoft.CodeAnalysis.ITypeSymbol source, Microsoft.CodeAnalysis.ITypeSymbol destination) -> Microsoft.CodeAnalysis.Operations.CommonConversion override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ContainsSymbolsWithName(string name, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> bool override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetSymbolsWithName(string name, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Collections.Generic.IEnumerable +override Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.NullableContextOptions.get -> Microsoft.CodeAnalysis.NullableContextOptions override Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitDiscardPattern(Microsoft.CodeAnalysis.CSharp.Syntax.DiscardPatternSyntax node) -> Microsoft.CodeAnalysis.SyntaxNode override Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitNullableDirectiveTrivia(Microsoft.CodeAnalysis.CSharp.Syntax.NullableDirectiveTriviaSyntax node) -> Microsoft.CodeAnalysis.SyntaxNode override Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitPositionalPatternClause(Microsoft.CodeAnalysis.CSharp.Syntax.PositionalPatternClauseSyntax node) -> Microsoft.CodeAnalysis.SyntaxNode diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableContextTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableContextTests.cs new file mode 100644 index 0000000000000000000000000000000000000000..794287ac99279fd101df3582246d54c2600fced6 --- /dev/null +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableContextTests.cs @@ -0,0 +1,149 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Linq; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.CSharp.Test.Utilities; +using Xunit; + +namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics +{ + public class NullableContextTests : CSharpTestBase + { + [InlineData("#nullable enable", NullableContextOptions.Disable, NullableContext.Enabled)] + [InlineData("#nullable enable", NullableContextOptions.Annotations, NullableContext.Enabled)] + [InlineData("#nullable enable", NullableContextOptions.Warnings, NullableContext.Enabled)] + [InlineData("#nullable enable", NullableContextOptions.Enable, NullableContext.Enabled)] + + [InlineData("#nullable enable warnings", NullableContextOptions.Disable, NullableContext.WarningsEnabled | NullableContext.AnnotationsContextInherited)] + [InlineData("#nullable enable warnings", NullableContextOptions.Warnings, NullableContext.WarningsEnabled | NullableContext.AnnotationsContextInherited)] + [InlineData("#nullable enable warnings", NullableContextOptions.Annotations, NullableContext.Enabled | NullableContext.AnnotationsContextInherited)] + [InlineData("#nullable enable warnings", NullableContextOptions.Enable, NullableContext.Enabled | NullableContext.AnnotationsContextInherited)] + + [InlineData("#nullable enable annotations", NullableContextOptions.Disable, NullableContext.AnnotationsEnabled | NullableContext.WarningsContextInherited)] + [InlineData("#nullable enable annotations", NullableContextOptions.Warnings, NullableContext.Enabled | NullableContext.WarningsContextInherited)] + [InlineData("#nullable enable annotations", NullableContextOptions.Annotations, NullableContext.AnnotationsEnabled | NullableContext.WarningsContextInherited)] + [InlineData("#nullable enable annotations", NullableContextOptions.Enable, NullableContext.Enabled | NullableContext.WarningsContextInherited)] + + [InlineData("#nullable disable", NullableContextOptions.Disable, NullableContext.Disabled)] + [InlineData("#nullable disable", NullableContextOptions.Annotations, NullableContext.Disabled)] + [InlineData("#nullable disable", NullableContextOptions.Warnings, NullableContext.Disabled)] + [InlineData("#nullable disable", NullableContextOptions.Enable, NullableContext.Disabled)] + + [InlineData("#nullable disable warnings", NullableContextOptions.Disable, NullableContext.Disabled | NullableContext.AnnotationsContextInherited)] + [InlineData("#nullable disable warnings", NullableContextOptions.Warnings, NullableContext.Disabled | NullableContext.AnnotationsContextInherited)] + [InlineData("#nullable disable warnings", NullableContextOptions.Annotations, NullableContext.AnnotationsEnabled | NullableContext.AnnotationsContextInherited)] + [InlineData("#nullable disable warnings", NullableContextOptions.Enable, NullableContext.AnnotationsEnabled | NullableContext.AnnotationsContextInherited)] + + [InlineData("#nullable disable annotations", NullableContextOptions.Disable, NullableContext.Disabled | NullableContext.WarningsContextInherited)] + [InlineData("#nullable disable annotations", NullableContextOptions.Warnings, NullableContext.WarningsEnabled | NullableContext.WarningsContextInherited)] + [InlineData("#nullable disable annotations", NullableContextOptions.Annotations, NullableContext.Disabled | NullableContext.WarningsContextInherited)] + [InlineData("#nullable disable annotations", NullableContextOptions.Enable, NullableContext.WarningsEnabled | NullableContext.WarningsContextInherited)] + [Theory] + public void NullableContextExplicitlySpecifiedAndRestoredInFile(string pragma, NullableContextOptions globalContext, NullableContext expectedContext) + { + var source = $@" +{pragma} +class C +{{ +#nullable restore + void M() {{}} +}}"; + + var comp = CreateCompilation(source, options: WithNonNullTypes(globalContext)); + var syntaxTree = comp.SyntaxTrees[0]; + var model = comp.GetSemanticModel(syntaxTree); + + var classDeclPosition = syntaxTree.GetRoot().DescendantNodes().OfType().Single().SpanStart; + var methodDeclPosition = syntaxTree.GetRoot().DescendantNodes().OfType().Single().SpanStart; + + Assert.Equal(expectedContext, model.GetNullableContext(classDeclPosition)); + + // The context at the start of the file should always be inherited and match the global context + var restoredContext = ((NullableContext)globalContext) | NullableContext.ContextInherited; + Assert.Equal(restoredContext, model.GetNullableContext(0)); + Assert.Equal(restoredContext, model.GetNullableContext(methodDeclPosition)); + } + + [Fact] + public void NullableContextMultipleFiles() + { + var source1 = @" +#nullable enable +partial class C +{ + void M1() {}; +}"; + + var source2 = @" +partial class C +{ +#nullable enable + void M2(); +}"; + + var comp = CreateCompilation(new[] { source1, source2 }, options: WithNonNullTypesTrue()); + + var syntaxTree1 = comp.SyntaxTrees[0]; + var model1 = comp.GetSemanticModel(syntaxTree1); + var syntaxTree2 = comp.SyntaxTrees[1]; + var model2 = comp.GetSemanticModel(syntaxTree2); + + var classDecl1 = syntaxTree1.GetRoot().DescendantNodes().OfType().Single().SpanStart; + var classDecl2 = syntaxTree2.GetRoot().DescendantNodes().OfType().Single().SpanStart; + + Assert.Equal(NullableContext.Enabled, model1.GetNullableContext(classDecl1)); + Assert.Equal(NullableContext.Enabled | NullableContext.ContextInherited, model2.GetNullableContext(classDecl2)); + } + + [Fact] + public void NullableContextOptionsFlags() + { + Assert.True(NullableContextOptions.Enable.AnnotationsEnabled()); + Assert.True(NullableContextOptions.Enable.WarningsEnabled()); + + Assert.True(NullableContextOptions.Annotations.AnnotationsEnabled()); + Assert.False(NullableContextOptions.Annotations.WarningsEnabled()); + + Assert.False(NullableContextOptions.Warnings.AnnotationsEnabled()); + Assert.True(NullableContextOptions.Warnings.WarningsEnabled()); + + Assert.False(NullableContextOptions.Disable.AnnotationsEnabled()); + Assert.False(NullableContextOptions.Disable.WarningsEnabled()); + } + + [Fact] + public void NullableContextFlags() + { + AssertEnabledForInheritence(NullableContext.Disabled, warningsEnabled: false, annotationsEnabled: false); + AssertEnabledForInheritence(NullableContext.WarningsEnabled, warningsEnabled: true, annotationsEnabled: false); + AssertEnabledForInheritence(NullableContext.AnnotationsEnabled, warningsEnabled: false, annotationsEnabled: true); + AssertEnabledForInheritence(NullableContext.Enabled, warningsEnabled: true, annotationsEnabled: true); + + void AssertEnabledForInheritence(NullableContext context, bool warningsEnabled, bool annotationsEnabled) + { + Assert.Equal(warningsEnabled, context.WarningsEnabled()); + Assert.Equal(annotationsEnabled, context.AnnotationsEnabled()); + Assert.False(context.WarningsInherited()); + Assert.False(context.AnnotationsInherited()); + + var warningsInherited = context | NullableContext.WarningsContextInherited; + Assert.Equal(warningsEnabled, warningsInherited.WarningsEnabled()); + Assert.Equal(annotationsEnabled, warningsInherited.AnnotationsEnabled()); + Assert.True(warningsInherited.WarningsInherited()); + Assert.False(warningsInherited.AnnotationsInherited()); + + var annotationsInherited = context | NullableContext.AnnotationsContextInherited; + Assert.Equal(warningsEnabled, annotationsInherited.WarningsEnabled()); + Assert.Equal(annotationsEnabled, annotationsInherited.AnnotationsEnabled()); + Assert.False(annotationsInherited.WarningsInherited()); + Assert.True(annotationsInherited.AnnotationsInherited()); + + var contextInherited = context | NullableContext.ContextInherited; + Assert.Equal(warningsEnabled, contextInherited.WarningsEnabled()); + Assert.Equal(annotationsEnabled, contextInherited.AnnotationsEnabled()); + Assert.True(contextInherited.WarningsInherited()); + Assert.True(contextInherited.AnnotationsInherited()); + } + } + } +} diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs index b86873c87ddee6a2281543e4deed39c50d50f45b..007e5dbfd48f7ecc02b5e6e7e47702e300914888 100644 --- a/src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs @@ -134,6 +134,7 @@ public void Invariants() TestProperty((old, value) => old.WithTopLevelBinderFlags(value), opt => opt.TopLevelBinderFlags, BinderFlags.IgnoreCorLibraryDuplicatedTypes); TestProperty((old, value) => old.WithMetadataImportOptions(value), opt => opt.MetadataImportOptions, MetadataImportOptions.Internal); TestProperty((old, value) => old.WithReferencesSupersedeLowerVersions(value), opt => opt.ReferencesSupersedeLowerVersions, true); + TestProperty((old, value) => old.WithNullableContextOptions(value), opt => opt.NullableContextOptions, NullableContextOptions.Enable); } [Fact] diff --git a/src/Compilers/Core/CodeAnalysisTest/CommonCompilationOptionsTests.cs b/src/Compilers/Core/CodeAnalysisTest/CommonCompilationOptionsTests.cs index c5dbca3f8d12027003b9d961a81417955402dd05..f98a3bf587353620bbb34d9782af3c508bfb7049 100644 --- a/src/Compilers/Core/CodeAnalysisTest/CommonCompilationOptionsTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/CommonCompilationOptionsTests.cs @@ -37,6 +37,7 @@ public void TestFieldsForEqualsAndGetHashCode() "MetadataImportOptions", "MetadataReferenceResolver", "ModuleName", + "NullableContextOptions", "OptimizationLevel", "OutputKind", "Platform", diff --git a/src/Compilers/Core/Portable/Compilation/CompilationOptions.cs b/src/Compilers/Core/Portable/Compilation/CompilationOptions.cs index a7a7d2a9095e7fb5f813975fcb0e6ee8c88bf0fa..93bb28f04517be6b83db6217718ad9fcc60cc81a 100644 --- a/src/Compilers/Core/Portable/Compilation/CompilationOptions.cs +++ b/src/Compilers/Core/Portable/Compilation/CompilationOptions.cs @@ -233,6 +233,15 @@ public abstract class CompilationOptions /// public AssemblyIdentityComparer AssemblyIdentityComparer { get; protected set; } + /// + /// Gets the default nullable context state in this compilation. + /// + /// + /// This context does not apply to files that are marked as generated. Nullable is off + /// by default in those locations. + /// + public abstract NullableContextOptions NullableContextOptions { get; protected set; } + /// /// A set of strings designating experimental compiler features that are to be enabled. /// @@ -624,7 +633,8 @@ protected bool EqualsHelper(CompilationOptions other) object.Equals(this.SourceReferenceResolver, other.SourceReferenceResolver) && object.Equals(this.StrongNameProvider, other.StrongNameProvider) && object.Equals(this.AssemblyIdentityComparer, other.AssemblyIdentityComparer) && - this.PublicSign == other.PublicSign; + this.PublicSign == other.PublicSign && + this.NullableContextOptions == other.NullableContextOptions; return equal; } @@ -658,7 +668,8 @@ protected int GetHashCodeHelper() Hash.Combine(this.SourceReferenceResolver, Hash.Combine(this.StrongNameProvider, Hash.Combine(this.AssemblyIdentityComparer, - Hash.Combine(this.PublicSign, 0)))))))))))))))))))))))))); + Hash.Combine(this.PublicSign, + Hash.Combine((int)this.NullableContextOptions, 0))))))))))))))))))))))))))); } public static bool operator ==(CompilationOptions left, CompilationOptions right) diff --git a/src/Compilers/Core/Portable/Compilation/NullableContext.cs b/src/Compilers/Core/Portable/Compilation/NullableContext.cs new file mode 100644 index 0000000000000000000000000000000000000000..b16254220bcaca69d603b0eabf9b53fa015852ac --- /dev/null +++ b/src/Compilers/Core/Portable/Compilation/NullableContext.cs @@ -0,0 +1,100 @@ +// 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; + +namespace Microsoft.CodeAnalysis +{ + /// + /// Represents the state of the nullable analysis at a specific point in a file. Bits one and + /// two correspond to whether the nullable feature is enabled. Bits three and four correspond + /// to whether the context was inherited from the global context. + /// + [Flags] + public enum NullableContext + { + /// + /// Nullable warnings and annotations are explicitly turned off at this location. + /// + Disabled = 0, + + /// + /// Nullable warnings are enabled and will be reported at this file location. + /// + WarningsEnabled = 1, + + /// + /// Nullable annotations are enabled and will be shown when APIs defined at + /// this location are used in other contexts. + /// + AnnotationsEnabled = 1 << 1, + + /// + /// The nullable feature is fully enabled. + /// + Enabled = WarningsEnabled | AnnotationsEnabled, + + /// + /// The nullable warning state is inherited from the project default. + /// + /// + /// The project default can change depending on the file type. Generated + /// files have nullable off by default, regardless of of the project-level + /// default setting. + /// + WarningsContextInherited = 1 << 2, + + /// + /// The nullable annotation state is inherited from the project default. + /// + /// + /// The project default can change depending on the file type. Generated + /// files have nullable off by default, regardless of of the project-level + /// default setting. + /// + AnnotationsContextInherited = 1 << 3, + + /// + /// The current state of both warnings and annotations are inherited from + /// the project default. + /// + /// + /// This flag is set by default at the start of all files. + /// + /// The project default can change depending on the file type. Generated + /// files have nullable off by default, regardless of of the project-level + /// default setting. + /// + ContextInherited = WarningsContextInherited | AnnotationsContextInherited + } + + public static class NullableContextExtensions + { + private static bool IsFlagSet(NullableContext context, NullableContext flag) => + (context & flag) == flag; + + /// + /// Returns whether nullable warnings are enabled for this context. + /// + public static bool WarningsEnabled(this NullableContext context) => + IsFlagSet(context, NullableContext.WarningsEnabled); + + /// + /// Returns whether nullable annotations are enabled for this context. + /// + public static bool AnnotationsEnabled(this NullableContext context) => + IsFlagSet(context, NullableContext.AnnotationsEnabled); + + /// + /// Returns whether the nullable warning state was inherited from the project default for this file type. + /// + public static bool WarningsInherited(this NullableContext context) => + IsFlagSet(context, NullableContext.WarningsContextInherited); + + /// + /// Returns whether the nullable annotation state was inherited from the project default for this file type. + /// + public static bool AnnotationsInherited(this NullableContext context) => + IsFlagSet(context, NullableContext.AnnotationsContextInherited); + } +} diff --git a/src/Compilers/Core/Portable/Compilation/NullableContextOptions.cs b/src/Compilers/Core/Portable/Compilation/NullableContextOptions.cs new file mode 100644 index 0000000000000000000000000000000000000000..2f42550e4268296d567598981d108fe3317e30c5 --- /dev/null +++ b/src/Compilers/Core/Portable/Compilation/NullableContextOptions.cs @@ -0,0 +1,53 @@ +// 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; + +namespace Microsoft.CodeAnalysis +{ + /// + /// Represents the default state of nullable analysis in this compilation. + /// + [Flags] + public enum NullableContextOptions + { + /// + /// The nullable analysis feature is disabled. + /// + Disable = 0, + + /// + /// Nullable warnings are enabled and will be reported by default. + /// + Warnings = 1, + + /// + /// Nullable annotations are enabled and will be shown when APIs + /// defined in this project are used in other contexts. + /// + Annotations = 1 << 1, + + /// + /// The nullable analysis feature is fully enabled. + /// + Enable = Warnings | Annotations, + } + + public static class NullableContextOptionsExtensions + { + private static bool IsFlagSet(NullableContextOptions context, NullableContextOptions flag) => + (context & flag) == flag; + + /// + /// Returns whether nullable warnings are enabled. + /// + public static bool WarningsEnabled(this NullableContextOptions context) => + IsFlagSet(context, NullableContextOptions.Warnings); + + /// + /// Returns whether nullable annotations are enabled. + /// + public static bool AnnotationsEnabled(this NullableContextOptions context) => + IsFlagSet(context, NullableContextOptions.Annotations); + } +} diff --git a/src/Compilers/Core/Portable/Compilation/SemanticModel.cs b/src/Compilers/Core/Portable/Compilation/SemanticModel.cs index 05f84af26cd57365630ac6555813dd633c9c55f1..ff92649ff02dead400bd69893dd50c272e947a54 100644 --- a/src/Compilers/Core/Portable/Compilation/SemanticModel.cs +++ b/src/Compilers/Core/Portable/Compilation/SemanticModel.cs @@ -888,5 +888,11 @@ protected internal virtual SyntaxNode GetTopmostNodeForDiagnosticAnalysis(ISymbo /// Root of this semantic model /// protected abstract SyntaxNode RootCore { get; } + + /// + /// Gets the at a position in the file. + /// + /// The position to get the context for. + public abstract NullableContext GetNullableContext(int position); } } diff --git a/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt b/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt index 54156229d86e8fa99dc953615dc26c22ed886935..815532cb1ec682ff09d569ea6b70d003dfd8b519 100644 --- a/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt +++ b/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt @@ -15,6 +15,8 @@ Microsoft.CodeAnalysis.Compilation.CreateTupleTypeSymbol(Microsoft.CodeAnalysis. *REMOVED*Microsoft.CodeAnalysis.Compilation.CreateTupleTypeSymbol(System.Collections.Immutable.ImmutableArray elementTypes, System.Collections.Immutable.ImmutableArray elementNames = default(System.Collections.Immutable.ImmutableArray), System.Collections.Immutable.ImmutableArray elementLocations = default(System.Collections.Immutable.ImmutableArray)) -> Microsoft.CodeAnalysis.INamedTypeSymbol Microsoft.CodeAnalysis.Compilation.CreateTupleTypeSymbol(System.Collections.Immutable.ImmutableArray elementTypes, System.Collections.Immutable.ImmutableArray elementNames = default(System.Collections.Immutable.ImmutableArray), System.Collections.Immutable.ImmutableArray elementLocations = default(System.Collections.Immutable.ImmutableArray), System.Collections.Immutable.ImmutableArray elementNullableAnnotations = default(System.Collections.Immutable.ImmutableArray)) -> Microsoft.CodeAnalysis.INamedTypeSymbol Microsoft.CodeAnalysis.Compilation.CreateTupleTypeSymbol(System.Collections.Immutable.ImmutableArray elementTypes, System.Collections.Immutable.ImmutableArray elementNames, System.Collections.Immutable.ImmutableArray elementLocations) -> Microsoft.CodeAnalysis.INamedTypeSymbol +Microsoft.CodeAnalysis.Compilation.HasImplicitConversion(Microsoft.CodeAnalysis.ITypeSymbol fromType, Microsoft.CodeAnalysis.ITypeSymbol toType) -> bool +Microsoft.CodeAnalysis.Compilation.IsSymbolAccessibleWithin(Microsoft.CodeAnalysis.ISymbol symbol, Microsoft.CodeAnalysis.ISymbol within, Microsoft.CodeAnalysis.ITypeSymbol throughType = null) -> bool Microsoft.CodeAnalysis.Diagnostics.DiagnosticSuppressor Microsoft.CodeAnalysis.Diagnostics.DiagnosticSuppressor.DiagnosticSuppressor() -> void Microsoft.CodeAnalysis.Diagnostics.Suppression @@ -56,6 +58,21 @@ Microsoft.CodeAnalysis.NullableAnnotation.Annotated = 3 -> Microsoft.CodeAnalysi Microsoft.CodeAnalysis.NullableAnnotation.Disabled = 1 -> Microsoft.CodeAnalysis.NullableAnnotation Microsoft.CodeAnalysis.NullableAnnotation.NotAnnotated = 2 -> Microsoft.CodeAnalysis.NullableAnnotation Microsoft.CodeAnalysis.NullableAnnotation.NotApplicable = 0 -> Microsoft.CodeAnalysis.NullableAnnotation +Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContext.AnnotationsContextInherited = 8 -> Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContext.AnnotationsEnabled = 2 -> Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContext.ContextInherited = Microsoft.CodeAnalysis.NullableContext.WarningsContextInherited | Microsoft.CodeAnalysis.NullableContext.AnnotationsContextInherited -> Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContext.Disabled = 0 -> Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContext.Enabled = Microsoft.CodeAnalysis.NullableContext.WarningsEnabled | Microsoft.CodeAnalysis.NullableContext.AnnotationsEnabled -> Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContext.WarningsContextInherited = 4 -> Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContext.WarningsEnabled = 1 -> Microsoft.CodeAnalysis.NullableContext +Microsoft.CodeAnalysis.NullableContextExtensions +Microsoft.CodeAnalysis.NullableContextOptions +Microsoft.CodeAnalysis.NullableContextOptions.Annotations = 2 -> Microsoft.CodeAnalysis.NullableContextOptions +Microsoft.CodeAnalysis.NullableContextOptions.Disable = 0 -> Microsoft.CodeAnalysis.NullableContextOptions +Microsoft.CodeAnalysis.NullableContextOptions.Enable = Microsoft.CodeAnalysis.NullableContextOptions.Warnings | Microsoft.CodeAnalysis.NullableContextOptions.Annotations -> Microsoft.CodeAnalysis.NullableContextOptions +Microsoft.CodeAnalysis.NullableContextOptions.Warnings = 1 -> Microsoft.CodeAnalysis.NullableContextOptions +Microsoft.CodeAnalysis.NullableContextOptionsExtensions Microsoft.CodeAnalysis.NullableFlowState Microsoft.CodeAnalysis.NullableFlowState.MaybeNull = 2 -> Microsoft.CodeAnalysis.NullableFlowState Microsoft.CodeAnalysis.NullableFlowState.NotApplicable = 0 -> Microsoft.CodeAnalysis.NullableFlowState @@ -82,6 +99,7 @@ Microsoft.CodeAnalysis.TypeInfo.Nullability.get -> Microsoft.CodeAnalysis.Nullab abstract Microsoft.CodeAnalysis.Compilation.ClassifyCommonConversion(Microsoft.CodeAnalysis.ITypeSymbol source, Microsoft.CodeAnalysis.ITypeSymbol destination) -> Microsoft.CodeAnalysis.Operations.CommonConversion abstract Microsoft.CodeAnalysis.Compilation.ContainsSymbolsWithName(string name, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> bool abstract Microsoft.CodeAnalysis.Compilation.GetSymbolsWithName(string name, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Collections.Generic.IEnumerable +abstract Microsoft.CodeAnalysis.CompilationOptions.NullableContextOptions.get -> Microsoft.CodeAnalysis.NullableContextOptions abstract Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions.TryGetValue(string key, out string value) -> bool abstract Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptionsProvider.GetOptions(Microsoft.CodeAnalysis.AdditionalText textFile) -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions abstract Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptionsProvider.GetOptions(Microsoft.CodeAnalysis.SyntaxTree tree) -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions @@ -94,6 +112,7 @@ abstract Microsoft.CodeAnalysis.Diagnostics.SymbolStartAnalysisContext.RegisterO abstract Microsoft.CodeAnalysis.Diagnostics.SymbolStartAnalysisContext.RegisterOperationBlockStartAction(System.Action action) -> void abstract Microsoft.CodeAnalysis.Diagnostics.SymbolStartAnalysisContext.RegisterSymbolEndAction(System.Action action) -> void abstract Microsoft.CodeAnalysis.Diagnostics.SymbolStartAnalysisContext.RegisterSyntaxNodeAction(System.Action action, System.Collections.Immutable.ImmutableArray syntaxKinds) -> void +abstract Microsoft.CodeAnalysis.SemanticModel.GetNullableContext(int position) -> Microsoft.CodeAnalysis.NullableContext const Microsoft.CodeAnalysis.WellKnownMemberNames.CountPropertyName = "Count" -> string const Microsoft.CodeAnalysis.WellKnownMemberNames.DisposeAsyncMethodName = "DisposeAsync" -> string const Microsoft.CodeAnalysis.WellKnownMemberNames.DisposeMethodName = "Dispose" -> string @@ -119,8 +138,6 @@ Microsoft.CodeAnalysis.AnalyzerConfigOptionsResult.TreeOptions.get -> System.Col Microsoft.CodeAnalysis.AnalyzerConfigSet Microsoft.CodeAnalysis.AnalyzerConfigSet.GetOptionsForSourcePath(string sourcePath) -> Microsoft.CodeAnalysis.AnalyzerConfigOptionsResult Microsoft.CodeAnalysis.CommandLineArguments.AnalyzerConfigPaths.get -> System.Collections.Immutable.ImmutableArray -Microsoft.CodeAnalysis.Compilation.HasImplicitConversion(Microsoft.CodeAnalysis.ITypeSymbol fromType, Microsoft.CodeAnalysis.ITypeSymbol toType) -> bool -Microsoft.CodeAnalysis.Compilation.IsSymbolAccessibleWithin(Microsoft.CodeAnalysis.ISymbol symbol, Microsoft.CodeAnalysis.ISymbol within, Microsoft.CodeAnalysis.ITypeSymbol throughType = null) -> bool Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions.AnalyzerConfigOptions() -> void Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptionsProvider @@ -330,6 +347,12 @@ static Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraph.Create(Microsoft.Cod static Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraph.Create(Microsoft.CodeAnalysis.SyntaxNode node, Microsoft.CodeAnalysis.SemanticModel semanticModel, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraph static Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraphExtensions.GetAnonymousFunctionControlFlowGraphInScope(this Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraph controlFlowGraph, Microsoft.CodeAnalysis.FlowAnalysis.IFlowAnonymousFunctionOperation anonymousFunction, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraph static Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraphExtensions.GetLocalFunctionControlFlowGraphInScope(this Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraph controlFlowGraph, Microsoft.CodeAnalysis.IMethodSymbol localFunction, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowGraph +static Microsoft.CodeAnalysis.NullableContextExtensions.AnnotationsEnabled(this Microsoft.CodeAnalysis.NullableContext context) -> bool +static Microsoft.CodeAnalysis.NullableContextExtensions.AnnotationsInherited(this Microsoft.CodeAnalysis.NullableContext context) -> bool +static Microsoft.CodeAnalysis.NullableContextExtensions.WarningsEnabled(this Microsoft.CodeAnalysis.NullableContext context) -> bool +static Microsoft.CodeAnalysis.NullableContextExtensions.WarningsInherited(this Microsoft.CodeAnalysis.NullableContext context) -> bool +static Microsoft.CodeAnalysis.NullableContextOptionsExtensions.AnnotationsEnabled(this Microsoft.CodeAnalysis.NullableContextOptions context) -> bool +static Microsoft.CodeAnalysis.NullableContextOptionsExtensions.WarningsEnabled(this Microsoft.CodeAnalysis.NullableContextOptions context) -> bool static Microsoft.CodeAnalysis.Operations.OperationExtensions.GetCorrespondingOperation(this Microsoft.CodeAnalysis.Operations.IBranchOperation operation) -> Microsoft.CodeAnalysis.IOperation static readonly Microsoft.CodeAnalysis.SyntaxTree.EmptyDiagnosticOptions -> System.Collections.Immutable.ImmutableDictionary virtual Microsoft.CodeAnalysis.Diagnostics.AnalysisContext.RegisterSymbolStartAction(System.Action action, Microsoft.CodeAnalysis.SymbolKind symbolKind) -> void diff --git a/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb b/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb index 84aa4693a5f4a2433e4695d36d1b3cd00a0aedb0..d6284168f8fc298712d413473bb245373b5406ea 100644 --- a/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb +++ b/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb @@ -3475,6 +3475,10 @@ _Default: Return declaringSyntax End Function + + Public NotOverridable Overrides Function GetNullableContext(position As Integer) As NullableContext + Return NullableContext.Disabled Or NullableContext.ContextInherited + End Function #End Region #Region "Logging Helpers" diff --git a/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt b/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt index 2bc9a1b1f035e1791a39b806446b1998455104a4..ea7dd78cb88812d9ed38789dfa64ab63ddb2ae4b 100644 --- a/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt +++ b/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt @@ -85,6 +85,7 @@ Overloads Microsoft.CodeAnalysis.VisualBasic.VisualBasicCommandLineArguments.Par Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.ClassifyCommonConversion(source As Microsoft.CodeAnalysis.ITypeSymbol, destination As Microsoft.CodeAnalysis.ITypeSymbol) -> Microsoft.CodeAnalysis.Operations.CommonConversion Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.ContainsSymbolsWithName(name As String, filter As Microsoft.CodeAnalysis.SymbolFilter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, cancellationToken As System.Threading.CancellationToken = Nothing) -> Boolean Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.GetSymbolsWithName(name As String, filter As Microsoft.CodeAnalysis.SymbolFilter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, cancellationToken As System.Threading.CancellationToken = Nothing) -> System.Collections.Generic.IEnumerable(Of Microsoft.CodeAnalysis.ISymbol) +Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilationOptions.NullableContextOptions() -> Microsoft.CodeAnalysis.NullableContextOptions Shared Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.CarriageReturn() -> Microsoft.CodeAnalysis.SyntaxTrivia Shared Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.CarriageReturnLineFeed() -> Microsoft.CodeAnalysis.SyntaxTrivia Shared Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.ElasticCarriageReturn() -> Microsoft.CodeAnalysis.SyntaxTrivia diff --git a/src/Compilers/VisualBasic/Portable/VisualBasicCompilationOptions.vb b/src/Compilers/VisualBasic/Portable/VisualBasicCompilationOptions.vb index 90083faa3a9fbe68f58c6608414e87b3a3b97952..4ff34d84bfe66f3052416bc5598eb2a150b74320 100644 --- a/src/Compilers/VisualBasic/Portable/VisualBasicCompilationOptions.vb +++ b/src/Compilers/VisualBasic/Portable/VisualBasicCompilationOptions.vb @@ -4,7 +4,6 @@ Imports System.Collections.Immutable Imports System.ComponentModel Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.PooledObjects -Imports Roslyn.Utilities Namespace Microsoft.CodeAnalysis.VisualBasic ''' @@ -1348,5 +1347,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Protected Overrides Function CommonWithCheckOverflow(checkOverflow As Boolean) As CompilationOptions Return WithOverflowChecks(checkOverflow) End Function + + Public Overrides Property NullableContextOptions As NullableContextOptions + Get + Return NullableContextOptions.Disable + End Get + Protected Set(value As NullableContextOptions) + Throw New NotImplementedException() + End Set + End Property End Class End Namespace diff --git a/src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb b/src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb index 0aacf14d4d8f1486912f534ca641ff2900b6c446..2ca14742a56368e569df0c7492f29dccb912af37 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb @@ -536,6 +536,7 @@ BC2042: The options /vbruntime* and /target:module cannot be combined. "OptionCompareText", "EmbedVbCoreRuntime", "SuppressEmbeddedDeclarations", + "NullableContextOptions", "ParseOptions", "IgnoreCorLibraryDuplicatedTypes") End Sub diff --git a/src/Workspaces/CoreTest/Execution/SnapshotSerializationTests.cs b/src/Workspaces/CoreTest/Execution/SnapshotSerializationTests.cs index 3288ba7cb38bb2cf7f28e5427156608a13e36bf9..7b9331a076ac9e65552781f5445e030d1d325a94 100644 --- a/src/Workspaces/CoreTest/Execution/SnapshotSerializationTests.cs +++ b/src/Workspaces/CoreTest/Execution/SnapshotSerializationTests.cs @@ -573,7 +573,7 @@ public void TestEncodingSerialization() [Fact] public void TestCompilationOptions_NullableAndImport() { - var csharpOptions = CSharp.CSharpCompilation.Create("dummy").Options.WithNullableContextOptions(CSharp.NullableContextOptions.Warnings).WithMetadataImportOptions(MetadataImportOptions.All); + var csharpOptions = CSharp.CSharpCompilation.Create("dummy").Options.WithNullableContextOptions(NullableContextOptions.Warnings).WithMetadataImportOptions(MetadataImportOptions.All); var vbOptions = VisualBasic.VisualBasicCompilation.Create("dummy").Options.WithMetadataImportOptions(MetadataImportOptions.Internal); var hostServices = MefHostServices.Create(MefHostServices.DefaultAssemblies);