]> git.lizzy.rs Git - rust.git/commitdiff
Adding documentation, indentation fixes
authorgaurikholkar <f2013002@goa.bits-pilani.ac.in>
Thu, 27 Apr 2017 20:26:12 +0000 (13:26 -0700)
committergaurikholkar <f2013002@goa.bits-pilani.ac.in>
Fri, 28 Apr 2017 10:58:04 +0000 (03:58 -0700)
src/librustc/hir/map/mod.rs
src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs
src/librustc_borrowck/borrowck/gather_loans/move_error.rs
src/test/ui/issue-40402-ref-hints/issue-40402-1.rs
src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr
src/test/ui/issue-40402-ref-hints/issue-40402-2.rs
src/test/ui/issue-40402-ref-hints/issue-40402-2.stderr

index 1948dd909f470b21b48f5b11a6dc2c1594d5e6b5..48b8a819fff03eb53d018baebac65a13f04db6de 100644 (file)
@@ -95,14 +95,6 @@ enum MapEntry<'hir> {
     RootCrate,
 }
 
-/// Represents the kind of pattern
-#[derive(Debug, Clone, Copy)]
-pub enum PatternSource<'hir> {
-    MatchExpr(&'hir Expr),
-    LetDecl(&'hir Local),
-    Other,
-}
-
 impl<'hir> Clone for MapEntry<'hir> {
     fn clone(&self) -> MapEntry<'hir> {
         *self
@@ -645,7 +637,7 @@ pub fn get_module_parent(&self, id: NodeId) -> NodeId {
             Err(id) => id,
         }
     }
-    
+
     /// Returns the nearest enclosing scope. A scope is an item or block.
     /// FIXME it is not clear to me that all items qualify as scopes - statics
     /// and associated types probably shouldn't, for example. Behaviour in this
index 18c9858e8d6b51a3fbdf3bc43835cf9628c847db..f193588dd7d6c605e77849de7abd94b2c249d2e2 100644 (file)
@@ -11,7 +11,7 @@
 //! Computes moves.
 
 use borrowck::*;
-use borrowck::gather_loans::move_error::MoveSpanAndPath;
+use borrowck::gather_loans::move_error::MovePlace;
 use borrowck::gather_loans::move_error::{MoveError, MoveErrorCollector};
 use borrowck::move_data::*;
 use rustc::middle::expr_use_visitor as euv;
 use syntax_pos::Span;
 use rustc::hir::*;
 use rustc::hir::map::Node::*;
-use rustc::hir::map::{PatternSource};
 
 struct GatherMoveInfo<'tcx> {
     id: ast::NodeId,
     kind: MoveKind,
     cmt: mc::cmt<'tcx>,
-    span_path_opt: Option<MoveSpanAndPath<'tcx>>
+    span_path_opt: Option<MovePlace<'tcx>>
 }
 
-/// Returns the kind of the Pattern
+/// Represents the kind of pattern
+#[derive(Debug, Clone, Copy)]
+pub enum PatternSource<'tcx> {
+    MatchExpr(&'tcx Expr),
+    LetDecl(&'tcx Local),
+    Other,
+}
+
+/// Analyzes the context where the pattern appears to determine the
+/// kind of hint we want to give. In particular, if the pattern is in a `match`
+/// or nested within other patterns, we want to suggest a `ref` binding:
+///
+///     let (a, b) = v[0]; // like the `a` and `b` patterns here
+///     match v[0] { a => ... } // or the `a` pattern here
+///
+/// But if the pattern is the outermost pattern in a `let`, we would rather
+/// suggest that the author add a `&` to the initializer:
+///
+///     let x = v[0]; // suggest `&v[0]` here
+///
+/// In this latter case, this function will return `PatternSource::LetDecl`
+/// with a reference to the let
 fn get_pattern_source<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &Pat) -> PatternSource<'tcx> {
 
     let parent = tcx.hir.get_parent_node(pat.id);
@@ -132,7 +152,7 @@ pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
     let source = get_pattern_source(bccx.tcx,move_pat);
     let pat_span_path_opt = match move_pat.node {
         PatKind::Binding(_, _, ref path1, _) => {
-            Some(MoveSpanAndPath {
+            Some(MovePlace {
                      span: move_pat.span,
                      name: path1.node,
                      pat_source: source,
index f047374cab2563c15a2fa29e7cb2d26beb8e5f5b..9a72f3866a0e1c34579ccdbe53a2aa8590f8b897 100644 (file)
@@ -17,7 +17,7 @@
 use syntax::ast;
 use syntax_pos;
 use errors::DiagnosticBuilder;
-use rustc::hir::map::PatternSource;
+use borrowck::gather_loans::gather_moves::PatternSource;
 
 pub struct MoveErrorCollector<'tcx> {
     errors: Vec<MoveError<'tcx>>
@@ -41,12 +41,12 @@ pub fn report_potential_errors<'a>(&self, bccx: &BorrowckCtxt<'a, 'tcx>) {
 
 pub struct MoveError<'tcx> {
     move_from: mc::cmt<'tcx>,
-    move_to: Option<MoveSpanAndPath<'tcx>>
+    move_to: Option<MovePlace<'tcx>>
 }
 
 impl<'tcx> MoveError<'tcx> {
     pub fn with_move_info(move_from: mc::cmt<'tcx>,
-                          move_to: Option<MoveSpanAndPath<'tcx>>)
+                          move_to: Option<MovePlace<'tcx>>)
                           -> MoveError<'tcx> {
         MoveError {
             move_from: move_from,
@@ -56,7 +56,7 @@ pub fn with_move_info(move_from: mc::cmt<'tcx>,
 }
 
 #[derive(Clone)]
-pub struct MoveSpanAndPath<'tcx> {
+pub struct MovePlace<'tcx> {
     pub span: syntax_pos::Span,
     pub name: ast::Name,
     pub pat_source: PatternSource<'tcx>,
@@ -64,7 +64,7 @@ pub struct MoveSpanAndPath<'tcx> {
 
 pub struct GroupedMoveErrors<'tcx> {
     move_from: mc::cmt<'tcx>,
-    move_to_places: Vec<MoveSpanAndPath<'tcx>>
+    move_to_places: Vec<MovePlace<'tcx>>
 }
 
 fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<MoveError<'tcx>>) {
@@ -72,11 +72,11 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<Move
     for error in &grouped_errors {
         let mut err = report_cannot_move_out_of(bccx, error.move_from.clone());
         let mut is_first_note = true;
-       
-       if let Some(pattern_source) = error.move_to_places.get(0){
-        match pattern_source.pat_source {
-            PatternSource::LetDecl(_) => {}
+        match error.move_to_places.get(0) {
+            Some(&MovePlace { pat_source: PatternSource::LetDecl(_), .. }) => {
+                // ignore patterns that are found at the top-level of a `let`;
+                // see `get_pattern_source()` for details
+            }
             _ => {
                 for move_to in &error.move_to_places {
 
@@ -85,14 +85,14 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<Move
                 }
             }
         }
-    }
         if let NoteClosureEnv(upvar_id) = error.move_from.note {
-            err.span_label(bccx.tcx.hir.span(upvar_id.var_id), &"captured   outer variable");
+            err.span_label(bccx.tcx.hir.span(upvar_id.var_id),
+                           &"captured outer variable");
         }
         err.emit();
-       
-       }
+
     }
+}
 
 fn group_errors_with_same_origin<'tcx>(errors: &Vec<MoveError<'tcx>>)
                                        -> Vec<GroupedMoveErrors<'tcx>> {
index fd42abc752e490c133d790344f87e765b41aa1bf..7efa3bd9d5b311980e104b5c90b9b9050c409c2c 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// Check that we do not suggest `ref f` here in the `main()` function.
 struct Foo {
     pub v: Vec<String>,
 }
index 03b5beee2a4179f9cb89efe5677a9d836e6f254f..5e743b6bd3fe7b2ec2188e1ca22077b7466c377d 100644 (file)
@@ -1,7 +1,7 @@
 error[E0507]: cannot move out of indexed content
-  --> $DIR/issue-40402-1.rs:18:13
+  --> $DIR/issue-40402-1.rs:19:13
    |
-18 |     let e = f.v[0];
+19 |     let e = f.v[0];
    |             ^^^^^^ cannot move out of indexed content
 
 error: aborting due to previous error
index 0879614243fb2bca3a329298e87b236db70b78b6..76e038b696e8fe7122f9ffd6183da01c2bbbafbd 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// Check that we do suggest `(ref a, ref b)` here, since `a` and `b`
+// are nested within a pattern
 fn main() {
     let x = vec![(String::new(), String::new())];
     let (a, b) = x[0];
index ab72590001519c951abebca5ad47aba91ab90d71..0060b683bba43637789a732186b8de60b21e75f5 100644 (file)
@@ -1,7 +1,7 @@
 error[E0507]: cannot move out of indexed content
-  --> $DIR/issue-40402-2.rs:13:18
+  --> $DIR/issue-40402-2.rs:15:18
    |
-13 |     let (a, b) = x[0];
+15 |     let (a, b) = x[0];
    |          -  -    ^^^^ cannot move out of indexed content
    |          |  |
    |          |  ...and here (use `ref b` or `ref mut b`)