-
Notifications
You must be signed in to change notification settings - Fork 1
Enable to call Fn closures via FnMut and FnOnce #48
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a37f42f
Move ReborrowVisitor to a separate module
coord-e 8064554
Ensure Fn trait calls resolve to closure DefId
coord-e f08dfcf
Add RustCallVisitor to adjust self type of closure calls
coord-e 2ea6cb2
Analyzer::local_fn_sig doesn't need to be limited to LocalDefId
coord-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,136 +1,5 @@ | ||
| use rustc_middle::mir::{self, Local}; | ||
| use rustc_middle::ty::{self as mir_ty, TyCtxt}; | ||
| mod reborrow; | ||
| mod rust_call; | ||
|
|
||
| use crate::analyze::ReplacePlacesVisitor; | ||
|
|
||
| pub struct ReborrowVisitor<'a, 'tcx, 'ctx> { | ||
| tcx: TyCtxt<'tcx>, | ||
| analyzer: &'a mut super::Analyzer<'tcx, 'ctx>, | ||
| } | ||
|
|
||
| impl<'tcx> ReborrowVisitor<'_, 'tcx, '_> { | ||
| fn insert_borrow(&mut self, place: mir::Place<'tcx>, inner_ty: mir_ty::Ty<'tcx>) -> Local { | ||
| let r = mir_ty::Region::new_from_kind(self.tcx, mir_ty::RegionKind::ReErased); | ||
| let ty = mir_ty::Ty::new_mut_ref(self.tcx, r, inner_ty); | ||
| let decl = mir::LocalDecl::new(ty, Default::default()).immutable(); | ||
| let new_local = self.analyzer.local_decls.push(decl); | ||
| let new_local_ty = self.analyzer.borrow_place_(place, inner_ty); | ||
| self.analyzer.bind_local(new_local, new_local_ty); | ||
| tracing::info!(old_place = ?place, ?new_local, "implicitly borrowed"); | ||
| new_local | ||
| } | ||
|
|
||
| fn insert_reborrow(&mut self, place: mir::Place<'tcx>, inner_ty: mir_ty::Ty<'tcx>) -> Local { | ||
| let r = mir_ty::Region::new_from_kind(self.tcx, mir_ty::RegionKind::ReErased); | ||
| let ty = mir_ty::Ty::new_mut_ref(self.tcx, r, inner_ty); | ||
| let decl = mir::LocalDecl::new(ty, Default::default()).immutable(); | ||
| let new_local = self.analyzer.local_decls.push(decl); | ||
| let new_local_ty = self.analyzer.borrow_place_(place, inner_ty); | ||
| self.analyzer.bind_local(new_local, new_local_ty); | ||
| tracing::info!(old_place = ?place, ?new_local, "implicitly reborrowed"); | ||
| new_local | ||
| } | ||
| } | ||
|
|
||
| impl<'a, 'tcx, 'ctx> mir::visit::MutVisitor<'tcx> for ReborrowVisitor<'a, 'tcx, 'ctx> { | ||
| fn tcx(&self) -> TyCtxt<'tcx> { | ||
| self.tcx | ||
| } | ||
|
|
||
| fn visit_assign( | ||
| &mut self, | ||
| place: &mut mir::Place<'tcx>, | ||
| rvalue: &mut mir::Rvalue<'tcx>, | ||
| location: mir::Location, | ||
| ) { | ||
| if !self.analyzer.is_defined(place.local) { | ||
| self.super_assign(place, rvalue, location); | ||
| return; | ||
| } | ||
|
|
||
| if place.projection.is_empty() && self.analyzer.is_mut_local(place.local) { | ||
| let ty = self.analyzer.local_decls[place.local].ty; | ||
| let new_local = self.insert_borrow(place.local.into(), ty); | ||
| let new_place = self.tcx.mk_place_deref(new_local.into()); | ||
| ReplacePlacesVisitor::with_replacement(self.tcx, place.local.into(), new_place) | ||
| .visit_rvalue(rvalue, location); | ||
| *place = new_place; | ||
| self.super_assign(place, rvalue, location); | ||
| return; | ||
| } | ||
|
|
||
| let inner_place = if place.projection.last() == Some(&mir::PlaceElem::Deref) { | ||
| // *m = *m + 1 => m1 = &mut m; *m1 = *m + 1 | ||
| let mut projection = place.projection.as_ref().to_vec(); | ||
| projection.pop(); | ||
| mir::Place { | ||
| local: place.local, | ||
| projection: self.tcx.mk_place_elems(&projection), | ||
| } | ||
| } else { | ||
| // s.0 = s.0 + 1 => m1 = &mut s.0; *m1 = *m1 + 1 | ||
| *place | ||
| }; | ||
|
|
||
| let ty = inner_place.ty(&self.analyzer.local_decls, self.tcx).ty; | ||
| let (new_local, new_place) = match ty.kind() { | ||
| mir_ty::TyKind::Ref(_, inner_ty, m) if m.is_mut() => { | ||
| let new_local = self.insert_reborrow(*place, *inner_ty); | ||
| (new_local, new_local.into()) | ||
| } | ||
| mir_ty::TyKind::Adt(adt, args) if adt.is_box() => { | ||
| let inner_ty = args.type_at(0); | ||
| let new_local = self.insert_borrow(*place, inner_ty); | ||
| (new_local, new_local.into()) | ||
| } | ||
| _ => { | ||
| let new_local = self.insert_borrow(*place, ty); | ||
| (new_local, self.tcx.mk_place_deref(new_local.into())) | ||
| } | ||
| }; | ||
|
|
||
| ReplacePlacesVisitor::with_replacement(self.tcx, inner_place, new_place) | ||
| .visit_rvalue(rvalue, location); | ||
| *place = self.tcx.mk_place_deref(new_local.into()); | ||
| self.super_assign(place, rvalue, location); | ||
| } | ||
|
|
||
| // TODO: is it always true that the operand is not referred again in rvalue | ||
| fn visit_operand(&mut self, operand: &mut mir::Operand<'tcx>, location: mir::Location) { | ||
| let Some(p) = operand.place() else { | ||
| self.super_operand(operand, location); | ||
| return; | ||
| }; | ||
|
|
||
| let mir_ty::TyKind::Ref(_, inner_ty, m) = | ||
| p.ty(&self.analyzer.local_decls, self.tcx).ty.kind() | ||
| else { | ||
| self.super_operand(operand, location); | ||
| return; | ||
| }; | ||
|
|
||
| if m.is_mut() { | ||
| let new_local = self.insert_reborrow(self.tcx.mk_place_deref(p), *inner_ty); | ||
| *operand = mir::Operand::Move(new_local.into()); | ||
| } | ||
|
|
||
| self.super_operand(operand, location); | ||
| } | ||
| } | ||
|
|
||
| impl<'a, 'tcx, 'ctx> ReborrowVisitor<'a, 'tcx, 'ctx> { | ||
| pub fn new(analyzer: &'a mut super::Analyzer<'tcx, 'ctx>) -> Self { | ||
| let tcx = analyzer.tcx; | ||
| Self { analyzer, tcx } | ||
| } | ||
|
|
||
| pub fn visit_statement(&mut self, stmt: &mut mir::Statement<'tcx>) { | ||
| // dummy location | ||
| mir::visit::MutVisitor::visit_statement(self, stmt, mir::Location::START); | ||
| } | ||
|
|
||
| pub fn visit_terminator(&mut self, term: &mut mir::Terminator<'tcx>) { | ||
| // dummy location | ||
| mir::visit::MutVisitor::visit_terminator(self, term, mir::Location::START); | ||
| } | ||
| } | ||
| pub use reborrow::ReborrowVisitor; | ||
| pub use rust_call::RustCallVisitor; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.