diff --git a/src/Compilers/CSharp/Portable/Binder/Binder.cs b/src/Compilers/CSharp/Portable/Binder/Binder.cs
index db19b1ad557e2..73d8f7ae02a5e 100644
--- a/src/Compilers/CSharp/Portable/Binder/Binder.cs
+++ b/src/Compilers/CSharp/Portable/Binder/Binder.cs
@@ -216,7 +216,7 @@ internal virtual Symbol ContainingMemberOrLambda
///
/// Are we in a context where un-annotated types should be interpreted as non-null?
///
- internal Symbol NonNullTypesContext => ContainingMember().OriginalDefinition;
+ internal INonNullTypesContext NonNullTypesContext => (Flags & BinderFlags.InEEMethodBinder) == 0 ? ContainingMember().OriginalDefinition : NonNullTypesTrueContext.Instance;
///
/// Is the contained code within a member method body?
diff --git a/src/Compilers/CSharp/Portable/Binder/BinderFlags.cs b/src/Compilers/CSharp/Portable/Binder/BinderFlags.cs
index c031a20bca13f..83fbd5752a6e5 100644
--- a/src/Compilers/CSharp/Portable/Binder/BinderFlags.cs
+++ b/src/Compilers/CSharp/Portable/Binder/BinderFlags.cs
@@ -103,6 +103,11 @@ internal enum BinderFlags : uint
///
InContextualAttributeBinder = 1 << 29,
+ ///
+ /// Are we binding for the purpose of an Expression Evaluator
+ ///
+ InEEMethodBinder = 1 << 30,
+
// Groups
AllClearedAtExecutableCodeBoundary = InLockBody | InCatchBlock | InCatchFilter | InFinallyBlock | InTryBlockOfTryCatch | InNestedFinallyBlock,
diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
index e804c3918a752..43d4bb7d5b0ab 100644
--- a/src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
+++ b/src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
@@ -1494,28 +1494,22 @@ internal Symbol ResultSymbol(
var srcSymbol = symbols[best.Index];
var mdSymbol = symbols[secondBest.Index];
+ object arg0;
+
+ if (best.IsFromSourceModule)
+ {
+ arg0 = srcSymbol.Locations.First().SourceTree.FilePath;
+ }
+ else
+ {
+ Debug.Assert(best.IsFromAddedModule);
+ arg0 = srcSymbol.ContainingModule;
+ }
+
//if names match, arities match, and containing symbols match (recursively), ...
if (srcSymbol.ToDisplayString(SymbolDisplayFormat.QualifiedNameArityFormat) ==
mdSymbol.ToDisplayString(SymbolDisplayFormat.QualifiedNameArityFormat))
{
- if (srcSymbol.Equals(Compilation.GetWellKnownType(WellKnownType.System_Runtime_CompilerServices_NonNullTypesAttribute)))
- {
- // Silently prefer the injected symbol
- return originalSymbols[best.Index];
- }
-
- object arg0;
- if (best.IsFromSourceModule)
- {
- SyntaxTree tree = srcSymbol.Locations.FirstOrNone().SourceTree;
- arg0 = tree != null ? (object)tree.FilePath : MessageID.IDS_InjectedDeclaration.Localize();
- }
- else
- {
- Debug.Assert(best.IsFromAddedModule);
- arg0 = srcSymbol.ContainingModule;
- }
-
if (srcSymbol.Kind == SymbolKind.Namespace && mdSymbol.Kind == SymbolKind.NamedType)
{
// ErrorCode.WRN_SameFullNameThisNsAgg: The namespace '{1}' in '{0}' conflicts with the imported type '{3}' in '{2}'. Using the namespace defined in '{0}'.
diff --git a/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs b/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs
index 2401b8ac30c13..075c785850e69 100644
--- a/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs
+++ b/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs
@@ -499,7 +499,7 @@ private BoundLambda ReallyBind(NamedTypeSymbol delegateType)
if (!returnType.IsNull)
{
- if (returnType.ContainsNullableReferenceTypes())
+ if (returnType.NeedsNullableAttribute())
{
binder.Compilation.EnsureNullableAttributeExists(diagnostics, lambdaSymbol.DiagnosticLocation, modifyCompilation: false);
// Note: we don't need to warn on annotations used without NonNullTypes context for lambdas, as this is handled in binding already
diff --git a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs
index 5f4563f3e6979..5df81a6d77ded 100644
--- a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs
+++ b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs
@@ -19,7 +19,7 @@ namespace Microsoft.CodeAnalysis.CSharp {
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
- [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class CSharpResources {
@@ -4308,15 +4308,6 @@ internal static string ERR_ExplicitMethodImplAccessor {
}
}
- ///
- /// Looks up a localized string similar to Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed..
- ///
- internal static string ERR_ExplicitNonNullTypesAttribute {
- get {
- return ResourceManager.GetString("ERR_ExplicitNonNullTypesAttribute", resourceCulture);
- }
- }
-
///
/// Looks up a localized string similar to Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed..
///
@@ -11365,15 +11356,6 @@ internal static string IDS_GlobalNamespace {
}
}
- ///
- /// Looks up a localized string similar to injected declaration.
- ///
- internal static string IDS_InjectedDeclaration {
- get {
- return ResourceManager.GetString("IDS_InjectedDeclaration", resourceCulture);
- }
- }
-
///
/// Looks up a localized string similar to invariantly.
///
diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx
index 339320c39390f..f7fd13a0408a5 100644
--- a/src/Compilers/CSharp/Portable/CSharpResources.resx
+++ b/src/Compilers/CSharp/Portable/CSharpResources.resx
@@ -4284,9 +4284,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<text>
-
- injected declaration
-
null propagating operator
@@ -5628,9 +5625,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
Cannot use a collection of dynamic type in an asynchronous foreach
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
Expected enable or disable
diff --git a/src/Compilers/CSharp/Portable/Emitter/Model/PEAssemblyBuilder.cs b/src/Compilers/CSharp/Portable/Emitter/Model/PEAssemblyBuilder.cs
index c633e0f83a93e..7b8299f4b5b98 100644
--- a/src/Compilers/CSharp/Portable/Emitter/Model/PEAssemblyBuilder.cs
+++ b/src/Compilers/CSharp/Portable/Emitter/Model/PEAssemblyBuilder.cs
@@ -18,13 +18,11 @@ internal abstract class PEAssemblyBuilderBase : PEModuleBuilder, Cci.IAssemblyRe
private readonly ImmutableArray _additionalTypes;
private ImmutableArray _lazyFiles;
+ private SynthesizedEmbeddedAttributeSymbol _lazyEmbeddedAttribute;
private SynthesizedEmbeddedAttributeSymbol _lazyIsReadOnlyAttribute;
private SynthesizedEmbeddedAttributeSymbol _lazyIsByRefLikeAttribute;
private SynthesizedEmbeddedAttributeSymbol _lazyIsUnmanagedAttribute;
private SynthesizedEmbeddedAttributeSymbol _lazyNullableAttribute;
- private ImmutableArray _lazyInjectedTypes;
- private bool _needsNonNullTypesAttribute;
- private bool _needsEmbeddedAttribute;
///
/// The behavior of the C# command-line compiler is as follows:
@@ -63,18 +61,6 @@ public PEAssemblyBuilderBase(
AssemblyOrModuleSymbolToModuleRefMap.Add(sourceAssembly, this);
}
- protected override void EnsureNonNullTypesAttributeExists()
- {
- Debug.Assert(!InjectedSymbolsAreFrozen);
- _needsNonNullTypesAttribute = true;
- }
-
- protected override void EnsureEmbeddedAttributeExists()
- {
- Debug.Assert(!InjectedSymbolsAreFrozen);
- _needsEmbeddedAttribute = true;
- }
-
public override ISourceAssemblySymbolInternal SourceAssemblyOpt => _sourceAssembly;
internal override ImmutableArray GetAdditionalTopLevelTypes(DiagnosticBag diagnostics)
@@ -87,6 +73,10 @@ internal override ImmutableArray GetEmbeddedTypes(DiagnosticBag
var builder = ArrayBuilder.GetInstance();
CreateEmbeddedAttributesIfNeeded(diagnostics);
+ if ((object)_lazyEmbeddedAttribute != null)
+ {
+ builder.Add(_lazyEmbeddedAttribute);
+ }
if ((object)_lazyIsReadOnlyAttribute != null)
{
@@ -182,6 +172,15 @@ protected override void AddEmbeddedResourcesFromAddedModules(ArrayBuilder _sourceAssembly.Identity;
public Version AssemblyVersionPattern => _sourceAssembly.AssemblyVersionPattern;
+ internal override SynthesizedAttributeData SynthesizeEmbeddedAttribute()
+ {
+ // _lazyEmbeddedAttribute should have been created before calling this method.
+ return new SynthesizedAttributeData(
+ _lazyEmbeddedAttribute.Constructors[0],
+ ImmutableArray.Empty,
+ ImmutableArray>.Empty);
+ }
+
internal override SynthesizedAttributeData SynthesizeNullableAttribute(WellKnownMember member, ImmutableArray arguments)
{
if ((object)_lazyNullableAttribute != null)
@@ -239,6 +238,8 @@ private void CreateEmbeddedAttributesIfNeeded(DiagnosticBag diagnostics)
{
if (this.NeedsGeneratedIsReadOnlyAttribute)
{
+ CreateEmbeddedAttributeItselfIfNeeded(diagnostics);
+
CreateEmbeddedAttributeIfNeeded(
ref _lazyIsReadOnlyAttribute,
diagnostics,
@@ -247,6 +248,8 @@ private void CreateEmbeddedAttributesIfNeeded(DiagnosticBag diagnostics)
if (this.NeedsGeneratedIsByRefLikeAttribute)
{
+ CreateEmbeddedAttributeItselfIfNeeded(diagnostics);
+
CreateEmbeddedAttributeIfNeeded(
ref _lazyIsByRefLikeAttribute,
diagnostics,
@@ -255,6 +258,8 @@ private void CreateEmbeddedAttributesIfNeeded(DiagnosticBag diagnostics)
if (this.NeedsGeneratedIsUnmanagedAttribute)
{
+ CreateEmbeddedAttributeItselfIfNeeded(diagnostics);
+
CreateEmbeddedAttributeIfNeeded(
ref _lazyIsUnmanagedAttribute,
diagnostics,
@@ -263,78 +268,32 @@ private void CreateEmbeddedAttributesIfNeeded(DiagnosticBag diagnostics)
if (this.NeedsGeneratedNullableAttribute)
{
+ CreateEmbeddedAttributeItselfIfNeeded(diagnostics);
+
CreateEmbeddedAttributeIfNeeded(
ref _lazyNullableAttribute,
diagnostics,
AttributeDescription.NullableAttribute,
- GetAdditionalNullableAttributeConstructors);
+ GetNullableAttributeConstructors);
}
}
- protected override bool InjectedSymbolsAreFrozen => !_lazyInjectedTypes.IsDefault;
-
- ///
- /// Get injected types that are needed (ie. were used) and report the diagnostics they held onto (only once).
- ///
- protected sealed override ImmutableArray GetInjectedTypes(DiagnosticBag diagnostics)
+ private void CreateEmbeddedAttributeItselfIfNeeded(DiagnosticBag diagnostics)
{
- if (!_lazyInjectedTypes.IsDefault)
- {
- return _lazyInjectedTypes;
- }
-
- if (_needsNonNullTypesAttribute)
- {
- EnsureEmbeddedAttributeExists();
- }
-
- ArrayBuilder builder = null;
- if (_needsEmbeddedAttribute)
- {
- addInjectedAttribute(WellKnownType.Microsoft_CodeAnalysis_EmbeddedAttribute, canUseFromSource: false);
- }
-
- if (_needsNonNullTypesAttribute)
- {
- addInjectedAttribute(WellKnownType.System_Runtime_CompilerServices_NonNullTypesAttribute, canUseFromSource: true);
- }
-
- var result = builder is null ? ImmutableArray.Empty : builder.ToImmutableAndFree();
- ImmutableInterlocked.InterlockedInitialize(ref _lazyInjectedTypes, result);
- return _lazyInjectedTypes;
-
- void addInjectedAttribute(WellKnownType wellKnownType, bool canUseFromSource)
- {
- NamedTypeSymbol attribute = Compilation.GetWellKnownType(wellKnownType);
-
- if (attribute is InjectedAttributeSymbol injected)
- {
- injected.AddDiagnostics(diagnostics);
- builder = builder ?? ArrayBuilder.GetInstance();
- builder.Add(attribute);
- }
- else if (attribute.IsErrorType())
- {
- diagnostics.Add(attribute.GetUseSiteDiagnostic(), NoLocation.Singleton);
- }
- else if (!canUseFromSource)
- {
- // if the attribute is defined in source, we can't embed it (we'll produce a diagnostic)
- diagnostics.Add(ErrorCode.ERR_TypeReserved, attribute.Locations[0], attribute);
- }
- }
+ CreateEmbeddedAttributeIfNeeded(
+ ref _lazyEmbeddedAttribute,
+ diagnostics,
+ AttributeDescription.CodeAnalysisEmbeddedAttribute);
}
private void CreateEmbeddedAttributeIfNeeded(
ref SynthesizedEmbeddedAttributeSymbol symbol,
DiagnosticBag diagnostics,
AttributeDescription description,
- Func> getAdditionalConstructors = null)
+ Func> getConstructors = null)
{
if ((object)symbol == null)
{
- EnsureEmbeddedAttributeExists();
-
var attributeMetadataName = MetadataTypeName.FromFullName(description.FullName);
var userDefinedAttribute = _sourceAssembly.SourceModule.LookupTopLevelMetadataType(ref attributeMetadataName);
Debug.Assert((object)userDefinedAttribute.ContainingModule == _sourceAssembly.SourceModule);
@@ -344,25 +303,28 @@ private void CreateEmbeddedAttributeIfNeeded(
diagnostics.Add(ErrorCode.ERR_TypeReserved, userDefinedAttribute.Locations[0], description.FullName);
}
- symbol = new SynthesizedEmbeddedAttributeSymbol(description, _sourceAssembly.DeclaringCompilation, getAdditionalConstructors, diagnostics);
+ symbol = new SynthesizedEmbeddedAttributeSymbol(description, _sourceAssembly.DeclaringCompilation, getConstructors, diagnostics);
}
}
- private static ImmutableArray GetAdditionalNullableAttributeConstructors(
+ private static ImmutableArray GetNullableAttributeConstructors(
CSharpCompilation compilation,
NamedTypeSymbol containingType,
DiagnosticBag diagnostics)
{
- var boolType = compilation.GetSpecialType(SpecialType.System_Boolean);
- Binder.ReportUseSiteDiagnostics(boolType, diagnostics, Location.None);
- var boolArray = TypeSymbolWithAnnotations.Create(
+ var byteType = TypeSymbolWithAnnotations.Create(compilation.GetSpecialType(SpecialType.System_Byte));
+ Binder.ReportUseSiteDiagnostics(byteType.TypeSymbol, diagnostics, Location.None);
+ var byteArray = TypeSymbolWithAnnotations.Create(
ArrayTypeSymbol.CreateSZArray(
- boolType.ContainingAssembly,
- TypeSymbolWithAnnotations.Create(boolType)));
+ byteType.TypeSymbol.ContainingAssembly,
+ byteType));
return ImmutableArray.Create(
new SynthesizedEmbeddedAttributeConstructorSymbol(
containingType,
- m => ImmutableArray.Create(SynthesizedParameterSymbol.Create(m, boolArray, 0, RefKind.None))));
+ m => ImmutableArray.Create(SynthesizedParameterSymbol.Create(m, byteType, 0, RefKind.None))),
+ new SynthesizedEmbeddedAttributeConstructorSymbol(
+ containingType,
+ m => ImmutableArray.Create(SynthesizedParameterSymbol.Create(m, byteArray, 0, RefKind.None))));
}
}
diff --git a/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs b/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs
index 7298e0ec9fa22..39b55bc2f07cb 100644
--- a/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs
+++ b/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs
@@ -43,10 +43,6 @@ public override NoPia.EmbeddedTypesManager EmbeddedTypesManagerOpt
private bool _needsGeneratedIsUnmanagedAttribute_Value;
private bool _needsGeneratedAttributes_IsFrozen;
private bool _needsGeneratedNullableAttribute_Value;
- private readonly InjectedNonNullTypesAttributeSymbol _injectedNonNullTypesAttribute;
-
- protected abstract void EnsureNonNullTypesAttributeExists();
- protected abstract void EnsureEmbeddedAttributeExists();
///
/// Returns a value indicating whether this builder has a symbol that needs IsReadOnlyAttribute to be generated during emit phase.
@@ -117,8 +113,6 @@ internal PEModuleBuilder(
{
_embeddedTypesManagerOpt = new NoPia.EmbeddedTypesManager(this);
}
-
- _injectedNonNullTypesAttribute = Compilation.GetWellKnownType(WellKnownType.System_Runtime_CompilerServices_NonNullTypesAttribute) as InjectedNonNullTypesAttributeSymbol;
}
public override string Name
@@ -455,11 +449,6 @@ internal virtual bool IsEncDelta
{
namespacesToProcess.Push(memberNamespace);
}
- else if (member is InjectedAttributeSymbol injectedNonNullTypes)
- {
- // Skip injected attributes. We'll only embed those that are used.
- continue;
- }
else
{
var type = (NamedTypeSymbol)member;
@@ -953,11 +942,6 @@ internal Cci.INamedTypeReference Translate(
}
}
- if (!InjectedSymbolsAreFrozen && namedTypeSymbol.Equals(_injectedNonNullTypesAttribute))
- {
- EnsureNonNullTypesAttributeExists();
- }
-
// NoPia: See if this is a type, which definition we should copy into our assembly.
Debug.Assert(namedTypeSymbol.IsDefinition);
@@ -1461,6 +1445,8 @@ protected override Cci.IMethodDefinition CreatePrivateImplementationDetailsStati
return new SynthesizedPrivateImplementationDetailsStaticConstructor(SourceModule, details, GetUntranslatedSpecialType(SpecialType.System_Void, syntaxOpt, diagnostics));
}
+ internal abstract SynthesizedAttributeData SynthesizeEmbeddedAttribute();
+
internal SynthesizedAttributeData SynthesizeIsReadOnlyAttribute(Symbol symbol)
{
if ((object)Compilation.SourceModule != symbol.ContainingModule)
@@ -1507,34 +1493,34 @@ internal SynthesizedAttributeData SynthesizeNullableAttribute(Symbol symbol, Typ
return null;
}
- var flagsBuilder = ArrayBuilder.GetInstance();
+ var flagsBuilder = ArrayBuilder.GetInstance();
type.AddNullableTransforms(flagsBuilder);
Debug.Assert(flagsBuilder.Any());
- Debug.Assert(flagsBuilder.Contains(true));
+ Debug.Assert(flagsBuilder.Contains((byte)NullableAnnotation.NotNullable) || flagsBuilder.Contains((byte)NullableAnnotation.Nullable));
WellKnownMember constructor;
ImmutableArray arguments;
+ NamedTypeSymbol byteType = Compilation.GetSpecialType(SpecialType.System_Byte);
+ Debug.Assert((object)byteType != null);
- if (flagsBuilder.Count == 1 && flagsBuilder[0])
+ if (flagsBuilder.All(flag => flag == (byte)NullableAnnotation.NotNullable) || flagsBuilder.All(flag => flag == (byte)NullableAnnotation.Nullable))
{
- constructor = WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctor;
- arguments = ImmutableArray.Empty;
+ constructor = WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorByte;
+ arguments = ImmutableArray.Create(new TypedConstant(byteType, TypedConstantKind.Primitive, flagsBuilder[0]));
}
else
{
- NamedTypeSymbol booleanType = Compilation.GetSpecialType(SpecialType.System_Boolean);
- Debug.Assert((object)booleanType != null);
var constantsBuilder = ArrayBuilder.GetInstance(flagsBuilder.Count);
- foreach (bool flag in flagsBuilder)
+ foreach (byte flag in flagsBuilder)
{
- constantsBuilder.Add(new TypedConstant(booleanType, TypedConstantKind.Primitive, flag));
+ constantsBuilder.Add(new TypedConstant(byteType, TypedConstantKind.Primitive, flag));
}
- var boolArray = ArrayTypeSymbol.CreateSZArray(booleanType.ContainingAssembly, TypeSymbolWithAnnotations.Create(booleanType));
+ var byteArray = ArrayTypeSymbol.CreateSZArray(byteType.ContainingAssembly, TypeSymbolWithAnnotations.Create(byteType));
constructor = WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorTransformFlags;
- arguments = ImmutableArray.Create(new TypedConstant(boolArray, constantsBuilder.ToImmutableAndFree()));
+ arguments = ImmutableArray.Create(new TypedConstant(byteArray, constantsBuilder.ToImmutableAndFree()));
}
flagsBuilder.Free();
diff --git a/src/Compilers/CSharp/Portable/Emitter/Model/PENetModuleBuilder.cs b/src/Compilers/CSharp/Portable/Emitter/Model/PENetModuleBuilder.cs
index f7a54b83e75fa..dc29115069cd9 100644
--- a/src/Compilers/CSharp/Portable/Emitter/Model/PENetModuleBuilder.cs
+++ b/src/Compilers/CSharp/Portable/Emitter/Model/PENetModuleBuilder.cs
@@ -21,6 +21,12 @@ internal PENetModuleBuilder(
{
}
+ internal override SynthesizedAttributeData SynthesizeEmbeddedAttribute()
+ {
+ // Embedded attributes should never be synthesized in modules.
+ throw ExceptionUtilities.Unreachable;
+ }
+
protected override void AddEmbeddedResourcesFromAddedModules(ArrayBuilder builder, DiagnosticBag diagnostics)
{
throw ExceptionUtilities.Unreachable;
@@ -29,10 +35,5 @@ protected override void AddEmbeddedResourcesFromAddedModules(ArrayBuilder 0;
public override IEnumerable GetFiles(EmitContext context) => SpecializedCollections.EmptyEnumerable();
public override ISourceAssemblySymbolInternal SourceAssemblyOpt => null;
-
- protected override void EnsureNonNullTypesAttributeExists() => throw ExceptionUtilities.Unreachable;
- protected override void EnsureEmbeddedAttributeExists() => throw ExceptionUtilities.Unreachable;
- protected override ImmutableArray GetInjectedTypes(DiagnosticBag diagnostics) => ImmutableArray.Empty;
- protected override bool InjectedSymbolsAreFrozen => true;
}
}
diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
index 55f1f82c99538..3c74fe1af97c7 100644
--- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
+++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
@@ -1631,7 +1631,7 @@ internal enum ErrorCode
WRN_NullabilityMismatchInConstraintsOnImplicitImplementation = 8633,
WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint = 8634,
ERR_TripleDotNotAllowed = 8635,
- ERR_ExplicitNonNullTypesAttribute = 8636,
+ // Available -> 8636,
ERR_NullableDirectiveQualifierExpected = 8637,
WRN_CantInferNullabilityOfMethodTypeArgs = 8638,
WRN_NoBestNullabilityArrayElements = 8639,
diff --git a/src/Compilers/CSharp/Portable/Errors/MessageID.cs b/src/Compilers/CSharp/Portable/Errors/MessageID.cs
index a28ea45ecbd89..8364932ee5706 100644
--- a/src/Compilers/CSharp/Portable/Errors/MessageID.cs
+++ b/src/Compilers/CSharp/Portable/Errors/MessageID.cs
@@ -165,11 +165,10 @@ internal enum MessageID
IDS_FeatureAltInterpolatedVerbatimStrings = MessageBase + 12745,
IDS_FeatureCoalesceAssignmentExpression = MessageBase + 12746,
IDS_FeatureUnconstrainedTypeParameterInNullCoalescingOperator = MessageBase + 12747,
- IDS_InjectedDeclaration = MessageBase + 12748,
- IDS_FeatureObjectGenericTypeConstraint = MessageBase + 12749,
- IDS_FeatureIndexOperator = MessageBase + 12750,
- IDS_FeatureRangeOperator = MessageBase + 12751,
- IDS_FeatureAsyncStreams = MessageBase + 12752,
+ IDS_FeatureObjectGenericTypeConstraint = MessageBase + 12748,
+ IDS_FeatureIndexOperator = MessageBase + 12749,
+ IDS_FeatureRangeOperator = MessageBase + 12750,
+ IDS_FeatureAsyncStreams = MessageBase + 12751,
}
// Message IDs may refer to strings that need to be localized.
diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
index 59270a46b3e20..16ffee9aec2a0 100644
--- a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
+++ b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
@@ -269,16 +269,17 @@ private void Populate(ref LocalState state, int start)
int capacity = state.Capacity;
for (int slot = start; slot < capacity; slot++)
{
- var value = GetDefaultState(ref state, slot);
+ (NullableAnnotation value, bool assigned) = GetDefaultState(ref state, slot);
state[slot] = value;
+ state.SetAssigned(slot, assigned);
}
}
- private NullableAnnotation GetDefaultState(ref LocalState state, int slot)
+ private (NullableAnnotation annotation, bool assigned) GetDefaultState(ref LocalState state, int slot)
{
if (slot == 0)
{
- return NullableAnnotation.Unknown;
+ return (NullableAnnotation.Unknown, false);
}
var variable = variableBySlot[slot];
@@ -287,35 +288,35 @@ private NullableAnnotation GetDefaultState(ref LocalState state, int slot)
switch (symbol.Kind)
{
case SymbolKind.Local:
- return NullableAnnotation.Unknown;
+ return (NullableAnnotation.Unknown, false);
case SymbolKind.Parameter:
{
var parameter = (ParameterSymbol)symbol;
if (parameter.RefKind == RefKind.Out)
{
- return NullableAnnotation.Unknown;
+ return (NullableAnnotation.Unknown, false);
}
TypeSymbolWithAnnotations parameterType;
if (!_variableTypes.TryGetValue(parameter, out parameterType))
{
parameterType = parameter.Type;
}
- return parameterType.NullableAnnotation;
+
+ return (parameterType.NullableAnnotation, true);
}
case SymbolKind.Field:
case SymbolKind.Property:
case SymbolKind.Event:
{
- // https://github.com/dotnet/roslyn/issues/30065 State of containing struct should not be important.
- // And if it is important, what about fields of structs that are fields of other structs, etc.?
int containingSlot = variable.ContainingSlot;
if (containingSlot > 0 &&
variableBySlot[containingSlot].Symbol.GetTypeOrReturnType().TypeKind == TypeKind.Struct &&
- state[containingSlot] == NullableAnnotation.Unknown)
+ !state.IsAssigned(containingSlot))
{
- return NullableAnnotation.Unknown;
+ return (NullableAnnotation.Unknown, false);
}
- return symbol.GetTypeOrReturnType().NullableAnnotation;
+
+ return (symbol.GetTypeOrReturnType().NullableAnnotation, true);
}
default:
throw ExceptionUtilities.UnexpectedValue(symbol.Kind);
@@ -814,19 +815,19 @@ private void InheritNullableStateOfTrackableType(int targetSlot, int valueSlot,
protected override LocalState ReachableState()
{
- var state = new LocalState(reachable: true, new ArrayBuilder(nextVariableSlot));
+ var state = new LocalState(reachable: true, BitVector.Create(nextVariableSlot), new ArrayBuilder(nextVariableSlot));
Populate(ref state, start: 0);
return state;
}
protected override LocalState UnreachableState()
{
- return new LocalState(reachable: false, null);
+ return new LocalState(reachable: false, BitVector.Empty, null);
}
protected override LocalState AllBitsSet()
{
- return new LocalState(reachable: true, new ArrayBuilder(nextVariableSlot));
+ return new LocalState(reachable: true, BitVector.AllSet(nextVariableSlot), new ArrayBuilder(nextVariableSlot));
}
private void EnterParameters()
@@ -4632,6 +4633,13 @@ protected override void UnionWith(ref LocalState self, ref LocalState other)
{
self[slot] = union;
}
+
+ bool selfIsAssigned = self.IsAssigned(slot);
+ bool isAssigned = selfIsAssigned || other.IsAssigned(slot);
+ if (selfIsAssigned != isAssigned)
+ {
+ self.SetAssigned(slot, isAssigned);
+ }
}
}
@@ -4707,6 +4715,14 @@ protected override bool IntersectWith(ref LocalState self, ref LocalState other)
self[slot] = intersection;
result = true;
}
+
+ bool selfIsAssigned = self.IsAssigned(slot);
+ bool isAssigned = selfIsAssigned && other.IsAssigned(slot);
+ if (selfIsAssigned != isAssigned)
+ {
+ self.SetAssigned(slot, isAssigned);
+ result = true;
+ }
}
return result;
@@ -4736,12 +4752,15 @@ internal class LocalState : AbstractLocalState
internal struct LocalState : AbstractLocalState
#endif
{
+ private BitVector _assigned;
private ArrayBuilder _state;
public bool Reachable { get; }
- internal LocalState(bool reachable, ArrayBuilder state)
+ internal LocalState(bool reachable, BitVector assigned, ArrayBuilder state)
{
+ Debug.Assert(!assigned.IsNull);
this.Reachable = reachable;
+ this._assigned = assigned;
this._state = state;
}
@@ -4749,6 +4768,8 @@ internal LocalState(bool reachable, ArrayBuilder state)
internal void EnsureCapacity(int capacity)
{
+ _assigned.EnsureCapacity(capacity);
+
if (_state == null)
{
_state = new ArrayBuilder(capacity);
@@ -4775,9 +4796,20 @@ internal NullableAnnotation this[int slot]
{
EnsureCapacity(slot + 1);
_state[slot] = value;
+ SetAssigned(slot, true);
}
}
+ internal void SetAssigned(int slot, bool value)
+ {
+ _assigned[slot] = value;
+ }
+
+ internal bool IsAssigned(int slot)
+ {
+ return _assigned[slot];
+ }
+
///
/// Produce a duplicate of this flow analysis state.
///
@@ -4797,7 +4829,7 @@ public LocalState Clone()
clone.AddRange(_state);
}
- return new LocalState(Reachable, clone);
+ return new LocalState(Reachable, _assigned.Clone(), clone);
}
internal string GetDebuggerDisplay()
diff --git a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter.cs b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter.cs
index e664171430cb3..8259c6948c80f 100644
--- a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter.cs
+++ b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter.cs
@@ -244,15 +244,15 @@ public override BoundNode VisitLocalFunctionStatement(BoundLocalFunctionStatemen
_factory.CompilationState.ModuleBuilderOpt?.EnsureIsUnmanagedAttributeExists();
}
- bool hasConstraintsWithNullableReferenceTypes = typeParameters.Any(
- typeParameter => typeParameter.ReferenceTypeConstraintIsNullable == true ||
+ bool constraintsNeedNullableAttribute = typeParameters.Any(
+ typeParameter => (typeParameter.HasReferenceTypeConstraint && typeParameter.ReferenceTypeConstraintIsNullable != null) ||
typeParameter.ConstraintTypesNoUseSiteDiagnostics.Any(
- typeConstraint => typeConstraint.ContainsNullableReferenceTypes()));
+ typeConstraint => typeConstraint.NeedsNullableAttribute()));
- bool hasReturnTypeWithNullableReferenceTypes = node.Symbol.ReturnType.ContainsNullableReferenceTypes();
- bool hasParametersWithNullableReferenceTypes = node.Symbol.ParameterTypes.Any(parameter => parameter.ContainsNullableReferenceTypes());
+ bool returnTypeNeedsNullableAttribute = node.Symbol.ReturnType.NeedsNullableAttribute();
+ bool parametersNeedNullableAttribute = node.Symbol.ParameterTypes.Any(parameter => parameter.NeedsNullableAttribute());
- if (hasConstraintsWithNullableReferenceTypes || hasReturnTypeWithNullableReferenceTypes || hasParametersWithNullableReferenceTypes)
+ if (constraintsNeedNullableAttribute || returnTypeNeedsNullableAttribute || parametersNeedNullableAttribute)
{
_factory.CompilationState.ModuleBuilderOpt?.EnsureNullableAttributeExists();
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/ArrayTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/ArrayTypeSymbol.cs
index 7ca8893987b84..b8d0a424665af 100644
--- a/src/Compilers/CSharp/Portable/Symbols/ArrayTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/ArrayTypeSymbol.cs
@@ -379,17 +379,17 @@ public override int GetHashCode()
return Hash.Combine(current, hash);
}
- internal override void AddNullableTransforms(ArrayBuilder transforms)
+ internal override void AddNullableTransforms(ArrayBuilder transforms)
{
ElementType.AddNullableTransforms(transforms);
}
- internal override bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result)
+ internal override bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbol result)
{
TypeSymbolWithAnnotations oldElementType = ElementType;
TypeSymbolWithAnnotations newElementType;
- if (!oldElementType.ApplyNullableTransforms(transforms, nonNullTypesContext, ref position, out newElementType))
+ if (!oldElementType.ApplyNullableTransforms(defaultTransformFlag, transforms, ref position, out newElementType))
{
result = this;
return false;
diff --git a/src/Compilers/CSharp/Portable/Symbols/Compilation_WellKnownMembers.cs b/src/Compilers/CSharp/Portable/Symbols/Compilation_WellKnownMembers.cs
index a76ddfdace1ca..d27a2ddec426b 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Compilation_WellKnownMembers.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Compilation_WellKnownMembers.cs
@@ -166,14 +166,10 @@ internal NamedTypeSymbol GetWellKnownType(WellKnownType type)
}
else
{
- // Well-known types that are injected cannot be referenced from another assembly
- bool includeReferences = type != WellKnownType.System_Runtime_CompilerServices_NonNullTypesAttribute &&
- type != WellKnownType.Microsoft_CodeAnalysis_EmbeddedAttribute;
-
// well-known types introduced before CSharp7 allow lookup ambiguity and report a warning
DiagnosticBag legacyWarnings = (type <= WellKnownType.CSharp7Sentinel) ? warnings : null;
result = this.Assembly.GetTypeByMetadataName(
- mdName, includeReferences: includeReferences, useCLSCompliantNameArityEncoding: true, isWellKnownType: true, conflicts: out conflicts,
+ mdName, includeReferences: true, useCLSCompliantNameArityEncoding: true, isWellKnownType: true, conflicts: out conflicts,
warnings: legacyWarnings, ignoreCorLibraryDuplicatedTypes: ignoreCorLibraryDuplicatedTypes);
}
@@ -447,13 +443,6 @@ internal SynthesizedAttributeData TrySynthesizeAttribute(
return new SynthesizedAttributeData(ctorSymbol, arguments, namedStringArguments);
}
- internal SynthesizedAttributeData TrySynthesizeNonNullTypesAttribute(bool value)
- {
- return TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_NonNullTypesAttribute__ctor,
- ImmutableArray.Create(new TypedConstant(GetSpecialType(SpecialType.System_Boolean), TypedConstantKind.Primitive, value)),
- isOptionalUse: true);
- }
-
internal SynthesizedAttributeData SynthesizeDecimalConstantAttribute(decimal value)
{
bool isNegative;
@@ -583,7 +572,7 @@ internal bool CheckIfNullableAttributeShouldBeEmbedded(DiagnosticBag diagnostics
diagnosticsOpt,
locationOpt,
WellKnownType.System_Runtime_CompilerServices_NullableAttribute,
- WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctor,
+ WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorByte,
WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorTransformFlags);
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/DynamicTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/DynamicTypeSymbol.cs
index a9525f3c2f8b6..b6f11481b887c 100644
--- a/src/Compilers/CSharp/Portable/Symbols/DynamicTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/DynamicTypeSymbol.cs
@@ -217,11 +217,11 @@ internal override bool Equals(TypeSymbol t2, TypeCompareKind comparison)
return false;
}
- internal override void AddNullableTransforms(ArrayBuilder transforms)
+ internal override void AddNullableTransforms(ArrayBuilder transforms)
{
}
- internal override bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result)
+ internal override bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbol result)
{
result = this;
return true;
diff --git a/src/Compilers/CSharp/Portable/Symbols/ExtraAnnotations.cs b/src/Compilers/CSharp/Portable/Symbols/ExtraAnnotations.cs
index bda6f3322adb0..67249ab52f4eb 100644
--- a/src/Compilers/CSharp/Portable/Symbols/ExtraAnnotations.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/ExtraAnnotations.cs
@@ -19,8 +19,8 @@ internal static class ExtraAnnotations
// 1) don't accept null input
// 2) return a reference type
// All types in a member which can be annotated should be annotated. Value types and void can be skipped (with a `default`)
- private static readonly ImmutableDictionary>> Annotations =
- new Dictionary>>
+ private static readonly ImmutableDictionary>> Annotations =
+ new Dictionary>>
{
{ "System.Boolean System.Boolean.Parse(System.String)", Array(default, Nullable(false)) },
{ "System.Void System.Buffer.BlockCopy(System.Array, System.Int32, System.Array, System.Int32, System.Int32)", Array(default, Nullable(false), default, Nullable(false), default, default) },
@@ -121,19 +121,18 @@ internal static string MakeMethodKey(MethodSymbol method)
// use assembly qualified name format (used in metadata) rather than symbol display?
}
- private static ImmutableArray> Array(params ImmutableArray[] values)
+ private static ImmutableArray> Array(params ImmutableArray[] values)
=> values.ToImmutableArray();
private static ImmutableArray Array(params FlowAnalysisAnnotations[] values)
=> values.ToImmutableArray();
- private static ImmutableArray Nullable(params bool[] values)
+ private static ImmutableArray Nullable(bool isNullable)
{
- Debug.Assert(values.Length > 0);
- return values.ToImmutableArray();
+ return ImmutableArray.Create((byte)(isNullable ? NullableAnnotation.Nullable : NullableAnnotation.NotNullable));
}
- internal static ImmutableArray> GetExtraAnnotations(string key)
+ internal static ImmutableArray> GetExtraAnnotations(string key)
{
if (key is null)
{
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/NullableTypeDecoder.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/NullableTypeDecoder.cs
index ca1c905f5a944..6ab9e768aecba 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/NullableTypeDecoder.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/NullableTypeDecoder.cs
@@ -16,22 +16,27 @@ internal static class NullableTypeDecoder
internal static TypeSymbolWithAnnotations TransformType(
TypeSymbolWithAnnotations metadataType,
EntityHandle targetSymbolToken,
- PEModuleSymbol containingModule,
- INonNullTypesContext nonNullTypesContext)
+ PEModuleSymbol containingModule)
{
Debug.Assert(!metadataType.IsNull);
- ImmutableArray nullableTransformFlags;
- containingModule.Module.HasNullableAttribute(targetSymbolToken, out nullableTransformFlags);
+ byte defaultTransformFlag;
+ ImmutableArray nullableTransformFlags;
+ containingModule.Module.HasNullableAttribute(targetSymbolToken, out defaultTransformFlag, out nullableTransformFlags);
- return TransformType(metadataType, nullableTransformFlags, nonNullTypesContext);
+ return TransformType(metadataType, defaultTransformFlag, nullableTransformFlags);
}
- internal static TypeSymbolWithAnnotations TransformType(TypeSymbolWithAnnotations metadataType, ImmutableArray nullableTransformFlags, INonNullTypesContext nonNullTypesContext)
+ internal static TypeSymbolWithAnnotations TransformType(TypeSymbolWithAnnotations metadataType, byte defaultTransformFlag, ImmutableArray nullableTransformFlags)
{
+ if (nullableTransformFlags.IsDefault && defaultTransformFlag == 0)
+ {
+ return metadataType;
+ }
+
int position = 0;
TypeSymbolWithAnnotations result;
- if (metadataType.ApplyNullableTransforms(nullableTransformFlags, nonNullTypesContext, ref position, out result) &&
+ if (metadataType.ApplyNullableTransforms(defaultTransformFlag, nullableTransformFlags, ref position, out result) &&
(nullableTransformFlags.IsDefault || position == nullableTransformFlags.Length))
{
return result;
@@ -46,16 +51,15 @@ internal static TypeSymbolWithAnnotations TransformType(
TypeSymbolWithAnnotations metadataType,
EntityHandle targetSymbolToken,
PEModuleSymbol containingModule,
- INonNullTypesContext nonNullTypesContext,
- ImmutableArray extraAnnotations)
+ ImmutableArray extraAnnotations)
{
if (extraAnnotations.IsDefault)
{
- return NullableTypeDecoder.TransformType(metadataType, targetSymbolToken, containingModule, nonNullTypesContext);
+ return NullableTypeDecoder.TransformType(metadataType, targetSymbolToken, containingModule);
}
else
{
- return NullableTypeDecoder.TransformType(metadataType, extraAnnotations, NonNullTypesTrueContext.Instance);
+ return NullableTypeDecoder.TransformType(metadataType, defaultTransformFlag: 0, extraAnnotations);
}
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEEventSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEEventSymbol.cs
index ddd88fe4d1715..883b62712a809 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEEventSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEEventSymbol.cs
@@ -84,7 +84,7 @@ internal PEEventSymbol(
if (eventType.IsNil)
{
- _eventType = TypeSymbolWithAnnotations.Create(containingType, new UnsupportedMetadataTypeSymbol(mrEx));
+ _eventType = TypeSymbolWithAnnotations.Create(new UnsupportedMetadataTypeSymbol(mrEx));
}
}
@@ -98,11 +98,11 @@ internal PEEventSymbol(
var typeSymbol = DynamicTypeDecoder.TransformType(originalEventType, targetSymbolCustomModifierCount, handle, moduleSymbol);
// We start without annotation (they will be decoded below)
- var type = TypeSymbolWithAnnotations.Create(nonNullTypesContext: containingType, typeSymbol);
+ var type = TypeSymbolWithAnnotations.Create(typeSymbol);
// Decode nullable before tuple types to avoid converting between
// NamedTypeSymbol and TupleTypeSymbol unnecessarily.
- type = NullableTypeDecoder.TransformType(type, handle, moduleSymbol, nonNullTypesContext: this);
+ type = NullableTypeDecoder.TransformType(type, handle, moduleSymbol);
type = TupleTypeDecoder.DecodeTupleTypesIfApplicable(type, handle, moduleSymbol);
_eventType = type;
}
@@ -478,10 +478,7 @@ public override bool? NonNullTypes
{
get
{
- var moduleSymbol = _containingType.ContainingPEModule;
- bool optOut;
-
- return moduleSymbol.Module.HasNonNullTypesAttribute(_handle, out optOut) ? optOut : base.NonNullTypes;
+ throw ExceptionUtilities.Unreachable;
}
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs
index 7bc5b7b5c422d..3e52ae3197ff7 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs
@@ -214,11 +214,11 @@ private void EnsureSignatureIsLoaded()
typeSymbol = DynamicTypeDecoder.TransformType(typeSymbol, customModifiersArray.Length, _handle, moduleSymbol);
// We start without annotations
- var type = TypeSymbolWithAnnotations.Create(nonNullTypesContext: this, typeSymbol, customModifiers: customModifiersArray);
+ var type = TypeSymbolWithAnnotations.Create(typeSymbol, customModifiers: customModifiersArray);
// Decode nullable before tuple types to avoid converting between
// NamedTypeSymbol and TupleTypeSymbol unnecessarily.
- type = NullableTypeDecoder.TransformType(type, _handle, moduleSymbol, nonNullTypesContext: this);
+ type = NullableTypeDecoder.TransformType(type, _handle, moduleSymbol);
type = TupleTypeDecoder.DecodeTupleTypesIfApplicable(type, _handle, moduleSymbol);
_lazyIsVolatile = isVolatile;
@@ -511,10 +511,7 @@ public override bool? NonNullTypes
{
get
{
- var moduleSymbol = _containingType.ContainingPEModule;
- bool optOut;
-
- return moduleSymbol.Module.HasNonNullTypesAttribute(_handle, out optOut) ? optOut : base.NonNullTypes;
+ throw ExceptionUtilities.Unreachable;
}
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEMethodSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEMethodSymbol.cs
index 2bc03136be22b..ff03b941d5a36 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEMethodSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEMethodSymbol.cs
@@ -583,7 +583,7 @@ private SignatureData LoadSignature()
bool isBadParameter;
string key = ExtraAnnotations.MakeMethodKey(this, paramInfo);
- ImmutableArray> extraMethodAnnotations = ExtraAnnotations.GetExtraAnnotations(key);
+ ImmutableArray> extraMethodAnnotations = ExtraAnnotations.GetExtraAnnotations(key);
if (count > 0)
{
@@ -591,7 +591,7 @@ private SignatureData LoadSignature()
for (int i = 0; i < count; i++)
{
// zero-th annotation is for the return type
- ImmutableArray extraAnnotations = extraMethodAnnotations.IsDefault ? default : extraMethodAnnotations[i + 1];
+ ImmutableArray extraAnnotations = extraMethodAnnotations.IsDefault ? default : extraMethodAnnotations[i + 1];
builder.Add(PEParameterSymbol.Create(
moduleSymbol, this, this.IsMetadataVirtual(), i,
@@ -615,7 +615,7 @@ private SignatureData LoadSignature()
paramInfo[0].Type = returnType;
- ImmutableArray extraReturnAnnotations = extraMethodAnnotations.IsDefault ? default : extraMethodAnnotations[0];
+ ImmutableArray extraReturnAnnotations = extraMethodAnnotations.IsDefault ? default : extraMethodAnnotations[0];
var returnParam = PEParameterSymbol.Create(
moduleSymbol, this, this.IsMetadataVirtual(), 0,
paramInfo[0], extraReturnAnnotations, isReturn: true, out isBadParameter);
@@ -715,9 +715,7 @@ public override bool? NonNullTypes
{
get
{
- var moduleSymbol = _containingType.ContainingPEModule;
- bool nonNullTypes;
- return moduleSymbol.Module.HasNonNullTypesAttribute(_handle, out nonNullTypes) ? nonNullTypes : base.NonNullTypes;
+ throw ExceptionUtilities.Unreachable;
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEModuleSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEModuleSymbol.cs
index a8992cb349e01..7425742ddb627 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEModuleSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEModuleSymbol.cs
@@ -701,7 +701,7 @@ public override bool? NonNullTypes
{
get
{
- return _module.HasNonNullTypesAttribute(EntityHandle.ModuleDefinition, out bool nonNullTypes) ? (bool?)nonNullTypes : null;
+ throw ExceptionUtilities.Unreachable;
}
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs
index 9464afffd3fec..45f02313843f7 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs
@@ -448,7 +448,7 @@ private NamedTypeSymbol GetDeclaredBaseType(bool ignoreNullability)
if ((object)declaredBase != null)
{
declaredBase = (NamedTypeSymbol)NullableTypeDecoder.TransformType(
- TypeSymbolWithAnnotations.Create(nonNullTypesContext: this, declaredBase), _handle, ContainingPEModule, nonNullTypesContext: this).TypeSymbol;
+ TypeSymbolWithAnnotations.Create(declaredBase), _handle, ContainingPEModule).TypeSymbol;
}
Interlocked.CompareExchange(ref _lazyDeclaredBaseTypeWithNullability, declaredBase, ErrorTypeSymbol.UnknownResultType);
}
@@ -511,7 +511,7 @@ private ImmutableArray MakeDeclaredInterfaces()
TypeSymbol typeSymbol = tokenDecoder.GetTypeOfToken(interfaceHandle);
typeSymbol = TupleTypeDecoder.DecodeTupleTypesIfApplicable(typeSymbol, interfaceImpl, moduleSymbol);
- typeSymbol = NullableTypeDecoder.TransformType(TypeSymbolWithAnnotations.Create(nonNullTypesContext: this, typeSymbol), interfaceImpl, moduleSymbol, nonNullTypesContext: this).TypeSymbol;
+ typeSymbol = NullableTypeDecoder.TransformType(TypeSymbolWithAnnotations.Create(typeSymbol), interfaceImpl, moduleSymbol).TypeSymbol;
var namedTypeSymbol = typeSymbol as NamedTypeSymbol ?? new UnsupportedMetadataTypeSymbol(); // interface list contains a bad type
symbols.Add(namedTypeSymbol);
@@ -1990,8 +1990,7 @@ public override bool? NonNullTypes
{
get
{
- bool nonNullTypes;
- return this.ContainingPEModule.Module.HasNonNullTypesAttribute(_handle, out nonNullTypes) ? nonNullTypes : base.NonNullTypes;
+ throw ExceptionUtilities.Unreachable;
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEParameterSymbol.cs
index 31c5cd16ed037..a7f8e984a29dd 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEParameterSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEParameterSymbol.cs
@@ -148,7 +148,7 @@ internal static PEParameterSymbol Create(
bool isContainingSymbolVirtual,
int ordinal,
ParamInfo parameterInfo,
- ImmutableArray extraAnnotations,
+ ImmutableArray extraAnnotations,
bool isReturn,
out bool isBad)
{
@@ -176,7 +176,7 @@ internal static PEParameterSymbol Create(
int ordinal,
ParameterHandle handle,
ParamInfo parameterInfo,
- ImmutableArray extraAnnotations,
+ ImmutableArray extraAnnotations,
out bool isBad)
{
return Create(
@@ -191,7 +191,7 @@ private PEParameterSymbol(
int ordinal,
bool isByRef,
TypeSymbolWithAnnotations type,
- ImmutableArray extraAnnotations,
+ ImmutableArray extraAnnotations,
ParameterHandle handle,
int countOfCustomModifiers,
out bool isBad)
@@ -219,8 +219,7 @@ private PEParameterSymbol(
type;
if (!extraAnnotations.IsDefault)
{
- // https://github.com/dotnet/roslyn/issues/29821 any external annotation is taken to imply a `[NonNullTypes(true)]` context
- type = NullableTypeDecoder.TransformType(type, extraAnnotations, nonNullTypesContext: NonNullTypesTrueContext.Instance);
+ type = NullableTypeDecoder.TransformType(type, defaultTransformFlag: 0, extraAnnotations);
}
_lazyCustomAttributes = ImmutableArray.Empty;
@@ -262,7 +261,7 @@ private PEParameterSymbol(
type = type.WithTypeAndModifiers(typeSymbol, type.CustomModifiers);
// Decode nullable before tuple types to avoid converting between
// NamedTypeSymbol and TupleTypeSymbol unnecessarily.
- type = NullableTypeDecoder.TransformType(type, handle, moduleSymbol, nonNullTypesContext: this, extraAnnotations);
+ type = NullableTypeDecoder.TransformType(type, handle, moduleSymbol, extraAnnotations);
type = TupleTypeDecoder.DecodeTupleTypesIfApplicable(type, handle, moduleSymbol);
}
@@ -297,14 +296,14 @@ private static PEParameterSymbol Create(
bool isByRef,
ImmutableArray> refCustomModifiers,
TypeSymbol type,
- ImmutableArray extraAnnotations,
+ ImmutableArray extraAnnotations,
ParameterHandle handle,
ImmutableArray> customModifiers,
bool isReturn,
out bool isBad)
{
// We start without annotation (they will be decoded below)
- var typeWithModifiers = TypeSymbolWithAnnotations.Create(containingSymbol, type, customModifiers: CSharpCustomModifier.Convert(customModifiers));
+ var typeWithModifiers = TypeSymbolWithAnnotations.Create(type, customModifiers: CSharpCustomModifier.Convert(customModifiers));
PEParameterSymbol parameter = customModifiers.IsDefaultOrEmpty && refCustomModifiers.IsDefaultOrEmpty
? new PEParameterSymbol(moduleSymbol, containingSymbol, ordinal, isByRef, typeWithModifiers, extraAnnotations, handle, 0, out isBad)
@@ -342,7 +341,7 @@ public PEParameterSymbolWithCustomModifiers(
bool isByRef,
ImmutableArray> refCustomModifiers,
TypeSymbolWithAnnotations type,
- ImmutableArray extraAnnotations,
+ ImmutableArray extraAnnotations,
ParameterHandle handle,
out bool isBad) :
base(moduleSymbol, containingSymbol, ordinal, isByRef, type, extraAnnotations, handle,
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEPropertySymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEPropertySymbol.cs
index 059cf3413ac5a..3473c8b20e6b2 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEPropertySymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEPropertySymbol.cs
@@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.DocumentationComments;
using Microsoft.CodeAnalysis.CSharp.Emit;
using Microsoft.CodeAnalysis.PooledObjects;
+using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE
{
@@ -167,11 +168,11 @@ private PEPropertySymbol(
originalPropertyType = originalPropertyType.AsDynamicIfNoPia(_containingType);
// We start without annotation (they will be decoded below)
- var propertyType = TypeSymbolWithAnnotations.Create(nonNullTypesContext: this, originalPropertyType, isAnnotated: false, typeCustomModifiers);
+ var propertyType = TypeSymbolWithAnnotations.Create(originalPropertyType, customModifiers: typeCustomModifiers);
// Decode nullable before tuple types to avoid converting between
// NamedTypeSymbol and TupleTypeSymbol unnecessarily.
- propertyType = NullableTypeDecoder.TransformType(propertyType, handle, moduleSymbol, nonNullTypesContext: this);
+ propertyType = NullableTypeDecoder.TransformType(propertyType, handle, moduleSymbol);
propertyType = TupleTypeDecoder.DecodeTupleTypesIfApplicable(propertyType, handle, moduleSymbol);
_propertyType = propertyType;
@@ -738,9 +739,7 @@ public override bool? NonNullTypes
{
get
{
- var moduleSymbol = _containingType.ContainingPEModule;
- bool nonNullTypes;
- return moduleSymbol.Module.HasNonNullTypesAttribute(_handle, out nonNullTypes) ? nonNullTypes : base.NonNullTypes;
+ throw ExceptionUtilities.Unreachable;
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PETypeParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PETypeParameterSymbol.cs
index e54e050210182..d400dd0723d14 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PETypeParameterSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PETypeParameterSymbol.cs
@@ -206,9 +206,8 @@ private ImmutableArray GetDeclaredConstraintTypes()
}
}
- // https://github.com/dotnet/roslyn/issues/30075: Test different [NonNullTypes] on method and containing type.
- var type = TypeSymbolWithAnnotations.Create(this, typeSymbol);
- type = NullableTypeDecoder.TransformType(type, constraintHandle, moduleSymbol, nonNullTypesContext: this);
+ var type = TypeSymbolWithAnnotations.Create(typeSymbol);
+ type = NullableTypeDecoder.TransformType(type, constraintHandle, moduleSymbol);
// Drop 'System.Object?' constraint type.
if (type.SpecialType == SpecialType.System_Object && type.NullableAnnotation.IsAnyNullable())
@@ -287,15 +286,15 @@ internal override bool? ReferenceTypeConstraintIsNullable
return false;
}
- if (((PEModuleSymbol)this.ContainingModule).Module.HasNullableAttribute(_handle, out ImmutableArray nullableTransformFlags) &&
- nullableTransformFlags.Length == 1 && nullableTransformFlags[0])
+ if (((PEModuleSymbol)this.ContainingModule).Module.HasNullableAttribute(_handle, out byte transformFlag, out _))
{
- return true;
- }
-
- if (NonNullTypes == true)
- {
- return false;
+ switch ((NullableAnnotation)transformFlag)
+ {
+ case NullableAnnotation.Nullable:
+ return true;
+ case NullableAnnotation.NotNullable:
+ return false;
+ }
}
return null;
diff --git a/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs
index 508f9b6a77793..30f985d341ed9 100644
--- a/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs
@@ -1174,7 +1174,7 @@ internal virtual void AddSynthesizedReturnTypeAttributes(PEModuleBuilder moduleB
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeTupleNamesAttribute(type.TypeSymbol));
}
- if (type.ContainsNullableReferenceTypes())
+ if (type.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, type));
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/NamedTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/NamedTypeSymbol.cs
index db8d6cb9f5375..5ac9afc8057ed 100644
--- a/src/Compilers/CSharp/Portable/Symbols/NamedTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/NamedTypeSymbol.cs
@@ -758,7 +758,7 @@ private bool EqualsComplicatedCases(NamedTypeSymbol other, TypeCompareKind compa
return true;
}
- internal override void AddNullableTransforms(ArrayBuilder transforms)
+ internal override void AddNullableTransforms(ArrayBuilder transforms)
{
ContainingType?.AddNullableTransforms(transforms);
@@ -768,7 +768,7 @@ internal override void AddNullableTransforms(ArrayBuilder transforms)
}
}
- internal override bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result)
+ internal override bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbol result)
{
if (!IsGenericType)
{
@@ -784,7 +784,7 @@ internal override bool ApplyNullableTransforms(ImmutableArray transforms,
{
TypeSymbolWithAnnotations oldTypeArgument = allTypeArguments[i];
TypeSymbolWithAnnotations newTypeArgument;
- if (!oldTypeArgument.ApplyNullableTransforms(transforms, nonNullTypesContext, ref position, out newTypeArgument))
+ if (!oldTypeArgument.ApplyNullableTransforms(defaultTransformFlag, transforms, ref position, out newTypeArgument))
{
allTypeArguments.Free();
result = this;
diff --git a/src/Compilers/CSharp/Portable/Symbols/NamespaceSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/NamespaceSymbol.cs
index 11dba599e9705..dfda7bf961895 100644
--- a/src/Compilers/CSharp/Portable/Symbols/NamespaceSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/NamespaceSymbol.cs
@@ -119,7 +119,7 @@ public sealed override SymbolKind Kind
}
}
- public override bool IsImplicitlyDeclared
+ public override sealed bool IsImplicitlyDeclared
{
get
{
diff --git a/src/Compilers/CSharp/Portable/Symbols/PointerTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/PointerTypeSymbol.cs
index cabc47f1402da..313d423e69a1e 100644
--- a/src/Compilers/CSharp/Portable/Symbols/PointerTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/PointerTypeSymbol.cs
@@ -249,17 +249,17 @@ private bool Equals(PointerTypeSymbol other, TypeCompareKind comparison)
return true;
}
- internal override void AddNullableTransforms(ArrayBuilder transforms)
+ internal override void AddNullableTransforms(ArrayBuilder transforms)
{
PointedAtType.AddNullableTransforms(transforms);
}
- internal override bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result)
+ internal override bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbol result)
{
TypeSymbolWithAnnotations oldPointedAtType = PointedAtType;
TypeSymbolWithAnnotations newPointedAtType;
- if (!oldPointedAtType.ApplyNullableTransforms(transforms, nonNullTypesContext, ref position, out newPointedAtType))
+ if (!oldPointedAtType.ApplyNullableTransforms(defaultTransformFlag, transforms, ref position, out newPointedAtType))
{
result = this;
return false;
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/CustomModifierUtils.cs b/src/Compilers/CSharp/Portable/Symbols/Source/CustomModifierUtils.cs
index 5658a163d6df3..9f54a1239934d 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/CustomModifierUtils.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/CustomModifierUtils.cs
@@ -53,7 +53,7 @@ internal static void CopyMethodCustomModifiers(
TypeSymbol returnTypeWithCustomModifiers = sourceMethodReturnType.TypeSymbol;
if (returnTypeSymbol.Equals(returnTypeWithCustomModifiers, TypeCompareKind.AllIgnoreOptions))
{
- returnType = returnType.WithTypeAndModifiers(CopyTypeCustomModifiers(returnTypeWithCustomModifiers, returnTypeSymbol, destinationMethod.ContainingAssembly, nonNullTypesContext: destinationMethod),
+ returnType = returnType.WithTypeAndModifiers(CopyTypeCustomModifiers(returnTypeWithCustomModifiers, returnTypeSymbol, destinationMethod.ContainingAssembly),
sourceMethodReturnType.CustomModifiers);
}
}
@@ -61,12 +61,10 @@ internal static void CopyMethodCustomModifiers(
/// Type that already has custom modifiers.
/// Same as , but without custom modifiers. May differ in object/dynamic.
/// The assembly containing the signature referring to the destination type.
- /// The NonNullTypes context at the destination.
/// with custom modifiers copied from .
- internal static TypeSymbol CopyTypeCustomModifiers(TypeSymbol sourceType, TypeSymbol destinationType, AssemblySymbol containingAssembly, INonNullTypesContext nonNullTypesContext)
+ internal static TypeSymbol CopyTypeCustomModifiers(TypeSymbol sourceType, TypeSymbol destinationType, AssemblySymbol containingAssembly)
{
Debug.Assert(sourceType.Equals(destinationType, TypeCompareKind.AllIgnoreOptions));
- Debug.Assert(nonNullTypesContext != null);
const RefKind refKind = RefKind.None;
@@ -94,11 +92,11 @@ internal static TypeSymbol CopyTypeCustomModifiers(TypeSymbol sourceType, TypeSy
// If the destination had some of those annotations but not all, then clearly the destination
// was incorrect. Or if the destination is C#7, then the destination will advertise annotations
// that the author did not write and did not validate.
- var flagsBuilder = ArrayBuilder.GetInstance();
+ var flagsBuilder = ArrayBuilder.GetInstance();
destinationType.AddNullableTransforms(flagsBuilder);
int position = 0;
int length = flagsBuilder.Count;
- bool transformResult = resultType.ApplyNullableTransforms(flagsBuilder.ToImmutableAndFree(), nonNullTypesContext, ref position, out resultType);
+ bool transformResult = resultType.ApplyNullableTransforms(defaultTransformFlag: 0, flagsBuilder.ToImmutableAndFree(), ref position, out resultType);
Debug.Assert(transformResult && position == length);
Debug.Assert(resultType.Equals(sourceType, TypeCompareKind.IgnoreDynamicAndTupleNames | TypeCompareKind.IgnoreNullableModifiersForReferenceTypes)); // Same custom modifiers as source type.
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/FieldSymbolWithAttributesAndModifiers.cs b/src/Compilers/CSharp/Portable/Symbols/Source/FieldSymbolWithAttributesAndModifiers.cs
index b7b5420157e53..c6d202c9a7237 100755
--- a/src/Compilers/CSharp/Portable/Symbols/Source/FieldSymbolWithAttributesAndModifiers.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/FieldSymbolWithAttributesAndModifiers.cs
@@ -233,11 +233,6 @@ internal override void DecodeWellKnownAttribute(ref DecodeWellKnownAttributeArgu
// NullableAttribute should not be set explicitly.
arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNullableAttribute, arguments.AttributeSyntaxOpt.Location);
}
- else if (attribute.IsTargetAttribute(this, AttributeDescription.NonNullTypesAttribute))
- {
- // NonNullTypesAttribute should not be set explicitly.
- arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNonNullTypesAttribute, arguments.AttributeSyntaxOpt.Location);
- }
}
///
@@ -394,9 +389,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
DeclaringCompilation.SynthesizeTupleNamesAttribute(type.TypeSymbol));
}
- AddSynthesizedNonNullTypesAttributeForMember(ref attributes);
-
- if (type.ContainsNullableReferenceTypes())
+ if (type.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, type));
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs
index 438c6a47fbf64..c0f312c1decb9 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs
@@ -235,7 +235,7 @@ internal void ComputeReturnType()
DeclaringCompilation.EnsureIsReadOnlyAttributeExists(diagnostics, location, modifyCompilation: false);
}
- if (returnType.ContainsNullableReferenceTypes())
+ if (returnType.NeedsNullableAttribute())
{
DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: false);
// Note: we don't need to warn on annotations used without NonNullTypes context for local functions, as this is handled in binding already
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs b/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs
index 96df6d00dfde5..fe7bf2bad5576 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs
@@ -135,7 +135,7 @@ internal static void EnsureNullableAttributeExists(ImmutableArray attributes)
@@ -325,9 +320,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
DeclaringCompilation.SynthesizeTupleNamesAttribute(type.TypeSymbol));
}
- AddSynthesizedNonNullTypesAttributeForMember(ref attributes);
-
- if (type.ContainsNullableReferenceTypes())
+ if (type.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, type));
}
@@ -558,10 +551,9 @@ protected void CheckModifiersAndType(DiagnosticBag diagnostics)
return SourceDocumentationCommentUtils.GetAndCacheDocumentationComment(this, expandIncludes, ref _lazyDocComment);
}
- protected static void CopyEventCustomModifiers(EventSymbol eventWithCustomModifiers, ref TypeSymbolWithAnnotations type, AssemblySymbol containingAssembly, Symbol nonNullTypesContext)
+ protected static void CopyEventCustomModifiers(EventSymbol eventWithCustomModifiers, ref TypeSymbolWithAnnotations type, AssemblySymbol containingAssembly)
{
Debug.Assert((object)eventWithCustomModifiers != null);
- Debug.Assert(nonNullTypesContext != null);
TypeSymbol overriddenEventType = eventWithCustomModifiers.Type.TypeSymbol;
@@ -570,7 +562,7 @@ protected static void CopyEventCustomModifiers(EventSymbol eventWithCustomModifi
// we want to retain the original (incorrect) type to avoid hiding the type given in source.
if (type.TypeSymbol.Equals(overriddenEventType, TypeCompareKind.IgnoreCustomModifiersAndArraySizesAndLowerBounds | TypeCompareKind.IgnoreNullableModifiersForReferenceTypes | TypeCompareKind.IgnoreDynamic))
{
- type = type.WithTypeAndModifiers(CustomModifierUtils.CopyTypeCustomModifiers(overriddenEventType, type.TypeSymbol, containingAssembly, nonNullTypesContext),
+ type = type.WithTypeAndModifiers(CustomModifierUtils.CopyTypeCustomModifiers(overriddenEventType, type.TypeSymbol, containingAssembly),
eventWithCustomModifiers.Type.CustomModifiers);
}
}
@@ -688,7 +680,7 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions,
this.CheckModifiersAndType(diagnostics);
this.Type.CheckAllConstraints(conversions, location, diagnostics);
- if (this.Type.ContainsNullableReferenceTypes())
+ if (this.Type.NeedsNullableAttribute())
{
this.DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: true);
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldLikeEventSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldLikeEventSymbol.cs
index 9e0a3958085a4..de90ba74b008f 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldLikeEventSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldLikeEventSymbol.cs
@@ -51,7 +51,7 @@ internal SourceFieldLikeEventSymbol(SourceMemberContainerTypeSymbol containingTy
EventSymbol overriddenEvent = this.OverriddenEvent;
if ((object)overriddenEvent != null)
{
- CopyEventCustomModifiers(overriddenEvent, ref _type, ContainingAssembly, nonNullTypesContext: this);
+ CopyEventCustomModifiers(overriddenEvent, ref _type, ContainingAssembly);
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldSymbol.cs
index 7e13447629903..663d0bf8f0648 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceFieldSymbol.cs
@@ -128,7 +128,7 @@ internal sealed override void DecodeWellKnownAttribute(ref DecodeWellKnownAttrib
internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions, DiagnosticBag diagnostics)
{
var location = ErrorLocation;
- if (this.Type.ContainsNullableReferenceTypes())
+ if (this.Type.NeedsNullableAttribute())
{
DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: true);
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs
index 4a6edd640d076..f753b59a5c3b0 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs
@@ -1417,8 +1417,8 @@ protected void AfterMembersChecks(DiagnosticBag diagnostics)
// https://github.com/dotnet/roslyn/issues/30080: Report diagnostics for base type and interfaces at more specific locations.
var baseType = BaseTypeNoUseSiteDiagnostics;
var interfaces = InterfacesNoUseSiteDiagnostics();
- if (baseType?.ContainsNullableReferenceTypes() == true ||
- interfaces.Any(t => t.ContainsNullableReferenceTypes()))
+ if (baseType?.NeedsNullableAttribute() == true ||
+ interfaces.Any(t => t.NeedsNullableAttribute()))
{
this.DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: true);
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs
index 7a5e4ea26f3b5..e0ddab2886172 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs
@@ -1206,11 +1206,6 @@ private void DecodeWellKnownAttributeAppliedToMethod(ref DecodeWellKnownAttribut
arguments.Diagnostics.Add(ErrorCode.ERR_SecurityCriticalOrSecuritySafeCriticalOnAsync, arguments.AttributeSyntaxOpt.Location, arguments.AttributeSyntaxOpt.GetErrorDisplayName());
}
}
- else if (attribute.IsTargetAttribute(this, AttributeDescription.NonNullTypesAttribute))
- {
- // NonNullTypesAttribute should not be set explicitly.
- arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNonNullTypesAttribute, arguments.AttributeSyntaxOpt.Location);
- }
else
{
var compilation = this.DeclaringCompilation;
@@ -1623,8 +1618,6 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);
- AddSynthesizedNonNullTypesAttributeForMember(ref attributes);
-
bool isAsync = this.IsAsync;
bool isIterator = this.IsIterator;
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceModuleSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceModuleSymbol.cs
index d2bcfa0424aaf..4168ec02aa9a6 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceModuleSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceModuleSymbol.cs
@@ -511,11 +511,6 @@ internal override void DecodeWellKnownAttribute(ref DecodeWellKnownAttributeArgu
arguments.GetOrCreateData().DefaultCharacterSet = charSet;
}
}
- else if (attribute.IsTargetAttribute(this, AttributeDescription.NonNullTypesAttribute))
- {
- // NonNullTypesAttribute should not be set explicitly.
- arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNonNullTypesAttribute, arguments.AttributeSyntaxOpt.Location);
- }
}
public override bool? NonNullTypes
@@ -540,13 +535,6 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
WellKnownMember.System_Security_UnverifiableCodeAttribute__ctor));
}
}
-
- bool? nonNullTypes = NonNullTypes;
- if (nonNullTypes != null)
- {
- AddSynthesizedAttribute(ref attributes,
- compilation.TrySynthesizeNonNullTypesAttribute(nonNullTypes.GetValueOrDefault()));
- }
}
internal override bool HasAssemblyCompilationRelaxationsAttribute
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
index a7b81143b23cd..8a7a41cc24135 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
@@ -748,11 +748,6 @@ internal sealed override void DecodeWellKnownAttribute(ref DecodeWellKnownAttrib
// NullableAttribute should not be set explicitly.
arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNullableAttribute, arguments.AttributeSyntaxOpt.Location);
}
- else if (attribute.IsTargetAttribute(this, AttributeDescription.NonNullTypesAttribute))
- {
- // NonNullTypesAttribute should not be set explicitly.
- arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNonNullTypesAttribute, arguments.AttributeSyntaxOpt.Location);
- }
else
{
var compilation = this.DeclaringCompilation;
@@ -1224,18 +1219,11 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeTupleNamesAttribute(baseType));
}
- if (baseType.ContainsNullableReferenceTypes())
+ if (baseType.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, TypeSymbolWithAnnotations.Create(baseType)));
}
}
-
- bool? nonNullTypes = NonNullTypes;
- if (nonNullTypes.HasValue && nonNullTypes != ((Symbol)ContainingType ?? ContainingModule).NonNullTypes)
- {
- AddSynthesizedAttribute(ref attributes,
- compilation.TrySynthesizeNonNullTypesAttribute(nonNullTypes.GetValueOrDefault()));
- }
}
#endregion
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.cs
index 06d7bc04171f9..46c490ff3548e 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.cs
@@ -296,7 +296,7 @@ private Dictionary> MakeNameToMemb
{
builder.Add(BuildSymbol(declaration, diagnostics));
}
- builder.AddInjectedSymbols(containingNamespace: this);
+
var result = builder.CreateMap();
CheckMembers(this, result, diagnostics);
@@ -534,190 +534,6 @@ public Dictionary> CreateMap()
return result;
}
-
- public void AddInjectedSymbols(NamespaceSymbol containingNamespace)
- {
- if (containingNamespace.DeclaringCompilation.Options.OutputKind.IsNetModule())
- {
- return;
- }
-
- const string codeAnalysis = "CodeAnalysis";
- const string system = "System";
- const string microsoft = "Microsoft";
- const string runtime = "Runtime";
- const string compilerServices = "CompilerServices";
-
- if (containingNamespace.IsGlobalNamespace)
- {
- if (!_dictionary.ContainsKey(system))
- {
- Add(makeEmptyNamespace(system));
- }
- if (!_dictionary.ContainsKey(microsoft))
- {
- Add(makeEmptyNamespace(microsoft));
- }
- return;
- }
-
- switch (containingNamespace.Name)
- {
- case system:
- if (containingNamespace.ContainingNamespace.IsGlobalNamespace && !_dictionary.ContainsKey(runtime))
- {
- Add(makeEmptyNamespace(runtime));
- }
- return;
- case runtime:
- if (isSystem(containingNamespace.ContainingNamespace) && !_dictionary.ContainsKey(compilerServices))
- {
- Add(makeEmptyNamespace(compilerServices));
- }
- return;
- case microsoft:
- if (containingNamespace.ContainingNamespace.IsGlobalNamespace && !_dictionary.ContainsKey(codeAnalysis))
- {
- Add(makeEmptyNamespace(codeAnalysis));
- }
- return;
- case compilerServices:
- if (isRuntime(containingNamespace.ContainingNamespace) && !_dictionary.ContainsKey("NonNullTypesAttribute"))
- {
- Add(InjectedNonNullTypesAttributeSymbol.Create(containingNamespace));
- }
- return;
- case codeAnalysis:
- if (isMicrosoft(containingNamespace.ContainingNamespace) && !_dictionary.ContainsKey("EmbeddedAttribute"))
- {
- Add(InjectedEmbeddedAttributeSymbol.Create(containingNamespace));
- }
- return;
- }
-
- InjectedNamespaceSymbol makeEmptyNamespace(string name)
- {
- return new InjectedNamespaceSymbol(containingNamespace, name);
- }
-
- bool isSystem(NamespaceSymbol symbol)
- {
- return symbol.Name == system && symbol.ContainingNamespace.IsGlobalNamespace;
- }
-
- bool isRuntime(NamespaceSymbol symbol)
- {
- return symbol.Name == runtime && isSystem(symbol.ContainingNamespace);
- }
-
- bool isMicrosoft(NamespaceSymbol symbol)
- {
- return symbol.Name == microsoft && symbol.ContainingNamespace.IsGlobalNamespace;
- }
- }
-
- class InjectedNamespaceSymbol : NamespaceSymbol
- {
- private readonly NamespaceSymbol _containingNamespace;
- private readonly string _name;
- private Dictionary> _nameToMembersMap;
- private Dictionary> _nameToTypeMembersMap;
- private ImmutableArray _lazyAllMembersOrdered;
- private ImmutableArray _lazyTypeMembersOrdered;
-
- public InjectedNamespaceSymbol(NamespaceSymbol containingNamespace, string name)
- {
- Debug.Assert(name != null);
- _containingNamespace = containingNamespace;
- _name = name;
- }
-
- public override string Name
- => _name;
-
- public override AssemblySymbol ContainingAssembly
- => _containingNamespace.ContainingAssembly;
-
- public override Symbol ContainingSymbol
- => _containingNamespace;
-
- public override ImmutableArray Locations
- => ImmutableArray.Empty;
-
- public override ImmutableArray DeclaringSyntaxReferences
- => ImmutableArray.Empty;
-
- internal override NamespaceExtent Extent
- => _containingNamespace.Extent;
-
- public override ImmutableArray GetMembers()
- {
- if (_lazyAllMembersOrdered == null)
- {
- var members = StaticCast.From(this.GetNameToMembersMap().Flatten(null).Sort(LexicalOrderSymbolComparer.Instance));
- ImmutableInterlocked.InterlockedInitialize(ref _lazyAllMembersOrdered, members);
- }
- return _lazyAllMembersOrdered;
- }
-
- public override ImmutableArray GetMembers(string name)
- {
- ImmutableArray members;
- return this.GetNameToMembersMap().TryGetValue(name, out members)
- ? members.Cast()
- : ImmutableArray.Empty;
- }
-
- private Dictionary> GetNameToMembersMap()
- {
- if (_nameToMembersMap == null)
- {
- var builder = new NameToSymbolMapBuilder(1);
- builder.AddInjectedSymbols(containingNamespace: this);
- var map = builder.CreateMap();
-
- var diagnostics = DiagnosticBag.GetInstance();
- CheckMembers(this, map, diagnostics);
- this.DeclaringCompilation.DeclarationDiagnostics.AddRange(diagnostics);
- diagnostics.Free();
-
- Interlocked.CompareExchange(ref _nameToMembersMap, map, null);
- }
-
- return _nameToMembersMap;
- }
-
- private Dictionary> GetNameToTypeMembersMap()
- {
- if (_nameToTypeMembersMap == null)
- {
- Interlocked.CompareExchange(ref _nameToTypeMembersMap, GetTypesFromMemberMap(GetNameToMembersMap()), null);
- }
-
- return _nameToTypeMembersMap;
- }
-
- public override ImmutableArray GetTypeMembers()
- {
- if (_lazyTypeMembersOrdered.IsDefault)
- {
- var members = this.GetNameToTypeMembersMap().Flatten(LexicalOrderSymbolComparer.Instance);
- ImmutableInterlocked.InterlockedInitialize(ref _lazyTypeMembersOrdered, members);
- }
-
- return _lazyTypeMembersOrdered;
- }
-
- public override ImmutableArray GetTypeMembers(string name)
- {
- ImmutableArray members;
- return this.GetNameToTypeMembersMap().TryGetValue(name, out members)
- ? members
- : ImmutableArray.Empty;
- }
-
- public override bool IsImplicitlyDeclared => true;
- }
}
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceOrdinaryMethodSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceOrdinaryMethodSymbol.cs
index e3df2b1ca91c7..8e4451ba65707 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceOrdinaryMethodSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceOrdinaryMethodSymbol.cs
@@ -1044,7 +1044,7 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions,
ParameterHelpers.EnsureIsReadOnlyAttributeExists(Parameters, diagnostics, modifyCompilation: true);
- if (ReturnType.ContainsNullableReferenceTypes())
+ if (ReturnType.NeedsNullableAttribute())
{
this.DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: true);
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbol.cs
index d0a737994dab6..18486860f8eef 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbol.cs
@@ -116,7 +116,7 @@ internal override ParameterSymbol WithCustomModifiersAndParams(TypeSymbol newTyp
internal SourceParameterSymbol WithCustomModifiersAndParamsCore(TypeSymbol newType, ImmutableArray newCustomModifiers, ImmutableArray newRefCustomModifiers, bool newIsParams)
{
- newType = CustomModifierUtils.CopyTypeCustomModifiers(newType, this.Type.TypeSymbol, this.ContainingAssembly, nonNullTypesContext: this);
+ newType = CustomModifierUtils.CopyTypeCustomModifiers(newType, this.Type.TypeSymbol, this.ContainingAssembly);
TypeSymbolWithAnnotations newTypeWithModifiers = this.Type.WithTypeAndModifiers(newType, newCustomModifiers);
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbolBase.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbolBase.cs
index c73267474b5da..d87804cf50ca4 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbolBase.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceParameterSymbolBase.cs
@@ -96,7 +96,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeIsReadOnlyAttribute(this));
}
- if (type.ContainsNullableReferenceTypes())
+ if (type.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, type));
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertyAccessorSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertyAccessorSymbol.cs
index e7c5832e502ba..a9d49a2e685b0 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertyAccessorSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertyAccessorSymbol.cs
@@ -289,7 +289,7 @@ protected override void MethodChecks(DiagnosticBag diagnostics)
PropertySymbol associatedProperty = _property;
var type = associatedProperty.Type;
_lazyReturnType = _lazyReturnType.WithTypeAndModifiers(
- CustomModifierUtils.CopyTypeCustomModifiers(type.TypeSymbol, _lazyReturnType.TypeSymbol, this.ContainingAssembly, nonNullTypesContext: associatedProperty),
+ CustomModifierUtils.CopyTypeCustomModifiers(type.TypeSymbol, _lazyReturnType.TypeSymbol, this.ContainingAssembly),
type.CustomModifiers);
_lazyRefCustomModifiers = associatedProperty.RefCustomModifiers;
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbol.cs
index 6548d3f21cfa0..a41eb0f770eff 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbol.cs
@@ -274,7 +274,7 @@ private SourcePropertySymbol(
if (type.TypeSymbol.Equals(overriddenPropertyType.TypeSymbol, TypeCompareKind.IgnoreCustomModifiersAndArraySizesAndLowerBounds | TypeCompareKind.IgnoreNullableModifiersForReferenceTypes | TypeCompareKind.IgnoreDynamic))
{
type = type.WithTypeAndModifiers(
- CustomModifierUtils.CopyTypeCustomModifiers(overriddenPropertyType.TypeSymbol, type.TypeSymbol, this.ContainingAssembly, nonNullTypesContext: this),
+ CustomModifierUtils.CopyTypeCustomModifiers(overriddenPropertyType.TypeSymbol, type.TypeSymbol, this.ContainingAssembly),
overriddenPropertyType.CustomModifiers);
_lazyType = default;
_lazyType.InterlockedInitialize(type);
@@ -769,7 +769,7 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions,
ParameterHelpers.EnsureIsReadOnlyAttributeExists(Parameters, diagnostics, modifyCompilation: true);
- if (this.Type.ContainsNullableReferenceTypes())
+ if (this.Type.NeedsNullableAttribute())
{
DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: true);
}
@@ -1178,9 +1178,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
DeclaringCompilation.SynthesizeTupleNamesAttribute(type.TypeSymbol));
}
- AddSynthesizedNonNullTypesAttributeForMember(ref attributes);
-
- if (type.ContainsNullableReferenceTypes())
+ if (type.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, type));
}
@@ -1315,11 +1313,6 @@ internal override void DecodeWellKnownAttribute(ref DecodeWellKnownAttributeArgu
// NullableAttribute should not be set explicitly.
arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNullableAttribute, arguments.AttributeSyntaxOpt.Location);
}
- else if (attribute.IsTargetAttribute(this, AttributeDescription.NonNullTypesAttribute))
- {
- // NonNullTypesAttribute should not be set explicitly.
- arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNonNullTypesAttribute, arguments.AttributeSyntaxOpt.Location);
- }
}
internal override void PostDecodeWellKnownAttributes(ImmutableArray boundAttributes, ImmutableArray allAttributeSyntaxNodes, DiagnosticBag diagnostics, AttributeLocation symbolPart, WellKnownAttributeData decodedData)
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceTypeParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceTypeParameterSymbol.cs
index aff2acf7a46ed..f791484f996a4 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceTypeParameterSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceTypeParameterSymbol.cs
@@ -305,8 +305,8 @@ private bool ModifyCompilationForAttributeEmbedding()
private void CheckNullableAnnotationsInConstraints(DiagnosticBag diagnostics)
{
- if (this.ReferenceTypeConstraintIsNullable == true ||
- this.ConstraintTypesNoUseSiteDiagnostics.Any(c => c.ContainsNullableReferenceTypes()))
+ if ((this.HasReferenceTypeConstraint && this.ReferenceTypeConstraintIsNullable != null) ||
+ this.ConstraintTypesNoUseSiteDiagnostics.Any(c => c.NeedsNullableAttribute()))
{
DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, this.GetNonNullSyntaxNode().Location, ModifyCompilationForAttributeEmbedding());
}
@@ -361,11 +361,18 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeIsUnmanagedAttribute(this));
}
- if (this.ReferenceTypeConstraintIsNullable == true)
+ if (this.HasReferenceTypeConstraint && this.ReferenceTypeConstraintIsNullable != null)
{
+ NamedTypeSymbol byteType = DeclaringCompilation.GetSpecialType(SpecialType.System_Byte);
+ Debug.Assert((object)byteType != null);
+
AddSynthesizedAttribute(
ref attributes,
- moduleBuilder.SynthesizeNullableAttribute(WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctor, ImmutableArray.Empty));
+ moduleBuilder.SynthesizeNullableAttribute(WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorByte,
+ ImmutableArray.Create(new TypedConstant(byteType, TypedConstantKind.Primitive,
+ (byte)(this.ReferenceTypeConstraintIsNullable == true ?
+ NullableAnnotation.Nullable :
+ NullableAnnotation.NotNullable)))));
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceUserDefinedOperatorSymbolBase.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceUserDefinedOperatorSymbolBase.cs
index 25d5d69be297c..55e93abc9aca6 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceUserDefinedOperatorSymbolBase.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceUserDefinedOperatorSymbolBase.cs
@@ -654,7 +654,7 @@ internal sealed override void AfterAddingTypeMembersChecks(ConversionsBase conve
ParameterHelpers.EnsureIsReadOnlyAttributeExists(Parameters, diagnostics, modifyCompilation: true);
var location = ReturnTypeSyntax.Location;
- if (ReturnType.ContainsNullableReferenceTypes())
+ if (ReturnType.NeedsNullableAttribute())
{
this.DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: true);
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Symbol.cs b/src/Compilers/CSharp/Portable/Symbols/Symbol.cs
index 045e9635e6074..86633e660bc90 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Symbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Symbol.cs
@@ -631,16 +631,6 @@ internal virtual void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, re
{
}
- protected void AddSynthesizedNonNullTypesAttributeForMember(ref ArrayBuilder attributes)
- {
- bool? nonNullTypes = NonNullTypes;
- if (nonNullTypes.HasValue && nonNullTypes != ContainingType.NonNullTypes)
- {
- AddSynthesizedAttribute(ref attributes,
- DeclaringCompilation.TrySynthesizeNonNullTypesAttribute(nonNullTypes.GetValueOrDefault()));
- }
- }
-
///
/// Convenience helper called by subclasses to add a synthesized attribute to a collection of attributes.
///
diff --git a/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedAttributeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedAttributeSymbol.cs
deleted file mode 100644
index 91d996f9c5bbf..0000000000000
--- a/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedAttributeSymbol.cs
+++ /dev/null
@@ -1,42 +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.
-
-using System;
-using System.Collections.Immutable;
-
-namespace Microsoft.CodeAnalysis.CSharp.Symbols
-{
- ///
- /// Injected attribute symbols are injected early in the compilation, and so can be referenced in source.
- /// But we track their usage and only emit them if they are used.
- /// Their method bodies are always compiled, in case we do need to emit them.
- ///
- internal abstract class InjectedAttributeSymbol : SynthesizedEmbeddedAttributeSymbol
- {
- internal abstract void AddDiagnostics(DiagnosticBag recipient);
-
- // All the diagnostics involved in constructing this symbol will only be produced
- // if the symbol is referenced and so ends up emitted.
- // We collect all those diagnostics here.
- protected readonly DiagnosticBag _diagnostics;
-
- public InjectedAttributeSymbol(
- AttributeDescription description,
- NamespaceSymbol containingNamespace,
- CSharpCompilation compilation,
- Func> getConstructors)
- : this(description, containingNamespace, compilation, getConstructors, new DiagnosticBag())
- {
- }
-
- private InjectedAttributeSymbol(
- AttributeDescription description,
- NamespaceSymbol containingNamespace,
- CSharpCompilation compilation,
- Func> getConstructors,
- DiagnosticBag diagnostics)
- : base(description, containingNamespace, compilation, getConstructors, diagnostics)
- {
- _diagnostics = diagnostics;
- }
- }
-}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedEmbeddedAttributeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedEmbeddedAttributeSymbol.cs
deleted file mode 100644
index cb93f6014b241..0000000000000
--- a/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedEmbeddedAttributeSymbol.cs
+++ /dev/null
@@ -1,64 +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.
-
-using System;
-using System.Collections.Immutable;
-using System.Diagnostics;
-
-namespace Microsoft.CodeAnalysis.CSharp.Symbols
-{
- ///
- /// Makes the Microsoft.CodeAnalysis.EmbeddedAttribute available in every compilation.
- ///
- internal sealed class InjectedEmbeddedAttributeSymbol : InjectedAttributeSymbol
- {
- private InjectedEmbeddedAttributeSymbol(
- AttributeDescription description,
- NamespaceSymbol containingNamespace,
- CSharpCompilation compilation,
- Func> getConstructors)
- : base(description, containingNamespace, compilation, getConstructors)
- {
- }
-
- public static InjectedEmbeddedAttributeSymbol Create(NamespaceSymbol containingNamespace)
- {
- return new InjectedEmbeddedAttributeSymbol(AttributeDescription.CodeAnalysisEmbeddedAttribute, containingNamespace, containingNamespace.DeclaringCompilation, makeConstructor);
-
- ImmutableArray makeConstructor(CSharpCompilation compilation, NamedTypeSymbol containingType, DiagnosticBag diagnostics)
- {
- return ImmutableArray.Create(new EmbeddedAttributeConstructorSymbol(containingType));
- }
- }
-
- internal override AttributeUsageInfo GetAttributeUsageInfo()
- => new AttributeUsageInfo(validTargets: AttributeTargets.All, allowMultiple: false, inherited: false);
-
- internal override void AddDiagnostics(DiagnosticBag recipient)
- => recipient.AddRange(_diagnostics);
-
- private sealed class EmbeddedAttributeConstructorSymbol : SynthesizedInstanceConstructor
- {
- internal EmbeddedAttributeConstructorSymbol(NamedTypeSymbol containingType)
- : base(containingType)
- {
- Debug.Assert(containingType is InjectedEmbeddedAttributeSymbol);
- }
-
- public override ImmutableArray Parameters
- => ImmutableArray.Empty;
-
- internal override bool SynthesizesLoweredBoundBody
- => true;
-
- ///
- /// Note: this method captures diagnostics into the containing type (an injected attribute symbol) instead,
- /// as we don't yet know if the containing type will be emitted.
- ///
- internal override void GenerateMethodBody(TypeCompilationState compilationState, DiagnosticBag diagnostics)
- {
- var containingType = (InjectedEmbeddedAttributeSymbol)ContainingType;
- GenerateMethodBodyCore(compilationState, containingType._diagnostics);
- }
- }
- }
-}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedNonNullTypesAttributeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedNonNullTypesAttributeSymbol.cs
deleted file mode 100644
index 33f94ddd2887b..0000000000000
--- a/src/Compilers/CSharp/Portable/Symbols/Synthesized/InjectedNonNullTypesAttributeSymbol.cs
+++ /dev/null
@@ -1,130 +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.
-
-using System;
-using System.Collections.Generic;
-using System.Collections.Immutable;
-using System.Diagnostics;
-
-namespace Microsoft.CodeAnalysis.CSharp.Symbols
-{
- ///
- /// Makes the System.Runtime.CompilerServices.NonNullTypesAttribute available in every compilation.
- ///
- internal sealed class InjectedNonNullTypesAttributeSymbol : InjectedAttributeSymbol
- {
- private ImmutableArray _lazyCustomAttributes;
-
- private InjectedNonNullTypesAttributeSymbol(
- AttributeDescription description,
- NamespaceSymbol containingNamespace,
- CSharpCompilation compilation,
- Func> getConstructors)
- : base(description, containingNamespace, compilation, getConstructors)
- {
- }
-
- public static InjectedNonNullTypesAttributeSymbol Create(NamespaceSymbol containingNamespace)
- {
- return new InjectedNonNullTypesAttributeSymbol(AttributeDescription.NonNullTypesAttribute, containingNamespace, containingNamespace.DeclaringCompilation, makeConstructor);
-
- ImmutableArray makeConstructor(CSharpCompilation compilation, NamedTypeSymbol containingType, DiagnosticBag diagnostics)
- {
- var boolType = compilation.GetSpecialType(SpecialType.System_Boolean);
-
- Binder.ReportUseSiteDiagnostics(boolType, diagnostics, Location.None);
-
- var boolWithAnnotations = TypeSymbolWithAnnotations.Create(boolType);
- // https://github.com/dotnet/roslyn/issues/30143: Constructor should save the parameter into a field (for users of reflection)
- return ImmutableArray.Create(
- new NonNullTypesAttributeConstructorSymbol(
- containingType,
- m => ImmutableArray.Create(SynthesizedParameterSymbol.Create(m, boolWithAnnotations, 0, ConstantValue.True, name: "flag"))));
- }
- }
-
- internal override void AddDiagnostics(DiagnosticBag recipient)
- {
- // pull on the attributes to collect their diagnostics too
- _ = GetAttributes();
- recipient.AddRange(_diagnostics);
- }
-
- public override ImmutableArray GetAttributes()
- {
- if (_lazyCustomAttributes.IsDefault)
- {
- // https://github.com/dotnet/roslyn/issues/29732 A race condition can produce duplicate diagnostics here
- ImmutableInterlocked.InterlockedInitialize(ref _lazyCustomAttributes, MakeAttributes());
- }
- return _lazyCustomAttributes;
- }
-
- ///
- /// Adds an `[AttributeUsage(AttributeTargets.Class | ...)]` (if possible) and captures any diagnostics in the process.
- ///
- private ImmutableArray MakeAttributes()
- {
- var ctor = (MethodSymbol)Binder.GetWellKnownTypeMember(DeclaringCompilation, WellKnownMember.System_AttributeUsageAttribute__ctor, _diagnostics, Location.None);
- if (ctor is null)
- {
- // member is missing
- return ImmutableArray.Empty;
- }
-
- NamedTypeSymbol attributeTargets = DeclaringCompilation.GetWellKnownType(WellKnownType.System_AttributeTargets);
- Binder.ReportUseSiteDiagnostics(attributeTargets, _diagnostics, Location.None);
-
- var usage = new SynthesizedAttributeData(ctor,
- arguments: ImmutableArray.Create(new TypedConstant(attributeTargets, TypedConstantKind.Enum, attributeUsage)),
- namedArguments: ImmutableArray>.Empty);
-
- return ImmutableArray.Create(usage);
- }
-
- internal override AttributeUsageInfo GetAttributeUsageInfo()
- => new AttributeUsageInfo(validTargets: attributeUsage, allowMultiple: false, inherited: false);
-
- private const AttributeTargets attributeUsage =
- AttributeTargets.Class |
- AttributeTargets.Constructor |
- AttributeTargets.Delegate |
- AttributeTargets.Enum |
- AttributeTargets.Event |
- AttributeTargets.Field |
- AttributeTargets.Interface |
- AttributeTargets.Method |
- AttributeTargets.Module |
- AttributeTargets.Property |
- AttributeTargets.Struct;
-
- private sealed class NonNullTypesAttributeConstructorSymbol : SynthesizedInstanceConstructor
- {
- private readonly ImmutableArray _parameters;
-
- internal NonNullTypesAttributeConstructorSymbol(
- NamedTypeSymbol containingType,
- Func> getParameters) :
- base(containingType)
- {
- Debug.Assert(containingType is InjectedNonNullTypesAttributeSymbol);
- _parameters = getParameters(this);
- }
-
- public override ImmutableArray Parameters
- => _parameters;
-
- internal override bool SynthesizesLoweredBoundBody
- => true;
-
- ///
- /// Note: this method captures diagnostics into the containing type (an injected attribute symbol) instead,
- /// as we don't yet know if the containing type will be emitted.
- ///
- internal override void GenerateMethodBody(TypeCompilationState compilationState, DiagnosticBag diagnostics)
- {
- var containingType = (InjectedNonNullTypesAttributeSymbol)ContainingType;
- GenerateMethodBodyCore(compilationState, containingType._diagnostics);
- }
- }
- }
-}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedEmbeddedAttributeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedEmbeddedAttributeSymbol.cs
index 45346ce40eac4..6e2c322e38d51 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedEmbeddedAttributeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedEmbeddedAttributeSymbol.cs
@@ -33,19 +33,21 @@ internal class SynthesizedEmbeddedAttributeSymbol : NamedTypeSymbol
public SynthesizedEmbeddedAttributeSymbol(
AttributeDescription description,
CSharpCompilation compilation,
- Func> getAdditionalConstructors,
+ Func> getConstructors,
DiagnosticBag diagnostics)
{
_name = description.Name;
_baseType = MakeBaseType(compilation, diagnostics);
- var builder = ArrayBuilder.GetInstance();
- builder.Add(new SynthesizedEmbeddedAttributeConstructorSymbol(this, m => ImmutableArray.Empty));
- if (getAdditionalConstructors != null)
+ if (getConstructors != null)
+ {
+ _constructors = getConstructors(compilation, this, diagnostics);
+ }
+ else
{
- builder.AddRange(getAdditionalConstructors(compilation, this, diagnostics));
+ _constructors = ImmutableArray.Create(new SynthesizedEmbeddedAttributeConstructorSymbol(this, m => ImmutableArray.Empty));
}
- _constructors = builder.ToImmutableAndFree();
+
Debug.Assert(_constructors.Length == description.Signatures.Length);
_module = compilation.SourceModule;
@@ -190,12 +192,9 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
ref attributes,
moduleBuilder.Compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
- if (!DeclaringCompilation.GetWellKnownType(WellKnownType.Microsoft_CodeAnalysis_EmbeddedAttribute).IsErrorType())
- {
- AddSynthesizedAttribute(
- ref attributes,
- moduleBuilder.Compilation.TrySynthesizeAttribute(WellKnownMember.Microsoft_CodeAnalysis_EmbeddedAttribute__ctor));
- }
+ AddSynthesizedAttribute(
+ ref attributes,
+ moduleBuilder.SynthesizeEmbeddedAttribute());
}
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedFieldSymbolBase.cs b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedFieldSymbolBase.cs
index 5f7864dd9c986..35a59448458de 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedFieldSymbolBase.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedFieldSymbolBase.cs
@@ -67,7 +67,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
compilation.SynthesizeTupleNamesAttribute(Type.TypeSymbol));
}
- if (Type.ContainsNullableReferenceTypes())
+ if (Type.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, this.Type));
}
diff --git a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedInstanceConstructor.cs b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedInstanceConstructor.cs
index 3b70aad622de5..34dbac24f3345 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedInstanceConstructor.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedInstanceConstructor.cs
@@ -147,7 +147,7 @@ public override RefKind RefKind
public sealed override TypeSymbolWithAnnotations ReturnType
{
- get { return TypeSymbolWithAnnotations.Create(nonNullTypesContext: this, ContainingAssembly.GetSpecialType(SpecialType.System_Void)); }
+ get { return TypeSymbolWithAnnotations.Create(ContainingAssembly.GetSpecialType(SpecialType.System_Void)); }
}
public override ImmutableArray RefCustomModifiers
diff --git a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedParameterSymbol.cs
index b157d09485e2e..d1e5f33333b41 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedParameterSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedParameterSymbol.cs
@@ -159,7 +159,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
compilation.SynthesizeTupleNamesAttribute(type.TypeSymbol));
}
- if (Type.ContainsNullableReferenceTypes())
+ if (Type.NeedsNullableAttribute())
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullableAttribute(this, Type));
}
@@ -199,17 +199,6 @@ public static ParameterSymbol Create(
return new SynthesizedParameterSymbolWithCustomModifiers(container, type, ordinal, refKind, name, refCustomModifiers);
}
- public static ParameterSymbol Create(
- MethodSymbol container,
- TypeSymbolWithAnnotations type,
- int ordinal,
- ConstantValue defaultValue,
- string name = "")
- {
- Debug.Assert(defaultValue != null);
- return new SynthesizedParameterSymbolWithDefaultValue(container, type, ordinal, defaultValue, name);
- }
-
///
/// For each parameter of a source method, construct a corresponding synthesized parameter
/// for a destination method.
@@ -236,25 +225,6 @@ public override ImmutableArray RefCustomModifiers
get { return ImmutableArray.Empty; }
}
- private sealed class SynthesizedParameterSymbolWithDefaultValue : SynthesizedParameterSymbolBase
- {
- private readonly ConstantValue _defaultValue;
- public SynthesizedParameterSymbolWithDefaultValue(MethodSymbol container, TypeSymbolWithAnnotations type, int ordinal, ConstantValue defaultValue, string name)
- : base(container, type, ordinal, RefKind.None, name)
- {
- Debug.Assert(!defaultValue.IsBad);
- _defaultValue = defaultValue;
- }
-
- internal override bool IsMetadataOptional => true;
- internal override ConstantValue ExplicitDefaultConstantValue => _defaultValue;
-
- public override ImmutableArray RefCustomModifiers
- {
- get { return ImmutableArray.Empty; }
- }
- }
-
private sealed class SynthesizedParameterSymbolWithCustomModifiers : SynthesizedParameterSymbolBase
{
private readonly ImmutableArray _refCustomModifiers;
diff --git a/src/Compilers/CSharp/Portable/Symbols/Tuples/TupleTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Tuples/TupleTypeSymbol.cs
index 13feaadca1d23..4478e9e139eed 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Tuples/TupleTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Tuples/TupleTypeSymbol.cs
@@ -1430,14 +1430,14 @@ internal override bool IsInterface
}
}
- internal override void AddNullableTransforms(ArrayBuilder transforms)
+ internal override void AddNullableTransforms(ArrayBuilder transforms)
{
_underlyingType.AddNullableTransforms(transforms);
}
- internal override bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result)
+ internal override bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbol result)
{
- if (_underlyingType.ApplyNullableTransforms(transforms, nonNullTypesContext, ref position, out TypeSymbol underlying))
+ if (_underlyingType.ApplyNullableTransforms(defaultTransformFlag, transforms, ref position, out TypeSymbol underlying))
{
result = this.WithUnderlyingType((NamedTypeSymbol)underlying);
return true;
diff --git a/src/Compilers/CSharp/Portable/Symbols/TypeParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/TypeParameterSymbol.cs
index 51b361ae92a27..37f3c63106d0d 100644
--- a/src/Compilers/CSharp/Portable/Symbols/TypeParameterSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/TypeParameterSymbol.cs
@@ -667,11 +667,11 @@ public override int GetHashCode()
return Hash.Combine(ContainingSymbol, Ordinal);
}
- internal override void AddNullableTransforms(ArrayBuilder transforms)
+ internal override void AddNullableTransforms(ArrayBuilder transforms)
{
}
- internal override bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result)
+ internal override bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbol result)
{
result = this;
return true;
diff --git a/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs
index 1777c2e77ae7b..eff1c95406703 100644
--- a/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs
@@ -624,14 +624,14 @@ public virtual NamedTypeSymbol TupleUnderlyingType
///
internal abstract bool IsManagedType { get; }
- internal bool ContainsNullableReferenceTypes()
+ internal bool NeedsNullableAttribute()
{
- return TypeSymbolWithAnnotations.ContainsNullableReferenceTypes(typeWithAnnotationsOpt: default, typeOpt: this);
+ return TypeSymbolWithAnnotations.NeedsNullableAttribute(typeWithAnnotationsOpt: default, typeOpt: this);
}
- internal abstract void AddNullableTransforms(ArrayBuilder transforms);
+ internal abstract void AddNullableTransforms(ArrayBuilder transforms);
- internal abstract bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result);
+ internal abstract bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbol result);
internal abstract TypeSymbol SetUnknownNullabilityForReferenceTypes();
diff --git a/src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs b/src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
index 985f1980fa97f..93a843ed2c10b 100644
--- a/src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
@@ -1649,7 +1649,7 @@ internal static Cci.TypeReferenceWithAttributes GetTypeRefWithAttributes(
}
}
- if (type.ContainsNullableReferenceTypes())
+ if (type.NeedsNullableAttribute())
{
SynthesizedAttributeData attr = moduleBuilder.SynthesizeNullableAttribute(declaringSymbol, type);
if (attr != null)
diff --git a/src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs b/src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
index 4e6c4276d8dc2..e0e492c5f55f5 100644
--- a/src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/TypeSymbolWithAnnotations.cs
@@ -284,7 +284,7 @@ public TypeSymbolWithAnnotations SetIsAnnotated(CSharpCompilation compilation)
return CreateLazyNullableType(compilation, this);
}
- public TypeSymbolWithAnnotations AsNullableReferenceType(bool fromDeclaration) => _extensions.AsNullableReferenceType(this, fromDeclaration);
+ private TypeSymbolWithAnnotations AsNullableReferenceType() => _extensions.AsNullableReferenceType(this);
public TypeSymbolWithAnnotations AsNotNullableReferenceType() => _extensions.AsNotNullableReferenceType(this);
///
@@ -752,46 +752,58 @@ public bool Is(TypeParameterSymbol other)
public TypeSymbolWithAnnotations WithTypeAndModifiers(TypeSymbol typeSymbol, ImmutableArray customModifiers) =>
_extensions.WithTypeAndModifiers(this, typeSymbol, customModifiers);
- public bool ContainsNullableReferenceTypes()
+ public bool NeedsNullableAttribute()
{
- return ContainsNullableReferenceTypes(this, typeOpt: null);
+ return NeedsNullableAttribute(this, typeOpt: null);
}
- public static bool ContainsNullableReferenceTypes(
+ public static bool NeedsNullableAttribute(
TypeSymbolWithAnnotations typeWithAnnotationsOpt,
TypeSymbol typeOpt)
{
var type = TypeSymbolExtensions.VisitType(
typeWithAnnotationsOpt,
typeOpt,
- typeWithAnnotationsPredicateOpt: (t, a, b) => t.NullableAnnotation.IsAnyNullable() && !t.TypeSymbol.IsErrorType() && !t.TypeSymbol.IsValueType,
+ typeWithAnnotationsPredicateOpt: (t, a, b) => t.NullableAnnotation != NullableAnnotation.Unknown && !t.TypeSymbol.IsErrorType() && !t.TypeSymbol.IsValueType,
typePredicateOpt: null,
arg: (object)null);
return (object)type != null;
}
- public void AddNullableTransforms(ArrayBuilder transforms)
+ public void AddNullableTransforms(ArrayBuilder transforms)
{
var typeSymbol = TypeSymbol;
- transforms.Add(NullableAnnotation.IsAnyNullable() && !typeSymbol.IsValueType);
+ byte flag;
+
+ if (NullableAnnotation == NullableAnnotation.Unknown || typeSymbol.IsValueType)
+ {
+ flag = (byte)NullableAnnotation.Unknown;
+ }
+ else if (NullableAnnotation.IsAnyNullable())
+ {
+ flag = (byte)NullableAnnotation.Nullable;
+ }
+ else
+ {
+ flag = (byte)NullableAnnotation.NotNullable;
+ }
+
+ transforms.Add(flag);
typeSymbol.AddNullableTransforms(transforms);
}
- public bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbolWithAnnotations result)
+ public bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray transforms, ref int position, out TypeSymbolWithAnnotations result)
{
- Debug.Assert(nonNullTypesContext != null);
-
result = this;
- bool isAnnotated;
+ byte transformFlag;
if (transforms.IsDefault)
{
- // No explicit transforms. All reference types are unannotated.
- isAnnotated = false;
+ transformFlag = defaultTransformFlag;
}
else if (position < transforms.Length)
{
- isAnnotated = transforms[position++];
+ transformFlag = transforms[position++];
}
else
{
@@ -801,7 +813,7 @@ public bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTyp
TypeSymbol oldTypeSymbol = TypeSymbol;
TypeSymbol newTypeSymbol;
- if (!oldTypeSymbol.ApplyNullableTransforms(transforms, nonNullTypesContext, ref position, out newTypeSymbol))
+ if (!oldTypeSymbol.ApplyNullableTransforms(defaultTransformFlag, transforms, ref position, out newTypeSymbol))
{
return false;
}
@@ -811,17 +823,23 @@ public bool ApplyNullableTransforms(ImmutableArray transforms, INonNullTyp
result = result.WithTypeAndModifiers(newTypeSymbol, result.CustomModifiers);
}
- if (isAnnotated)
+ switch ((NullableAnnotation)transformFlag)
{
- result = result.AsNullableReferenceType(fromDeclaration: true);
- }
- else if (nonNullTypesContext.NonNullTypes == true)
- {
- result = result.AsNotNullableReferenceType();
- }
- else if (result.NullableAnnotation != NullableAnnotation.Unknown && (!result.NullableAnnotation.IsAnyNullable() || !oldTypeSymbol.IsValueType))
- {
- result = CreateNonLazyType(newTypeSymbol, NullableAnnotation.Unknown, result.CustomModifiers);
+ case NullableAnnotation.Nullable:
+ result = result.AsNullableReferenceType();
+ break;
+
+ case NullableAnnotation.NotNullable:
+ result = result.AsNotNullableReferenceType();
+ break;
+
+ default:
+ Debug.Assert((NullableAnnotation)transformFlag == NullableAnnotation.Unknown);
+ if (result.NullableAnnotation != NullableAnnotation.Unknown && (!result.NullableAnnotation.IsAnyNullable() || !oldTypeSymbol.IsNullableType()))
+ {
+ result = CreateNonLazyType(newTypeSymbol, NullableAnnotation.Unknown, result.CustomModifiers);
+ }
+ break;
}
return true;
@@ -930,7 +948,7 @@ internal static Extensions CreateLazy(CSharpCompilation compilation, TypeSymbolW
internal abstract TypeSymbol GetResolvedType(TypeSymbol defaultType);
internal abstract ImmutableArray CustomModifiers { get; }
- internal abstract TypeSymbolWithAnnotations AsNullableReferenceType(TypeSymbolWithAnnotations type, bool fromDeclaration);
+ internal abstract TypeSymbolWithAnnotations AsNullableReferenceType(TypeSymbolWithAnnotations type);
internal abstract TypeSymbolWithAnnotations AsNotNullableReferenceType(TypeSymbolWithAnnotations type);
internal abstract TypeSymbolWithAnnotations WithModifiers(TypeSymbolWithAnnotations type, ImmutableArray customModifiers);
@@ -1007,9 +1025,9 @@ internal override TypeSymbolWithAnnotations WithTypeAndModifiers(TypeSymbolWithA
return CreateNonLazyType(typeSymbol, type.NullableAnnotation, customModifiers);
}
- internal override TypeSymbolWithAnnotations AsNullableReferenceType(TypeSymbolWithAnnotations type, bool fromDeclaration)
+ internal override TypeSymbolWithAnnotations AsNullableReferenceType(TypeSymbolWithAnnotations type)
{
- return CreateNonLazyType(type._defaultType, fromDeclaration ? NullableAnnotation.Nullable : NullableAnnotation.NullableBasedOnAnalysis, _customModifiers);
+ return CreateNonLazyType(type._defaultType, NullableAnnotation.Nullable, _customModifiers);
}
internal override TypeSymbolWithAnnotations AsNotNullableReferenceType(TypeSymbolWithAnnotations type)
@@ -1143,7 +1161,7 @@ internal override TypeSymbolWithAnnotations WithTypeAndModifiers(TypeSymbolWithA
return CreateNonLazyType(typeSymbol, type.NullableAnnotation, customModifiers);
}
- internal override TypeSymbolWithAnnotations AsNullableReferenceType(TypeSymbolWithAnnotations type, bool fromDeclaration)
+ internal override TypeSymbolWithAnnotations AsNullableReferenceType(TypeSymbolWithAnnotations type)
{
return type;
}
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
index 2605040599b67..fb84069a096e3 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
index 2e477a60b1e99..f96c357011abe 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<NULL>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
index 17201e1983248..ccefb78c82501 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<NULL>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
index 76eeb27a227e0..b7213bb5a887e 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<Null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
index 8d5bc57003923..c723442e4045c 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<Null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
index 3a5acdb049b17..bf6f2db55e5e4 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
index 20d9442eb1467..52df09b8cf973 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
index 4ece8f73e92f4..27c25bfaeb56e 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
index 929b19b9142f1..d0e823f3ecc79 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<nulo>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
index 3131e671ed5ce..6465bd1994ca7 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<NULL>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
index 35b7df1794bfa..566349b569b99 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
index 3210e370e7eaa..7ca3a8c796d1a 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<null>
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
index 46b613200da94..42f0afffe55d6 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
@@ -47,11 +47,6 @@
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
-
-
- Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
-
-
Explicit application of 'System.Runtime.CompilerServices.NullableAttribute' is not allowed.
@@ -307,11 +302,6 @@
unmanaged generic type constraints
-
-
- injected declaration
-
-
<null>
diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Embedded.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Embedded.cs
index b6f01e6239dd0..03306fdbb17ed 100644
--- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Embedded.cs
+++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Embedded.cs
@@ -10,70 +10,6 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
public class AttributeTests_Embedded : CSharpTestBase
{
- [Fact]
- public void EmbeddedAttributeNamespace()
- {
- var code = @"
-namespace Microsoft.CodeAnalysis.EmbeddedAttribute
-{
- class C { }
-}
-namespace TestReference
-{
- [Microsoft.CodeAnalysis.Embedded]
- internal class TestType1 { }
-
- [Microsoft.CodeAnalysis.EmbeddedAttribute]
- internal class TestType2 { }
-
- internal class TestType3 { }
-}";
-
- CreateCompilation(code).VerifyEmitDiagnostics(
- // (11,29): error CS0616: 'Microsoft.CodeAnalysis.EmbeddedAttribute' is not an attribute class
- // [Microsoft.CodeAnalysis.EmbeddedAttribute]
- Diagnostic(ErrorCode.ERR_NotAnAttributeClass, "EmbeddedAttribute").WithArguments("Microsoft.CodeAnalysis.EmbeddedAttribute").WithLocation(11, 29),
- // (8,29): error CS0616: 'Microsoft.CodeAnalysis.EmbeddedAttribute' is not an attribute class
- // [Microsoft.CodeAnalysis.Embedded]
- Diagnostic(ErrorCode.ERR_NotAnAttributeClass, "Embedded").WithArguments("Microsoft.CodeAnalysis.EmbeddedAttribute").WithLocation(8, 29)
- );
- }
-
- [Fact]
- public void EmbeddedAttributeInMergedNamespaceSymbol()
- {
- var reference_cs = @"
-namespace Microsoft.CodeAnalysis
-{
- internal class EmbeddedAttribute : System.Attribute { }
-}";
- var reference = CreateCompilation(new[] { reference_cs });
- reference.VerifyDiagnostics();
-
- var comp_cs = @"
-namespace Microsoft.CodeAnalysis.EmbeddedAttribute
-{
- class C { }
-}";
- var comp = CreateCompilation(new[] { comp_cs }, options: WithNonNullTypesTrue(), references: new[] { reference.ToMetadataReference() });
- comp.VerifyEmitDiagnostics(
- // error CS0518: Predefined type 'Microsoft.CodeAnalysis.EmbeddedAttribute' is not defined or imported
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("Microsoft.CodeAnalysis.EmbeddedAttribute").WithLocation(1, 1)
- );
-
- var type = comp.GetWellKnownType(WellKnownType.Microsoft_CodeAnalysis_EmbeddedAttribute);
- Assert.True(type.IsErrorType());
-
- // https://github.com/dotnet/roslyn/issues/29683 CSharpCompilation.AbstractSymbolSearcher needs to inject namespaces and types too
- //Assert.False(comp.ContainsSymbolsWithName("NonNullTypesAttribute", SymbolFilter.Type));
- //Assert.Empty(comp.GetSymbolsWithName("NonNullTypesAttribute", SymbolFilter.Type));
- //Assert.Empty(comp.GetSymbolsWithName(n => n == "NonNullTypesAttribute", SymbolFilter.Type));
-
- //Assert.True(comp.ContainsSymbolsWithName("EmbeddedAttribute", SymbolFilter.Type));
- //Assert.Equal("Microsoft.CodeAnalysis.EmbeddedAttribute", comp.GetSymbolsWithName("EmbeddedAttribute", SymbolFilter.Type).Single().ToTestDisplayString());
- //Assert.Equal("Microsoft.CodeAnalysis.EmbeddedAttribute", comp.GetSymbolsWithName(n => n == "EmbeddedAttribute", SymbolFilter.Type).Single().ToTestDisplayString());
- }
-
[Fact]
public void ReferencingEmbeddedAttributesFromTheSameAssemblySucceeds()
{
@@ -178,8 +114,6 @@ public static void Main()
}";
CreateCompilation(code, references: new[] { reference }, assemblyName: "Source").VerifyDiagnostics(
- // error CS0101: The namespace 'Microsoft.CodeAnalysis' already contains a definition for 'EmbeddedAttribute'
- Diagnostic(ErrorCode.ERR_DuplicateNameInNS).WithArguments("EmbeddedAttribute", "Microsoft.CodeAnalysis").WithLocation(1, 1),
// (6,38): error CS0234: The type or namespace name 'TestType1' does not exist in the namespace 'TestReference' (are you missing an assembly reference?)
// var obj1 = new TestReference.TestType1();
Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "TestType1").WithArguments("TestType1", "TestReference").WithLocation(6, 38),
@@ -298,8 +232,8 @@ public void M(in int p)
}";
CreateCompilation(code, references: new[] { moduleRef }).VerifyEmitDiagnostics(
- // error CS0101: The namespace 'Microsoft.CodeAnalysis' already contains a definition for 'EmbeddedAttribute'
- Diagnostic(ErrorCode.ERR_DuplicateNameInNS).WithArguments("EmbeddedAttribute", "Microsoft.CodeAnalysis").WithLocation(1, 1));
+ // error CS8004: Type 'EmbeddedAttribute' exported from module 'testModule.netmodule' conflicts with type declared in primary module of this assembly.
+ Diagnostic(ErrorCode.ERR_ExportedTypeConflictsWithDeclaration).WithArguments("Microsoft.CodeAnalysis.EmbeddedAttribute", "testModule.netmodule").WithLocation(1, 1));
}
[Fact]
@@ -322,12 +256,8 @@ public void M(in int p)
}";
CreateCompilation(code, references: new[] { reference }).VerifyEmitDiagnostics(
- // (2,99): warning CS0436: The type 'EmbeddedAttribute' in 'injected declaration' conflicts with the imported type 'EmbeddedAttribute' in 'reference, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'injected declaration'.
- // [assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(Microsoft.CodeAnalysis.EmbeddedAttribute))]
- Diagnostic(ErrorCode.WRN_SameFullNameThisAggAgg, "EmbeddedAttribute").WithArguments("injected declaration", "Microsoft.CodeAnalysis.EmbeddedAttribute", "reference, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "Microsoft.CodeAnalysis.EmbeddedAttribute").WithLocation(2, 99),
- // (2,12): error CS0729: Type 'EmbeddedAttribute' is defined in this assembly, but a type forwarder is specified for it
- // [assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(Microsoft.CodeAnalysis.EmbeddedAttribute))]
- Diagnostic(ErrorCode.ERR_ForwardedTypeInThisAssembly, "System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(Microsoft.CodeAnalysis.EmbeddedAttribute))").WithArguments("Microsoft.CodeAnalysis.EmbeddedAttribute").WithLocation(2, 12));
+ // error CS8006: Forwarded type 'EmbeddedAttribute' conflicts with type declared in primary module of this assembly.
+ Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithDeclaration).WithArguments("Microsoft.CodeAnalysis.EmbeddedAttribute").WithLocation(1, 1));
}
[Fact]
@@ -382,6 +312,8 @@ public void M(in object x) { } // should trigger synthesizing IsReadOnly
}";
CreateEmptyCompilation(code).VerifyEmitDiagnostics(CodeAnalysis.Emit.EmitOptions.Default.WithRuntimeMetadataVersion("v4.0.30319"),
+ // error CS0518: Predefined type 'System.Attribute' is not defined or imported
+ Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Attribute").WithLocation(1, 1),
// error CS0518: Predefined type 'System.Attribute' is not defined or imported
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Attribute").WithLocation(1, 1));
}
@@ -449,6 +381,8 @@ public class Test
// public class Test
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "Test").WithArguments("System.Void").WithLocation(7, 14),
// error CS0518: Predefined type 'System.Void' is not defined or imported
+ Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Void").WithLocation(1, 1),
+ // error CS0518: Predefined type 'System.Void' is not defined or imported
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Void").WithLocation(1, 1));
}
@@ -471,6 +405,8 @@ public void M(in object x) { } // should trigger synthesizing IsReadOnly
}";
CreateEmptyCompilation(code).VerifyEmitDiagnostics(CodeAnalysis.Emit.EmitOptions.Default.WithRuntimeMetadataVersion("v4.0.30319"),
+ // error CS1729: 'Attribute' does not contain a constructor that takes 0 arguments
+ Diagnostic(ErrorCode.ERR_BadCtorArgCount).WithArguments("System.Attribute", "0").WithLocation(1, 1),
// error CS1729: 'Attribute' does not contain a constructor that takes 0 arguments
Diagnostic(ErrorCode.ERR_BadCtorArgCount).WithArguments("System.Attribute", "0").WithLocation(1, 1));
}
diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_NonNullTypes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_NonNullTypes.cs
deleted file mode 100644
index 9d2d77534492b..0000000000000
--- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_NonNullTypes.cs
+++ /dev/null
@@ -1,445 +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.
-
-using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
-using Microsoft.CodeAnalysis.Test.Utilities;
-using System.Collections.Immutable;
-using System.Linq;
-using Xunit;
-
-namespace Microsoft.CodeAnalysis.CSharp.UnitTests
-{
- public class AttributeTests_NonNullTypes : CSharpTestBase
- {
- [Fact]
- public void EmbeddedAttributeInAddedModule()
- {
- var module = CreateCompilation(@"
-namespace Microsoft.CodeAnalysis
-{
- public class EmbeddedAttribute : System.Attribute { }
-}
-", options: TestOptions.ReleaseModule);
-
- var reference = ModuleMetadata.CreateFromImage(module.EmitToArray()).GetReference();
-
- var comp = CreateCompilation("", options: WithNonNullTypesTrue(), references: new[] { reference });
- comp.VerifyEmitDiagnostics(
- // error CS0101: The namespace 'Microsoft.CodeAnalysis' already contains a definition for 'EmbeddedAttribute'
- Diagnostic(ErrorCode.ERR_DuplicateNameInNS).WithArguments("EmbeddedAttribute", "Microsoft.CodeAnalysis").WithLocation(1, 1)
- );
- }
-
- [Fact(Skip = "https://github.com/dotnet/roslyn/issues/29732 Crashing")]
- public void EmbeddedAttributeInAddedModule_Injected()
- {
- var code = "[module: System.Runtime.CompilerServices.NonNullTypes]";
-
- var module = CreateCompilation(code, options: TestOptions.ReleaseModule);
- var reference = ModuleMetadata.CreateFromImage(module.EmitToArray()).GetReference();
-
- var comp = CreateCompilation(code, references: new[] { reference });
- comp.VerifyEmitDiagnostics();
- }
-
- [Fact]
- public void NonNullTypesAttributeNamespace()
- {
- var code = @"
-namespace System.Runtime.CompilerServices.NonNullTypesAttribute
-{
- class C { }
-}
-
-[System.Runtime.CompilerServices.NonNullTypes]
-internal class TestType1 { }
-
-[System.Runtime.CompilerServices.NonNullTypesAttribute(false)]
-internal class TestType2 { }
-";
-
- var comp = CreateCompilation(code);
- comp.VerifyEmitDiagnostics(
- // (7,34): error CS0616: 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not an attribute class
- // [System.Runtime.CompilerServices.NonNullTypes]
- Diagnostic(ErrorCode.ERR_NotAnAttributeClass, "NonNullTypes").WithArguments("System.Runtime.CompilerServices.NonNullTypesAttribute").WithLocation(7, 34),
- // (10,34): error CS0616: 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not an attribute class
- // [System.Runtime.CompilerServices.NonNullTypesAttribute(false)]
- Diagnostic(ErrorCode.ERR_NotAnAttributeClass, "NonNullTypesAttribute").WithArguments("System.Runtime.CompilerServices.NonNullTypesAttribute").WithLocation(10, 34)
- );
- Assert.Equal(SymbolKind.Namespace, comp.GlobalNamespace.GetMember("System.Runtime.CompilerServices.NonNullTypesAttribute").Kind);
- }
-
- [Fact]
- public void NonNullTypesAttributeNamespace_WithoutUsage()
- {
- var code = @"
-namespace System.Runtime.CompilerServices.NonNullTypesAttribute
-{
- class C { }
-}
-";
-
- var comp = CreateCompilation(code);
- comp.VerifyEmitDiagnostics();
- Assert.Equal(SymbolKind.Namespace, comp.GlobalNamespace.GetMember("System.Runtime.CompilerServices.NonNullTypesAttribute").Kind);
- }
-
- [Fact]
- public void ReferencingNonNullTypesAttributesFromTheSameAssemblyAllowed()
- {
- var code = @"
-namespace System.Runtime.CompilerServices
-{
- internal class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}
-
-" + NonNullTypesOn() + @"
-internal class TestType1 { }
-
-" + NonNullTypesOff() + @"
-internal class TestType2 { }
-";
-
- var comp = CreateCompilation(code);
- comp.VerifyEmitDiagnostics();
- Assert.True(comp.GetMember("TestType1").NonNullTypes);
- Assert.False(comp.GetMember("TestType2").NonNullTypes);
-
- CompileAndVerify(comp, symbolValidator:
- (m) =>
- {
- Assert.Same(m, m.GlobalNamespace.GetMember("TestType1").GetAttributes().Single().AttributeClass.ContainingModule);
- Assert.Same(m, m.GlobalNamespace.GetMember("TestType2").GetAttributes().Single().AttributeClass.ContainingModule);
- Assert.True(m.GlobalNamespace.GetMember("TestType1").NonNullTypes);
- Assert.False(m.GlobalNamespace.GetMember("TestType2").NonNullTypes);
- }
- );
-
- }
-
- [Fact]
- public void CannotReferenceNonNullTypesAttributesFromADifferentAssembly_Internal()
- {
- var reference = CreateCompilation(@"
-[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute(""Source"")]
-namespace System.Runtime.CompilerServices
-{
- internal class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}
-
-" + NonNullTypesOn() + @"
-internal class TestType1 { }
-
-" + NonNullTypesOn() + @"
-internal class TestType2 { }
-");
- Assert.Empty(reference.GetMember("TestType1").GetAttributes());
- Assert.Empty(reference.GetMember("TestType2").GetAttributes());
-
- CompileAndVerify(reference, symbolValidator:
- (m) =>
- {
- Assert.Same(m, m.GlobalNamespace.GetMember("TestType1").GetAttributes().Single().AttributeClass.ContainingModule);
- Assert.Same(m, m.GlobalNamespace.GetMember("TestType2").GetAttributes().Single().AttributeClass.ContainingModule);
- }
- );
-
- // NonNullTypesAttribute from referenced assembly is ignored, and we use the injected one instead
- var comp = CreateCompilation("", options: WithNonNullTypesTrue(), references: new[] { reference.ToMetadataReference() }, assemblyName: "Source");
- comp.VerifyDiagnostics();
- Assert.Empty(comp.SourceModule.GetAttributes());
- Assert.True(comp.SourceModule.NonNullTypes);
-
- CompileAndVerify(comp, symbolValidator:
- (m) =>
- {
- Assert.Same(m, m.GetAttributes().Single().AttributeClass.ContainingModule);
- Assert.True(m.NonNullTypes);
- }
- );
-
- var system = (INamespaceSymbol)comp.GlobalNamespace.GetMember("System");
- Assert.Equal(NamespaceKind.Compilation, system.NamespaceKind);
- }
-
- [Fact]
- public void CannotReferenceNonNullTypesAttributesFromADifferentAssembly_Module()
- {
- var module = CreateCompilation(@"
-namespace System.Runtime.CompilerServices
-{
- internal class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}
-
-" + NonNullTypesOn() + @"
-internal class TestType1 { }
-", options: TestOptions.ReleaseModule);
-
- var reference = ModuleMetadata.CreateFromImage(module.EmitToArray()).GetReference();
-
- var system = (INamespaceSymbol)module.GlobalNamespace.GetMember("System");
- Assert.Equal(NamespaceKind.Compilation, system.NamespaceKind);
-
- // NonNullTypesAttribute from module conflicts with injected symbol
- var comp = CreateCompilation("", references: new[] { reference }, options: WithNonNullTypesTrue());
- comp.VerifyDiagnostics(
- // error CS0101: The namespace 'System.Runtime.CompilerServices' already contains a definition for 'NonNullTypesAttribute'
- Diagnostic(ErrorCode.ERR_DuplicateNameInNS).WithArguments("NonNullTypesAttribute", "System.Runtime.CompilerServices").WithLocation(1, 1)
- );
-
- system = (INamespaceSymbol)comp.GlobalNamespace.GetMember("System");
- Assert.Equal(NamespaceKind.Compilation, system.NamespaceKind);
- }
-
- [Fact]
- public void CannotReferenceNonNullTypesAttributesFromADifferentAssembly_Public()
- {
- var reference = CreateCompilation(@"
-namespace System.Runtime.CompilerServices
-{
- public class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}
-
-" + NonNullTypesOn() + @"
-public class TestType1 { }
-
-" + NonNullTypesOff() + @"
-public class TestType2 { }
-");
- Assert.Empty(reference.GetMember("TestType1").GetAttributes());
- Assert.Empty(reference.GetMember("TestType2").GetAttributes());
-
- CompileAndVerify(reference, symbolValidator:
- (m) =>
- {
- Assert.Same(m, m.GlobalNamespace.GetMember("TestType1").GetAttributes().Single().AttributeClass.ContainingModule);
- Assert.Same(m, m.GlobalNamespace.GetMember("TestType2").GetAttributes().Single().AttributeClass.ContainingModule);
- });
-
- // NonNullTypesAttribute from referenced assembly is ignored, and we use the injected one instead
- var comp = CreateCompilation("", references: new[] { reference.ToMetadataReference() }, options: WithNonNullTypesTrue());
-
- Assert.Empty(comp.SourceModule.GetAttributes());
-
- CompileAndVerify(comp, symbolValidator:
- (m) =>
- {
- Assert.Same(m, m.GetAttributes().Single().AttributeClass.ContainingModule);
- Assert.True(m.NonNullTypes);
- });
- }
-
- [Fact]
- public void CanReferenceNonNullTypesAttributesFromADifferentAssemblyViaExternDeclaration()
- {
- var reference = CreateCompilation(@"
-namespace System.Runtime.CompilerServices
-{
- public class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}
-
-[System.Runtime.CompilerServices.NonNullTypes]
-public class TestType1 { }
-");
- Assert.False(reference.GetMember("TestType1").GetAttributes().Single().AttributeClass.IsImplicitlyDeclared);
-
- var code = @"
-extern alias Reference;
-[module: Reference::System.Runtime.CompilerServices.NonNullTypes]
-class C
-{
- string? M() => throw null;
-}
-";
-
- var comp = CreateCompilation(code, references: new[] { reference.ToMetadataReference(aliases: ImmutableArray.Create("Reference")) });
- comp.VerifyDiagnostics(
- // (3,10): error CS8635: Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
- // [module: Reference::System.Runtime.CompilerServices.NonNullTypes]
- Diagnostic(ErrorCode.ERR_ExplicitNonNullTypesAttribute, "Reference::System.Runtime.CompilerServices.NonNullTypes").WithLocation(3, 10),
- // (6,11): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
- // string? M() => throw null;
- Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(6, 11)
- );
-
- Assert.False(comp.SourceModule.GetAttributes().Single().AttributeClass.IsImplicitlyDeclared);
- Assert.Null(comp.SourceModule.NonNullTypes);
- }
-
- [Fact]
- public void NonNullTypesAttributeInSourceCanBeUsed()
- {
- var code = @"
-namespace System.Runtime.CompilerServices
-{
- public class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}";
-
- var comp = CreateCompilation(code, options: WithNonNullTypesTrue());
- comp.VerifyEmitDiagnostics();
- Assert.True(comp.SourceModule.NonNullTypes);
- Assert.Empty(comp.SourceModule.GetAttributes());
- Assert.True(comp.SourceModule.NonNullTypes);
-
- CompileAndVerify(comp, symbolValidator:
- (m) =>
- {
- Assert.Same(m, m.GetAttributes().Single().AttributeClass.ContainingModule);
- Assert.True(m.NonNullTypes);
- });
- }
-
- [Fact]
- public void NonNullTypesAttributeInReferencedModuleCausesAmbiguity()
- {
- var module = CreateCompilation(options: TestOptions.ReleaseModule, assemblyName: "testModule", source: @"
-namespace System.Runtime.CompilerServices
-{
- public class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}");
-
- var moduleRef = ModuleMetadata.CreateFromImage(module.EmitToArray()).GetReference();
-
- var code = " /* NonNullTypesAttribute is injected when not present in source */ ";
-
- // It is okay to have a duplicate of NonNullTypesAttribute when the other comes from a referenced assembly,
- // but we don't allow such collisions when the other comes from an added module.
-
- CreateCompilation(code, references: new[] { moduleRef }).VerifyEmitDiagnostics(
- // error CS0101: The namespace 'System.Runtime.CompilerServices' already contains a definition for 'NonNullTypesAttribute'
- Diagnostic(ErrorCode.ERR_DuplicateNameInNS).WithArguments("NonNullTypesAttribute", "System.Runtime.CompilerServices").WithLocation(1, 1));
- }
-
- [Fact]
- public void NonNullTypesAttributeForwardedToAnotherAssemblyShouldTriggerAnError()
- {
- var reference = CreateCompilation(@"
-namespace System.Runtime.CompilerServices
-{
- public class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}", assemblyName: "reference").ToMetadataReference();
-
- var code = @"
-[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(System.Runtime.CompilerServices.NonNullTypesAttribute))]
-
-
-
-class Test
-{
-}";
-
- CreateCompilation(code, references: new[] { reference },
- options: WithNonNullTypesTrue() // This should trigger generating another NonNullTypesAttribute
- ).VerifyEmitDiagnostics(
- // (2,12): error CS0729: Type 'NonNullTypesAttribute' is defined in this assembly, but a type forwarder is specified for it
- // [assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(System.Runtime.CompilerServices.NonNullTypesAttribute))]
- Diagnostic(ErrorCode.ERR_ForwardedTypeInThisAssembly, "System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(System.Runtime.CompilerServices.NonNullTypesAttribute))").WithArguments("System.Runtime.CompilerServices.NonNullTypesAttribute").WithLocation(2, 12));
- }
-
- [Fact]
- public void CompilerShouldIgnorePublicNonNullTypesAttributesInReferencedAssemblies()
- {
- var reference = CreateCompilation(assemblyName: "testRef", source: @"
-namespace System.Runtime.CompilerServices
-{
- public class NonNullTypesAttribute : System.Attribute { public NonNullTypesAttribute(bool flag = true) { } }
-}
-public class C { }
-").ToMetadataReference();
-
- var code = @"
-public class D : C { }";
-
- CompileAndVerify(code, options: WithNonNullTypesTrue(), references: new[] { reference }, symbolValidator: module =>
- {
- var attributeName = AttributeDescription.NonNullTypesAttribute.FullName;
-
- var referenceAttribute = module.GetReferencedAssemblySymbols().Single(assembly => assembly.Name == "testRef").GetTypeByMetadataName(attributeName);
- Assert.NotNull(referenceAttribute);
-
- var generatedAttribute = module.ContainingAssembly.GetTypeByMetadataName(attributeName);
- Assert.NotNull(generatedAttribute);
-
- Assert.False(referenceAttribute.Equals(generatedAttribute));
- });
- }
-
- [Fact]
- public void SynthesizingAttributeRequiresSystemAttribute_NoSystemAttribute()
- {
- var code = @"
-
-
-namespace System
-{
- public class Object {}
- public class Void {}
-}";
-
- CreateEmptyCompilation(code,
- options: WithNonNullTypesTrue() // This should trigger generating NonNullTypesAttribute
- ).VerifyEmitDiagnostics(CodeAnalysis.Emit.EmitOptions.Default.WithRuntimeMetadataVersion("v4.0.30319"),
- // error CS0518: Predefined type 'System.Boolean' is not defined or imported
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Boolean").WithLocation(1, 1),
- // error CS0518: Predefined type 'System.Attribute' is not defined or imported
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Attribute").WithLocation(1, 1),
- // error CS0518: Predefined type 'System.Boolean' is not defined or imported
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Boolean").WithLocation(1, 1),
- // error CS0656: Missing compiler required member 'System.AttributeUsageAttribute..ctor'
- Diagnostic(ErrorCode.ERR_MissingPredefinedMember).WithArguments("System.AttributeUsageAttribute", ".ctor").WithLocation(1, 1),
- // error CS0518: Predefined type 'System.Attribute' is not defined or imported
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Attribute").WithLocation(1, 1),
- // error CS0518: Predefined type 'System.Attribute' is not defined or imported
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Attribute").WithLocation(1, 1));
- }
-
- [Fact]
- public void SynthesizingAttributeRequiresSystemAttribute_NoSystemObject()
- {
- var code = @"
-
-
-namespace System
-{
- public class Attribute {}
- public class Void {}
-}";
-
- CreateEmptyCompilation(code,
- options: WithNonNullTypesTrue() // This should trigger generating NonNullTypesAttribute
- ).VerifyEmitDiagnostics(CodeAnalysis.Emit.EmitOptions.Default.WithRuntimeMetadataVersion("v4.0.30319"),
- // (6,18): error CS0518: Predefined type 'System.Object' is not defined or imported
- // public class Attribute {}
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "Attribute").WithArguments("System.Object").WithLocation(6, 18),
- // (7,18): error CS0518: Predefined type 'System.Object' is not defined or imported
- // public class Void {}
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "Void").WithArguments("System.Object").WithLocation(7, 18),
- // (6,18): error CS1729: 'object' does not contain a constructor that takes 0 arguments
- // public class Attribute {}
- Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Attribute").WithArguments("object", "0").WithLocation(6, 18),
- // (7,18): error CS1729: 'object' does not contain a constructor that takes 0 arguments
- // public class Void {}
- Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Void").WithArguments("object", "0").WithLocation(7, 18));
- }
-
- [Fact]
- public void SynthesizingAttributeRequiresSystemAttribute_NoSystemVoid()
- {
- var code = @"
-
-
-namespace System
-{
- public class Attribute {}
- public class Object {}
-}";
-
- CreateEmptyCompilation(code,
- options: WithNonNullTypesTrue() // This should trigger generating NonNullTypesAttribute
- ).VerifyEmitDiagnostics(CodeAnalysis.Emit.EmitOptions.Default.WithRuntimeMetadataVersion("v4.0.30319"),
- // (6,18): error CS0518: Predefined type 'System.Void' is not defined or imported
- // public class Attribute {}
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "Attribute").WithArguments("System.Void").WithLocation(6, 18));
- }
- }
-}
diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Nullable.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Nullable.cs
index 5de959c452062..8f4232d1da2cf 100644
--- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Nullable.cs
+++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Nullable.cs
@@ -34,8 +34,8 @@ public void ExplicitAttributeFromSource()
{
public sealed class NullableAttribute : Attribute
{
- public NullableAttribute() { }
- public NullableAttribute(bool[] b) { }
+ public NullableAttribute(byte a) { }
+ public NullableAttribute(byte[] b) { }
}
}
class C
@@ -54,8 +54,8 @@ public void ExplicitAttributeFromMetadata()
{
public sealed class NullableAttribute : Attribute
{
- public NullableAttribute() { }
- public NullableAttribute(bool[] b) { }
+ public NullableAttribute(byte a) { }
+ public NullableAttribute(byte[] b) { }
}
}";
var comp0 = CreateCompilation(source0, parseOptions: TestOptions.Regular7);
@@ -78,7 +78,7 @@ public void ExplicitAttribute_MissingParameterlessConstructor()
{
public sealed class NullableAttribute : Attribute
{
- public NullableAttribute(bool[] b) { }
+ public NullableAttribute(byte[] b) { }
}
}
class C
@@ -87,6 +87,9 @@ static void F(object? x, object?[] y) { }
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue(), parseOptions: TestOptions.Regular8);
comp.VerifyEmitDiagnostics(
+ // (5,34): error CS0656: Missing compiler required member 'System.Runtime.CompilerServices.NullableAttribute..ctor'
+ // public NullableAttribute(byte[] b) { }
+ Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "byte[] b").WithArguments("System.Runtime.CompilerServices.NullableAttribute", ".ctor").WithLocation(5, 34),
// (10,19): error CS0656: Missing compiler required member 'System.Runtime.CompilerServices.NullableAttribute..ctor'
// static void F(object? x, object?[] y) { }
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "object? x").WithArguments("System.Runtime.CompilerServices.NullableAttribute", ".ctor").WithLocation(10, 19),
@@ -137,6 +140,9 @@ static void F(object? x, object?[] y) { }
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue(), parseOptions: TestOptions.Regular8);
comp.VerifyEmitDiagnostics(
+ // (5,34): error CS0656: Missing compiler required member 'System.Runtime.CompilerServices.NullableAttribute..ctor'
+ // public NullableAttribute(string[] b) { }
+ Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "string[] b").WithArguments("System.Runtime.CompilerServices.NullableAttribute", ".ctor").WithLocation(5, 34),
// (10,19): error CS0656: Missing compiler required member 'System.Runtime.CompilerServices.NullableAttribute..ctor'
// static void F(object? x, object?[] y) { }
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "object? x").WithArguments("System.Runtime.CompilerServices.NullableAttribute", ".ctor").WithLocation(10, 19),
@@ -146,7 +152,7 @@ static void F(object? x, object?[] y) { }
}
[Fact]
- public void NullableAttribute_MissingBoolean()
+ public void NullableAttribute_MissingByte()
{
var source0 =
@"namespace System
@@ -174,8 +180,8 @@ public class Attribute
// (3,11): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// object? F() => null;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(3, 11),
- // error CS0518: Predefined type 'System.Boolean' is not defined or imported
- Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Boolean").WithLocation(1, 1));
+ // error CS0518: Predefined type 'System.Byte' is not defined or imported
+ Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Byte").WithLocation(1, 1));
}
[Fact]
@@ -187,7 +193,7 @@ public void NullableAttribute_MissingAttribute()
public class Object { }
public abstract class ValueType { }
public struct Void { }
- public struct Boolean { }
+ public struct Byte { }
}";
var comp0 = CreateEmptyCompilation(source0, parseOptions: TestOptions.Regular7);
var ref0 = comp0.EmitToImageReference();
@@ -206,6 +212,8 @@ public struct Boolean { }
// object? F() => null;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(3, 11),
// error CS0518: Predefined type 'System.Attribute' is not defined or imported
+ Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Attribute").WithLocation(1, 1),
+ // error CS0518: Predefined type 'System.Attribute' is not defined or imported
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Attribute").WithLocation(1, 1));
}
@@ -219,6 +227,7 @@ public class Object { }
public abstract class ValueType { }
public struct Void { }
public struct Boolean { }
+ public struct Byte { }
public class Attribute
{
static Attribute() { }
@@ -244,6 +253,8 @@ public Attribute(object o) { }
// error CS1729: 'Attribute' does not contain a constructor that takes 0 arguments
Diagnostic(ErrorCode.ERR_BadCtorArgCount).WithArguments("System.Attribute", "0").WithLocation(1, 1),
// error CS1729: 'Attribute' does not contain a constructor that takes 0 arguments
+ Diagnostic(ErrorCode.ERR_BadCtorArgCount).WithArguments("System.Attribute", "0").WithLocation(1, 1),
+ // error CS1729: 'Attribute' does not contain a constructor that takes 0 arguments
Diagnostic(ErrorCode.ERR_BadCtorArgCount).WithArguments("System.Attribute", "0").WithLocation(1, 1));
}
@@ -350,7 +361,7 @@ void local(T t) where T : C
CompileAndVerify(comp, symbolValidator: module =>
{
var assembly = module.ContainingAssembly;
- Assert.Null(assembly.GetTypeByMetadataName("System.Runtime.CompilerServices.NullableAttribute"));
+ Assert.NotNull(assembly.GetTypeByMetadataName("System.Runtime.CompilerServices.NullableAttribute"));
});
}
@@ -423,7 +434,7 @@ public class B2 : A