From 90b135c44bdf91603f2e2cdf0ab6f168087bab36 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 3 Jul 2024 10:06:07 -0300 Subject: [PATCH] fix: go to definition from aliased use (#5396) # Description ## Problem Resolves https://github.com/noir-lang/noir/pull/5390#issuecomment-2205880047 ## Summary Go to definition didn't work in this case: ```rust use some::name as aliased_name; // ~~~~ click here ``` https://github.com/noir-lang/noir/assets/209371/e35ca028-69e0-4e74-8fce-9c63a1376232 Also adds a test that clicking "aliased_name" also works the same way. ## Additional Context ## Documentation Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- .../src/hir/def_collector/dc_crate.rs | 7 +++- tooling/lsp/src/requests/goto_definition.rs | 34 +++++++++++++++++++ .../go_to_definition/src/main.nr | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index 5f860e971c9..cd670167d2c 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -348,7 +348,12 @@ impl DefCollector { .import(name.clone(), ns, resolved_import.is_prelude); let file_id = current_def_map.file_id(module_id); - add_import_reference(ns, &name, &mut context.def_interner, file_id); + let last_segment = collected_import.path.last_segment(); + + add_import_reference(ns, &last_segment, &mut context.def_interner, file_id); + if let Some(ref alias) = collected_import.alias { + add_import_reference(ns, alias, &mut context.def_interner, file_id); + } if let Err((first_def, second_def)) = result { let err = DefCollectorErrorKind::Duplicate { diff --git a/tooling/lsp/src/requests/goto_definition.rs b/tooling/lsp/src/requests/goto_definition.rs index 158b8a1dc35..d52211c08f9 100644 --- a/tooling/lsp/src/requests/goto_definition.rs +++ b/tooling/lsp/src/requests/goto_definition.rs @@ -46,6 +46,7 @@ mod goto_definition_tests { use std::panic; use crate::test_utils::{self, search_in_file}; + use lsp_types::{Position, Range}; use tokio::test; use super::*; @@ -93,4 +94,37 @@ mod goto_definition_tests { async fn goto_from_function_location_to_declaration() { expect_goto("go_to_definition", "another_function", 0).await; } + + #[test] + async fn goto_from_use_as() { + let (mut state, noir_text_document) = test_utils::init_lsp_server("go_to_definition").await; + + let params = GotoDefinitionParams { + text_document_position_params: lsp_types::TextDocumentPositionParams { + text_document: lsp_types::TextDocumentIdentifier { + uri: noir_text_document.clone(), + }, + position: Position { line: 7, character: 29 }, // The word after `as` + }, + work_done_progress_params: Default::default(), + partial_result_params: Default::default(), + }; + + let response = on_goto_definition_request(&mut state, params) + .await + .expect("Could execute on_goto_definition_request") + .unwrap_or_else(|| panic!("Didn't get a goto definition response")); + + if let GotoDefinitionResponse::Scalar(location) = response { + assert_eq!( + location.range, + Range { + start: Position { line: 1, character: 11 }, + end: Position { line: 1, character: 27 } + } + ); + } else { + panic!("Expected a scalar response"); + }; + } } diff --git a/tooling/lsp/test_programs/go_to_definition/src/main.nr b/tooling/lsp/test_programs/go_to_definition/src/main.nr index 01ea5a3eadf..c62abb257f2 100644 --- a/tooling/lsp/test_programs/go_to_definition/src/main.nr +++ b/tooling/lsp/test_programs/go_to_definition/src/main.nr @@ -5,6 +5,7 @@ mod foo { } use foo::another_function; +use foo::another_function as aliased_function; fn some_function() -> Field { 1 + 2