Commit graph

208553 commits

Author SHA1 Message Date
Owen Avery
1740fbe1dd gccrs: Remove TraitImplItem
gcc/rust/ChangeLog:

	* ast/rust-ast-full-decls.h
	(class TraitImplItem): Remove forward declaration.
	(class AssociatedItem): Add forward declaration.
	* ast/rust-ast.h
	(class TraitImplItem): Remove.
	(class TraitItem): Inherit from AssociatedItem.
	(SingleASTNode::take_trait_impl_item):
	Return std::unique_ptr<AssociatedItem> instead of
	std::unique_ptr<TraitImplItem>.
	* ast/rust-item.h
	(class Function): Inherit from AssociatedItem instead of
	TraitImplItem.
	(class TypeAlias): Likewise.
	(class ConstantItem): Likewise.
	(class TraitImpl): Store items as AssociatedItem.
	* expand/rust-derive-clone.cc
	(DeriveClone::clone_fn): Return std::unique_ptr<AssociatedItem>.
	(DeriveClone::clone_impl): Take std::unique_ptr<AssociatedItem>.
	* expand/rust-derive-clone.h
	(DeriveClone::clone_fn): Return std::unique_ptr<AssociatedItem>.
	(DeriveClone::clone_impl): Take std::unique_ptr<AssociatedItem>.
	* expand/rust-expand-visitor.cc
	(ExpandVisitor::visit): Handle changes to
	SingleASTNode::take_trait_impl_item.
	* parse/rust-parse-impl.h
	(Parser::parse_impl): Parse TraitImpl as containing AssociatedItem.
	(Parser::parse_trait_impl_item): Return
	std::unique_ptr<AssociatedItem>.
	(Parser::parse_trait_impl_function_or_method): Likewise.
	* parse/rust-parse.h
	(Parser::parse_trait_impl_item): Return
	std::unique_ptr<AssociatedItem>.
	(Parser::parse_trait_impl_function_or_method): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-30 12:36:52 +01:00
Robert Goss
c3ce1d6d87 gccrs: Add improved error when no fields in initializer
If a struct type with a variant that has fields is initialized with some fields the expression  HIR StructExprStructFields is checked that all the fields are assigned. However, if no fields are initialized the HIR StructExprStruct is generated. This doesn't check if the struct is a unit during typechekc and only fails at the compile stage with a ICE.

Add a check at the typecheck stage that makes sure the struct does not have a variant with fields and give an error message based on the rustc one.

We have also updated the message given in the case where one field was present to list the missing fields and match more closely the new message.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit) Add additional check
	* typecheck/rust-hir-type-check-struct-field.h: A helper method to make error added
	* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve) Update message

gcc/testsuite/ChangeLog:

	* rust/compile/missing_constructor_fields.rs: Added case with no initializers

Signed-off-by: Robert Goss <goss.robert@gmail.com>
2024-01-30 12:36:52 +01:00
Jakub Dupak
b49afa9ba4 gccrs: Test: check implemented for lifetime handling
gcc/testsuite/ChangeLog:

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

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:52 +01:00
Jakub Dupak
4b13ec24b1 gccrs: AST: Fix for lifetime lowering
gcc/rust/ChangeLog:

	* hir/rust-ast-lower-type.cc (ASTLoweringTypeBounds::visit): fix for lifetimes
	(ASTLowerWhereClauseItem::visit): fix for lifetimes

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:52 +01:00
Jakub Dupak
37d4cf4846 gccrs: AST: Fix for lifetime parsing
gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_where_clause): fix parsing
	(Parser::parse_where_clause_item): fix parsing
	(Parser::parse_type_bound_where_clause_item): fix parsing
	(Parser::parse_trait_bound): fix parsing
	* parse/rust-parse.h: fix parsing

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:52 +01:00
Jakub Dupak
aed23b5711 gccrs: Test: fix missing lifetime in a test
This test did not compile with rustc.

gcc/testsuite/ChangeLog:

	* rust/compile/torture/utf8_identifiers.rs: add mising lifetime

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:52 +01:00
Kushal Pal
adb62fe2b4 gccrs: Added newline to get more readable lexdump
Fixes #2783

