Skip to content

Commit a39a982

Browse files
committed
some diag
1 parent 9be0441 commit a39a982

File tree

5 files changed

+233
-107
lines changed

5 files changed

+233
-107
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

Lines changed: 35 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use rustc_errors::{
77
use rustc_middle::ty::{self, Ty, TyCtxt};
88
use rustc_span::Span;
99

10-
use crate::session_diagnostics::{TemporaryDroppedErr, ThreadLocalOutliveErr};
10+
use crate::session_diagnostics::{
11+
ActMovedValueErr, AssignBorrowErr, AssignErr, BorrowAcrossDestructor,
12+
BorrowAcrossGeneratorYield, ClosureVarOutliveErr, ImmuteArgAssign, ImmuteVarReassign,
13+
InteriorDropMoveErr, MovedOutErr, MutateInImmute, PathShortLive, ReturnRefLocalErr,
14+
TemporaryDroppedErr, ThreadLocalOutliveErr,
15+
};
1116

1217
impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
1318
pub(crate) fn cannot_move_when_borrowed(
@@ -238,17 +243,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
238243
borrow_span: Span,
239244
desc: &str,
240245
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
241-
let mut err = struct_span_err!(
242-
self,
243-
span,
244-
E0506,
245-
"cannot assign to {} because it is borrowed",
246-
desc,
247-
);
248-
249-
err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
250-
err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
251-
err
246+
self.infcx.tcx.sess.create_err(AssignBorrowErr { desc, span, borrow_span })
252247
}
253248

254249
pub(crate) fn cannot_reassign_immutable(
@@ -257,24 +252,27 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
257252
desc: &str,
258253
is_arg: bool,
259254
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
260-
let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
261-
struct_span_err!(self, span, E0384, "cannot assign {} {}", msg, desc)
255+
if is_arg {
256+
self.infcx.tcx.sess.create_err(ImmuteArgAssign { desc, span })
257+
} else {
258+
self.infcx.tcx.sess.create_err(ImmuteVarReassign { desc, span })
259+
}
262260
}
263261

264262
pub(crate) fn cannot_assign(
265263
&self,
266264
span: Span,
267265
desc: &str,
268266
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
269-
struct_span_err!(self, span, E0594, "cannot assign to {}", desc)
267+
self.infcx.tcx.sess.create_err(AssignErr { desc, span })
270268
}
271269

272270
pub(crate) fn cannot_move_out_of(
273271
&self,
274272
move_from_span: Span,
275273
move_from_desc: &str,
276274
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
277-
struct_span_err!(self, move_from_span, E0507, "cannot move out of {}", move_from_desc,)
275+
self.infcx.tcx.sess.create_err(MovedOutErr { move_from_desc, move_from_span })
278276
}
279277

280278
/// Signal an error due to an attempt to move out of the interior
@@ -308,15 +306,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
308306
move_from_span: Span,
309307
container_ty: Ty<'_>,
310308
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
311-
let mut err = struct_span_err!(
312-
self,
313-
move_from_span,
314-
E0509,
315-
"cannot move out of type `{}`, which implements the `Drop` trait",
316-
container_ty,
317-
);
318-
err.span_label(move_from_span, "cannot move out of here");
319-
err
309+
self.infcx.tcx.sess.create_err(InteriorDropMoveErr { container_ty, move_from_span })
320310
}
321311

322312
pub(crate) fn cannot_act_on_moved_value(
@@ -327,18 +317,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
327317
moved_path: Option<String>,
328318
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
329319
let moved_path = moved_path.map(|mp| format!(": `{}`", mp)).unwrap_or_default();
330-
331-
struct_span_err!(
332-
self,
333-
use_span,
334-
E0382,
335-
"{} of {}moved value{}",
320+
self.infcx.tcx.sess.create_err(ActMovedValueErr {
336321
verb,
337322
optional_adverb_for_moved,
338323
moved_path,
339-
)
324+
use_span,
325+
})
340326
}
341327

328+
//FIXME: nested with other file, replace reason with subdiag.
342329
pub(crate) fn cannot_borrow_path_as_mutable_because(
343330
&self,
344331
span: Span,
@@ -356,53 +343,36 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
356343
immutable_section: &str,
357344
action: &str,
358345
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
359-
let mut err = struct_span_err!(
360-
self,
361-
mutate_span,
362-
E0510,
363-
"cannot {} {} in {}",
346+
self.infcx.tcx.sess.create_err(MutateInImmute {
364347
action,
365348
immutable_place,
366349
immutable_section,
367-
);
368-
err.span_label(mutate_span, format!("cannot {}", action));
369-
err.span_label(immutable_span, format!("value is immutable in {}", immutable_section));
370-
err
350+
mutate_span,
351+
immutable_span,
352+
})
371353
}
372354

373355
pub(crate) fn cannot_borrow_across_generator_yield(
374356
&self,
375357
span: Span,
376358
yield_span: Span,
377359
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
378-
let mut err = struct_span_err!(
379-
self,
380-
span,
381-
E0626,
382-
"borrow may still be in use when generator yields",
383-
);
384-
err.span_label(yield_span, "possible yield occurs here");
385-
err
360+
self.infcx.tcx.sess.create_err(BorrowAcrossGeneratorYield { span, yield_span })
386361
}
387362

388363
pub(crate) fn cannot_borrow_across_destructor(
389364
&self,
390365
borrow_span: Span,
391366
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
392-
struct_span_err!(
393-
self,
394-
borrow_span,
395-
E0713,
396-
"borrow may still be in use when destructor runs",
397-
)
367+
self.infcx.tcx.sess.create_err(BorrowAcrossDestructor { borrow_span })
398368
}
399369

400370
pub(crate) fn path_does_not_live_long_enough(
401371
&self,
402372
span: Span,
403373
path: &str,
404374
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
405-
struct_span_err!(self, span, E0597, "{} does not live long enough", path,)
375+
self.infcx.tcx.sess.create_err(PathShortLive { path, span })
406376
}
407377

408378
pub(crate) fn cannot_return_reference_to_local(
@@ -412,22 +382,12 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
412382
reference_desc: &str,
413383
path_desc: &str,
414384
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
415-
let mut err = struct_span_err!(
416-
self,
385+
self.infcx.tcx.sess.create_err(ReturnRefLocalErr {
386+
return_kind,
387+
reference: reference_desc,
388+
local: path_desc,
417389
span,
418-
E0515,
419-
"cannot {RETURN} {REFERENCE} {LOCAL}",
420-
RETURN = return_kind,
421-
REFERENCE = reference_desc,
422-
LOCAL = path_desc,
423-
);
424-
425-
err.span_label(
426-
span,
427-
format!("{}s a {} data owned by the current function", return_kind, reference_desc),
428-
);
429-
430-
err
390+
})
431391
}
432392

433393
pub(crate) fn cannot_capture_in_long_lived_closure(
@@ -437,18 +397,12 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
437397
borrowed_path: &str,
438398
capture_span: Span,
439399
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
440-
let mut err = struct_span_err!(
441-
self,
442-
closure_span,
443-
E0373,
444-
"{} may outlive the current function, but it borrows {}, which is owned by the current \
445-
function",
400+
self.infcx.tcx.sess.create_err(ClosureVarOutliveErr {
446401
closure_kind,
447402
borrowed_path,
448-
);
449-
err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
450-
.span_label(closure_span, format!("may outlive borrowed value {}", borrowed_path));
451-
err
403+
closure_span,
404+
capture_span,
405+
})
452406
}
453407

454408
pub(crate) fn thread_local_value_does_not_live_long_enough(

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ impl<'tcx> BorrowExplanation<'tcx> {
226226
Some(local_name) if !local_decl.from_compiler_desugaring() => {
227227
let sub_label = UsedLaterDropped::UsedHere {
228228
borrow_desc,
229-
local_name: &local_name.to_ident_string(),
229+
local_name,
230230
type_desc: &type_desc,
231231
dtor_desc,
232232
span: body.source_info(drop_loc).span,
233233
};
234234
err.subdiagnostic(sub_label);
235235

236236
if should_note_order {
237-
let sub_note = UsedLaterDropped::OppositeOrder {};
237+
let sub_note = UsedLaterDropped::OppositeOrder;
238238
err.subdiagnostic(sub_note);
239239
}
240240
}
@@ -262,26 +262,16 @@ impl<'tcx> BorrowExplanation<'tcx> {
262262
})
263263
.unwrap_or(false)
264264
{
265-
let sub_suggest = UsedLaterDropped::AddSemicolon {
265+
err.subdiagnostic(UsedLaterDropped::AddSemicolon {
266266
span: info.span.shrink_to_hi(),
267-
};
268-
err.subdiagnostic(sub_suggest);
267+
});
269268
}
270269
} else {
271-
let sub_note = UsedLaterDropped::ManualDrop {};
272-
err.subdiagnostic(sub_note);
273-
274-
//FIXME: waiting for multipart suggestion derive
275-
err.multipart_suggestion(
276-
"for example, you could save the expression's value in a new \
277-
local variable `x` and then make `x` be the expression at the \
278-
end of the block",
279-
vec![
280-
(info.span.shrink_to_lo(), "let x = ".to_string()),
281-
(info.span.shrink_to_hi(), "; x".to_string()),
282-
],
283-
Applicability::MaybeIncorrect,
284-
);
270+
err.subdiagnostic(UsedLaterDropped::ManualDrop);
271+
err.subdiagnostic(UsedLaterDropped::MoveBlockEnd {
272+
lo_span: info.span.shrink_to_lo(),
273+
hi_span: info.span.shrink_to_hi(),
274+
});
285275
};
286276
}
287277
}

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
524524
}
525525

526526
if binds_to.len() > 1 {
527-
err.subdiagnostic(AddMoveErr::MovedNotCopy {});
527+
err.subdiagnostic(AddMoveErr::MovedNotCopy);
528528
}
529529
}
530530
}

0 commit comments

Comments
 (0)