-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[WIP] Generating member wise initializer with local refactoring #16983
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Person { | ||
internal init(firstName: String?, lastName: String?, age: Int?) { | ||
self.firstName = firstName | ||
self.lastName = lastName | ||
self.age = age | ||
} | ||
|
||
var firstName: String! | ||
var lastName: String! | ||
var age: Int! | ||
var planet = "Earth", solarSystem = "Milky Way" | ||
var avgHeight = 175 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Person { | ||
var firstName: String! | ||
var lastName: String! | ||
var age: Int! | ||
var planet = "Earth", solarSystem = "Milky Way" | ||
var avgHeight = 175 | ||
} | ||
|
||
// RUN: %empty-directory(%t.result) | ||
// RUN: %refactor -memberwise-init -source-filename %s -pos=1:8 > %t.result/class_members.swift | ||
// RUN: diff -u %S/Outputs/class_members/class_members.swift.expected %t.result/class_members.swift | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also add an applicable test in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nkcsgexi should this test be written in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a new test file will be better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nkcsgexi So I have a file
and this seems to be causing an assertion failure: Any idea what's going on? Am I missing some LLVM command or something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nkcsgexi the above assertion seems to be failing for the the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured out the problem, I was passing the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Person { | ||
var firstName: String! | ||
var lastName: String! | ||
var age: Int! | ||
var planet = "Earth", solarSystem = "Milky Way" | ||
var avgHeight = 175 | ||
} | ||
|
||
let _ = Person() | ||
|
||
// RUN: %refactor -source-filename %s -pos=9:10 | %FileCheck %s -check-prefix=CHECK-NONE | ||
// CHECK-NONE: Action begins | ||
// CHECK-NONE-NEXT: Action ends |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this for-loop into
canGenerateMemberwiseInit
? Instead of returningClassDecl*
,canGenerateMemberwiseInit
should populatememberTypeVector
andmemberNameVector
.isApplicable
returns true only when these two vectors aren't empty. This way ensures that whenever the refactoring is available, we are going to change the users' codebase after applying it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not clear about this. If I move the said for loop into
canGenerateMemberwiseInit
,memberTypeVector
andmemberNameVector
will be local to that function. WhenperformChange
is called, it checks ifcanGeneratememberWiseInit
returns a valid object and then callsgenerateMemberwiseInit
which needs these vectors to emit the generated initializer. So my question is should we pass in references to the two vectors so that they are available out side the scope of the function and is it okay to have these vectors be populated twice?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohitathwani yeah, you're right! We should pass
memberTypeVector
andmemberTypeVector
as reference tocanGenerateMemberwiseInit
(maybe we should rename it tocolllectMemberForInit
.isApplicable
will check whether these vectors are empty andperformChange
will use these two vectors for actual code change.