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

Associated items support #747

Closed
lnicola opened this issue Feb 5, 2019 · 5 comments
Closed

Associated items support #747

lnicola opened this issue Feb 5, 2019 · 5 comments
Labels
E-has-instructions Issue has some instructions and pointers to code to get started E-medium

Comments

@lnicola
Copy link
Member

lnicola commented Feb 5, 2019

struct Foo;

impl Foo {
    const BAR: i32 = 42;
}

fn main() {
    let x = Foo::BAR;
}

Foo::BAR does not get resolved.

@DJMcNab
Copy link
Contributor

DJMcNab commented Feb 5, 2019

Neither do associated functions (or types, if they can exist without a trait)

@matklad matklad added E-medium E-has-instructions Issue has some instructions and pointers to code to get started labels Feb 11, 2019
@matklad
Copy link
Member

matklad commented Feb 11, 2019

Given that we already can detect inherent impls, this shouldn't be too hard to add.

The relevant completion code is here:

https://github.com/rust-analyzer/rust-analyzer/blob/db6d214411505de6534fce183e9bea8109bc5283/crates/ra_ide_api/src/completion/complete_path.rs#L9

impls_in_crate query could be used to get the set of the impls. Using this set, one can find impls for a particular type:

https://github.com/rust-analyzer/rust-analyzer/blob/db6d214411505de6534fce183e9bea8109bc5283/crates/ra_hir/src/ty/method_resolution.rs#L43-L56

To get the ty to find the impls, we should resolve path prefix (Foo) to a Def and then use a type_for_def query. Note that this is slightly inaccurate, because it won't take possible substitutions into account, but this should be OK for completion.

@lnicola
Copy link
Member Author

lnicola commented Feb 11, 2019

I tried to look into it, but for some reason I have trouble building the code after making any changes. It did work after I added the first log calls, but now it doesn't compile any more.

$ cargo install-code
[snip]
$ git stash pop
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   complete_path.rs

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0d39549b2709395fc5569444efdb9ab2b919202c)
$ cargo install-code # why is it rebuilding the tools?
   Compiling num-traits v0.2.6
   Compiling serde v1.0.87
   Compiling spin v0.4.10
   Compiling syn v0.15.26
   Compiling lazy_static v1.2.0
   Compiling thread_local v0.3.6
   Compiling regex v1.1.0
   Compiling num-integer v0.1.39
   Compiling serde_derive v1.0.87
   Compiling pest_generator v2.1.0
   Compiling synstructure v0.10.1
   Compiling pest_derive v2.1.0
   Compiling failure_derive v0.1.5
   Compiling failure v0.1.5
   Compiling chrono v0.4.6
   Compiling serde_json v1.0.38
   Compiling ron v0.4.1
   Compiling tera v0.11.20
   Compiling teraron v0.0.1
   Compiling tools v0.1.0 (/home/grayshade/rust-analyzer/crates/tools)
    Finished dev [unoptimized + debuginfo] target(s) in 32.41s
     Running `/home/grayshade/rust-analyzer/target/debug/tools install-code`

will run: cargo install --path crates/ra_lsp_server --force
  Installing ra_lsp_server v0.1.0 (/home/grayshade/rust-analyzer/crates/ra_lsp_server)
   Compiling ra_ide_api v0.1.0 (/home/grayshade/rust-analyzer/crates/ra_ide_api)
error[E0599]: no method named `parse` found for type `&db::RootDatabase` in the current scope
  --> crates/ra_ide_api/src/completion.rs:49:28
   |
49 |     let original_file = db.parse(position.file_id);
   |                            ^^^^^
   |
   = note: the method `parse` exists but the following trait bounds were not satisfied:
           `&db::RootDatabase : ra_db::SourceDatabase`
           `db::RootDatabase : ra_db::SourceDatabase`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `parse`, perhaps you need to implement it:
           candidate #1: `ra_db::SourceDatabase`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: failed to compile `ra_lsp_server v0.1.0 (/home/grayshade/rust-analyzer/crates/ra_lsp_server)`, intermediate artifacts can be found at `/home/grayshade/rust-analyzer/target`

Caused by:
  Could not compile `ra_ide_api`.

To learn more, run the command again with --verbose.
Error: ErrorMessage { msg: "`cargo install --path crates/ra_lsp_server --force` exited with exit code: 101" }
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   complete_path.rs

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff                 
diff --git i/crates/ra_ide_api/src/completion/complete_path.rs w/crates/ra_ide_api/src/completion/complete_path.rs
index 39aefdb1..e05b4eff 100644
--- i/crates/ra_ide_api/src/completion/complete_path.rs
+++ w/crates/ra_ide_api/src/completion/complete_path.rs
@@ -7,14 +7,17 @@ use crate::{
 };
 
 pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
+    log::info!("complete_path");
     let path = match &ctx.path_prefix {
         Some(path) => path.clone(),
         _ => return,
     };
+    log::info!("{:?}", path);
     let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() {
         Some(Resolution::Def(def)) => def,
         _ => return,
     };
+    log::info!("{:?}", def);
     match def {
         hir::ModuleDef::Module(module) => {
             let module_scope = module.scope(ctx.db);
@@ -48,6 +51,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
                 }
             });
         }
+        // hir::ModuleDef::Struct(s) => {
+        //     let ty = s.ty(ctx.db);
+        //     log::info!("{:?}", ty);
+        // }
         _ => return,
     };
 }

Notice that I haven't touched the file it complains about.

@lnicola
Copy link
Member Author

lnicola commented Feb 11, 2019

Ah, rust-lang/rust#58291.

@lnicola
Copy link
Member Author

lnicola commented Feb 12, 2019

Filed #801.

bors bot added a commit that referenced this issue Feb 12, 2019
801: Implement completion for associated items r=matklad a=lnicola

Fixes #747.

r? @matklad

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
@bors bors bot closed this as completed in #801 Feb 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-has-instructions Issue has some instructions and pointers to code to get started E-medium
Projects
None yet
Development

No branches or pull requests

3 participants