Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address various minor param-nullchecking issues #58321

Merged
merged 5 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6187,11 +6187,11 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_NullCheckingOnByRefParameter" xml:space="preserve">
<value>By-reference parameter '{0}' cannot be null-checked.</value>
</data>
<data name="WRN_NullCheckingOnNullableValueType" xml:space="preserve">
<value>Nullable value type '{0}' is null-checked and will throw if null.</value>
<data name="WRN_NullCheckingOnNullableType" xml:space="preserve">
<value>Nullable type '{0}' is null-checked and will throw if null.</value>
</data>
<data name="WRN_NullCheckingOnNullableValueType_Title" xml:space="preserve">
<value>Nullable value type is null-checked and will throw if null.</value>
<data name="WRN_NullCheckingOnNullableType_Title" xml:space="preserve">
<value>Nullable type is null-checked and will throw if null.</value>
</data>
<data name="ERR_ReAbstractionInNoPIAType" xml:space="preserve">
<value>Type '{0}' cannot be embedded because it has a re-abstraction of a member from base interface. Consider setting the 'Embed Interop Types' property to false.</value>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ internal enum ErrorCode
ERR_NonNullableValueTypeIsNullChecked = 8992,
WRN_NullCheckedHasDefaultNull = 8993,
ERR_NullCheckingOnByRefParameter = 8994,
WRN_NullCheckingOnNullableValueType = 8995,
WRN_NullCheckingOnNullableType = 8995,

#endregion

Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_NullabilityMismatchInTypeParameterNotNullConstraint:
case ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment:
case ErrorCode.WRN_NullCheckedHasDefaultNull:
case ErrorCode.WRN_NullCheckingOnNullableValueType:
case ErrorCode.WRN_NullCheckingOnNullableType:
case ErrorCode.WRN_ParameterConditionallyDisallowsNull:
case ErrorCode.WRN_NullReferenceInitializer:
case ErrorCode.WRN_ShouldNotReturn:
Expand Down
19 changes: 14 additions & 5 deletions src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ private NullableFlowState GetDefaultState(ref LocalState state, int slot)
{
parameterType = parameter.TypeWithAnnotations;
}
return GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State;
return GetParameterState(parameterType, parameter.FlowAnalysisAnnotations, parameter.IsNullChecked).State;
}
case SymbolKind.Field:
case SymbolKind.Property:
Expand Down Expand Up @@ -2044,7 +2044,11 @@ internal static bool AreParameterAnnotationsCompatible(
{
// pre-condition attributes
// Check whether we can assign a value from overridden parameter to overriding
var valueState = GetParameterState(overriddenType, overriddenAnnotations);
var valueState = GetParameterState(
overriddenType,
overriddenAnnotations,
// We don't consider '!!' when deciding whether 'overridden' is compatible with 'override'
isNullChecked: false);
if (isBadAssignment(valueState, overridingType, overridingAnnotations))
{
return false;
Expand Down Expand Up @@ -2470,7 +2474,7 @@ private void EnterParameter(ParameterSymbol parameter, TypeWithAnnotations param
Debug.Assert(!IsConditionalState);
if (slot > 0)
{
var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State;
var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations, parameter.IsNullChecked).State;
this.State[slot] = state;
if (EmptyStructTypeCache.IsTrackableStructType(parameterType.Type))
{
Expand Down Expand Up @@ -2503,8 +2507,13 @@ private void EnterParameter(ParameterSymbol parameter, TypeWithAnnotations param
return null;
}

internal static TypeWithState GetParameterState(TypeWithAnnotations parameterType, FlowAnalysisAnnotations parameterAnnotations)
internal static TypeWithState GetParameterState(TypeWithAnnotations parameterType, FlowAnalysisAnnotations parameterAnnotations, bool isNullChecked)
{
if (isNullChecked)
{
return TypeWithState.Create(parameterType.Type, NullableFlowState.NotNull);
}

if ((parameterAnnotations & FlowAnalysisAnnotations.AllowNull) != 0)
{
return TypeWithState.Create(parameterType.Type, NullableFlowState.MaybeDefault);
Expand Down Expand Up @@ -7977,7 +7986,7 @@ private void VisitThisOrBaseReference(BoundExpression node)
var parameter = node.ParameterSymbol;
int slot = GetOrCreateSlot(parameter);
var parameterType = GetDeclaredParameterResult(parameter);
var typeWithState = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations);
var typeWithState = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations, parameter.IsNullChecked);
Copy link
Member

Choose a reason for hiding this comment

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

parameter.IsNullChecked

Are we testing a case where this value makes a difference (from passing false for instance)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oop, I might not have addressed this. Looking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there's not an obvious scenario where we can observe a behavior difference so I'm not going to add specific tests for it at this time.

SetResult(node, GetAdjustedResult(typeWithState, slot), parameterType);
return null;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,16 +802,14 @@ internal static void ReportParameterNullCheckingErrors(DiagnosticBag diagnostics
{
diagnostics.Add(useSiteInfo.DiagnosticInfo, location);
}
if (parameter.Type.IsValueType && !parameter.Type.IsPointerOrFunctionPointer())
if (parameter.TypeWithAnnotations.NullableAnnotation.IsAnnotated()
|| parameter.Type.IsNullableTypeOrTypeParameter())
{
if (!parameter.Type.IsNullableTypeOrTypeParameter())
{
diagnostics.Add(ErrorCode.ERR_NonNullableValueTypeIsNullChecked, location, parameter);
}
else
{
diagnostics.Add(ErrorCode.WRN_NullCheckingOnNullableValueType, location, parameter);
}
diagnostics.Add(ErrorCode.WRN_NullCheckingOnNullableType, location, parameter);
}
else if (parameter.Type.IsValueType && !parameter.Type.IsPointerOrFunctionPointer())
{
diagnostics.Add(ErrorCode.ERR_NonNullableValueTypeIsNullChecked, location, parameter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ TypeWithAnnotations getNotNullIfNotNullOutputType(TypeWithAnnotations outputType
{
var overrideParam = overrideParameters[i + overrideParameterOffset];
var baseParam = baseParameters[i];
if (notNullIfParameterNotNull.Contains(overrideParam.Name) && NullableWalker.GetParameterState(baseParam.TypeWithAnnotations, baseParam.FlowAnalysisAnnotations).IsNotNull)
if (notNullIfParameterNotNull.Contains(overrideParam.Name) && NullableWalker.GetParameterState(baseParam.TypeWithAnnotations, baseParam.FlowAnalysisAnnotations, baseParam.IsNullChecked).IsNotNull)
{
return outputType.AsNotAnnotated();
}
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Syntax/SyntaxKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public enum SyntaxKind : ushort
PercentEqualsToken = 8283,
/// <summary>Represents <c>??=</c> token.</summary>
QuestionQuestionEqualsToken = 8284,
/// <summary>Represents <c>!!</c> token.</summary>
ExclamationExclamationToken = 8285,

// Keywords
Expand Down
12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading