提交 67ece919 编写于 作者: C Charles Stoner

Fixes for empty struct cases and typos

上级 c505cfe9
......@@ -121,7 +121,7 @@ namespace System.Runtime.CompilerServices
**Opting in and opting out of nullability warnings**
It is possible to suppress all nulability warnings originating from declarations in certain referenced
It is possible to suppress all nullability warnings originating from declarations in certain referenced
assembly by applying the following attribute:
```
namespace System.Runtime.CompilerServices
......
......@@ -2749,7 +2749,7 @@ private BoundExpression BindImplicitArrayCreationExpression(ImplicitArrayCreatio
{
// For now erase all notion about nullability of reference types as the algorithm is not taking it into account yet.
// Default inferred reference type itself to a nullable state.
elementType = TypeSymbolWithAnnotations.Create(bestType.SetUnknownNullabilityForRefernceTypes(), isNullableIfReferenceType: true);
elementType = TypeSymbolWithAnnotations.Create(bestType.SetUnknownNullabilityForReferenceTypes(), isNullableIfReferenceType: true);
}
else
{
......@@ -6087,7 +6087,7 @@ private static void CombineExtensionMethodArguments(BoundExpression receiver, An
internal TypeSymbol GetFieldTypeWithAdjustedNullableAnnotations(FieldSymbol fieldSymbol, ConsList<FieldSymbol> fieldsBeingBound)
{
if (((CSharpParseOptions)Compilation.SyntaxTrees.FirstOrDefault()?.Options)?.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking) == true &&
if (Compilation.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking) &&
!IsBindingModuleLevelAttribute()) // TODO: It is possible to get into cycle while binding module level attributes because Opt-In/Opt-Out state depends on them
{
return Compilation.GetFieldTypeWithAdjustedNullableAnnotations(fieldSymbol, fieldsBeingBound).TypeSymbol;
......
......@@ -1014,7 +1014,7 @@ private static void CheckRestrictedTypeReceiver(BoundExpression expression, Comp
internal TypeSymbolWithAnnotations GetTypeOrReturnTypeWithAdjustedNullableAnnotations(Symbol symbol)
{
if (((CSharpParseOptions)Compilation.SyntaxTrees.FirstOrDefault()?.Options)?.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking) == true &&
if (Compilation.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking) &&
!IsBindingModuleLevelAttribute()) // TODO: It is possible to get into cycle while binding module level attributes because Opt-In/Opt-Out state depends on them
{
return Compilation.GetTypeOrReturnTypeWithAdjustedNullableAnnotations(symbol);
......
......@@ -406,7 +406,7 @@ private ImmutableArray<TypeSymbolWithAnnotations> GetResults(bool eraseNullabili
foreach (TypeSymbol t in _fixedResults)
{
var result = eraseNullability ?
TypeSymbolWithAnnotations.Create(t.SetUnknownNullabilityForRefernceTypes(), isNullableIfReferenceType: null) :
TypeSymbolWithAnnotations.Create(t.SetUnknownNullabilityForReferenceTypes(), isNullableIfReferenceType: null) :
TypeSymbolWithAnnotations.Create(t);
builder.Add(result);
}
......@@ -521,7 +521,7 @@ private MethodTypeInferenceResult InferTypeArgs(Binder binder, ref HashSet<Diagn
// SPEC: bounds. The second phase may have to be repeated a number of times.
InferTypeArgsFirstPhase(binder, ref useSiteDiagnostics);
bool success = InferTypeArgsSecondPhase(binder, ref useSiteDiagnostics);
bool eraseNullability = (((CSharpParseOptions)binder.Compilation.SyntaxTrees.FirstOrDefault()?.Options)?.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking) == true);
bool eraseNullability = binder.Compilation.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking);
return new MethodTypeInferenceResult(success, GetResults(eraseNullability));
}
......
......@@ -205,6 +205,7 @@
<Compile Include="Compilation\AttributeSemanticModel.cs" />
<Compile Include="Compilation\AwaitExpressionInfo.cs" />
<Compile Include="Compilation\BuiltInOperators.cs" />
<Compile Include="Compilation\CSharpCompilationExtensions.cs" />
<Compile Include="Compilation\CSharpScriptCompilationInfo.cs" />
<Compile Include="Compilation\SyntaxAndDeclarationManager.cs" />
<Compile Include="Compilation\SyntaxAndDeclarationManager.LazyState.cs" />
......
......@@ -3077,8 +3077,7 @@ internal bool EnableEnumArrayBlockInitialization
internal override bool IsIOperationFeatureEnabled()
{
var options = (CSharpParseOptions)this.SyntaxTrees.FirstOrDefault()?.Options;
return options?.IsFeatureEnabled(MessageID.IDS_FeatureIOperation) ?? false;
return this.IsFeatureEnabled(MessageID.IDS_FeatureIOperation);
}
internal TypeSymbolWithAnnotations GetTypeOrReturnTypeWithAdjustedNullableAnnotations(Symbol symbol)
......@@ -3092,7 +3091,7 @@ internal TypeSymbolWithAnnotations GetTypeOrReturnTypeWithAdjustedNullableAnnota
// Nullable annotations on definition should be ignored
TypeSymbolWithAnnotations definitionType = definition.Kind == SymbolKind.Parameter ? ((ParameterSymbol)definition).Type : definition.GetTypeOrReturnType();
TypeSymbolWithAnnotations adjustedDefinitionType = definitionType.SetUnknownNullabilityForRefernceTypes();
TypeSymbolWithAnnotations adjustedDefinitionType = definitionType.SetUnknownNullabilityForReferenceTypes();
if ((object)definition == symbol)
{
......@@ -3162,7 +3161,7 @@ internal TypeSymbolWithAnnotations GetFieldTypeWithAdjustedNullableAnnotations(F
// Nullable annotations on definition should be ignored
TypeSymbolWithAnnotations definitionType = definition.GetFieldType(fieldsBeingBound);
TypeSymbolWithAnnotations adjustedDefinitionType = definitionType.SetUnknownNullabilityForRefernceTypes();
TypeSymbolWithAnnotations adjustedDefinitionType = definitionType.SetUnknownNullabilityForReferenceTypes();
if ((object)definition == field)
{
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
namespace Microsoft.CodeAnalysis.CSharp
{
internal static class CSharpCompilationExtensions
{
internal static bool IsFeatureEnabled(this CSharpCompilation compilation, MessageID feature)
{
return ((CSharpParseOptions)compilation.SyntaxTrees.FirstOrDefault()?.Options)?.IsFeatureEnabled(feature) == true;
}
}
}
......@@ -784,6 +784,7 @@ protected int GetOrCreateSlot(Symbol symbol, int containingSlot = 0)
NormalizeNullable(ref this.State);
}
Debug.Assert(slot > 0);
return slot;
}
......@@ -1606,9 +1607,13 @@ private void InheritNullableStateOfFieldOrProperty(int targetContainerSlot, int
}
else if (EmptyStructTypeCache.IsTrackableStructType(fieldOrPropertyType.TypeSymbol))
{
InheritNullableStateOfTrackableStruct(fieldOrPropertyType.TypeSymbol,
GetOrCreateSlot(fieldOrProperty, targetContainerSlot),
valueContainerSlot > 0 ? GetOrCreateSlot(fieldOrProperty, valueContainerSlot) : -1, isByRefTarget);
var slot = GetOrCreateSlot(fieldOrProperty, targetContainerSlot);
if (slot > 0)
{
InheritNullableStateOfTrackableStruct(fieldOrPropertyType.TypeSymbol,
slot,
valueContainerSlot > 0 ? GetOrCreateSlot(fieldOrProperty, valueContainerSlot) : -1, isByRefTarget);
}
}
}
......@@ -2233,7 +2238,11 @@ public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpre
EmptyStructTypeCache.IsTrackableStructType(node.Type))
{
_implicitReceiver = GetOrCreateObjectCreationPlaceholder(node);
InheritNullableStateOfTrackableStruct(node.Type, MakeSlot(node), -1, false);
var slot = MakeSlot(node);
if (slot > 0)
{
InheritNullableStateOfTrackableStruct(node.Type, slot, -1, false);
}
}
var result = base.VisitObjectCreationExpression(node);
......
......@@ -399,10 +399,10 @@ internal override bool ApplyNullableTransforms(ImmutableArray<bool> transforms,
return true;
}
internal override TypeSymbol SetUnknownNullabilityForRefernceTypes()
internal override TypeSymbol SetUnknownNullabilityForReferenceTypes()
{
TypeSymbolWithAnnotations oldElementType = ElementType;
TypeSymbolWithAnnotations newElementType = oldElementType.SetUnknownNullabilityForRefernceTypes();
TypeSymbolWithAnnotations newElementType = oldElementType.SetUnknownNullabilityForReferenceTypes();
if ((object)oldElementType == newElementType)
{
......
......@@ -221,7 +221,7 @@ internal override bool ApplyNullableTransforms(ImmutableArray<bool> transforms,
return true;
}
internal override TypeSymbol SetUnknownNullabilityForRefernceTypes()
internal override TypeSymbol SetUnknownNullabilityForReferenceTypes()
{
return this;
}
......
......@@ -825,7 +825,7 @@ internal override bool ApplyNullableTransforms(ImmutableArray<bool> transforms,
return true;
}
internal override TypeSymbol SetUnknownNullabilityForRefernceTypes()
internal override TypeSymbol SetUnknownNullabilityForReferenceTypes()
{
if (!IsGenericType)
{
......@@ -839,7 +839,7 @@ internal override TypeSymbol SetUnknownNullabilityForRefernceTypes()
for (int i = 0; i < allTypeArguments.Count; i++)
{
TypeSymbolWithAnnotations oldTypeArgument = allTypeArguments[i];
TypeSymbolWithAnnotations newTypeArgument = oldTypeArgument.SetUnknownNullabilityForRefernceTypes();
TypeSymbolWithAnnotations newTypeArgument = oldTypeArgument.SetUnknownNullabilityForReferenceTypes();
if ((object)oldTypeArgument != newTypeArgument)
{
allTypeArguments[i] = newTypeArgument;
......
......@@ -266,10 +266,10 @@ internal override bool ApplyNullableTransforms(ImmutableArray<bool> transforms,
return true;
}
internal override TypeSymbol SetUnknownNullabilityForRefernceTypes()
internal override TypeSymbol SetUnknownNullabilityForReferenceTypes()
{
TypeSymbolWithAnnotations oldPointedAtType = PointedAtType;
TypeSymbolWithAnnotations newPointedAtType = oldPointedAtType.SetUnknownNullabilityForRefernceTypes();
TypeSymbolWithAnnotations newPointedAtType = oldPointedAtType.SetUnknownNullabilityForReferenceTypes();
if ((object)oldPointedAtType == newPointedAtType)
{
......
......@@ -623,7 +623,7 @@ internal override bool UtilizesNullableReferenceTypes
{
get
{
return ((CSharpParseOptions)_assemblySymbol.DeclaringCompilation.SyntaxTrees.FirstOrDefault()?.Options)?.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking) == true;
return _assemblySymbol.DeclaringCompilation.IsFeatureEnabled(MessageID.IDS_FeatureStaticNullChecking);
}
}
......
......@@ -506,7 +506,7 @@ public bool ApplyNullableTransforms(ImmutableArray<bool> transforms, ref int pos
return false;
}
public TypeSymbolWithAnnotations SetUnknownNullabilityForRefernceTypes()
public TypeSymbolWithAnnotations SetUnknownNullabilityForReferenceTypes()
{
var typeSymbol = TypeSymbol;
......@@ -514,7 +514,7 @@ public TypeSymbolWithAnnotations SetUnknownNullabilityForRefernceTypes()
{
if (!typeSymbol.IsNullableType() && typeSymbol.IsReferenceType)
{
typeSymbol = typeSymbol.SetUnknownNullabilityForRefernceTypes();
typeSymbol = typeSymbol.SetUnknownNullabilityForReferenceTypes();
var customModifiers = CustomModifiers;
if (customModifiers.IsEmpty)
......@@ -526,7 +526,7 @@ public TypeSymbolWithAnnotations SetUnknownNullabilityForRefernceTypes()
}
}
var newTypeSymbol = typeSymbol.SetUnknownNullabilityForRefernceTypes();
var newTypeSymbol = typeSymbol.SetUnknownNullabilityForReferenceTypes();
if ((object)newTypeSymbol != typeSymbol)
{
......
......@@ -541,7 +541,7 @@ internal override bool ApplyNullableTransforms(ImmutableArray<bool> transforms,
return true;
}
internal override TypeSymbol SetUnknownNullabilityForRefernceTypes()
internal override TypeSymbol SetUnknownNullabilityForReferenceTypes()
{
return this;
}
......
......@@ -651,7 +651,7 @@ public virtual NamedTypeSymbol TupleUnderlyingType
internal abstract bool ApplyNullableTransforms(ImmutableArray<bool> transforms, ref int position, out TypeSymbol result);
internal abstract TypeSymbol SetUnknownNullabilityForRefernceTypes();
internal abstract TypeSymbol SetUnknownNullabilityForReferenceTypes();
#region ITypeSymbol Members
......
......@@ -14578,6 +14578,68 @@ class C : I
Assert.Equal(new[] { "void I.M<T>(T? x)" }, implementations.SelectAsArray(m => m.ToTestDisplayString()));
}
[Fact]
public void EmptyStructDifferentAssembly()
{
var sourceA =
@"using System.Collections;
public struct S
{
public S(string f, IEnumerable g)
{
F = f;
G = g;
}
private string F { get; }
private IEnumerable G { get; }
}";
var compA = CreateStandardCompilation(sourceA);
var sourceB =
@"using System.Collections.Generic;
class C
{
static void Main()
{
var c = new List<object>();
c.Add(new S(string.Empty, new object[0]));
}
}";
var compB = CreateStandardCompilation(
new[] { sourceB, attributesDefinitions },
options: TestOptions.ReleaseExe,
parseOptions: TestOptions.Regular.WithNullCheckingFeature(),
references: new[] { compA.EmitToImageReference() });
CompileAndVerify(compB, expectedOutput: "");
}
[Fact]
public void EmptyStructField()
{
var source =
@"class A { }
struct B { }
struct S
{
public readonly A A;
public readonly B B;
public S(B b) : this(null, b)
{
}
public S(A a, B b)
{
this.A = a;
this.B = b;
}
}";
var comp = CreateStandardCompilation(
new[] { source, attributesDefinitions },
parseOptions: TestOptions.Regular.WithNullCheckingFeature());
comp.VerifyDiagnostics(
// (7,26): warning CS8604: Possible null reference argument for parameter 'a' in 'S.S(A a, B b)'.
// public S(B b) : this(null, b)
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "null").WithArguments("a", "S.S(A a, B b)").WithLocation(7, 26));
}
// PROTOTYPE(NullableReferenceTypes)
[Fact(Skip = "TODO")]
public void Test2()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册