Commit graph

208739 commits

Author SHA1 Message Date
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
Owen Avery
f84e564243 gccrs: Remove class AST::InherentImplItem
gcc/rust/ChangeLog:

	* ast/rust-ast-full-decls.h
	(class InherentImplItem): Remove.
	* ast/rust-ast.h
	(class InherentImplItem): Remove.
	(class SingleASTNode):
	Store pointer to AssociatedItem instead of InherentImplItem.

	* ast/rust-ast.cc
	(SingleASTNode::SingleASTNode):
	Use clone_associated_item instead of clone_inherent_impl_item.
	(SingleASTNode::operator=): Likewise.

	* ast/rust-item.h
	(class InherentImpl):
	Use AssociatedItem rather than InherentImplItem.
	(class Function): Likewise.
	(class ConstantItem): Likewise.
	* ast/rust-macro.h
	(class MacroInvocation): Likewise.
	* expand/rust-expand-visitor.cc
	(ExpandVisitor::visit): Likewise.
	* parse/rust-parse-impl.h
	(Parser::parse_impl): Likewise.
	(Parser::parse_inherent_impl_item): Likewise.
	(Parser::parse_inherent_impl_function_or_method): Likewise.
	* parse/rust-parse.h
	(Parser::parse_inherent_impl_item): Likewise.
	(Parser::parse_inherent_impl_function_or_method): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-30 12:36:47 +01:00
Philip Herron
b4fc851e3a gccrs: Remove backend dependancy on resolution rib information
When making more desugaring for the HIR we can need to add new Let bindings
which will require namesolution information but also rib information for
which block the let binding is associated which was very unnessecary. This
patch simply updates the BLOCK_CONTEXT of the current scope as we are
compiling and encounter HIR::LetStmts instead of trying to do it all
upfront which lots of wierd checks

gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc (HIRCompileBase::compile_locals_for_block): removed
	* backend/rust-compile-base.h: update header
	* backend/rust-compile-block.cc (CompileBlock::visit): remove old logic
	* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function): likewise
	* backend/rust-compile-stmt.cc (CompileStmt::visit): likewise
	* backend/rust-compile-var-decl.h: ensure we setup tuple bindings correctly

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
2024-01-30 12:36:47 +01:00
Pierre-Emmanuel Patry
088e3b0b7a gccrs: Add a regression test for unsafe module validation
Add a new test to check for unsafe modules during AST validation pass.

gcc/testsuite/ChangeLog:

	* rust/compile/unsafe_module.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
5959f32d28 gccrs: Emit an error on unsafe modules
An error should be emitted on unsafe modules during the AST validation
pass as the syntax allows those even though they're not alowed later down
the line.

gcc/rust/ChangeLog:

	* ast/rust-item.h: Add safety getter to modules.
	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Check
	a module's safety and emit an error when meeting an unsafe module.
	* checks/errors/rust-ast-validation.h: Add function prototype.
	* parse/rust-parse-impl.h (Parser::parse_module): Move the module locus
	to the first token instead of the mod keyword.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:47 +01:00
Pierre-Emmanuel Patry
d6a0d96559 gccrs: Parse module safety
Unsafe module are rejected at a later stage but could not be parsed
properly. This commit changes the parser in order to allow unsafe module
in the AST.

gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_vis_item): Dispatch to parse
	module when meeting an unsafe module.
	(Parser::parse_module): Set unsafe status when the parser encounter an
	unsafe keyword.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:46 +01:00
Pierre-Emmanuel Patry
7a4c2f8e43 gccrs: Add unsafety member to modules
The rust syntax allows unsafe module even if those are rejected at a
later stage.

gcc/rust/ChangeLog:

	* ast/rust-item.h: Add safety status to Modules in the AST.
	* parse/rust-parse-impl.h (Parser::parse_module): Adapt constructors.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30 12:36:46 +01:00
Owen Avery
b71668068b gccrs: Create base class for TupleStructItems and TuplePatternItems
gcc/rust/ChangeLog:

	* hir/tree/rust-hir-pattern.h
	(class TupleItems): New.
	(class TupleStructItems): Inherit from TupleItems.
	(class TuplePatternItems): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-30 12:36:46 +01:00
Arthur Cohen
8fa748692c gccrs: toplevel: Resolve use declarations
gcc/rust/ChangeLog:

	* resolve/rust-toplevel-name-resolver-2.0.cc
	(TopLevel::insert_or_error_out): New functions.
	(TopLevel::handle_use_dec): New function.
	(flatten_rebind): Likewise.
	(flatten_list): Likewise.
	(flatten_glob): Likewise.
	(flatten): Likewise.
	(TopLevel::visit): Visit various `use` declaration nodes.
	* resolve/rust-toplevel-name-resolver-2.0.h: Declare functions and
	visitors.
2024-01-30 12:36:46 +01:00
Arthur Cohen
17da301e2a gccrs: early: Move use declaration resolving to TopLevel
gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc
	(Early::visit): Remove visitors.
	* resolve/rust-early-name-resolver-2.0.h: Likewise.
2024-01-30 12:36:46 +01:00
Arthur Cohen
1133c1c671 gccrs: ast: Add NodeId to UseTree base class
gcc/rust/ChangeLog:

	* ast/rust-item.h (class UseTree): Add `node_id` member.
2024-01-30 12:36:46 +01:00
Arthur Cohen
0a5cc7a4cb gccrs: toplevel: Add comment about running the collector twice
gcc/rust/ChangeLog:

	* resolve/rust-toplevel-name-resolver-2.0.cc
	(TopLevel::insert_or_error_out): Add documentation comment.
	(TopLevel::go): Likewise.
2024-01-30 12:36:46 +01:00
Arthur Cohen
53d979f361 gccrs: early: Resolve paths properly
gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc
	(Early::insert_once): New function.
	(Early::visit): Likewise.
	* resolve/rust-early-name-resolver-2.0.h: Likewise.
2024-01-30 12:36:46 +01:00
Arthur Cohen
0f0ec052b4 gccrs: late: Start storing mappings properly in the resolver
gcc/rust/ChangeLog:

	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Store mappings
	after having resolved them.
	* resolve/rust-late-name-resolver-2.0.h: Add `TypePath` visitor.
2024-01-30 12:36:46 +01:00
Arthur Cohen
df6eadda5e gccrs: late: Start setting up builtin types
gcc/rust/ChangeLog:

	* resolve/rust-late-name-resolver-2.0.cc
	(Late::setup_builtin_types): New function.
	(Late::go): Setup builtin types.
	* resolve/rust-late-name-resolver-2.0.h:
	* resolve/rust-name-resolution-context.cc
	(NameResolutionContext::map_usage): New function.
	* resolve/rust-name-resolution-context.h: Likewise.
2024-01-30 12:36:45 +01:00