Skip to content

Commit ab5d16a

Browse files
committed
Adding documentation, indentation fixes
1 parent 5436f85 commit ab5d16a

File tree

7 files changed

+47
-32
lines changed

7 files changed

+47
-32
lines changed

src/librustc/hir/map/mod.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ enum MapEntry<'hir> {
9595
RootCrate,
9696
}
9797

98-
/// Represents the kind of pattern
99-
#[derive(Debug, Clone, Copy)]
100-
pub enum PatternSource<'hir> {
101-
MatchExpr(&'hir Expr),
102-
LetDecl(&'hir Local),
103-
Other,
104-
}
105-
10698
impl<'hir> Clone for MapEntry<'hir> {
10799
fn clone(&self) -> MapEntry<'hir> {
108100
*self
@@ -645,7 +637,7 @@ impl<'hir> Map<'hir> {
645637
Err(id) => id,
646638
}
647639
}
648-
640+
649641
/// Returns the nearest enclosing scope. A scope is an item or block.
650642
/// FIXME it is not clear to me that all items qualify as scopes - statics
651643
/// and associated types probably shouldn't, for example. Behaviour in this

src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! Computes moves.
1212
1313
use borrowck::*;
14-
use borrowck::gather_loans::move_error::MoveSpanAndPath;
14+
use borrowck::gather_loans::move_error::MovePlace;
1515
use borrowck::gather_loans::move_error::{MoveError, MoveErrorCollector};
1616
use borrowck::move_data::*;
1717
use rustc::middle::expr_use_visitor as euv;
@@ -25,16 +25,36 @@ use syntax::ast;
2525
use syntax_pos::Span;
2626
use rustc::hir::*;
2727
use rustc::hir::map::Node::*;
28-
use rustc::hir::map::{PatternSource};
2928

3029
struct GatherMoveInfo<'tcx> {
3130
id: ast::NodeId,
3231
kind: MoveKind,
3332
cmt: mc::cmt<'tcx>,
34-
span_path_opt: Option<MoveSpanAndPath<'tcx>>
33+
span_path_opt: Option<MovePlace<'tcx>>
3534
}
3635

37-
/// Returns the kind of the Pattern
36+
/// Represents the kind of pattern
37+
#[derive(Debug, Clone, Copy)]
38+
pub enum PatternSource<'tcx> {
39+
MatchExpr(&'tcx Expr),
40+
LetDecl(&'tcx Local),
41+
Other,
42+
}
43+
44+
/// Analyzes the context where the pattern appears to determine the
45+
/// kind of hint we want to give. In particular, if the pattern is in a `match`
46+
/// or nested within other patterns, we want to suggest a `ref` binding:
47+
///
48+
/// let (a, b) = v[0]; // like the `a` and `b` patterns here
49+
/// match v[0] { a => ... } // or the `a` pattern here
50+
///
51+
/// But if the pattern is the outermost pattern in a `let`, we would rather
52+
/// suggest that the author add a `&` to the initializer:
53+
///
54+
/// let x = v[0]; // suggest `&v[0]` here
55+
///
56+
/// In this latter case, this function will return `PatternSource::LetDecl`
57+
/// with a reference to the let
3858
fn get_pattern_source<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &Pat) -> PatternSource<'tcx> {
3959

4060
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>,
132152
let source = get_pattern_source(bccx.tcx,move_pat);
133153
let pat_span_path_opt = match move_pat.node {
134154
PatKind::Binding(_, _, ref path1, _) => {
135-
Some(MoveSpanAndPath {
155+
Some(MovePlace {
136156
span: move_pat.span,
137157
name: path1.node,
138158
pat_source: source,

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::ty;
1717
use syntax::ast;
1818
use syntax_pos;
1919
use errors::DiagnosticBuilder;
20-
use rustc::hir::map::PatternSource;
20+
use borrowck::gather_loans::gather_moves::PatternSource;
2121

2222
pub struct MoveErrorCollector<'tcx> {
2323
errors: Vec<MoveError<'tcx>>
@@ -41,12 +41,12 @@ impl<'tcx> MoveErrorCollector<'tcx> {
4141

4242
pub struct MoveError<'tcx> {
4343
move_from: mc::cmt<'tcx>,
44-
move_to: Option<MoveSpanAndPath<'tcx>>
44+
move_to: Option<MovePlace<'tcx>>
4545
}
4646

4747
impl<'tcx> MoveError<'tcx> {
4848
pub fn with_move_info(move_from: mc::cmt<'tcx>,
49-
move_to: Option<MoveSpanAndPath<'tcx>>)
49+
move_to: Option<MovePlace<'tcx>>)
5050
-> MoveError<'tcx> {
5151
MoveError {
5252
move_from: move_from,
@@ -56,27 +56,27 @@ impl<'tcx> MoveError<'tcx> {
5656
}
5757

5858
#[derive(Clone)]
59-
pub struct MoveSpanAndPath<'tcx> {
59+
pub struct MovePlace<'tcx> {
6060
pub span: syntax_pos::Span,
6161
pub name: ast::Name,
6262
pub pat_source: PatternSource<'tcx>,
6363
}
6464

6565
pub struct GroupedMoveErrors<'tcx> {
6666
move_from: mc::cmt<'tcx>,
67-
move_to_places: Vec<MoveSpanAndPath<'tcx>>
67+
move_to_places: Vec<MovePlace<'tcx>>
6868
}
6969

7070
fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<MoveError<'tcx>>) {
7171
let grouped_errors = group_errors_with_same_origin(errors);
7272
for error in &grouped_errors {
7373
let mut err = report_cannot_move_out_of(bccx, error.move_from.clone());
7474
let mut is_first_note = true;
75-
76-
if let Some(pattern_source) = error.move_to_places.get(0){
77-
78-
match pattern_source.pat_source {
79-
PatternSource::LetDecl(_) => {}
75+
match error.move_to_places.get(0) {
76+
Some(&MovePlace { pat_source: PatternSource::LetDecl(_), .. }) => {
77+
// ignore patterns that are found at the top-level of a `let`;
78+
// see `get_pattern_source()` for details
79+
}
8080
_ => {
8181
for move_to in &error.move_to_places {
8282

@@ -85,14 +85,14 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<Move
8585
}
8686
}
8787
}
88-
}
8988
if let NoteClosureEnv(upvar_id) = error.move_from.note {
90-
err.span_label(bccx.tcx.hir.span(upvar_id.var_id), &"captured outer variable");
89+
err.span_label(bccx.tcx.hir.span(upvar_id.var_id),
90+
&"captured outer variable");
9191
}
9292
err.emit();
93-
94-
}
93+
9594
}
95+
}
9696

9797
fn group_errors_with_same_origin<'tcx>(errors: &Vec<MoveError<'tcx>>)
9898
-> Vec<GroupedMoveErrors<'tcx>> {

src/test/ui/issue-40402-ref-hints/issue-40402-1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Check that we do not suggest `ref f` here in the `main()` function.
1112
struct Foo {
1213
pub v: Vec<String>,
1314
}

src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error[E0507]: cannot move out of indexed content
2-
--> $DIR/issue-40402-1.rs:18:13
2+
--> $DIR/issue-40402-1.rs:19:13
33
|
4-
18 | let e = f.v[0];
4+
19 | let e = f.v[0];
55
| ^^^^^^ cannot move out of indexed content
66

77
error: aborting due to previous error

src/test/ui/issue-40402-ref-hints/issue-40402-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Check that we do suggest `(ref a, ref b)` here, since `a` and `b`
12+
// are nested within a pattern
1113
fn main() {
1214
let x = vec![(String::new(), String::new())];
1315
let (a, b) = x[0];

src/test/ui/issue-40402-ref-hints/issue-40402-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error[E0507]: cannot move out of indexed content
2-
--> $DIR/issue-40402-2.rs:13:18
2+
--> $DIR/issue-40402-2.rs:15:18
33
|
4-
13 | let (a, b) = x[0];
4+
15 | let (a, b) = x[0];
55
| - - ^^^^ cannot move out of indexed content
66
| | |
77
| | ...and here (use `ref b` or `ref mut b`)

0 commit comments

Comments
 (0)