Skip to content

Add inline variable refactoring - #2754

Merged
ahoppen merged 7 commits into
swiftlang:mainfrom
somiljain2006:Inline
Sep 10, 2026
Merged

ahoppen merged 7 commits into
swiftlang:mainfrom
somiljain2006:Inline

Conversation

@somiljain2006

@somiljain2006 somiljain2006 commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2507

Implements Inline Variable for local let declarations with lexical lookup

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tackling this, @somiljain2006. I left a few suggestions inline.

Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift Outdated
@somiljain2006
somiljain2006 requested a review from ahoppen August 28, 2026 06:26

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Looks a lot simpler now already. I have left a few more suggestions inline.

Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift Outdated
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift Outdated
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift Outdated

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have a couple more nitpicky comments comments, otherwise looks great to me 👍🏽

Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One last nitpick, then we’re ready to ship.

Comment thread Sources/SwiftSyntaxCodeActions/InlineVariable.swift Outdated

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let’s 🚢 it

@ahoppen

ahoppen commented Sep 4, 2026

Copy link
Copy Markdown
Member

@swift-ci Please test

@somiljain2006

Copy link
Copy Markdown
Contributor Author

@ahoppen I'm having trouble identifying the exact cause of the Windows CI failure. The build fails during CMake generation because the SwiftSyntax::SwiftLexicalLookup target cannot be found. I checked the top-level CMakeLists.txt, where SwiftSyntax is already brought in with find_package(SwiftSyntax CONFIG REQUIRED), but I haven't been able to figure out whether the issue is with how the dependency is declared in SourceKit-LSP or with the SwiftSyntax CMake package used by the Windows Stage2 toolchain. Can you tell me what I'm missing?

@ahoppen

ahoppen commented Sep 4, 2026

Copy link
Copy Markdown
Member

Seems like we need to expose SwiftLexicalLookup before we can use it because this is the first change that’s using it. Opened swiftlang/swift#91985 to do so.

ahoppen added a commit to swiftlang/swift that referenced this pull request Sep 9, 2026
…ULES`

It seems like we can’t reference `SwiftLexicalLookup` from CMake in SourceKit-LSP because it’s not in this list. `SwiftLexicalLookup` will be used by swiftlang/sourcekit-lsp#2754, so we need to expose it.
@ahoppen

ahoppen commented Sep 9, 2026

Copy link
Copy Markdown
Member

The Windows issue should be fixed now. Let's try again.

@swift-ci Please test Windows

@ahoppen
ahoppen merged commit c55899a into swiftlang:main Sep 10, 2026
3 checks passed
@somiljain2006
somiljain2006 deleted the Inline branch September 10, 2026 06:35
@somiljain2006

Copy link
Copy Markdown
Contributor Author

@ahoppen How about extending the inline variable code action to support var declarations?

@ahoppen

ahoppen commented Sep 14, 2026

Copy link
Copy Markdown
Member

I don’t think extending it to var is all that important and reasoning about whether the variable is never modified is non-trivial (eg. need to also consider if it’s ever passed as inout to a function). But if you have a good proposal of how to implement it, I won’t object.

@somiljain2006

somiljain2006 commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor Author

@ahoppen I was thinking of this approach: only offer the action when the variable has an initializer and all resolved references are read-only. I would reject direct mutations (=, +=, etc.), inout uses such as &x, and any cases where a known mutation pattern is detected. More complex cases like closure captures and mutable pointer APIs could remain unsupported initially.

@ahoppen

ahoppen commented Sep 16, 2026

Copy link
Copy Markdown
Member

I think += actually raises a good point where you can’t fully decide if a variable is read-only. You could define your own operator ++= that takes a first argument as inout. Then, if you write, x ++= [2], x would be mutated but you had no way of knowing that without running the type checker.

I personally think that this would be a fairly big investment to do correctly with marginal pay-offs, so I wouldn’t do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Inline temp variable code action

2 participants