From 737b4496126589f1330abe4a99153710b46ef211 Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Mon, 29 May 2017 10:51:36 -0400 Subject: [PATCH 1/2] Add Borrow and Ownership structs and fields --- src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 4f9e634..92f1aaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ pub struct Analysis { pub refs: Vec, pub macro_refs: Vec, pub relations: Vec, + pub borrows: Vec, } impl Analysis { @@ -55,6 +56,7 @@ impl Analysis { refs: vec![], macro_refs: vec![], relations: vec![], + borrows: vec![], } } } @@ -240,3 +242,59 @@ pub struct SigElement { pub start: usize, pub end: usize, } + +#[derive(Debug, Clone, RustcDecodable, RustcEncodable)] +pub struct Borrows { + pub ref_id: Id, + pub assignments: Vec, + pub loans: Vec, + pub moves: Vec, +} + +#[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy)] +pub enum BorrowKind { + ImmBorrow, + MutBorrow, +} + +#[derive(Debug, Clone, RustcDecodable, RustcEncodable)] +pub enum CauseData { + ClosureCapture(SpanData), + AddrOf, + AutoRef, + AutoUnsafe, + RefBinding, + OverloadedOperator, + ClosureInvocation, + ForLoop, + MatchDiscriminant +} + +#[derive(Debug, Clone, RustcDecodable, RustcEncodable)] +pub struct Loan { + pub ref_id: Id, + pub kind: BorrowKind, + pub span: SpanData, + pub cause: CauseData, +} + +#[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy)] +pub enum MoveKindData { + Declared, + MoveExpr, + MovePat, + Captured, +} + +#[derive(Debug, Clone, RustcDecodable, RustcEncodable)] +pub struct Move { + pub ref_id: Id, + pub kind: MoveKindData, + pub span: SpanData, +} + +#[derive(Debug, Clone, RustcDecodable, RustcEncodable)] +pub struct Assignment { + pub ref_id: Id, + pub span: SpanData, +} From 6507f379787165f4aa858728283c5e8ebdd264fd Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Sat, 17 Jun 2017 09:46:00 -0400 Subject: [PATCH 2/2] Removed cause structs, documented borrow types and hid them behind new 'borrows' feature flag --- Cargo.lock | 2 +- Cargo.toml | 3 +++ src/lib.rs | 65 +++++++++++++++++++++++++++++++----------------------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3436121..030b228 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "rls-data" -version = "0.5.0" +version = "0.6.0" dependencies = [ "rls-span 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 7535133..fbef6e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,6 @@ categories = ["development-tools"] [dependencies] rls-span = { version = "0.4", features = ["serialize-rustc"] } rustc-serialize = "0.3" + +[features] +borrows=[] diff --git a/src/lib.rs b/src/lib.rs index 92f1aaf..4fdf7e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,10 +42,12 @@ pub struct Analysis { pub refs: Vec, pub macro_refs: Vec, pub relations: Vec, - pub borrows: Vec, + #[cfg(feature = "borrows")] + pub per_fn_borrows: Vec, } impl Analysis { + #[cfg(not(feature = "borrows"))] pub fn new() -> Analysis { Analysis { kind: Format::Json, @@ -56,7 +58,21 @@ impl Analysis { refs: vec![], macro_refs: vec![], relations: vec![], - borrows: 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![], } } } @@ -243,58 +259,51 @@ pub struct SigElement { 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 Borrows { +pub struct BorrowData { pub ref_id: Id, - pub assignments: Vec, + pub scopes: Vec, pub loans: Vec, pub moves: Vec, } +#[cfg(feature = "borrows")] #[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy)] pub enum BorrowKind { ImmBorrow, MutBorrow, } -#[derive(Debug, Clone, RustcDecodable, RustcEncodable)] -pub enum CauseData { - ClosureCapture(SpanData), - AddrOf, - AutoRef, - AutoUnsafe, - RefBinding, - OverloadedOperator, - ClosureInvocation, - ForLoop, - MatchDiscriminant -} - +// 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, - pub cause: CauseData, -} - -#[derive(Debug, RustcDecodable, RustcEncodable, Clone, Copy)] -pub enum MoveKindData { - Declared, - MoveExpr, - MovePat, - Captured, } +// 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 kind: MoveKindData, 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 Assignment { +pub struct Scope { pub ref_id: Id, pub span: SpanData, }