Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Jan 21, 2018
1 parent 6dd8a79 commit ab863c5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use {AnalysisHost, AnalysisLoader};
use {AnalysisHost, AnalysisLoader, Span};
use raw::DefKind;

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use span::{Row, Column, Position, Range};

#[cfg(test)]
extern crate env_logger;

#[derive(Clone, new)]
#[derive(Clone, new, Debug)]
struct TestAnalysisLoader {
path: PathBuf,
}
Expand Down Expand Up @@ -377,3 +378,51 @@ fn test_extern_fn() {
let def = host.goto_def(&spans[1]);
assert_eq!(def.unwrap(), spans[0]);
}

#[test]
fn test_trait_refs_defs() {
let host = AnalysisHost::new_with_loader(TestAnalysisLoader::new(
Path::new("test_data/traits/save-analysis").to_owned(),
));
host.reload(
Path::new("test_data/traits"),
Path::new("test_data/traits"),
).unwrap();

// make a span for row 4:
// let _ignored = mymod::MyStruct.trait_fn();
// --------
let row = Row::new_one_indexed(4);
let col_start = Column::new_one_indexed(36);
let col_end = Column::new_one_indexed(44);
let pos_start = Position::new(row, col_start);
let pos_end = Position::new(row, col_end);
let range = Range::from_positions(pos_start, pos_end).zero_indexed();

let span = Span::from_range(range, "test_data/traits/src/main.rs");

// this is an id for a Method
let id = host.crate_local_id(&span).expect("No id for span");

// Def { kind: Method }
let def = host.get_def(id.clone()).expect("No def for id");

// references to the method
let _spans = host.find_all_refs(&span, true, true).expect("No refs for span");

// TODO below we go awry... we don't necessarily want to step up to the
// type level, just to go down on method level again in the trait, or do we?
// regardless, it seems the find_impls() needs some help to work with
// type id connected to trait id, rather than the span.

// the type that owns the method
let typ = def.parent.expect("No parent for method");

// the impls of the type, which are just spans for the
// impl rows i.e.:
// impl MyTrait for MyStruct {
let impls = host.find_impls(typ);

println!("{:?}", impls);

}
4 changes: 4 additions & 0 deletions test_data/make_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function build {
output="$(pwd)/$2"
pushd "$1" > /dev/null
RUSTFLAGS=-Zsave-analysis cargo build
mkdir -p "$output"
cp target/debug/deps/save-analysis/*.json "$output"
# strip all hashes from filenames libfoo-[hash].json -> libfoo.json
for from in $output/*.json; do
Expand All @@ -33,3 +34,6 @@ build types types/save-analysis

# Expressions
build exprs exprs/save-analysis

# Traits
build traits traits/save-analysis
4 changes: 4 additions & 0 deletions test_data/traits/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test_data/traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "traits"
version = "0.1.0"
authors = ["Martin Algesten <martin@algesten.se>"]

[dependencies]
1 change: 1 addition & 0 deletions test_data/traits/save-analysis/traits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"config":{"output_file":null,"full_docs":false,"pub_only":false,"reachable_only":false,"distro_crate":false,"signatures":false,"borrow_data":false},"prelude":{"crate_id":{"name":"traits","disambiguator":[6574486321634384995,13266009993909822779]},"crate_root":"src","external_crates":[{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":1,"id":{"name":"std","disambiguator":[5635623747966431370,9971070040144799506]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":2,"id":{"name":"core","disambiguator":[6577361167932606010,8388015279830798219]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":3,"id":{"name":"alloc","disambiguator":[12461495361646917922,5069735510726305683]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":4,"id":{"name":"std_unicode","disambiguator":[15096347202641039511,3335382139143332623]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":5,"id":{"name":"alloc_system","disambiguator":[6362029436205834945,17747094235936482455]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":6,"id":{"name":"libc","disambiguator":[12382407166823970462,5402810058697335335]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":7,"id":{"name":"unwind","disambiguator":[16217520973502938390,17984160495595433110]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":8,"id":{"name":"compiler_builtins","disambiguator":[3090944145366651624,5924840772928208202]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":9,"id":{"name":"alloc_jemalloc","disambiguator":[12744368319836552009,7932904225750845754]}},{"file_name":"/Users/martin/dev/_dist/rls-analysis/test_data/traits/src/main.rs","num":10,"id":{"name":"panic_unwind","disambiguator":[4298327005270871991,11052037223498076100]}}],"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":0,"byte_end":262,"line_start":1,"line_end":18,"column_start":1,"column_end":2}},"imports":[{"kind":"Use","ref_id":{"krate":0,"index":14},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":11,"byte_end":18,"line_start":1,"line_end":1,"column_start":12,"column_end":19},"name":"MyTrait","value":"","parent":{"krate":0,"index":0}}],"defs":[{"kind":"Mod","id":{"krate":0,"index":0},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":0,"byte_end":262,"line_start":1,"line_end":18,"column_start":1,"column_end":2},"name":"","qualname":"::","value":"src/main.rs","parent":null,"children":[{"krate":0,"index":2},{"krate":0,"index":4},{"krate":0,"index":6},{"krate":0,"index":8},{"krate":0,"index":10}],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Function","id":{"krate":0,"index":8},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":28,"byte_end":32,"line_start":3,"line_end":3,"column_start":8,"column_end":12},"name":"main","qualname":"::main","value":"fn () -> ()","parent":null,"children":[],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Local","id":{"krate":0,"index":4294967286},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":45,"byte_end":53,"line_start":4,"line_end":4,"column_start":9,"column_end":17},"name":"_ignored","qualname":"_ignored$9","value":"()","parent":null,"children":[],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Mod","id":{"krate":0,"index":10},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":91,"byte_end":96,"line_start":7,"line_end":7,"column_start":5,"column_end":10},"name":"mymod","qualname":"::mymod","value":"src/main.rs","parent":null,"children":[{"krate":0,"index":12},{"krate":0,"index":14},{"krate":0,"index":18}],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Struct","id":{"krate":0,"index":12},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":114,"byte_end":122,"line_start":8,"line_end":8,"column_start":16,"column_end":24},"name":"MyStruct","qualname":"::mymod::MyStruct","value":"","parent":null,"children":[],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Trait","id":{"krate":0,"index":14},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":139,"byte_end":146,"line_start":10,"line_end":10,"column_start":15,"column_end":22},"name":"MyTrait","qualname":"::mymod::MyTrait","value":"MyTrait","parent":null,"children":[{"krate":0,"index":16}],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Method","id":{"krate":0,"index":16},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":160,"byte_end":168,"line_start":11,"line_end":11,"column_start":12,"column_end":20},"name":"trait_fn","qualname":"::mymod::MyTrait::trait_fn","value":"fn (&self) -> ()","parent":{"krate":0,"index":14},"children":[],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Local","id":{"krate":0,"index":4294967264},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":237,"byte_end":241,"line_start":15,"line_end":15,"column_start":22,"column_end":26},"name":"self","qualname":"<MyStruct as mymod::MyTrait>::trait_fn::self","value":"&mymod::MyStruct","parent":null,"children":[],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Method","id":{"krate":0,"index":20},"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":227,"byte_end":235,"line_start":15,"line_end":15,"column_start":12,"column_end":20},"name":"trait_fn","qualname":"<MyStruct as mymod::MyTrait>::trait_fn","value":"fn (&self) -> ()","parent":{"krate":0,"index":14},"children":[],"decl_id":{"krate":0,"index":16},"docs":"","sig":null,"attributes":[]}],"impls":[],"refs":[{"kind":"Mod","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":0,"byte_end":0,"line_start":1,"line_end":1,"column_start":1,"column_end":1},"ref_id":{"krate":4294967295,"index":4294967295}},{"kind":"Mod","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":0,"byte_end":0,"line_start":1,"line_end":1,"column_start":1,"column_end":1},"ref_id":{"krate":4294967295,"index":4294967295}},{"kind":"Mod","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":0,"byte_end":0,"line_start":1,"line_end":1,"column_start":1,"column_end":1},"ref_id":{"krate":4294967295,"index":4294967295}},{"kind":"Type","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":11,"byte_end":18,"line_start":1,"line_end":1,"column_start":12,"column_end":19},"ref_id":{"krate":0,"index":14}},{"kind":"Mod","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":4,"byte_end":9,"line_start":1,"line_end":1,"column_start":5,"column_end":10},"ref_id":{"krate":4294967295,"index":4294967295}},{"kind":"Function","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":72,"byte_end":80,"line_start":4,"line_end":4,"column_start":36,"column_end":44},"ref_id":{"krate":0,"index":16}},{"kind":"Type","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":63,"byte_end":71,"line_start":4,"line_end":4,"column_start":27,"column_end":35},"ref_id":{"krate":0,"index":12}},{"kind":"Mod","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":56,"byte_end":61,"line_start":4,"line_end":4,"column_start":20,"column_end":25},"ref_id":{"krate":4294967295,"index":4294967295}},{"kind":"Type","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":205,"byte_end":213,"line_start":14,"line_end":14,"column_start":22,"column_end":30},"ref_id":{"krate":0,"index":12}},{"kind":"Type","span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":193,"byte_end":200,"line_start":14,"line_end":14,"column_start":10,"column_end":17},"ref_id":{"krate":0,"index":14}}],"macro_refs":[],"relations":[{"span":{"file_name":[115,114,99,47,109,97,105,110,46,114,115],"byte_start":205,"byte_end":213,"line_start":14,"line_end":14,"column_start":22,"column_end":30},"kind":"Impl","from":{"krate":0,"index":12},"to":{"krate":0,"index":14}}]}
18 changes: 18 additions & 0 deletions test_data/traits/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use mymod::MyTrait;

pub fn main() {
let _ignored = mymod::MyStruct.trait_fn();
}

mod mymod {
pub struct MyStruct;

pub trait MyTrait {
fn trait_fn(&self);
}

impl MyTrait for MyStruct {
fn trait_fn(&self) {
}
}
}

0 comments on commit ab863c5

Please sign in to comment.