gcc/rust/ChangeLog:

	* lex/rust-lex.cc (Lexer::dump_and_skip):
	Changed " " to '\n'

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30 12:36:52 +01:00
Owen Avery
1460044f87 gccrs: Unify storage of associated items in SingleASTNode
gcc/rust/ChangeLog:

	* ast/rust-ast-fragment.cc
	(Fragment::assert_single_fragment): Update.

	* ast/rust-ast.h
	(class TraitImplItem): Move definition before that of TraitItem.
	(class TraitItem):
	Inherit from TraitImplItem instead of AssociatedItem.

	(class SingleASTNode): Unify handling of associated items.
	(SingleASTNode::take_assoc_item): Move from...
	(SingleASTNode::take_impl_item): ...here, but leave stub calling
	take_assoc_item behind.
	(SingleASTNode::take_trait_item):
	Cast associated item to TraitItem.
	(SingleASTNode::take_trait_impl_item):
	Cast associated item to TraitImplItem.

	* ast/rust-ast.cc
	(SingleASTNode::SingleASTNode):
	Unify handling of associated items.
	(SingleASTNode::operator=): Likewise.
	(SingleASTNode::accept_vis): Likewise.
	(SingleASTNode::is_error): Likewise.
	(SingleASTNode::as_string): Likewise.

	* ast/rust-item.h
	(class Function): Remove direct inheritence from AssociatedItem.
	(class ConstantItem): Likewise.
	* ast/rust-macro.h
	(class MacroInvocation):
	Remove direct inheritence from AssociatedItem and TraitImplItem.
2024-01-30 12:36:52 +01:00
Robert Goss
88a27c3bc0 gccrs: Add improved error when a field is redefined in a struct constructor
Fixes #2381

If a struct type is initialized with one of it's fields repeated it will currently issue an error at the use site. However we would like to give the rust error code and (like rustc) show both the specifications for the field to help the user diagnose the issue.

We move the check after the index for the field has been established so we can look up if the field has already been defined and get it's location.

We update the visit method to return if it has handled an error otherwise we then output a second fatal error as not all the field in the specification have been processed.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-struct-field.h: Allow visit to return a bool
	* typecheck/rust-hir-type-check-struct.cc: Improve check of repeat fields

gcc/testsuite/ChangeLog:

	* rust/compile/repeated_constructor_fields.rs: Added case with constructor field repeated

Signed-off-by: Robert Goss <goss.robert@gmail.com>
2024-01-30 12:36:51 +01:00
Kushal Pal
7a0c487d2c gccrs: Handle async keyword for regular implementations
Fixes #2788

gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_inherent_impl_item):
	Added switch-case for ASYNC token.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2788.rs: New test.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30 12:36:51 +01:00
Kushal Pal
f7c8a712be gccrs: Fix inconsistent formatting
gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
	Enclose const in single quotes.

gcc/testsuite/ChangeLog:

	* rust/compile/const_trait_fn.rs:
	Enclose const in single quotes.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30 12:36:51 +01:00
Kushal Pal
f46fdb6399 gccrs: Handle async functions in traits
Fixes #2785

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
	Added check for `async` functions inside trait.
	* parse/rust-parse-impl.h (Parser::parse_trait_item):
	Added switch-case for ASYNC token.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2785.rs: New test.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30 12:36:51 +01:00
Nirmal Patel
8b7d6adbbb gccrs: Handle newlines during string parsing while lexing
If newline strings are encountered while lexing, the lexer now handles
newline characters by incrementing current line number. This provides
correct line number when displaying errors. If the lexer encounters end
of file before string end, then it will use the start of the string as
the location to an report error.

gcc/rust/ChangeLog:
	* lex/rust-lex.cc (Lexer::parse_byte_string): Handle newline
	while parsing byte strings
	(Lexer::parse_string): Handle newline while parsing strings

Signed-off-by: Nirmal Patel <nirmal@nirmal.dev>
2024-01-30 12:36:51 +01:00
Jakub Dupak
14025f7359 gccrs: TyTy: Refactor FnType deprecated API
gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::visit): Use new API.
	* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Use new API.
	* typecheck/rust-tyty-cmp.h: Remove old API.
	* typecheck/rust-tyty.cc (FnPtr::is_equal): Use new API.
	* typecheck/rust-tyty.h: Remove old API.
	* typecheck/rust-unify.cc (UnifyRules::expect_fnptr): Use new API.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:51 +01:00
Jakub Dupak
d8e8ad5493 gccrs: ast: Lower 'for' lifetimes
gcc/rust/ChangeLog:

	* hir/rust-ast-lower-type.cc (ASTLoweringType::visit): For lifetimes.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:51 +01:00
Jakub Dupak
bc1a47f72d gccrs: ast: Infer static lifetime for const and static items
(probably incomplete propagation)

gcc/rust/ChangeLog:

	* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_lifetime): Propagate static
	requirement.
	* hir/rust-ast-lower-base.h: Propagate static requirement.
	* hir/rust-ast-lower-implitem.h: Propagate static requirement.
	* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Propagate static requirement.
	* hir/rust-ast-lower-type.cc (ASTLoweringType::translate): Propagate static requirement.
	(ASTLoweringType::visit): Propagate static requirement.
	* hir/rust-ast-lower-type.h: Propagate static requirement.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:51 +01:00
Jakub Dupak
6def638e66 gccrs: ast: Full lifetime elision handling
gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_generic_param): Lifetime elision control.
	(Parser::parse_lifetime_where_clause_item): Lifetime elision control.
	(Parser::parse_type_param_bound): Lifetime elision control.
	(Parser::parse_lifetime_bounds): Lifetime elision control.
	(Parser::parse_lifetime): Lifetime elision control.
	(Parser::parse_path_generic_args): Lifetime elision control.
	(Parser::parse_self_param): Lifetime elision control.
	(Parser::parse_break_expr): Lifetime elision control.
	(Parser::parse_continue_expr): Lifetime elision control.
	(Parser::parse_reference_type_inner): Lifetime elision control.
	* parse/rust-parse.h: Lifetime elision control.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:51 +01:00
Jakub Dupak
50f3059268 gccrs: ast: Unify explicitly and implicitly elided lifettimes
gcc/rust/ChangeLog:

	* ast/rust-ast.h: Elided lifetime static constructor
	* ast/rust-type.h: Default lifetime to elided.
	* parse/rust-parse-impl.h (Parser::parse_lifetime_param): Use elided lifetime.
	(Parser::parse_lifetime): Use elided lifetime/
	(Parser::lifetime_from_token): Use elided lifetime.
	(Parser::parse_self_param): Use elided lifetime.
	(Parser::parse_reference_type_inner): Use elided lifetime.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:50 +01:00
Jakub Dupak
c936a63cb7 gccrs: ast: Fix lifetime type parsing
There was a mismatch whether lifetime 'static is parsed as "static"
or "'static".

gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::lifetime_from_token): Fix matched pattern.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:50 +01:00
Kushal Pal
81fcf1087c gccrs: Generate error for async trait fucntions
Fixes #2767

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
	Added check for `async` function inside trait.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2767.rs: New test.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30 12:36:50 +01:00
Kushal Pal
bffceda8b9 gccrs: Handle async qualifier inside trait
Fixes #2778

gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_trait_impl_item):
	Handled `async` items

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30 12:36:50 +01:00
Raiki Tamura
7b80033eae gccrs: split rust-mangle.cc into two files
gcc/rust/ChangeLog:

	* Make-lang.in: Add .o files
	* backend/rust-mangle.cc (struct V0Path): moved to splitted files
	(v0_path): Likewise.
	(legacy_mangle_name): Likewise.
	(legacy_mangle_canonical_path): Likewise.
	(legacy_hash): Likewise.
	(v0_tuple_prefix): Likewise.
	(v0_numeric_prefix): Likewise.
	(v0_simple_type_prefix): Likewise.
	(v0_complex_type_prefix): Likewise.
	(v0_integer_62): Likewise.
	(v0_opt_integer_62): Likewise.
	(v0_disambiguator): Likewise.
	(v0_type_prefix): Likewise.
	(v0_generic_args): Likewise.
	(v0_identifier): Likewise.
	(v0_type_path): Likewise.
	(v0_function_path): Likewise.
	(v0_scope_path): Likewise.
	(v0_crate_path): Likewise.
	(v0_inherent_or_trait_impl_path): Likewise.
	(v0_closure): Likewise.
	(legacy_mangle_item): Likewise.
	(v0_mangle_item): Likewise.
	* backend/rust-mangle.h (legacy_mangle_item): Likewise.
	(v0_mangle_item): Likewise.
	* backend/rust-mangle-legacy.cc: New file.
	* backend/rust-mangle-v0.cc: New file.

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
2024-01-30 12:36:50 +01:00
Jakub Dupak
c54326a867 gccrs: BIR: Cleanup
gcc/rust/ChangeLog:

	* checks/errors/borrowck/rust-bir-place.h: Cleanup.
	* checks/errors/borrowck/rust-borrow-checker.h: Cleanup.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:50 +01:00
Jakub Dupak
75e6d0cf41 gccrs: TyTy: SubstitutionRef cast specialization
Allows skipping parent check when casting.

gcc/rust/ChangeLog:

	* typecheck/rust-tyty.h (BaseType::is): Cast API.
	(SubstitutionRef>): Cast API.
	(BaseType::as): Cast API.
	(BaseType::try_as): Cast API.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:50 +01:00
Jakub Dupak
44c58e2972 gccrs: TyTy: Common interface for fucntion-like types
gcc/rust/ChangeLog:

	* typecheck/rust-tyty.h (class ClosureType): Inherit interface.
	(class FnPtr): Inherit interface.
	(class FnType): Inherit interface.
	(class CallableTypeInterface): New interface.
	(BaseType::is): Detect interface members API.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:50 +01:00
Jakub Dupak
7eaef4f5b1 gccrs: TyTy: refactor to new API
gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Refactor.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:50 +01:00
Jakub Dupak
35d5f82eae gccrs: BIR: Fix missed nodiscard
gcc/rust/ChangeLog:

	* checks/errors/borrowck/rust-bir-builder-internal.h: Replace nodiscard.
	* checks/errors/borrowck/rust-bir-place.h: Replace nodiscard.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:50 +01:00
Jakub Dupak
ed198edc9b gccrs: TyTy: Fix missed nodiscard
gcc/rust/ChangeLog:

	* typecheck/rust-tyty.h: Fix nodiscard to warn unused.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:49 +01:00
Jakub Dupak
cc9465332e gccrs: HIR: add missing getters
gcc/rust/ChangeLog:

	* hir/tree/rust-hir-item.h: Ad lifetime getter.
	* hir/tree/rust-hir-path.h: Make getter const ref.
	* hir/tree/rust-hir.h: Const ref and new getter.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30 12:36:49 +01:00
Arthur Cohen
5bcfcfe985 gccrs: refactor builtins initialization and attributes
This commit performs builtin initialization in a more "GCC-y" way,
similarly to what the D frontend is doing. This way, we no longer have
to worry about invalid attributes or types when initializing them by
hand.

Also add attributes support through LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
lang hook.

Most of these changes are based on D frontend.

gcc/rust/ChangeLog:

	* Make-lang.in (GRS_OBJS): Add rust-attribs.o.
	* backend/rust-builtins.cc (builtin_const, builtin_noreturn)
	(builtin_novops): Remove.
	(BuiltinsContext::lookup_simple_builtin): Adjust.
	(BuiltinsContext::setup_overflow_fns): Remove.
	(BuiltinsContext::define_function_type): Set builtin type to
	errormark so the builtin is considered unavailable.
	(BuiltinsContext::setup_math_fns): Remove.
	(BuiltinsContext::setup_atomic_fns): Remove.
	(build_c_type_nodes): Refactor based on D frontend.
	(BuiltinsContext::define_builtin_types): Likewise.
	(DEF_PRIMITIVE_TYPE): New.
	(DEF_FUNCTION_TYPE_0): New.
	(DEF_FUNCTION_TYPE_1): New.
	(DEF_FUNCTION_TYPE_2): New.
	(DEF_FUNCTION_TYPE_3): New.
	(DEF_FUNCTION_TYPE_4): New.
	(DEF_FUNCTION_TYPE_5): New.
	(DEF_FUNCTION_TYPE_6): New.
	(DEF_FUNCTION_TYPE_7): New.
	(DEF_FUNCTION_TYPE_8): New.
	(DEF_FUNCTION_TYPE_9): New.
	(DEF_FUNCTION_TYPE_10): New.
	(DEF_FUNCTION_TYPE_11): New.
	(DEF_FUNCTION_TYPE_VAR_0): New.
	(DEF_FUNCTION_TYPE_VAR_1): New.
	(DEF_FUNCTION_TYPE_VAR_2): New.
	(DEF_FUNCTION_TYPE_VAR_3): New.
	(DEF_FUNCTION_TYPE_VAR_4): New.
	(DEF_FUNCTION_TYPE_VAR_5): New.
	(DEF_FUNCTION_TYPE_VAR_6): New.
	(DEF_FUNCTION_TYPE_VAR_7): New.
	(DEF_FUNCTION_TYPE_VAR_11): New.
	(DEF_POINTER_TYPE): New.
	(BuiltinsContext::setup): Adjust.
	(BuiltinsContext::define_builtin_attributes): New.
	(DEF_ATTR_NULL_TREE): New.
	(DEF_ATTR_INT): New.
	(DEF_ATTR_STRING): New.
	(DEF_ATTR_IDENT): New.
	(DEF_ATTR_TREE_LIST): New.
	(handle_flags): Remove.
	(BuiltinsContext::define_builtins): New.
	(DEF_BUILTIN): New.
	(BuiltinsContext::define_builtin): Remove.
	(BuiltinsContext::register_rust_mappings): New. Add all missing
	builtins.
	(BuiltinsContext::lookup_gcc_builtin): Adjust.
	* backend/rust-builtins.h (DEF_PRIMITIVE_TYPE): New.
	(DEF_FUNCTION_TYPE_0): New.
	(DEF_FUNCTION_TYPE_1): New.
	(DEF_FUNCTION_TYPE_2): New.
	(DEF_FUNCTION_TYPE_3): New.
	(DEF_FUNCTION_TYPE_4): New.
	(DEF_FUNCTION_TYPE_5): New.
	(DEF_FUNCTION_TYPE_6): New.
	(DEF_FUNCTION_TYPE_7): New.
	(DEF_FUNCTION_TYPE_8): New.
	(DEF_FUNCTION_TYPE_9): New.
	(DEF_FUNCTION_TYPE_10): New.
	(DEF_FUNCTION_TYPE_11): New.
	(DEF_FUNCTION_TYPE_VAR_0): New.
	(DEF_FUNCTION_TYPE_VAR_1): New.
	(DEF_FUNCTION_TYPE_VAR_2): New.
	(DEF_FUNCTION_TYPE_VAR_3): New.
	(DEF_FUNCTION_TYPE_VAR_4): New.
	(DEF_FUNCTION_TYPE_VAR_5): New.
	(DEF_FUNCTION_TYPE_VAR_6): New.
	(DEF_FUNCTION_TYPE_VAR_7): New.
	(DEF_FUNCTION_TYPE_VAR_11): New.
	(DEF_POINTER_TYPE): New.
	(DEF_ATTR_NULL_TREE): New.
	(DEF_ATTR_INT): New.
	(DEF_ATTR_STRING): New.
	(DEF_ATTR_IDENT): New.
	(DEF_ATTR_TREE_LIST): New.
	* backend/rust-compile-intrinsic.cc (Intrinsics::compile): Add
	comment.
	(op_with_overflow_inner): Adjust.
	(copy_handler_inner): Adjust.
	(prefetch_data_handler): Adjust.
	(build_atomic_builtin_name): Adjust.
	(atomic_load_handler_inner): Adjust.
	(uninit_handler): Adjust.
	(move_val_init_handler): Adjust.
	(expect_handler_inner): Adjust.
	* rust-gcc.cc (fetch_overflow_builtins): Adjust.
	* rust-lang.cc (rust_localize_identifier): Adjust.
	(LANG_HOOKS_COMMON_ATTRIBUTE_TABLE): New.
	* rust-attribs.cc: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/torture/intrinsics-4.rs: Adjust.
	* rust/compile/torture/intrinsics-math.rs: Adjust.
	* rust/execute/torture/atomic_load.rs: Adjust.
	* rust/execute/torture/atomic_store.rs: Adjust.
	* rust/compile/torture/intrinsics-1.rs: Removed.
	* rust/compile/torture/builtin_abort.rs: New test.
	* rust/execute/torture/builtin_abort.rs: New test.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2024-01-30 12:36:49 +01:00
Pierre-Emmanuel Patry
5ee51aa3e5 gccrs: Change cfg stripper to use default visitor
Cfg visitor used it's own visit patterns, this made the code longer than
required.

gcc/rust/ChangeLog:

	* expand/rust-cfg-strip.cc (CfgStrip::visit): Change calls from visitor
	to default visitor.
	(CfgStrip::go): Add call to visit crate.
	* expand/rust-cfg-strip.h (class CfgStrip): Update prototypes and
	remove empty ones.
	* ast/rust-ast-visitor.cc: add WhereClause condition check.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:49 +01:00
Pierre-Emmanuel Patry
65fb288e0d gccrs: Make expand visitor inherit from default visitor
Many visit functions in the expand visitor simply visit their components
like the default visitor. Making the expand visitor inherit from the
default visitor allows us to keep all visitor in sync without having to
change every visitor.

gcc/rust/ChangeLog:

	* expand/rust-expand-visitor.cc (ExpandVisitor::go): Add call to visit
	on the crate.
	(ExpandVisitor::visit): Remove some visit functions in favor of their
	default visitor counterpart.
	* expand/rust-expand-visitor.h (class ExpandVisitor): Inherit from
	default visitor and remove now useless function prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:49 +01:00
Pierre-Emmanuel Patry
b3b5115533 gccrs: Make default resolver inherit from default visitor
The default resolver put some scope in place but mostly has traversal
functions similar to the default ast visitor, making it inherit from the
default visitor allows us to avoid code duplication.

gcc/rust/ChangeLog:

	* resolve/rust-default-resolver.cc (DefaultResolver::visit): Remove
	duplicated functions.
	* resolve/rust-default-resolver.h (class DefaultResolver): Make the
	default resolver inherit from the default visitor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:49 +01:00
Owen Avery
bdbc24c36c gccrs: Allow enabling lang_items and no_core features
gcc/rust/ChangeLog:

	* checks/errors/rust-feature.cc
	(Feature::name_hash_map):
	Add entries for Name::LANG_ITEMS and Name::NO_CORE.
	* checks/errors/rust-feature.h
	(Feature::Name::LANG_ITEMS): New.
	(Feature::Name::NO_CORE): New.

gcc/testsuite/ChangeLog:

	* rust/compile/sized-stub.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-30 12:36:49 +01:00
Kushal Pal
d7b2d6704f gccrs: Renamed WIN64 to WIN_64
Fixes issue #2768

gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc (HIRCompileBase::setup_abi_options):
	Renamed `WIN64` to `WIN_64`
	* util/rust-abi.cc (get_abi_from_string): Likewise
	(get_string_from_abi): Likewise
	* util/rust-abi.h (enum ABI): Likewise

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30 12:36:49 +01:00
Nobel Singh
f48f9a9ac6 gccrs: Generate error for const trait functions
Fixes issue #2040

Add check to assure that a function cant be declared const inside trait impl
blocks.

gcc/rust/ChangeLog:

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

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2040.rs: New test.

Signed-off-by: Nobel Singh <nobel2073@gmail.com>
2024-01-30 12:36:49 +01:00
Pierre-Emmanuel Patry
a7991de39f gccrs: Add a regression test for function body check
Function body check emit an error message when a required function body
is missing.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
04fd5a043b gccrs: Add validation for functions without body
Add checks in the ast validation pass to error out with functions
(either free or associated) without a definition.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
	a validation check and emit an error depending on the context.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
