Skip to content

Commit b000803

Browse files
committed
borrowck some amount of diag migration
1 parent ff40f2e commit b000803

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1086
-322
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
13
use crate::nll::ToRegionVid;
24
use crate::path_utils::allow_two_phase_borrow;
35
use crate::place_ext::PlaceExt;

compiler/rustc_borrowck/src/borrowck_errors.rs

Lines changed: 26 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ use rustc_errors::{
44
use rustc_middle::ty::{self, Ty, TyCtxt};
55
use rustc_span::Span;
66

7+
use crate::session_diagnostics::{
8+
AssignBorrowErr, BorrowAcrossDestructor, BorrowAcrossGeneratorYield, ClosureConstructLabel,
9+
InteriorDropMoveErr, MoveBorrowedErr, PathShortLive, TwoClosuresUniquelyBorrowErr,
10+
UseMutBorrowErr,
11+
};
12+
713
impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
814
pub(crate) fn cannot_move_when_borrowed(
915
&self,
1016
span: Span,
1117
desc: &str,
1218
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
13-
struct_span_err!(self, span, E0505, "cannot move out of {} because it is borrowed", desc,)
19+
self.infcx.tcx.sess.create_err(MoveBorrowedErr { desc, span })
1420
}
1521

1622
pub(crate) fn cannot_use_when_mutably_borrowed(
@@ -20,17 +26,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
2026
borrow_span: Span,
2127
borrow_desc: &str,
2228
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
23-
let mut err = struct_span_err!(
24-
self,
25-
span,
26-
E0503,
27-
"cannot use {} because it was mutably borrowed",
28-
desc,
29-
);
30-
31-
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
32-
err.span_label(span, format!("use of borrowed {}", borrow_desc));
33-
err
29+
self.infcx.tcx.sess.create_err(UseMutBorrowErr { desc, borrow_desc, span, borrow_span })
3430
}
3531

3632
pub(crate) fn cannot_mutably_borrow_multiply(
@@ -90,26 +86,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
9086
old_loan_span: Span,
9187
old_load_end_span: Option<Span>,
9288
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
93-
let mut err = struct_span_err!(
94-
self,
95-
new_loan_span,
96-
E0524,
97-
"two closures require unique access to {} at the same time",
98-
desc,
99-
);
89+
let case: ClosureConstructLabel;
90+
let diff_span: Option<Span>;
10091
if old_loan_span == new_loan_span {
101-
err.span_label(
102-
old_loan_span,
103-
"closures are constructed here in different iterations of loop",
104-
);
92+
case = ClosureConstructLabel::Both { old_loan_span };
93+
diff_span = None;
10594
} else {
106-
err.span_label(old_loan_span, "first closure is constructed here");
107-
err.span_label(new_loan_span, "second closure is constructed here");
95+
case = ClosureConstructLabel::First { old_loan_span };
96+
diff_span = Some(new_loan_span);
10897
}
109-
if let Some(old_load_end_span) = old_load_end_span {
110-
err.span_label(old_load_end_span, "borrow from first closure ends here");
111-
}
112-
err
98+
self.infcx.tcx.sess.create_err(TwoClosuresUniquelyBorrowErr {
99+
desc,
100+
case,
101+
new_loan_span,
102+
old_load_end_span,
103+
diff_span,
104+
})
113105
}
114106

115107
pub(crate) fn cannot_uniquely_borrow_by_one_closure(
@@ -233,17 +225,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
233225
borrow_span: Span,
234226
desc: &str,
235227
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
236-
let mut err = struct_span_err!(
237-
self,
238-
span,
239-
E0506,
240-
"cannot assign to {} because it is borrowed",
241-
desc,
242-
);
243-
244-
err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
245-
err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
246-
err
228+
self.infcx.tcx.sess.create_err(AssignBorrowErr { desc, span, borrow_span })
247229
}
248230

249231
pub(crate) fn cannot_reassign_immutable(
@@ -303,15 +285,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
303285
move_from_span: Span,
304286
container_ty: Ty<'_>,
305287
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
306-
let mut err = struct_span_err!(
307-
self,
308-
move_from_span,
309-
E0509,
310-
"cannot move out of type `{}`, which implements the `Drop` trait",
311-
container_ty,
312-
);
313-
err.span_label(move_from_span, "cannot move out of here");
314-
err
288+
self.infcx.tcx.sess.create_err(InteriorDropMoveErr { container_ty, move_from_span })
315289
}
316290

317291
pub(crate) fn cannot_act_on_moved_value(
@@ -370,34 +344,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
370344
span: Span,
371345
yield_span: Span,
372346
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
373-
let mut err = struct_span_err!(
374-
self,
375-
span,
376-
E0626,
377-
"borrow may still be in use when generator yields",
378-
);
379-
err.span_label(yield_span, "possible yield occurs here");
380-
err
347+
self.infcx.tcx.sess.create_err(BorrowAcrossGeneratorYield { span, yield_span })
381348
}
382349

383350
pub(crate) fn cannot_borrow_across_destructor(
384351
&self,
385352
borrow_span: Span,
386353
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
387-
struct_span_err!(
388-
self,
389-
borrow_span,
390-
E0713,
391-
"borrow may still be in use when destructor runs",
392-
)
354+
self.infcx.tcx.sess.create_err(BorrowAcrossDestructor { borrow_span })
393355
}
394356

395357
pub(crate) fn path_does_not_live_long_enough(
396358
&self,
397359
span: Span,
398360
path: &str,
399361
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
400-
struct_span_err!(self, span, E0597, "{} does not live long enough", path,)
362+
self.infcx.tcx.sess.create_err(PathShortLive { path, span })
401363
}
402364

403365
pub(crate) fn cannot_return_reference_to_local(

compiler/rustc_borrowck/src/constraint_generation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
13
use rustc_infer::infer::InferCtxt;
24
use rustc_middle::mir::visit::TyContext;
35
use rustc_middle::mir::visit::Visitor;

compiler/rustc_borrowck/src/constraints/graph.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use rustc_data_structures::graph;
25
use rustc_index::vec::IndexVec;
36
use rustc_middle::mir::ConstraintCategory;

compiler/rustc_borrowck/src/constraints/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use rustc_data_structures::graph::scc::Sccs;
25
use rustc_index::vec::IndexVec;
36
use rustc_middle::mir::ConstraintCategory;

compiler/rustc_borrowck/src/consumers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
13
//! This file provides API for compiler consumers.
24
35
use rustc_hir::def_id::LocalDefId;

compiler/rustc_borrowck/src/dataflow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
13
use rustc_data_structures::fx::FxHashMap;
24
use rustc_index::bit_set::BitSet;
35
use rustc_middle::mir::{self, BasicBlock, Body, Location, Place};

compiler/rustc_borrowck/src/def_use.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
13
use rustc_middle::mir::visit::{
24
MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext,
35
};

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
25
use rustc_infer::infer::canonical::Canonical;
36
use rustc_infer::infer::error_reporting::nice_region_error::NiceRegionError;

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
180180
let move_spans = self.move_spans(moved_place.as_ref(), move_out.source);
181181
let move_span = move_spans.args_or_use();
182182

183-
let move_msg = if move_spans.for_closure() { " into closure" } else { "" };
183+
let move_msg = if move_spans.for_closure() { "InClosure" } else { "" };
184184

185185
let loop_message = if location == move_out.source || move_site.traversed_back_edge {
186-
", in previous iteration of loop"
186+
"InLoop"
187187
} else {
188188
""
189189
};

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_span::symbol::{kw, Symbol};
1616
use rustc_span::{sym, DesugaringKind, Span};
1717

1818
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
19+
use crate::session_diagnostics::{BorrowUsedHere, UsedLaterDropped};
1920
use crate::{
2021
borrow_set::BorrowData, nll::ConstraintDescription, region_infer::Cause, MirBorrowckCtxt,
2122
WriteKind,
@@ -66,6 +67,12 @@ impl<'tcx> BorrowExplanation<'tcx> {
6667
borrow_span: Option<Span>,
6768
multiple_borrow_span: Option<(Span, Span)>,
6869
) {
70+
let br_desc = match borrow_desc {
71+
"first " => "first",
72+
"immutable " => "immutable",
73+
"mutable " => "mutable",
74+
_ => "",
75+
};
6976
match *self {
7077
BorrowExplanation::UsedLater(later_use_kind, var_or_use_span, path_span) => {
7178
let message = match later_use_kind {
@@ -120,13 +127,12 @@ impl<'tcx> BorrowExplanation<'tcx> {
120127
// path_span is only present in the case of closure capture
121128
assert!(matches!(later_use_kind, LaterUseKind::ClosureCapture));
122129
if borrow_span.map(|sp| !sp.overlaps(var_or_use_span)).unwrap_or(true) {
123-
let path_label = "used here by closure";
130+
err.subdiagnostic(BorrowUsedHere::ByClosure { path_span });
124131
let capture_kind_label = message;
125132
err.span_label(
126133
var_or_use_span,
127134
format!("{}borrow later {}", borrow_desc, capture_kind_label),
128135
);
129-
err.span_label(path_span, path_label);
130136
}
131137
}
132138
}
@@ -162,42 +168,28 @@ impl<'tcx> BorrowExplanation<'tcx> {
162168

163169
match local_names[dropped_local] {
164170
Some(local_name) if !local_decl.from_compiler_desugaring() => {
165-
let message = format!(
166-
"{B}borrow might be used here, when `{LOC}` is dropped \
167-
and runs the {DTOR} for {TYPE}",
168-
B = borrow_desc,
169-
LOC = local_name,
170-
TYPE = type_desc,
171-
DTOR = dtor_desc
172-
);
173-
err.span_label(body.source_info(drop_loc).span, message);
174-
171+
err.subdiagnostic(UsedLaterDropped::UsedHere {
172+
borrow_desc: br_desc,
173+
local_name,
174+
type_desc: &type_desc,
175+
dtor_desc,
176+
span: body.source_info(drop_loc).span,
177+
});
175178
if should_note_order {
176-
err.note(
177-
"values in a scope are dropped \
178-
in the opposite order they are defined",
179-
);
179+
err.subdiagnostic(UsedLaterDropped::OppositeOrder);
180180
}
181181
}
182182
_ => {
183-
err.span_label(
184-
local_decl.source_info.span,
185-
format!(
186-
"a temporary with access to the {B}borrow \
187-
is created here ...",
188-
B = borrow_desc
189-
),
190-
);
191-
let message = format!(
192-
"... and the {B}borrow might be used here, \
193-
when that temporary is dropped \
194-
and runs the {DTOR} for {TYPE}",
195-
B = borrow_desc,
196-
TYPE = type_desc,
197-
DTOR = dtor_desc
198-
);
199-
err.span_label(body.source_info(drop_loc).span, message);
200-
183+
err.subdiagnostic(UsedLaterDropped::TemporaryCreatedHere {
184+
borrow_desc: br_desc,
185+
span: local_decl.source_info.span,
186+
});
187+
err.subdiagnostic(UsedLaterDropped::MightUsedHere {
188+
borrow_desc: br_desc,
189+
type_desc: &type_desc,
190+
dtor_desc,
191+
span: body.source_info(drop_loc).span,
192+
});
201193
if let Some(info) = &local_decl.is_block_tail {
202194
if info.tail_result_is_ignored {
203195
// #85581: If the first mutable borrow's scope contains
@@ -208,31 +200,16 @@ impl<'tcx> BorrowExplanation<'tcx> {
208200
})
209201
.unwrap_or(false)
210202
{
211-
err.span_suggestion_verbose(
212-
info.span.shrink_to_hi(),
213-
"consider adding semicolon after the expression so its \
214-
temporaries are dropped sooner, before the local variables \
215-
declared by the block are dropped",
216-
";",
217-
Applicability::MaybeIncorrect,
218-
);
203+
err.subdiagnostic(UsedLaterDropped::AddSemicolon {
204+
span: info.span.shrink_to_hi(),
205+
});
219206
}
220207
} else {
221-
err.note(
222-
"the temporary is part of an expression at the end of a \
223-
block;\nconsider forcing this temporary to be dropped sooner, \
224-
before the block's local variables are dropped",
225-
);
226-
err.multipart_suggestion(
227-
"for example, you could save the expression's value in a new \
228-
local variable `x` and then make `x` be the expression at the \
229-
end of the block",
230-
vec![
231-
(info.span.shrink_to_lo(), "let x = ".to_string()),
232-
(info.span.shrink_to_hi(), "; x".to_string()),
233-
],
234-
Applicability::MaybeIncorrect,
235-
);
208+
err.subdiagnostic(UsedLaterDropped::ManualDrop);
209+
err.subdiagnostic(UsedLaterDropped::MoveBlockEnd {
210+
lo_span: info.span.shrink_to_lo(),
211+
hi_span: info.span.shrink_to_hi(),
212+
});
236213
};
237214
}
238215
}

compiler/rustc_borrowck/src/diagnostics/find_all_local_uses.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use std::collections::BTreeSet;
25

36
use rustc_middle::mir::visit::{PlaceContext, Visitor};

compiler/rustc_borrowck/src/diagnostics/find_use.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use std::collections::VecDeque;
25
use std::rc::Rc;
36

0 commit comments

Comments
 (0)