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

Extract Method on part of an expression generates invalid code #38087

Closed
thetuvix opened this issue Aug 18, 2019 · 4 comments · Fixed by #76412
Closed

Extract Method on part of an expression generates invalid code #38087

thetuvix opened this issue Aug 18, 2019 · 4 comments · Fixed by #76412
Assignees
Labels
Area-Compilers Bug Feature - Extract Method help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Milestone

Comments

@thetuvix
Copy link
Contributor

Version Used:
Visual Studio 2019 version 16.2.2

Steps to Reproduce:

  1. Add the following code to a class:
private void Repro()
{
    int i = 1, j = 2;
    int k = i + j + 1;
}
  1. Select i + j and perform an Extract Method refactoring.

Expected Behavior:
Extract Method generates NewMethod(int i, int j):

private void Repro()
{
    int i = 1, j = 2;
    int k = NewMethod(i, j) + 1;
}

private static int NewMethod(int i, int j)
{
    return i + j;
}

Actual Behavior:
Extract Method generates NewMethod() without parameters to pass in i and j:

private void Repro()
{
    int i = 1, j = 2;
    int k = NewMethod() + 1;
}

private static int NewMethod()
{
    return i + j;
}
@RikkiGibson RikkiGibson added Area-IDE Bug IDE-CodeStyle Built-in analyzers, fixes, and refactorings labels Aug 18, 2019
@RikkiGibson
Copy link
Contributor

This could be a problem in IDE code or in dataflow analysis. We'll see once we take a closer look at the bug.

@vatsalyaagrawal vatsalyaagrawal added this to the Backlog milestone Aug 20, 2019
@vatsalyaagrawal vatsalyaagrawal added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Aug 20, 2019
@github-project-automation github-project-automation bot moved this to InQueue in Small Fixes Oct 22, 2024
@CyrusNajmabadi CyrusNajmabadi self-assigned this Dec 11, 2024
@CyrusNajmabadi
Copy link
Member

This could be a problem in IDE code or in dataflow analysis. We'll see once we take a closer look at the bug.

@RikkiGibson this appears to be an issue with dataflow analysis. We ask for dataflow on the i + j expression and we get back nothing. It's an entirely empty object.

I debugger to this point:

                    _succeeded = !_context.Failed;
                    var result = _context.Failed ? ImmutableArray<ISymbol>.Empty :
                        Normalize(DataFlowsInWalker.Analyze(_context.Compilation, _context.Member, _context.BoundNode, _context.FirstInRegion, _context.LastInRegion, UnassignedVariables, UnassignedVariableAddressOfSyntaxes, out _succeeded));
                    ImmutableInterlocked.InterlockedInitialize(ref _dataFlowsIn, result);

At which point _succeeded is set to false in the 'out' param of DataFlowsInWalker.Analyze.

@CyrusNajmabadi
Copy link
Member

This is where we set that it's bad:

Image

In this callstack:

>	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.AbstractFlowPass<Microsoft.CodeAnalysis.CSharp.DefiniteAssignmentPass.LocalState, Microsoft.CodeAnalysis.CSharp.DefiniteAssignmentPass.LocalFunctionState>.Scan(ref bool badRegion) Line 428	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.DefiniteAssignmentPass.Scan(ref bool badRegion) Line 371	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.AbstractRegionDataFlowPass.Scan(ref bool badRegion) Line 36	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.AbstractFlowPass<Microsoft.CodeAnalysis.CSharp.DefiniteAssignmentPass.LocalState, Microsoft.CodeAnalysis.CSharp.DefiniteAssignmentPass.LocalFunctionState>.Analyze(ref bool badRegion, Microsoft.CodeAnalysis.Optional<Microsoft.CodeAnalysis.CSharp.DefiniteAssignmentPass.LocalState> initialState) Line 447	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.DefiniteAssignmentPass.Analyze(ref bool badRegion, Microsoft.CodeAnalysis.DiagnosticBag diagnostics) Line 682	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.DataFlowsInWalker.Analyze(ref bool badRegion) Line 52	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.DataFlowsInWalker.Analyze(Microsoft.CodeAnalysis.CSharp.CSharpCompilation compilation, Microsoft.CodeAnalysis.CSharp.Symbol member, Microsoft.CodeAnalysis.CSharp.BoundNode node, Microsoft.CodeAnalysis.CSharp.BoundNode firstInRegion, Microsoft.CodeAnalysis.CSharp.BoundNode lastInRegion, System.Collections.Generic.HashSet<Microsoft.CodeAnalysis.CSharp.Symbol> unassignedVariables, System.Collections.Generic.HashSet<Microsoft.CodeAnalysis.CSharp.Syntax.PrefixUnaryExpressionSyntax> unassignedVariableAddressOfSyntaxes, out bool? succeeded) Line 40	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.CSharpDataFlowAnalysis.DataFlowsIn.get() Line 102	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.CSharpDataFlowAnalysis.Succeeded.get() Line 383	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.CSharpDataFlowAnalysis.AnalyzeReadWrite() Line 256	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.CSharpDataFlowAnalysis.Captured.get() Line 291	C#
 	Microsoft.CodeAnalysis.Features.dll!Microsoft.CodeAnalysis.ExtractMethod.MethodExtractor<Microsoft.CodeAnalysis.CSharp.ExtractMethod.CSharpSelectionResult, Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax>.Analyzer.GenerateVariableInfoMap(bool bestEffort, Microsoft.CodeAnalysis.SemanticModel model, Microsoft.CodeAnalysis.DataFlowAnalysis dataFlowAnalysisData, System.Collections.Generic.Dictionary<Microsoft.CodeAnalysis.ISymbol, System.Collections.Generic.List<Microsoft.CodeAnalysis.SyntaxToken>> symbolMap, out System.Collections.Generic.Dictionary<Microsoft.CodeAnalysis.ISymbol, Microsoft.CodeAnalysis.ExtractMethod.MethodExtractor<Microsoft.CodeAnalysis.CSharp.ExtractMethod.CSharpSelectionResult, Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax>.VariableInfo> variableInfoMap, out System.Collections.Generic.List<Microsoft.CodeAnalysis.ISymbol> failedVariables) Line 478	C#

@CyrusNajmabadi
Copy link
Member

Passing to compiler to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Feature - Extract Method help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

4 participants