Skip to content

Commit e58a738

Browse files
committed
Don't try to add [NotNullWhen] attributes to local functions.
1 parent 695a759 commit e58a738

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

NullabilityInference/NodeBuildingSyntaxVisitor.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public override TypeWithNode VisitParameter(ParameterSyntax node)
293293
parameterTypes.Add(symbol, type);
294294
typeSystem.AddSymbolType(symbol, type);
295295

296-
if (symbol.RefKind == RefKind.Out && (symbol.ContainingSymbol as IMethodSymbol)?.EffectiveReturnType().SpecialType == SpecialType.System_Boolean) {
296+
if (symbol.RefKind == RefKind.Out && CanUseOutParamFlow(symbol.ContainingSymbol as IMethodSymbol)) {
297297
typeSystem.RegisterOutParamFlowNodes(symbol);
298298
}
299299
}
@@ -302,6 +302,19 @@ public override TypeWithNode VisitParameter(ParameterSyntax node)
302302
return typeSystem.VoidType;
303303
}
304304

305+
private bool CanUseOutParamFlow(IMethodSymbol? method)
306+
{
307+
if (method == null)
308+
return false;
309+
if (method.ContainingSymbol is IMethodSymbol) {
310+
var compilation = (CSharpCompilation)semanticModel.Compilation;
311+
if (compilation.LanguageVersion <= LanguageVersion.CSharp8)
312+
return false; // C# 8 does not support attributes on local function parameters
313+
}
314+
return method.EffectiveReturnType().SpecialType == SpecialType.System_Boolean;
315+
}
316+
317+
305318
public override TypeWithNode VisitNameMemberCref(NameMemberCrefSyntax node)
306319
{
307320
Mapping.AddCrefNode(node);

0 commit comments

Comments
 (0)