Skip to content

Commit

Permalink
fix: go to definition from aliased use (#5396)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves
#5390 (comment)

## 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.
  • Loading branch information
asterite authored Jul 3, 2024
1 parent f35614a commit 90b135c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions tooling/lsp/src/requests/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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");
};
}
}
1 change: 1 addition & 0 deletions tooling/lsp/test_programs/go_to_definition/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod foo {
}

use foo::another_function;
use foo::another_function as aliased_function;

fn some_function() -> Field {
1 + 2
Expand Down

0 comments on commit 90b135c

Please sign in to comment.