7a98939479 gccrs: Make function bodies truly optional
Missing body on a function should be rejected at a later stage in the
compiler, not during parsing.

gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc (TokenCollector::visit): Adapt defintion
	getter.
	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
	* expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise.
	* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise.
	* hir/rust-ast-lower-implitem.h: Likewise.
	* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise.
	* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise.
	* resolve/rust-ast-resolve-stmt.h: Likewise.
	* resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise.
	* util/rust-attributes.cc (AttributeChecker::visit):  Likewise.
	* parse/rust-parse-impl.h: Allow empty function body during parsing.
	* ast/rust-ast.cc (Function::Function): Constructor now take an
	optional for the body.
	(Function::operator=): Adapt to new optional member.
	(Function::as_string): Likewise.
	* ast/rust-item.h (class Function): Make body optional and do not
	rely on nullptr anymore.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
f5e04e4049 gccrs: Add execution test for name resolution 2.0
We already have some compile tests but it lacked an execution test to
ensure everything compiles correctly to the correct value.

gcc/testsuite/ChangeLog:

	* rust/execute/torture/name_resolution.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
a3f4152a31 gccrs: Add multiple regression test in name resolution
Local variables and functions or global variables have different
resolution when binded to a variable. This was not covered before, even
though it was handled by the new name resolution. This commit highlight
this behavior from the new name resolution mechanism.

gcc/testsuite/ChangeLog:

	* rust/compile/name_resolution11.rs: New test.
	* rust/compile/name_resolution12.rs: New test.
	* rust/compile/name_resolution13.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
4cd0315aa9 gccrs: Make early name resolver inherit from default one
Many visit functions in the early name resolver are just plain old
traversal visit functions like the ones from the default visitor.

gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver.cc (EarlyNameResolver::resolve_generic_args):
	Move function.
	(EarlyNameResolver::resolve_qualified_path_type): Likewise.
	(EarlyNameResolver::visit): Add a top level visit function for crate
	and remove duplicated code.
	* resolve/rust-early-name-resolver.h (class EarlyNameResolver): Update
	overriden function list.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
1761af6b2a gccrs: Change the attribute checker visitor to default one
Make the attribute checker visitor inherit from the default visitor
in order to keep visit behavior shared.

gcc/rust/ChangeLog:

	* util/rust-attributes.cc (AttributeChecker::visit): Add visit function
	for crates.
	* util/rust-attributes.h (class AttributeChecker): Update function
	prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
9f6d301004 gccrs: Make feature gate visitor inherit from default one
The feature gating behavior may be shortened and kept cleaner using the
default visitor. This means less maintenance on visit functions as the
traversal is shared by multiple visitors.

gcc/rust/ChangeLog:

	* checks/errors/rust-feature-gate.cc (FeatureGate::visit): Add a visit
	function for the crate level.
	(FeatureGate::check): Add call to crate visit.
	* checks/errors/rust-feature-gate.h (class FeatureGate): Remove now
	useless visit functions (traversal only).

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:48 +01:00
Pierre-Emmanuel Patry
ab257dcd66 gccrs: Add regression test for const fn in trait
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>
2024-01-30 12:36:47 +01:00
Pierre-Emmanuel Patry
9279483f6e gccrs: Add AST validation check for const in trait
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>
2024-01-30 12:36:47 +01:00
Pierre-Emmanuel Patry
ac501a75b2 gccrs: Add a regression test for async const functions
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>
2024-01-30 12:36:47 +01:00
Pierre-Emmanuel Patry
04b3089a47 gccrs: Add async const function ast validation pass
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>
2024-01-30 12:36:47 +01:00
Pierre-Emmanuel Patry
68990dbead gccrs: Allow const and async specifiers in functions
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>
2024-01-30 12:36:47 +01:00
Pierre-Emmanuel Patry
12844c82de gccrs: Split async and const function qualifiers
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>
2024-01-30 12:36:47 +01:00