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

Fix issues with 'use conditional' when using pattern variables #68604

Merged
merged 6 commits into from
Jun 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -2044,5 +2044,63 @@ class C
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/68578")]
public async Task TestMissingWhenAssignmentReferencesPatternVariable()
{
await TestMissingAsync("""
using System;

public class Class1
{
public int i;
}

public class Program
{
public static void Test(object obj)
{
if (obj is Class1 c)
{
c.i = 1;
}
else
{
throw new Exception();
}
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/68578")]
public async Task TestMissingWhenAssignmentReferencesOutVariable()
{
await TestMissingAsync("""
using System;

public class Class1
{
public int i;
}

public class Program
{
public static void Test(object obj)
{
if (TryGetValue(out var c))
{
c.i = 1;
}
else
{
throw new Exception();
}
}

private static bool TryGetValue(out Class1 c) => throw new NotImplementedException();
}
""");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.UseConditionalExpression
{
Expand Down Expand Up @@ -68,6 +71,9 @@ public static bool TryMatchPattern(
return false;
}

if (ReferencesDeclaredVariableInAssignment(ifOperation.Condition, trueAssignment?.Target, falseAssignment?.Target))
return false;

isRef = trueAssignment?.IsRef == true;
return UseConditionalExpressionHelpers.CanConvert(
syntaxFacts, ifOperation, trueStatement, falseStatement);
Expand All @@ -83,6 +89,49 @@ static bool AreEqualOrHaveImplicitConversion(ITypeSymbol? firstType, ITypeSymbol
return compilation.ClassifyCommonConversion(firstType, secondType).IsImplicit
^ compilation.ClassifyCommonConversion(secondType, firstType).IsImplicit;
}

static bool ReferencesDeclaredVariableInAssignment(IOperation condition, IOperation? trueTarget, IOperation? falseTarget)
{
if (trueTarget is not null || falseTarget is not null)
{
using var _1 = PooledHashSet<ILocalSymbol>.GetInstance(out var symbolsDeclaredInConditional);
foreach (var operation in condition.DescendantsAndSelf())
{
// `if (x is String s)`
if (operation is IDeclarationPatternOperation { DeclaredSymbol: ILocalSymbol local })
symbolsDeclaredInConditional.AddIfNotNull(local);

// `if (Goo(out String s))`
if (operation is IDeclarationExpressionOperation { Expression: ILocalReferenceOperation localReference })
symbolsDeclaredInConditional.AddIfNotNull(localReference.Local);
}

if (symbolsDeclaredInConditional.Count > 0)
{
return ContainsLocalReference(symbolsDeclaredInConditional, trueTarget) ||
ContainsLocalReference(symbolsDeclaredInConditional, falseTarget);
}
}

return false;
}

static bool ContainsLocalReference(HashSet<ILocalSymbol> declaredPatternSymbols, IOperation? target)
{
if (target is not null)
{
foreach (var operation in target.DescendantsAndSelf())
{
if (operation is ILocalReferenceOperation { Local: var local } &&
declaredPatternSymbols.Contains(local))
{
return true;
}
}
}

return false;
}
}

private static bool TryGetAssignmentOrThrow(
Expand Down