Skip to content

'Convert to struct' refactoring bug, incorrect constructor call #45451

@sonnemaf

Description

@sonnemaf

Version Used:
Visual Studio 2019 Preview - Version 16.7.0 Preview 3.0

Steps to Reproduce:

  1. Create a console app with the following code.
using System;

namespace ConsoleApp1 {
    class Program {
        static void Main(string[] args) {
            var point = (X: 3, Y: 4);

            Console.WriteLine(point.X);
            Console.WriteLine(point.Y);
        }
    }
}
  1. Select the valuetuple (X:3, Y:4) and active the 'Convert to struct' refactoring
  2. The constructor call will look like var point = new NewStruct(X: 3, Y: 4); The named arguments are in uppercase and should be lowercase.

As you can see you get a compiler error (red squigly line) in the preview of the refactoring.
image

Expected Behavior:
I expected lower case argument names: var point = new NewStruct(x: 3, y: 4);.

The following code should have been generated

using System;

namespace ConsoleApp1 {
    class Program {
        static void Main(string[] args) {
            var point = new NewStruct(x: 3, y: 4);

            Console.WriteLine(point.X);
            Console.WriteLine(point.Y);
        }
    }

    internal struct NewStruct {
        public int X;
        public int Y;

        public NewStruct(int x, int y) {
            X = x;
            Y = y;
        }

        public override bool Equals(object obj) {
            return obj is NewStruct other &&
                   X == other.X &&
                   Y == other.Y;
        }

        public override int GetHashCode() {
            return HashCode.Combine(X, Y);
        }

        public void Deconstruct(out int x, out int y) {
            x = X;
            y = Y;
        }

        public static implicit operator (int X, int Y)(NewStruct value) {
            return (value.X, value.Y);
        }

        public static implicit operator NewStruct((int X, int Y) value) {
            return new NewStruct(value.X, value.Y);
        }
    }
}

Actual Behavior:
The following code is generated

using System;

namespace ConsoleApp1 {
    class Program {
        static void Main(string[] args) {
            var point = new NewStruct(X: 3, Y: 4); 

            Console.WriteLine(point.X);
            Console.WriteLine(point.Y);
        }
    }

    internal struct NewStruct {
        public int X;
        public int Y;

        public NewStruct(int x, int y) {
            X = x;
            Y = y;
        }

        public override bool Equals(object obj) {
            return obj is NewStruct other &&
                   X == other.X &&
                   Y == other.Y;
        }

        public override int GetHashCode() {
            return HashCode.Combine(X, Y);
        }

        public void Deconstruct(out int x, out int y) {
            x = X;
            y = Y;
        }

        public static implicit operator (int X, int Y)(NewStruct value) {
            return (value.X, value.Y);
        }

        public static implicit operator NewStruct((int X, int Y) value) {
            return new NewStruct(value.X, value.Y);
        }
    }
}
'''

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions