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

Add Borrow and Ownership structs and fields #5

Merged
merged 2 commits into from
Jun 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ categories = ["development-tools"]
[dependencies]
rls-span = { version = "0.4", features = ["serialize-rustc"] }
rustc-serialize = "0.3"

[features]
borrows=[]
67 changes: 67 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ pub struct Analysis {
pub refs: Vec<Ref>,
pub macro_refs: Vec<MacroRef>,
pub relations: Vec<Relation>,
#[cfg(feature = "borrows")]
pub per_fn_borrows: Vec<BorrowData>,
}

impl Analysis {
#[cfg(not(feature = "borrows"))]
pub fn new() -> Analysis {
Analysis {
kind: Format::Json,
Expand All @@ -57,6 +60,21 @@ impl Analysis {
relations: vec![],
}
}

#[cfg(feature = "borrows")]
pub fn new() -> Analysis {
Analysis {
kind: Format::Json,
prelude: None,
imports: vec![],
defs: vec![],
impls: vec![],
refs: vec![],
macro_refs: vec![],
relations: vec![],
per_fn_borrows: vec![],
}
}
}

// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
Expand Down Expand Up @@ -240,3 +258,52 @@ pub struct SigElement {
pub start: usize,
pub end: usize,
}

// Each `BorrowData` represents all of the scopes, loans and moves
// within an fn or closure referred to by `ref_id`.
#[cfg(feature = "borrows")]
#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
pub struct BorrowData {
pub ref_id: Id,
pub scopes: Vec<Scope>,
pub loans: Vec<Loan>,
pub moves: Vec<Move>,
}

#[cfg(feature = "borrows")]
#[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy)]
pub enum BorrowKind {
ImmBorrow,
MutBorrow,
}

// Each `Loan` is either temporary or assigned to a variable.
// The `ref_id` refers to the value that is being loaned/borrowed.
// Not all loans will be valid. Invalid loans can be used to help explain
// improper usage.
#[cfg(feature = "borrows")]
#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
pub struct Loan {
pub ref_id: Id,
pub kind: BorrowKind,
pub span: SpanData,
}

// Each `Move` represents an attempt to move the value referred to by `ref_id`.
// Not all `Move`s will be valid but can be used to help explain improper usage.
#[cfg(feature = "borrows")]
#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
pub struct Move {
pub ref_id: Id,
pub span: SpanData,
}

// Each `Scope` refers to "scope" of a variable (we don't track all values here).
// Its ref_id refers to the variable, and the span refers to the scope/region where
// the variable is "live".
#[cfg(feature = "borrows")]
#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
pub struct Scope {
pub ref_id: Id,
pub span: SpanData,
}