Skip to content

Rust-analyzer inserts & (ampersand) character on the wrong line when auto-completing variable reference #12717

Closed
@stevepryde

Description

@stevepryde

When I auto-complete a function parameter that wants a reference, I get a & character randomly inserted on the wrong line in the same file.

It appears to use the line-number relative to the file rather than relative to the current function.

See below for code and steps to reproduce the issue.

rust-analyzer version: (eg. output of "Rust Analyzer: Show RA Version" command)
rust-analyzer version: 0.0.0 (75b2232 2022-07-03)

rustc version: (eg. output of rustc -V)
rustc 1.62.0 (a8314ef7d 2022-06-27)

#[derive(Debug)]
pub struct QueryResultRequest {
    query_id: String,
}

pub async fn query_result(request: QueryResultRequest) -> Result<String, String> {
    Ok(request.query_id)
}

pub fn show_value(value: &str) {
    println!("value: {value}");
}

fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test() {
        let request = QueryResultRequest {
            query_id: "test".to_string(),
        };

        query_result(request).await.unwrap();
    }
}

Steps to reproduce:

  1. Locate the line of code at the bottom (query_result(...))
  2. Above that line, type the following, but auto-complete the variable name
    show_value(request.que (then press ENTER to auto-complete query_id)

Upon auto-completion, observe an & character has been inserted on line 6 of the file, making the function signature now look like this:

pub async fn query_result(request: QueryResultRe&quest) -> Result<String, String> {
                                                ^

Activity

stevepryde

stevepryde commented on Jul 8, 2022

@stevepryde
Author

if I change #[tokio::test] to #[test] and remove the async etc. I can't reproduce the issue. May be related to that macro, not sure.

Veykril

Veykril commented on Jul 8, 2022

@Veykril
Member

Ah we don't consider macros for these ye, these are all the problematic locations

item.ref_match(ref_match, receiver.syntax().text_range().start());

item.ref_match(ref_match, path_ctx.path.syntax().text_range().start());

if let Some(ref_match) = compute_ref_match(completion, &ret_type) {
match func_kind {
FuncKind::Function(path_ctx) => {
item.ref_match(ref_match, path_ctx.path.syntax().text_range().start());
}
FuncKind::Method(DotAccess { receiver: Some(receiver), .. }, _) => {
item.ref_match(ref_match, receiver.syntax().text_range().start());
}
_ => (),
}
}

if let Some(ref_match) = compute_ref_match(completion, &ty) {
item.ref_match(ref_match, path_ctx.path.syntax().text_range().start());
}

We'll need to use

/// Attempts to map the node out of macro expanded files.
/// This only work for attribute expansions, as other ones do not have nodes as input.
pub fn original_ast_node<N: AstNode>(&self, node: N) -> Option<N> {
self.imp.original_ast_node(node)
}
for this I believe, if we get None back we shouldn't emit this special completion for now.

added
E-has-instructionsIssue has some instructions and pointers to code to get started
C-bugCategory: bug
on Jul 8, 2022
abusch

abusch commented on Jul 8, 2022

@abusch

I can also reproduce this by typing show_value(request.query_id); (without using completion), then executing the "Add reference here" code action, so it doesn't seem to only concern completion.

Veykril

Veykril commented on Jul 8, 2022

@Veykril
Member

thats the same issue in a different part of the project, feel free to create a new issue regarding that

0xPoe

0xPoe commented on Jul 16, 2022

@0xPoe
Member

I am working on this.

0xPoe

0xPoe commented on Jul 16, 2022

@0xPoe
Member

@rustbot claim

added a commit that references this issue on Jul 20, 2022

Auto merge of #12800 - hi-rustin:rustin-patch-issue-12717, r=hi-rustin

added a commit that references this issue on Jul 27, 2022

Auto merge of #12830 - hi-rustin:rustin-patch-issue-12717-fix, r=Veykril

2 remaining items

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

Metadata

Metadata

Assignees

Labels

A-completionautocompletionC-bugCategory: bugE-has-instructionsIssue has some instructions and pointers to code to get started

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @lnicola@abusch@Veykril@stevepryde@0xPoe

      Issue actions

        Rust-analyzer inserts & (ampersand) character on the wrong line when auto-completing variable reference · Issue #12717 · rust-lang/rust-analyzer