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

REF: Fix introduce variable when first expression is inside nested block #5053

Merged

Conversation

dima74
Copy link
Member

@dima74 dima74 commented Mar 6, 2020

Fix that when extracting 7 to variable from this code:

let x1 = if true { 7 } else { 0 };
let x2 = 7;

it was transformed to

let x1 = if true {
    let i = 7;
    i
} else { 0 };
let x2 = i;

while correct result would be

let i = 7;
let x1 = if true { i } else { 0 };
let x2 = i;

@Undin Undin added the fix Pull requests that fix some bug(s) label Mar 6, 2020
@Undin Undin added this to In Progress in To test via automation Mar 6, 2020
Comment on lines 137 to 141
while (exprs.any { !PsiTreeUtil.isAncestor(anchor.parent, it, true) }) {
anchor = anchor.parent
}
return anchor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the following version does the same but faster

val parent = PsiTreeUtil.findCommonParent(anchor.parent, *exprs.toTypedArray())
while (anchor.parent != parent) {
    anchor = anchor.parent
}

Also, maybe it's worth to extract while loop into separate function because we have the same code in findAnchor above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you, updated

@dima74 dima74 force-pushed the fix-introduce-variable-nested-block branch from fb96f81 to a38b460 Compare March 8, 2020 12:26
@mchernyavsky mchernyavsky self-assigned this Mar 17, 2020
Copy link
Member

@mchernyavsky mchernyavsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors r+

@Undin Undin added this to the v119 milestone Mar 18, 2020
@mchernyavsky
Copy link
Member

bors r-

@mchernyavsky
Copy link
Member

bors r+

@Undin
Copy link
Member

Undin commented Mar 19, 2020

@mchernyavsky Looks like it's bors bug. The same was with #5057
@dima74 Could you rebase onto master, please? Last time it helped

@dima74 dima74 force-pushed the fix-introduce-variable-nested-block branch from a38b460 to 0938d93 Compare March 19, 2020 11:43
@dima74
Copy link
Member Author

dima74 commented Mar 19, 2020

@Undin yes, rebased

@mchernyavsky
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 19, 2020

@bors bors bot merged commit 2f89f6c into intellij-rust:master Mar 19, 2020
To test automation moved this from In Progress to Test Mar 19, 2020
@lancelote lancelote moved this from Test to Done in To test Mar 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Pull requests that fix some bug(s)
Projects
To test
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants