Skip to content

Commit f700728

Browse files
committed
make end-point optional in the borrow check
1 parent 5b2adcc commit f700728

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
484484
// 3. Where does old loan expire.
485485

486486
let previous_end_span =
487-
old_loan.kill_scope.span(self.tcx(), &self.bccx.region_scope_tree).end_point();
487+
Some(old_loan.kill_scope.span(self.tcx(), &self.bccx.region_scope_tree)
488+
.end_point());
488489

489490
let mut err = match (new_loan.kind, old_loan.kind) {
490491
(ty::MutBorrow, ty::MutBorrow) =>

src/librustc_mir/borrow_check.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
425425
context, lvalue_span, borrow),
426426
ReadKind::Borrow(bk) => {
427427
let end_issued_loan_span =
428-
flow_state.borrows.base_results.operator().region_span(
429-
&borrow.region).end_point();
428+
flow_state.borrows.base_results.operator().opt_region_end_span(
429+
&borrow.region);
430430
this.report_conflicting_borrow(
431431
context, common_prefix, lvalue_span, bk,
432432
&borrow, end_issued_loan_span)
@@ -438,8 +438,8 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
438438
match kind {
439439
WriteKind::MutableBorrow(bk) => {
440440
let end_issued_loan_span =
441-
flow_state.borrows.base_results.operator().region_span(
442-
&borrow.region).end_point();
441+
flow_state.borrows.base_results.operator().opt_region_end_span(
442+
&borrow.region);
443443
this.report_conflicting_borrow(
444444
context, common_prefix, lvalue_span, bk,
445445
&borrow, end_issued_loan_span)
@@ -1101,7 +1101,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
11011101
(lvalue, span): (&Lvalue, Span),
11021102
gen_borrow_kind: BorrowKind,
11031103
issued_borrow: &BorrowData,
1104-
end_issued_loan_span: Span) {
1104+
end_issued_loan_span: Option<Span>) {
11051105
use self::prefixes::IsPrefixOf;
11061106

11071107
assert!(common_prefix.is_prefix_of(lvalue));

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
116116
&self.borrows[idx].location
117117
}
118118

119-
pub fn region_span(&self, region: &Region) -> Span {
119+
/// Returns the span for the "end point" given region. This will
120+
/// return `None` if NLL is enabled, since that concept has no
121+
/// meaning there. Otherwise, it should return some.
122+
pub fn opt_region_end_span(&self, region: &Region) -> Option<Span> {
120123
let opt_span = self.region_span_map.get(region);
121124
assert!(opt_span.is_some(), "end region not found for {:?}", region);
122-
*opt_span.unwrap()
125+
opt_span.map(|s| s.end_point())
123126
}
124127
}
125128

src/librustc_mir/util/borrowck_errors.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub trait BorrowckErrors {
9393
opt_via: &str,
9494
old_loan_span: Span,
9595
old_opt_via: &str,
96-
old_load_end_span:Span,
96+
old_load_end_span: Option<Span>,
9797
o: Origin)
9898
-> DiagnosticBuilder
9999
{
@@ -106,13 +106,17 @@ pub trait BorrowckErrors {
106106
err.span_label(new_loan_span,
107107
format!("mutable borrow starts here in previous \
108108
iteration of loop{}", opt_via));
109-
err.span_label(old_load_end_span, "mutable borrow ends here");
109+
if let Some(old_load_end_span) = old_load_end_span {
110+
err.span_label(old_load_end_span, "mutable borrow ends here");
111+
}
110112
} else {
111113
err.span_label(old_loan_span,
112114
format!("first mutable borrow occurs here{}", old_opt_via));
113115
err.span_label(new_loan_span,
114116
format!("second mutable borrow occurs here{}", opt_via));
115-
err.span_label(old_load_end_span, "first borrow ends here");
117+
if let Some(old_load_end_span) = old_load_end_span {
118+
err.span_label(old_load_end_span, "first borrow ends here");
119+
}
116120
}
117121
err
118122
}
@@ -121,7 +125,7 @@ pub trait BorrowckErrors {
121125
new_loan_span: Span,
122126
desc: &str,
123127
old_loan_span: Span,
124-
old_load_end_span: Span,
128+
old_load_end_span: Option<Span>,
125129
o: Origin)
126130
-> DiagnosticBuilder
127131
{
@@ -134,9 +138,11 @@ pub trait BorrowckErrors {
134138
err.span_label(
135139
new_loan_span,
136140
"second closure is constructed here");
137-
err.span_label(
138-
old_load_end_span,
139-
"borrow from first closure ends here");
141+
if let Some(old_load_end_span) = old_load_end_span {
142+
err.span_label(
143+
old_load_end_span,
144+
"borrow from first closure ends here");
145+
}
140146
err
141147
}
142148

@@ -147,7 +153,7 @@ pub trait BorrowckErrors {
147153
old_loan_span: Span,
148154
noun_old: &str,
149155
old_opt_via: &str,
150-
previous_end_span: Span,
156+
previous_end_span: Option<Span>,
151157
o: Origin)
152158
-> DiagnosticBuilder
153159
{
@@ -158,7 +164,9 @@ pub trait BorrowckErrors {
158164
format!("closure construction occurs here{}", opt_via));
159165
err.span_label(old_loan_span,
160166
format!("borrow occurs here{}", old_opt_via));
161-
err.span_label(previous_end_span, "borrow ends here");
167+
if let Some(previous_end_span) = previous_end_span {
168+
err.span_label(previous_end_span, "borrow ends here");
169+
}
162170
err
163171
}
164172

@@ -169,7 +177,7 @@ pub trait BorrowckErrors {
169177
kind_new: &str,
170178
old_loan_span: Span,
171179
old_opt_via: &str,
172-
previous_end_span: Span,
180+
previous_end_span: Option<Span>,
173181
o: Origin)
174182
-> DiagnosticBuilder
175183
{
@@ -181,7 +189,9 @@ pub trait BorrowckErrors {
181189
format!("borrow occurs here{}", opt_via));
182190
err.span_label(old_loan_span,
183191
format!("closure construction occurs here{}", old_opt_via));
184-
err.span_label(previous_end_span, "borrow from closure ends here");
192+
if let Some(previous_end_span) = previous_end_span {
193+
err.span_label(previous_end_span, "borrow from closure ends here");
194+
}
185195
err
186196
}
187197

@@ -194,7 +204,7 @@ pub trait BorrowckErrors {
194204
noun_old: &str,
195205
kind_old: &str,
196206
msg_old: &str,
197-
old_load_end_span: Span,
207+
old_load_end_span: Option<Span>,
198208
o: Origin)
199209
-> DiagnosticBuilder
200210
{
@@ -203,7 +213,9 @@ pub trait BorrowckErrors {
203213
desc_new, msg_new, kind_new, noun_old, kind_old, msg_old, OGN=o);
204214
err.span_label(span, format!("{} borrow occurs here{}", kind_new, msg_new));
205215
err.span_label(old_span, format!("{} borrow occurs here{}", kind_old, msg_old));
206-
err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
216+
if let Some(old_load_end_span) = old_load_end_span {
217+
err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
218+
}
207219
err
208220
}
209221

0 commit comments

Comments
 (0)