Skip to content
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

borrowck: Added location support to BIR nodes #3013

Merged
merged 2 commits into from
Aug 1, 2024

Conversation

braw-lee
Copy link
Contributor

@braw-lee braw-lee commented May 20, 2024

depends on #3076
Added location field to BIR::Statement and BIR::Place, updated the BIR builders to take the source location from HIR and pass it to BIR node.

@braw-lee
Copy link
Contributor Author

should I work on my branch the whole time, and you can merge it after 2-3 months?
or should I just create new PRs whenever I get something done and you will merge those from time to time? @CohenArthur @P-E-P

@P-E-P
Copy link
Member

P-E-P commented May 21, 2024

should I work on my branch the whole time, and you can merge it after 2-3 months? or should I just create new PRs whenever I get something done and you will merge those from time to time? @CohenArthur @P-E-P

Actually it would be better to merge your content quite often. This way other contributors can follow your work and can work over it. Moreover it'll be easier to review and you won't have to make huge rebase.

We may be a little slow to merge some PRs sometime because reviewing them take some time. You may create some PRs on top of another with the mention "Requires #PRNUMBER", you may even put a link to the actual diff just like in #3014. If you do so please keep those PR in draft whilst it's dependencies are not merged so they don't end up merged.

@P-E-P P-E-P requested review from jdupak and P-E-P May 21, 2024 09:07
Comment on lines 258 to 262
void push_tmp_assignment (PlaceId rhs, location_t location)
{
push_tmp_assignment (new Assignment (rhs), ctx.place_db[rhs].tyty);
Copy link
Member

Choose a reason for hiding this comment

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

we're not using the location argument here yet - I assume this is intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

missed it, will clean it up

@@ -77,26 +77,40 @@ class Statement
// otherwise: <unused>
std::unique_ptr<AbstractExpr> expr;
TyTy::BaseType *type;
// stores location of the actual expression from source code
// currently only available when kind == Kind::ASSIGNMENT
tl::optional<location_t> location;
Copy link
Member

Choose a reason for hiding this comment

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

do we want to make this optional? if this is just a temporary measure, then we should add a FIXME comment

Copy link
Contributor

Choose a reason for hiding this comment

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

If this is a TODO, it would make more sense to use just location and init it to UNKNOWN_LOCATION.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, will fix that

Copy link
Contributor

@jdupak jdupak left a comment

Choose a reason for hiding this comment

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

Mostly looks good. There are some issues around Places.

@@ -82,7 +82,9 @@ class PatternBindingBuilder : protected AbstractBuilder,

if (init.has_value ())
{
push_assignment (translated, init.value ());
push_assignment (
translated, init.value (),
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe, it would make more sense here to pass the location to the visit identifier method.

@@ -78,12 +78,13 @@ struct Place
TyTy::BaseType *tyty;
FreeRegions regions{{}};
std::vector<LoanId> borrowed_by{};
location_t location;
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure, if this is needed. We might have just locations for Origin.

@@ -255,7 +256,9 @@ class PlaceDB

PlaceId add_variable (NodeId id, TyTy::BaseType *tyty)
{
return add_place ({Place::VARIABLE, id, {}, is_type_copy (tyty), tyty}, 0);
return add_place (
{Place::VARIABLE, id, {}, is_type_copy (tyty), tyty, tyty->get_locus ()},
Copy link
Contributor

Choose a reason for hiding this comment

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

Using location of type does not seem right. As I mentioned before, we might not oven need it here. Alternative could be to extract the location from NodeId. But that can always be done on demand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually I was first trying to use get_ast_item() method to retrieve the location, but it seems like the location is copied from AST->HIR->TyTy, so getting locus from NodeId or the TyTy should be same, unless we plan to change the location information of TyTy in future

@@ -77,26 +77,40 @@ class Statement
// otherwise: <unused>
std::unique_ptr<AbstractExpr> expr;
TyTy::BaseType *type;
// stores location of the actual expression from source code
// currently only available when kind == Kind::ASSIGNMENT
tl::optional<location_t> location;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is a TODO, it would make more sense to use just location and init it to UNKNOWN_LOCATION.

@braw-lee braw-lee marked this pull request as draft May 27, 2024 11:58
@braw-lee braw-lee force-pushed the add_location branch 4 times, most recently from 7c4222c to 0f34d93 Compare July 11, 2024 03:04
@braw-lee braw-lee marked this pull request as ready for review July 12, 2024 06:55
Comment on lines 342 to 349
template <typename T>
void move_all (T &args, std::vector<location_t> capture_locations)
{
std::transform (args.begin (), args.end (), args.begin (),
[this] (PlaceId arg) { return move_place (arg); });
rust_assert (args.size () == capture_locations.size ());
std::transform (args.begin (), args.end (), capture_locations.begin (),
args.begin (), [this] (PlaceId arg, location_t location) {
return move_place (arg, location);
});
Copy link
Member

Choose a reason for hiding this comment

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

hm, I don't know if the second parameters should be called capture_locations and not locations. as far as I understand it, this isn't strictly restricted to closure captures, right? @jdupak what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well , I think that captures are the only place, where you have multiple diff­erent loc­ations. But In tha­t case this function should be renamed. I'm not sure if such function makes sense in this context.

@CohenArthur
Copy link
Member

This needs to be rebased as well @braw-lee

Comment on lines 55 to 57
pub loan_errors: bool,
pub subset_errors: bool,
pub move_errors: bool,
pub loan_errors: *mut FFIVector,
pub move_errors: *mut FFIVector,
pub subset_errors: *mut FFIVector,
Copy link
Member

Choose a reason for hiding this comment

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

Are those changes generated using bindgen ?

@@ -46,6 +63,12 @@ impl From<GccrsAtom> for usize {
}
}

impl From<&GccrsAtom> for usize {
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to take this by reference ? GccrsAtom is Copy this looks weird since we already have a From implementation for GccrsAtom I bet every situation can be dealt with at the call site.

braw-lee added 2 commits July 18, 2024 02:44
This commit adds location_t to BIR::Statement where type is ASSIGNMENT
this information will be later used for reporting borrow-checking
errors.

gcc/rust/ChangeLog:

	* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
	(ExprStmtBuilder::visit): Added location parameter.
	* checks/errors/borrowck/rust-bir-builder-internal.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h:
	Likewise.
	* checks/errors/borrowck/rust-bir-builder-pattern.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder.h: Likewise.
	* checks/errors/borrowck/rust-bir.h: Likewise.

Signed-off-by: Kushal Pal <[email protected]>
This commit adds location_t to BIR::Loan, this location will point to
location is source code where the borrow occured, this information will
be useful for reporting borrow-checking errors.

gcc/rust/ChangeLog:

	* checks/errors/borrowck/rust-bir-builder-internal.h:
	Fill location for loan.
	* checks/errors/borrowck/rust-bir-place.h (struct Loan):
	Add location field.

Signed-off-by: Kushal Pal <[email protected]>
Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

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

LGTM!

@P-E-P P-E-P added this pull request to the merge queue Aug 1, 2024
Merged via the queue into Rust-GCC:master with commit bcd860f Aug 1, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

4 participants