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

Ast validation trait const #2753

Merged
merged 6 commits into from
Dec 1, 2023
Merged

Conversation

P-E-P
Copy link
Member

@P-E-P P-E-P commented Nov 22, 2023

Requires #2751

A function cannot be both async and const, however this should not be
handled in the parser but rather at a later stage in the compiler. This
commit change the AsyncConstStatus in the AST and the HIR to allows a
function to be both async and const.

gcc/rust/ChangeLog:

	* ast/rust-ast-builder.cc (AstBuilder::fn_qualifiers): Change
	constructor to match the new arguments.
	* ast/rust-ast-collector.cc (TokenCollector::visit): Change behavior
	to handle both const and async specifiers at the same time.
	* ast/rust-ast.cc (FunctionQualifiers::as_string): Likewise.
	* ast/rust-item.h (class FunctionQualifiers): Remove AsyncConstStatus
	and replace it with both Async and Const status. Also change the safety
	arguments to use an enum instead of a boolean.
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_qualifiers):
	Update constructor call.
	* hir/tree/rust-hir-item.h: Add Const and Async status, remove
	AsyncConstStatus, update the constructor.
	* hir/tree/rust-hir.cc (FunctionQualifiers::as_string): Update with
	the new status.
	* parse/rust-parse-impl.h (Parser::parse_function_qualifiers): Update
	constructor call.
	* util/rust-common.h (enum Mutability): Make an enum class.
	(enum class): Add Async and Const enum class to avoid booleans.
	(enum Unsafety): Change to an enum class.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
We need to account for const specifiers in async parsing as const
can be used in the syntax before the async keyword.

gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_vis_item): Allow parsing async
	items in const.
	(Parser::parse_async_item): Account for const offset during async
	lookahead.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add a check during AST validation pass to ensure functions are either
const or async but not both.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
	async const check.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Functions that are both async and const shall be rejected during the
AST validation pass. This new test highlight this behavior.

gcc/testsuite/ChangeLog:

	* rust/compile/const_async_function.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
@P-E-P P-E-P added this to the GCC 14.1 release milestone Nov 22, 2023
@P-E-P P-E-P self-assigned this Nov 22, 2023
Add a new check in AST validation pass that checks that no function
declaration in traits are declared const.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
	const check.
	* checks/errors/rust-ast-validation.h: Add visit function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Const fn declaration in trait declarations shall emit an error. This new
test highlight this behavior.

gcc/testsuite/ChangeLog:

	* rust/compile/const_trait_fn.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 November 22, 2023 15:15
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.

Looks great :) thank you!

Comment on lines +125 to +126
rust_error_at (decl.get_identifier ().get_locus (), ErrorCode::E0379,
"functions in traits cannot be declared const");
Copy link
Member

Choose a reason for hiding this comment

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

you can add the same check for async functions, even if that is about to change soon. but probably in another PR is better

@P-E-P P-E-P added this pull request to the merge queue Dec 1, 2023
Merged via the queue into Rust-GCC:master with commit 0d6d394 Dec 1, 2023
9 checks passed
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