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

Field rename does not rename struct initialization's fields #1837

Open
johan0A opened this issue Mar 24, 2024 · 1 comment
Open

Field rename does not rename struct initialization's fields #1837

johan0A opened this issue Mar 24, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@johan0A
Copy link

johan0A commented Mar 24, 2024

Zig Version

0.12.0-dev.3245+4f782d1e8

Zig Language Server Version

0.12.0-dev.496+96eddd0

Client / Code Editor / Extensions

vscode

Steps to Reproduce and Observed Behavior

Rename field inside TestStruct, initialize struct using syntax below ⇾ the initialization does not get updated

const TestStruct = struct {
    test_var_renamed: i32, //field was renamed here
};

test "test" {
    const test_struct: TestStruct = .{
        .test_var = 1, //does not get renamed
    };

    const test_struct2: TestStruct = TestStruct{
        .test_var = 1, //does not get renamed
    };

    std.debug.print("{}", .{test_struct});
    std.debug.print("{}", .{test_struct2});
}

Expected Behavior

const TestStruct = struct {
    test_var_renamed: i32, //field was renamed here
};

test "test" {
    const test_struct: TestStruct = .{
        .test_var_renamed = 1, //gets renamed
    };

    const test_struct2: TestStruct = TestStruct{
        .test_var_renamed = 1, //gets renamed
    };

    std.debug.print("{}", .{test_struct});
    std.debug.print("{}", .{test_struct2});
}

Relevant log output

No response

@johan0A johan0A added the bug Something isn't working label Mar 24, 2024
@johan0A johan0A changed the title Rename doesn't rename struct initialization Field rename does not rename struct initialization's fields Mar 28, 2024
@WillLillis
Copy link
Contributor

WillLillis commented May 26, 2024

I'm also running into this issue using 0.13.0-dev.29+bb19bee. It looks like zls is ignoring the initialization accesses to the struct's fields when it searches for references as part of the renaming request. These references are also missing when using the server's textDocument/reference capability. I'm not super familiar with the language/ zls code base yet, but after a bit of poking around it looks like the references are missed here:

fn referenceNode(self: *const Context, tree: Ast, node: Ast.Node.Index) error{OutOfMemory}!void {
const builder = self.builder;
const handle = self.handle;
const node_tags = tree.nodes.items(.tag);
const datas = tree.nodes.items(.data);
const token_tags = tree.tokens.items(.tag);
const starts = tree.tokens.items(.start);
switch (node_tags[node]) {
.identifier,
.test_decl,
=> {
const identifier_token = Analyser.getDeclNameToken(tree, node) orelse return;
if (token_tags[identifier_token] != .identifier) return;
const child = (try builder.analyser.lookupSymbolGlobal(
handle,
offsets.tokenToSlice(tree, identifier_token),
starts[identifier_token],
)) orelse return;
if (builder.decl_handle.eql(child)) {
try builder.add(handle, identifier_token);
}
},
.field_access => {
const lhs = try builder.analyser.resolveTypeOfNode(.{ .node = datas[node].lhs, .handle = handle }) orelse return;
const deref_lhs = try builder.analyser.resolveDerefType(lhs) orelse lhs;
const symbol = offsets.tokenToSlice(tree, datas[node].rhs);
const child = (try deref_lhs.lookupSymbol(builder.analyser, symbol)) orelse return;
if (builder.decl_handle.eql(child)) {
try builder.add(handle, datas[node].rhs);
}
},
else => {},
}
}
};

If I add a .assign_destructure arm to the switch block, it looks like that matches up with the missing references to the field initializations (I'm still not 100% confident, though). Still working on how to properly add the appropriate information to the references builder, but hopefully this helps narrow down the issue :) Nevermind, definitely not .assign_destructure.

Related #1700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants