Skip to content

Commit

Permalink
explain_borrow need help
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyJado committed Sep 1, 2022
1 parent 19269fe commit c480d9a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 54 deletions.
91 changes: 40 additions & 51 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// #![deny(rustc::untranslatable_diagnostic)]
// #![deny(rustc::diagnostic_outside_of_impl)]

//! Print diagnostics to explain why values are borrowed.

use std::collections::VecDeque;
Expand All @@ -18,6 +21,7 @@ use rustc_span::{sym, DesugaringKind, Span};
use crate::region_infer::BlameConstraint;
use crate::session_diagnostics::{
BorrowLaterBorrowUsedLaterInLoop, BorrowUsedHere, BorrowUsedLater, BorrowUsedLaterInLoop,
MustValidFor, UsedLaterDropped,
};
use crate::{
borrow_set::BorrowData, nll::ConstraintDescription, region_infer::Cause, MirBorrowckCtxt,
Expand Down Expand Up @@ -220,41 +224,33 @@ impl<'tcx> BorrowExplanation<'tcx> {

match local_names[dropped_local] {
Some(local_name) if !local_decl.from_compiler_desugaring() => {
let message = format!(
"{B}borrow might be used here, when `{LOC}` is dropped \
and runs the {DTOR} for {TYPE}",
B = borrow_desc,
LOC = local_name,
TYPE = type_desc,
DTOR = dtor_desc
);
err.span_label(body.source_info(drop_loc).span, message);
let sub_label = UsedLaterDropped::UsedHere {
borrow_desc,
local_name: &local_name.to_ident_string(),
type_desc: &type_desc,
dtor_desc,
span: body.source_info(drop_loc).span,
};
err.subdiagnostic(sub_label);

if should_note_order {
err.note(
"values in a scope are dropped \
in the opposite order they are defined",
);
let sub_note = UsedLaterDropped::OppositeOrder {};
err.subdiagnostic(sub_note);
}
}
_ => {
err.span_label(
local_decl.source_info.span,
format!(
"a temporary with access to the {B}borrow \
is created here ...",
B = borrow_desc
),
);
let message = format!(
"... and the {B}borrow might be used here, \
when that temporary is dropped \
and runs the {DTOR} for {TYPE}",
B = borrow_desc,
TYPE = type_desc,
DTOR = dtor_desc
);
err.span_label(body.source_info(drop_loc).span, message);
let sub_label = UsedLaterDropped::TemporaryCreatedHere {
borrow_desc,
span: local_decl.source_info.span,
};
err.subdiagnostic(sub_label);
let sub_label_2 = UsedLaterDropped::MightUsedHere {
borrow_desc,
type_desc: &type_desc,
dtor_desc,
span: body.source_info(drop_loc).span,
};
err.subdiagnostic(sub_label_2);

if let Some(info) = &local_decl.is_block_tail {
if info.tail_result_is_ignored {
Expand All @@ -266,21 +262,16 @@ impl<'tcx> BorrowExplanation<'tcx> {
})
.unwrap_or(false)
{
err.span_suggestion_verbose(
info.span.shrink_to_hi(),
"consider adding semicolon after the expression so its \
temporaries are dropped sooner, before the local variables \
declared by the block are dropped",
";",
Applicability::MaybeIncorrect,
);
let sub_suggest = UsedLaterDropped::AddSemicolon {
span: info.span.shrink_to_hi(),
};
err.subdiagnostic(sub_suggest);
}
} else {
err.note(
"the temporary is part of an expression at the end of a \
block;\nconsider forcing this temporary to be dropped sooner, \
before the block's local variables are dropped",
);
let sub_note = UsedLaterDropped::ManualDrop {};
err.subdiagnostic(sub_note);

//FIXME: waiting for multipart suggestion derive
err.multipart_suggestion(
"for example, you could save the expression's value in a new \
local variable `x` and then make `x` be the expression at the \
Expand All @@ -306,15 +297,13 @@ impl<'tcx> BorrowExplanation<'tcx> {
region_name.highlight_region_name(err);

if let Some(desc) = opt_place_desc {
err.span_label(
let sub_label = MustValidFor::Borrowed {
category: category.description(),
desc,
region_name,
span,
format!(
"{}requires that `{}` is borrowed for `{}`",
category.description(),
desc,
region_name,
),
);
};
err.subdiagnostic(sub_label);
} else {
err.span_label(
span,
Expand Down
53 changes: 53 additions & 0 deletions compiler/rustc_borrowck/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,56 @@ pub(crate) enum BorrowLaterBorrowUsedLaterInLoop<'a> {
span: Span,
},
}

#[derive(SessionSubdiagnostic)]
pub(crate) enum UsedLaterDropped<'a> {
#[label(borrowck::drop_local_might_cause_borrow)]
UsedHere {
borrow_desc: &'a str,
local_name: &'a str,
type_desc: &'a str,
dtor_desc: &'a str,
#[primary_span]
span: Span,
},
#[note(borrowck::var_dropped_in_wrong_order)]
OppositeOrder {},
#[label(borrowck::temporary_access_to_borrow)]
TemporaryCreatedHere {
borrow_desc: &'a str,
#[primary_span]
span: Span,
},
#[label(borrowck::drop_temporary_might_cause_borrow_use)]
MightUsedHere {
borrow_desc: &'a str,
type_desc: &'a str,
dtor_desc: &'a str,
#[primary_span]
span: Span,
},
#[suggestion_verbose(
borrowck::consider_add_semicolon,
applicability = "maybe-incorrect",
code = ";"
)]
AddSemicolon {
#[primary_span]
span: Span,
},

#[note(borrowck::consider_forcing_temporary_drop_sooner)]
ManualDrop {},
}

#[derive(SessionSubdiagnostic)]
pub(crate) enum MustValidFor<'a> {
#[label(borrowck::outlive_constraint_need_borrow_for)]
Borrowed {
category: &'a str,
desc: &'a str,
region_name: &'a RegionName,
#[primary_span]
span: Span,
},
}
6 changes: 3 additions & 3 deletions compiler/rustc_error_messages/locales/en-US/borrowck.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ borrowck_bl_used_borrow_in_later_iteration_loop =
{$borrow_desc}borrow later borrow used here, in later iteration of loop
borrowck_drop_local_might_cause_borrow =
{$borrow_desc}borrow might be used here, when `{$local_name}` is dropped and runs the {$dtor_name} for {$type_name}
{$borrow_desc}borrow might be used here, when `{$local_name}` is dropped and runs the {$dtor_desc} for {$type_desc}
borrowck_var_dropped_in_wrong_order =
values in a scope are dropped in the opposite order they are defined
borrowck_temporary_access_to_borrow =
a temporary with access to the {$borrow_desc}borrow is created here ...
borrowck_drop_temporary_might_cause_borrow = ... and the {$borrow_desc}borrow might be used here, when that temporary is dropped and runs the {$dtor_name} for {$type_name}
borrowck_drop_temporary_might_cause_borrow_use = ... and the {$borrow_desc}borrow might be used here, when that temporary is dropped and runs the {$dtor_desc} for {$type_desc}
borrowck_consider_add_semicolon =
consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
Expand All @@ -128,5 +128,5 @@ borrowck_perhaps_save_in_new_local_to_drop =
borrowck_outlive_constraint_need_borrow_for =
{$category}requires that `{$desc}` is borrowed for `{$region_name}`
borrowck_outlive_constraint_need_borrow_lasts =
borrowck_outlive_constraint_need_borrow_lasts_for =
{$category}requires that `{$borrow_desc}` lasts for `{$region_name}`

0 comments on commit c480d9a

Please sign in to comment.