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

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

Closed
stevepryde opened this issue Jul 8, 2022 · 7 comments
Assignees
Labels
A-completion autocompletion C-bug Category: bug E-has-instructions Issue has some instructions and pointers to code to get started

Comments

@stevepryde
Copy link

stevepryde commented Jul 8, 2022

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> {
                                                ^
@stevepryde
Copy link
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
Copy link
Member

Veykril commented Jul 8, 2022

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.

@Veykril Veykril added A-completion autocompletion E-has-instructions Issue has some instructions and pointers to code to get started C-bug Category: bug labels Jul 8, 2022
@abusch
Copy link

abusch commented Jul 8, 2022

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
Copy link
Member

Veykril commented Jul 8, 2022

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

@Rustin170506
Copy link
Member

I am working on this.

@Rustin170506
Copy link
Member

@rustbot claim

bors added a commit that referenced this issue Jul 20, 2022
Find original as node before compute ref match

part of #12717
bors added a commit that referenced this issue Jul 27, 2022
Find original ast node before compute ref match

ref #12717
@lnicola
Copy link
Member

lnicola commented Aug 1, 2022

I think this was fixed in #12800 and #12830.

@lnicola lnicola closed this as completed Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion autocompletion C-bug Category: bug E-has-instructions Issue has some instructions and pointers to code to get started
Projects
None yet
Development

No branches or pull requests

5 participants