Skip to content

Support borrow, scope and move visualization through RLS #42733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 47 additions & 28 deletions src/librustc_borrowck/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ mod move_error;

pub fn gather_loans_in_fn<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
body: hir::BodyId)
-> (Vec<Loan<'tcx>>, move_data::MoveData<'tcx>) {
-> (Vec<SafeLoan>, Vec<Loan<'tcx>>, move_data::MoveData<'tcx>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I feel like the right fix here might be removing the notion of SafeLoan altogether, and adjusting the follow-on code, per the strategy I describe in the NLL RFC.

let def_id = bccx.tcx.hir.body_owner_def_id(body);
let param_env = bccx.tcx.param_env(def_id);
let mut glcx = GatherLoanCtxt {
bccx: bccx,
safe_loans: Vec::new(),
all_loans: Vec::new(),
item_ub: region::CodeExtent::Misc(body.node_id),
move_data: MoveData::new(),
Expand All @@ -53,14 +54,15 @@ pub fn gather_loans_in_fn<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
.consume_body(body);

glcx.report_potential_errors();
let GatherLoanCtxt { all_loans, move_data, .. } = glcx;
(all_loans, move_data)
let GatherLoanCtxt { safe_loans, all_loans, move_data, .. } = glcx;
(safe_loans, all_loans, move_data)
}

struct GatherLoanCtxt<'a, 'tcx: 'a> {
bccx: &'a BorrowckCtxt<'a, 'tcx>,
move_data: move_data::MoveData<'tcx>,
move_error_collector: move_error::MoveErrorCollector<'tcx>,
safe_loans: Vec<SafeLoan>,
all_loans: Vec<Loan<'tcx>>,
/// `item_ub` is used as an upper-bound on the lifetime whenever we
/// ask for the scope of an expression categorized as an upvar.
Expand Down Expand Up @@ -289,6 +291,33 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
}
}

fn loan_scope_from_region(loan_region: &ty::Region<'tcx>) -> region::CodeExtent {
match *loan_region {
ty::ReScope(scope) => scope,

ty::ReEarlyBound(ref br) => {
self.bccx.region_maps.early_free_extent(self.tcx(), br)
}

ty::ReFree(ref fr) => {
self.bccx.region_maps.free_extent(self.tcx(), fr)
}

ty::ReStatic => self.item_ub,

ty::ReEmpty |
ty::ReLateBound(..) |
ty::ReVar(..) |
ty::ReSkolemized(..) |
ty::ReErased => {
span_bug!(
cmt.span,
"invalid borrow lifetime: {:?}",
loan_region);
}
}
}

/// Guarantees that `addr_of(cmt)` will be valid for the duration of `static_scope_r`, or
/// reports an error. This may entail taking out loans, which will be added to the
/// `req_loan_map`.
Expand Down Expand Up @@ -343,35 +372,25 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
// Create the loan record (if needed).
let loan = match restr {
RestrictionResult::Safe => {
// No restrictions---no loan record necessary
let loan_scope = loan_scope_from_region(loan_region);
debug!("loan_scope = {:?}", loan_scope);

let borrow_scope = region::CodeExtent::Misc(borrow_id);
let loan_scope = self.compute_gen_scope(borrow_scope, loan_scope);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be enough to show the span when the loan is being held (this is for temporary loans, I believe)? It seems correct when visualizing, but I'm definitely no expert on the internals of the borrow checker.


let safe_loan = SafeLoan {
kind: req_kind,
loan_scope: loan_scope,
span: borrow_span,
};
self.safe_loans.push(safe_loan);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also like to not run through this section unless we're running save analysis. However, I don't see a clear, clean way to notify this section of code about that. Any recommendations on that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't worry too much about that. Save-analysis isn't really the direction we want things to go anyway -- that is, having a distinct "save-analysis mode". I think we hope that eventually the RLS will just be polling the results of regular queries.


// No restrictions---no normal loan record necessary
return;
}

RestrictionResult::SafeIf(loan_path, restricted_paths) => {
let loan_scope = match *loan_region {
ty::ReScope(scope) => scope,

ty::ReEarlyBound(ref br) => {
self.bccx.region_maps.early_free_extent(self.tcx(), br)
}

ty::ReFree(ref fr) => {
self.bccx.region_maps.free_extent(self.tcx(), fr)
}

ty::ReStatic => self.item_ub,

ty::ReEmpty |
ty::ReLateBound(..) |
ty::ReVar(..) |
ty::ReSkolemized(..) |
ty::ReErased => {
span_bug!(
cmt.span,
"invalid borrow lifetime: {:?}",
loan_region);
}
};
let loan_scope = loan_scope_from_region(loan_region);
debug!("loan_scope = {:?}", loan_scope);

let borrow_scope = region::CodeExtent::Misc(borrow_id);
Expand Down
Loading