sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Renaming field doesn't update constructors on subclasses of subclasses

Open FMorschel opened this issue 6 months ago • 1 comments

Repro:

class Base {
  Base({required this.propriety});

  String propriety;
}

class Second extends Base {
  Second({required super.propriety});
}

class Third extends Second {
  Third({required super.propriety});
}

If you rename propriety to newName:

class Base {
  Base({required this.newName});

  String newName;
}

class Second extends Base {
  Second({required super.newName});
}

class Third extends Second {
  Third({required super.propriety}); // implicit_super_initializer_missing_arguments + super_formal_parameter_without_associated_named
}

I'll take a look at this

FMorschel avatar May 27 '25 15:05 FMorschel

Renaming a field to private also misses some cases:

class Base {
  Base(this.propriety)
    : assert(propriety.isNotEmpty, 'Propriety cannot be empty'),
      length = propriety.length {
    propriety;
  }

  String propriety;
  int length;
}

Becomes:

class Base {
  Base(this._propriety)
    : assert(propriety.isNotEmpty, 'Propriety cannot be empty'), // missing rename
      length = propriety.length { // missing rename
    _propriety;
  }

  String _propriety;
  int length;
}

Since this might be an easy fix (overlooked case), I'll see if I can work on this together. If I find it too difficult, I'll open a new issue for it.

FMorschel avatar Jun 05 '25 20:06 FMorschel

I think this has been fixed with https://github.com/dart-lang/sdk/issues/61194. Can you confirm @fshcheglov or @scheglov? Maybe we can leave this open for the comment above. I can test this out later when I update my master version.

FMorschel avatar Jul 25 '25 15:07 FMorschel

I took a look at it, and my change only works when renaming FormalParameterElement, while in this issue the example is about renaming FieldElement, which has references to formal parameters but is itself not a formal parameter. So - we need some additional enhancements to field rename refactoring here.

How do you want to proceed? Should I enhance field rename refactoring, or do you want to pick this up from here?

fshcheglov avatar Jul 25 '25 17:07 fshcheglov

I had some trouble when I was originally looking at this, so you'll probably be faster than me. If you have the time and would like to, feel free to do so.

I'm currently working on some other issues, so it would take me some time to revisit this and understand where I left of or to start from scratch either way.

FMorschel avatar Jul 25 '25 17:07 FMorschel

https://dart-review.googlesource.com/c/sdk/+/442249

fshcheglov avatar Jul 26 '25 20:07 fshcheglov

Thanks!

FMorschel avatar Jul 26 '25 23:07 FMorschel