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

ReplaceMethodWithPropertyCodeRefactoringProvider crash with System.InvalidOperationException : GetCurrentNode returned null #73637

Open
JeroenOortwijn opened this issue May 22, 2024 · 0 comments
Assignees

Comments

@JeroenOortwijn
Copy link

Version Used: Visual Studio 17.11.0 Preview 1.0

Steps to Reproduce:

Start with the following code:

namespace Test;

/// <summary>An array indexed by an Enum</summary>
/// <typeparam name="TValue">Type stored in array</typeparam>
/// <typeparam name="TEnumKey">Indexer Enum type</typeparam>
/// If you know your enum does not have much gapps in the value, this will be more efficient than a Dictionary
public struct ArrayByEnum<TEnumKey, TValue> : IEnumerable<TValue> where TEnumKey : Enum // requires C# 7.3 or later
{
    private readonly TValue[] _array;
    private readonly int _lower;

    public ArrayByEnum()
    {
        _lower = Convert.ToInt32(Enum.GetValues(typeof(TEnumKey)).Cast<TEnumKey>().Min());
        int upper = Convert.ToInt32(Enum.GetValues(typeof(TEnumKey)).Cast<TEnumKey>().Max());
        _array = new TValue[1 + upper - _lower];
    }

    public TValue this[TEnumKey key]
    {
        get { return _array[Convert.ToInt32(key) - _lower]; }
        set { _array[Convert.ToInt32(key) - _lower] = value; }
    }

    public IEnumerator<TValue> GetEnumerator()
    {
        var myThis = this;
        return Enum.GetValues(typeof(TEnumKey))
                .Cast<TEnumKey>()
                .Select(i => myThis._array[Convert.ToInt32(i) - myThis._lower])
                .GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
}
  1. Go to the second-last line.
  2. Invoke the Quick Actions and Refactorings menu:
    image
  3. Press
  4. Wait for it to crash:
    image

Expected Behavior:
A code preview is shown.

Actual Behavior:
ReplaceMethodWithPropertyCodeRefactoringProvider crashes:

System.InvalidOperationException : GetCurrentNode returned null with the following node: foreach (System.Reflection.FieldInfo subfield in fieldValue.GetType().GetFields())
            {
                Console.WriteLine($"  {subfield.Name} = {subfield.GetValue(fieldValue)}");
            } - file SyntaxEditor.cs line 255
   at Roslyn.Utilities.Contract.Fail(String message,Int32 lineNumber,String filePath)
   at Microsoft.CodeAnalysis.Editing.SyntaxEditor.GetChangedRoot()
   at async Microsoft.CodeAnalysis.ReplaceMethodWithProperty.ReplaceMethodWithPropertyCodeRefactoringProvider.UpdateReferencesInDocumentAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.ReplaceMethodWithProperty.ReplaceMethodWithPropertyCodeRefactoringProvider.UpdateReferencesAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.ReplaceMethodWithProperty.ReplaceMethodWithPropertyCodeRefactoringProvider.ReplaceMethodsWithPropertyAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass16_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels May 22, 2024
@genlu genlu added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants