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

Further ast validation #2729

Merged
merged 15 commits into from
Nov 14, 2023
Merged

Further ast validation #2729

merged 15 commits into from
Nov 14, 2023

Conversation

P-E-P
Copy link
Member

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

The grammar let slip some invalid rust code (on purpose). We need to reject this invalid code in a later pass.

Fixes #2710
Fixes #2711

@P-E-P P-E-P added this to the GCC 14.1 release milestone Nov 8, 2023
@P-E-P P-E-P self-assigned this Nov 8, 2023
@P-E-P P-E-P force-pushed the further-ast-validation branch 3 times, most recently from 65226bc to 9420d3f Compare November 9, 2023 22:28
P-E-P added 14 commits November 9, 2023 23:38
Add lifetime name check in ast validation visitor.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (RS_TOKEN): Add keyword set.
	(RS_TOKEN_KEYWORD): Likewise.
	(ASTValidation::visit): Add validation on lifetime visit.
	* checks/errors/rust-ast-validation.h: Add function prototype.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Lifetime name are restricted and cannot be keyword, this commit add a
test failing the ast validation pass due to some keyword name.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
This file was missing a licence text and it's header guard was not
matching the file name.

gcc/rust/ChangeLog:

	* util/rust-attribute-values.h (RUST_ATTRIBUTES_VALUE_H): Remove old
	header guard.
	(RUST_ATTRIBUTE_VALUES_H): Add new one.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Much like attributes values, keywords are known beforehand and never
change. Instead of relying on handcrafted string we could centralize
everything in one place. We may require to check whether a word is a
keyword, which can now be done easily thanks to the keyword set.

gcc/rust/ChangeLog:

	* Make-lang.in: Add rust-keyword-values.cc to the list.
	* util/rust-keyword-values.cc: New file.
	* util/rust-keyword-values.h: New file.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
We don't require that local set anymore.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (RS_TOKEN): Remove locale set.
	(RS_TOKEN_KEYWORD): Likewise.
	(ASTValidation::visit): Change keyword set call to the one from utils.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Some part of the code requires the token id behind a given keyword, a map
keep the "set" aspect whilst providing this additional feature.

gcc/rust/ChangeLog:

	* lex/rust-lex.cc (RS_TOKEN): Remove local map.
	(RS_TOKEN_KEYWORD): Likewise.
	(Lexer::classify_keyword): Change call to utils.
	* util/rust-keyword-values.cc (get_keywords): Add init function.
	(RS_TOKEN_KEYWORD): Call to X macro.
	* util/rust-keyword-values.h: Change from set to a map.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Change the keyword values from a raw string value to their matching const
value in utils.

gcc/rust/ChangeLog:

	* lex/rust-lex.cc (Lexer::parse_raw_identifier): Use const value.
	* parse/rust-parse-impl.h (Parser::parse_simple_path_segment):
	Likewise.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Break expression were using a raw lifetime value instead of a loop label
this behavior would have lead to some errors in ast validation.

gcc/rust/ChangeLog:

	* ast/rust-expr.h (class BreakExpr): Change Lifetime to LoopLabel.
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Lower lifetime
	inside the label instead.
	* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Resolve the
	inner lifetime.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Prevent from using reserved keyword in label name.

gcc/rust/ChangeLog:

	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Check if there is
	a label before visit.
	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Emit an
	error when a label has a forbidden name.
	* checks/errors/rust-ast-validation.h: Add function prototype.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Loop label error reporting during ast validation was done at loop label
level. It lead to some innacurate error reporting in break loop labels
due to the way the label is built.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Change
	reported error location to the lifetime location.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
An error message should be emitted when the rust code contains invalid
label name. Add a new test for this behavior.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Some construct are forbidden in trait context (eg. pub, async...) and
we'll need to reject those. To do so we need to identify a trait context.

gcc/rust/ChangeLog:

	* ast/rust-ast-visitor.cc (ContextualASTVisitor::visit): Push the new
	trait context when visiting a trait.
	* ast/rust-ast-visitor.h: Add visit function prototype and TRAIT
	context.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Variadics are forbidden alone as well as non final position, this should
be checked during ast validation.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
	check for additional named argument as well as variadic argument's
	position.
	* checks/errors/rust-ast-validation.h: Add visit function prototype for
	external functions.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
Highlight invalid variadic filtering through the ast validation checker.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
@P-E-P P-E-P force-pushed the further-ast-validation branch 3 times, most recently from 2160071 to fd8b13e Compare November 14, 2023 15:08
Make self param and variadic param Param, introduce Param class and make
function parameters param too.
Self can now be represented as a standard parameter and is thus no longer
required as a separate function attribute.
Prevent self pointers and allow self in standard functions during parsing
so they could be rejected at a later stage.

gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc (TokenCollector::visit): Add visitor for
	VariadicParam and remove Self parameter visitor from Function visit.
	* expand/rust-cfg-strip.cc (CfgStrip::maybe_strip_self_param): Remove
	function.
	(CfgStrip::maybe_strip_trait_method_decl): Remove self parameter visit.
	(CfgStrip::maybe_strip_function_params): Handle new function
	parameters.
	(CfgStrip::visit): Handle VariadicParam, SelfParam and FunctionParam.
	* expand/rust-expand-visitor.cc (ExpandVisitor::expand_self_param):
	Remove function.
	(ExpandVisitor::expand_trait_method_decl): Do not visit self parameter.
	(ExpandVisitor::visit): Add visit for VariadicParam, FunctionParam and
	SelfParam.
	(ExpandVisitor::expand_function_params): Visit parameters instead.
	* expand/rust-expand-visitor.h: Update function prototypes.
	* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Update visit
	with new parameters.
	(ResolveTraitItems::visit): Likewise.
	* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
	Update visit functions with the new visitor functions for VariadicParam
	SelfParam and FunctionParam.
	* resolve/rust-early-name-resolver.h: Update function prototypes.
	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Update visitor
	according to the new function parameter structures.
	* ast/rust-ast-visitor.h: Update prototypes and add visitor virtual
	functions for SelfParam, FunctionParam and VariadicParam.
	* ast/rust-ast.cc (Function::Function): Move constructor in
	implementation instead of header.
	(Function::operator=): Likewise.
	(Function::as_string): Update function with pointer dereference.
	(VariadicParam::as_string): Likewise.
	(TraitFunctionDecl::as_string): Likewise.
	(TraitMethodDecl::as_string): Likewise.
	(FunctionParam::accept_vis): Add function for visitor.
	(SelfParam::accept_vis): Likewise.
	(VariadicParam::accept_vis): Likewise.
	(TraitItemFunc::TraitItemFunc): Move constructor to implementation
	file.
	(TraitItemFunc::operator=): Likewise.
	(TraitItemMethod::TraitItemMethod): Likewise.
	(TraitItemMethod::operator=): Likewise.
	* ast/rust-item.h (class Function): Remove self optional member.
	(class TraitMethodDecl): Likewise.
	(class TraitFunctionDecl): Likewise.
	(class Param): Add abstract parameter class.
	(class SelfParam): Inherit from Param and remove parameter common
	members.
	(class FunctionParam): Likewise.
	(class VariadicParam): Likewise.
	(struct Visibility): Move structure declaration.
	(class VisItem):  Likewise.
	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
	a self parameter check during AST validation.
	* checks/errors/rust-ast-validation.h: Add function prototype.
	* expand/rust-derive-clone.cc (DeriveClone::clone_fn): Update function
	constructor.
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_self): Rework
	function for the new parameters.
	(ASTLoweringBase::visit): Add visit functions for VariadicParam,
	FunctionParam and SelfParam.
	* hir/rust-ast-lower-base.h: Update function prototypes.
	* parse/rust-parse-impl.h (Parser::parse_function): Update function
	according to new function representation.
	(Parser::parse_function_param): Return vector of abstract param instead
	of FunctionParam.
	(Parser::parse_method): Update according to new representation.
	(Parser::parse_trait_item): Likewise.
	(Parser::parse_self_param): Error out with
	self pointers and prevent the lexer from eating regular function
	parameters. Update return type.
	* parse/rust-parse.h: Update function return types.
	* ast/rust-ast-collector.h: Add VariadicParam visit prototype.
	* ast/rust-ast.h (struct Visibility): Move struct declaration.
	(class VisItem): Likewise.
	* ast/rust-expr.h: Update included files.
	* checks/errors/rust-feature-gate.h: Add visitor functions for
	SelfParam, FunctionParam and VariadicParam.
	* expand/rust-cfg-strip.h: Update function prototypes.
	* expand/rust-derive.h: Likewise.
	* hir/rust-ast-lower-implitem.h: Handle special arguments.
	* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise.
	* metadata/rust-export-metadata.cc (ExportContext::emit_function):
	Likewise.
	* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Add visitor
	functions.
	* resolve/rust-ast-resolve-base.h: Update prototypes.
	* resolve/rust-ast-resolve-stmt.h: Handle new parameter kind.
	* resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise.
	* resolve/rust-default-resolver.h: Update prototype.
	* util/rust-attributes.cc (AttributeChecker::visit): Add visitor
	functions for SelfParam and VariadicParam.
	* util/rust-attributes.h: Add visit prototypes.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
@P-E-P P-E-P force-pushed the further-ast-validation branch from fd8b13e to c8b4b20 Compare November 14, 2023 15:44
@P-E-P P-E-P requested a review from CohenArthur November 14, 2023 15:45
@P-E-P P-E-P marked this pull request as ready for review November 14, 2023 17:20
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 stuff :) thank you!

@P-E-P P-E-P added this pull request to the merge queue Nov 14, 2023
Merged via the queue into Rust-GCC:master with commit fe0d34c Nov 14, 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.

Emit error when missing named argument (variadic) Emit an error for variadic at wrong position
2 participants