Skip to content

fix: prevent renaming aliased functions during rename request#3189

Open
nikhilcodes777 wants to merge 1 commit intozigtools:masterfrom
nikhilcodes777:fix/lsp-rename
Open

fix: prevent renaming aliased functions during rename request#3189
nikhilcodes777 wants to merge 1 commit intozigtools:masterfrom
nikhilcodes777:fix/lsp-rename

Conversation

@nikhilcodes777
Copy link
Copy Markdown

Fixes #3184

Description

This fixes an issue where triggering a textDocument/rename on a local alias of a function (e.g., const testName = std.debug.print;) would incorrectly resolve the alias to its source and rename the original declaration (e.g., renaming print inside the Zig standard library).

Root Cause:
During a rename request, referencesHandler and the reference-gathering Builder were aggressively resolving aliases using analyser.resolveVarDeclAlias. While this is desired for textDocument/references or highlight, it results in cross-file destruction during a rename.

Changes:

  • Added a resolve_aliases boolean flag to the Builder struct in src/features/references.zig.
  • Conditionally bypassed resolveVarDeclAlias in both referencesHandler and Builder.referenceNode if request == .rename.
  • This ensures that a rename operation strictly targets the literal identifier the user initiated the rename on, preserving the underlying source declaration.

Environment / Tested On

  • zig 0.16.0
  • zig 0.17.0-dev.215+8c5542bd3
  • emacs/neovim

Test Case Used

const std = @import("std");

const foo = struct {
    pub fn print(comptime fmt: []const u8, args: anytype) void {
        std.debug.print(fmt, args);
    }
};

const fein = foo.print;

pub fn main() void {
    // Renaming `fein` here no longer renames `foo.print`
    fein("Hello world \n", .{});
}

neoto added a commit to neoto/zls that referenced this pull request May 2, 2026
Handles references to aliased decls more correctly by distinguishing
between containers and runtime types. Compared to
zigtools#3189 which special cases renames,
this _should_ address the root cause, fixing both renaming and
highlighting symbols. I also added regression tests.

Closes zigtools#3184
neoto added a commit to neoto/zls that referenced this pull request May 2, 2026
Handles references to aliased decls more correctly by distinguishing
between containers and runtime types. Compared to
zigtools#3189 which special cases renames,
this _should_ address the root cause, fixing both renaming and
highlighting symbols. I also added regression tests that fail without
the changes on this branch.

Closes zigtools#3184
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.

Rename symbol renames the value instead of the constant binding

1 participant