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

Parse raw borrow #3020

Merged
merged 4 commits into from
Jun 12, 2024
Merged

Parse raw borrow #3020

merged 4 commits into from
Jun 12, 2024

Conversation

P-E-P
Copy link
Member

@P-E-P P-E-P commented May 21, 2024

@P-E-P P-E-P requested a review from CohenArthur May 21, 2024 09:03
@P-E-P P-E-P self-assigned this May 21, 2024
@P-E-P P-E-P force-pushed the parse_raw_borrow branch 2 times, most recently from 00f00eb to 922e7fc Compare May 28, 2024 11:55
The raw ref operator is an unstable feature required to obtain a pointer
to unaligned adresses (mainly unaligned struct fields) without UB.

gcc/rust/ChangeLog:

	* ast/rust-ast-builder.cc (Builder::ref): Adapt constructor to the new
	API.
	* ast/rust-ast-collector.cc (TokenCollector::visit): Emit a raw weak
	keyword when required.
	* ast/rust-ast.cc (BorrowExpr::as_string): Change as_string
	representation to handle raw ref operator.
	* ast/rust-expr.h (class BorrowExpr): Add raw discriminant.
	* expand/rust-macro-builtins-include.cc: Adapt constructor to the new
	API.
	* parse/rust-parse-impl.h: Handle the raw weak keyword.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
We do not handle those kind of references yet, we shall not let them
pass as a regular reference.

gcc/rust/ChangeLog:

	* ast/rust-expr.h: Add a getter for mutability.
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Panic when a
	raw reference is met.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Raw ref operators are still experimental and shall not slip within
stable rust, they should therefore be feature gated.

gcc/rust/ChangeLog:

	* checks/errors/rust-feature-gate.cc (FeatureGate::visit): Gate raw
	borrow.
	* checks/errors/rust-feature-gate.h: Update function prototype.
	* checks/errors/rust-feature.cc: Add raw_ref_op gate.
	* checks/errors/rust-feature.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add a test for the feature gate, as well as some test to ensure the raw
keyword stays weak. Also add some tests to check whether the raw_ref_op
syntax is parsed correctly.

gcc/testsuite/ChangeLog:

	* rust/compile/not_raw_ref_op.rs: New test.
	* rust/compile/raw_ref_op.rs: New test.
	* rust/compile/raw_ref_op_feature_gate.rs: New test.
	* rust/compile/raw_ref_op_invalid.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
@P-E-P P-E-P marked this pull request as ready for review June 12, 2024 09:15
@P-E-P P-E-P enabled auto-merge June 12, 2024 09:47
@P-E-P P-E-P added this pull request to the merge queue Jun 12, 2024
Merged via the queue into Rust-GCC:master with commit 6fc8ad5 Jun 12, 2024
9 checks passed
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.

great work, the testcases are good, edge cases are handled, well done ❤️

Comment on lines +12404 to +12405
rust_error_at (lexer.peek_token ()->get_locus (),
"raw borrow should be either const or mut");
Copy link
Member

Choose a reason for hiding this comment

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

I can't remember so feel free to disregard, but shouldn't we do something like add_error(Error(...)) here? so that parser errors are only emitted if we want them to be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants