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

List patterns: Slice value is assumed to be never null #57457

Merged
merged 19 commits into from
Jan 20, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,12 @@ private static void MakeCheckNotNull(
ArrayBuilder<Tests> tests)
{
// Add a null test if needed
if (input.Type.CanContainNull())
if (input.Type.CanContainNull() &&
// The slice value is assumed to be never null
input.Source is not BoundDagSliceEvaluation)
alrz marked this conversation as resolved.
Show resolved Hide resolved
{
tests.Add(new Tests.One(new BoundDagNonNullTest(syntax, isExplicitTest, input)));
}
}

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6730,9 +6730,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="IDS_FeatureListPattern" xml:space="preserve">
<value>list pattern</value>
</data>
<data name="IDS_FeatureLengthPattern" xml:space="preserve">
<value>length pattern</value>
</data>
<data name="ERR_UnsupportedTypeForListPattern" xml:space="preserve">
<value>List patterns may not be used for a value of type '{0}'.</value>
</data>
Expand Down Expand Up @@ -6893,4 +6890,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="IDS_FeatureNewLinesInInterpolations" xml:space="preserve">
<value>newlines in interpolations</value>
</data>
<data name="WRN_AnnotationOnSliceReturnType" xml:space="preserve">
<value>The annotation on the output type '{0}' is ignored.</value>
</data>
<data name="WRN_AnnotationOnSliceReturnType_Title" xml:space="preserve">
<value>The annotation on the output type is ignored.</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,8 @@ internal enum ErrorCode
ERR_UnsupportedTypeForSlicePattern,
ERR_MisplacedSlicePattern,

WRN_AnnotationOnSliceReturnType = 8978,

#endregion

// Note: you will need to re-generate compiler code after adding warnings (eng\generate-compiler-code.cmd)
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ static ErrorFacts()
nullableWarnings.Add(GetId(ErrorCode.WRN_ParameterDisallowsNull));
nullableWarnings.Add(GetId(ErrorCode.WRN_ParameterNotNullIfNotNull));
nullableWarnings.Add(GetId(ErrorCode.WRN_ReturnNotNullIfNotNull));
nullableWarnings.Add(GetId(ErrorCode.WRN_AnnotationOnSliceReturnType));

NullableWarnings = nullableWarnings.ToImmutable();
}
Expand Down Expand Up @@ -486,6 +487,7 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_InterpolatedStringHandlerArgumentAttributeIgnoredOnLambdaParameters:
case ErrorCode.WRN_CompileTimeCheckedOverflow:
case ErrorCode.WRN_MethGrpToNonDel:
case ErrorCode.WRN_AnnotationOnSliceReturnType:
return 1;
default:
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ public PossiblyConditionalState Clone()
var output = new BoundDagTemp(e.Syntax, type.Type, e);
var outputSlot = makeDagTempSlot(type, output);
Debug.Assert(outputSlot > 0);
ignoreAnnotationAndReport(outputSlot, type, ref this.State, e);
addToTempMap(output, outputSlot, type.Type);
break;
}
Expand Down Expand Up @@ -719,6 +720,18 @@ public PossiblyConditionalState Clone()
nodeStateMap.Free();
return labelStateMap;

void ignoreAnnotationAndReport(int outputSlot, TypeWithAnnotations outputType, ref LocalState state, BoundDagSliceEvaluation e)
{
if (PossiblyNullableType(outputType.Type))
{
if (state[outputSlot] == NullableFlowState.MaybeNull)
{
ReportDiagnostic(ErrorCode.WRN_AnnotationOnSliceReturnType, e.Syntax, outputType);
state[outputSlot] = NullableFlowState.NotNull;
}
}
}
alrz marked this conversation as resolved.
Show resolved Hide resolved

void learnFromNonNullTest(int inputSlot, ref LocalState state)
{
if (stateWhenNotNullOpt is { } stateWhenNotNull && inputSlot == originalInputSlot)
Expand Down

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

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 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.

15 changes: 10 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

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

Loading