]> git.ipfire.org Git - thirdparty/gcc.git/log
thirdparty/gcc.git
6 days agogccrs: fix clang formatting
Philip Herron [Thu, 31 Jul 2025 16:26:14 +0000 (17:26 +0100)] 
gccrs: fix clang formatting

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): formatting
* typecheck/rust-tyty-variance-analysis-private.h: likewise
* typecheck/rust-tyty.cc (VariantDef::clone): likewise
(VariantDef::monomorphized_clone): likewise
* typecheck/rust-tyty.h: likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Add initial support for const generics
Philip Herron [Sun, 27 Jul 2025 20:47:54 +0000 (21:47 +0100)] 
gccrs: Add initial support for const generics

In order to support const generics we map the declarations to a ConstType this
means we reuse all our existing type coercion, unification code and generic
substitutions code to support this with minimal impact.

This patch adds support for:

1. Default const generics
2. Infer const generics
3. ADTType suport
4. unconstrained checks
5. ensure types of the const generic and default
6. validation if passing a const argument to a type argument

Lots of test cases now work and new ones added. More work is needed to support this
on functions and method resolution of impls like Foo<1> vs Foo<2> once thats in we
can look to support some of the const generic array impls next.

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc: useful debug
* backend/rust-compile-stmt.cc (CompileStmt::visit): likewise
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): fold the capacity into ConstType
* hir/tree/rust-hir-generic-param.h: make const
* hir/tree/rust-hir-path.h: take into account const arguments now
* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::resolve_literal): needs const
* typecheck/rust-hir-type-check-base.h: add error handling for const supported locations
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): const type the arrays
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit): update
(TypeCheckImplItem::visit): likewise
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): likewise
(TypeCheckItem::resolve_impl_block_substitutions): likewise
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): wrap up const type
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): likewise
(TypeResolveGenericParam::visit): likewise
(TypeResolveGenericParam::apply_trait_bounds): remove HIR::Generic from Param
* typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): cleanup
* typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::SubstitutionParamMapping):
handle const generics
(SubstitutionParamMapping::get_type_representation): likewise
(SubstitutionParamMapping::param_has_default_ty): likewise
(SubstitutionParamMapping::get_default_ty): likewise
(SubstitutionRef::infer_substitions): likewise
* typecheck/rust-tyty-subst.h: likewise
* typecheck/rust-tyty-util.cc (TyVar::get_implicit_const_infer_var): new helper
* typecheck/rust-tyty-util.h (class ConstType): likewise
* typecheck/rust-tyty.cc (BaseType::is_concrete): check for array const concrete
(ArrayType::as_string): update to const
(ArrayType::handle_substitions): likewise
(ParamType::ParamType): likewise
(ParamType::get_generic_param): likewise
(ParamType::clone): likewise
(ConstType::ConstType): likewise
(ConstType::set_value): likewise
(ConstType::clone): likewise
(ConstType::get_generic_param): likewise
(generate_tree_str): new helper to pretty print gimple
(ConstType::get_name): uses the generate_tree_str
(ConstType::handle_substitions): handle const infer's
* typecheck/rust-tyty.h (RUST_TYTY): likewise
* typecheck/rust-unify.cc (UnifyRules::expect_array): likewise
(UnifyRules::expect_const): likewise

gcc/testsuite/ChangeLog:

* rust/compile/const_generics_3.rs: this works now
* rust/compile/const_generics_5.rs: likewise
* rust/compile/const_generics_8.rs: move the failure to another test case
* rust/compile/const_generics_10.rs: New test.
* rust/compile/const_generics_11.rs: New test.
* rust/compile/const_generics_12.rs: New test.
* rust/compile/const_generics_13.rs: New test.
* rust/compile/const_generics_14.rs: New test.
* rust/compile/const_generics_15.rs: New test.
* rust/compile/const_generics_16.rs: New test.
* rust/compile/const_generics_9.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Add ConstType boiler plate to handle const generics
Philip Herron [Sun, 27 Jul 2025 19:19:41 +0000 (20:19 +0100)] 
gccrs: Add ConstType boiler plate to handle const generics

This patch is all about just putting in the boiler plate for the new
BaseGeneric TyTy::ConstType. Nothing is really change but just the
nessecary cogs added to the machine.

gcc/rust/ChangeLog:

* backend/rust-compile-type.cc (TyTyResolveCompile::visit): error_mark_node for const types
* backend/rust-compile-type.h: boilerplate
* checks/errors/borrowck/rust-bir-fact-collector.h: likewise
* checks/errors/borrowck/rust-bir-place.h: likewise
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_base_type_privacy):
likewise
* typecheck/rust-substitution-mapper.cc (SubstMapperInternal::visit): likewise
* typecheck/rust-substitution-mapper.h: likewise
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::assemble_marker_builtins): likewise
* typecheck/rust-tyty-call.h: likewise
* typecheck/rust-tyty-cmp.h (class ConstCmp): likewise
* typecheck/rust-tyty-variance-analysis-private.h: likewise
* typecheck/rust-tyty-visitor.h: likewise
* typecheck/rust-tyty.cc (TypeKindFormat::to_string): likewise
(BaseType::is_unit): likewise
(BaseType::has_substitutions_defined): likewise
(BaseType::needs_generic_substitutions): likewise
(ConstType::ConstType): likewise
(ConstType::accept_vis): likewise
(ConstType::as_string): likewise
(ConstType::can_eq): likewise
(ConstType::clone): likewise
(ConstType::get_symbol): likewise
(ConstType::get_generic_param): likewise
(ConstType::can_resolve): likewise
(ConstType::resolve): likewise
(ConstType::get_name): likewise
(ConstType::is_equal): likewise
(ConstType::handle_substitions): likewise
* typecheck/rust-tyty.h (enum TypeKind): new tyty_kind
(class ConstType): new type
* typecheck/rust-unify.cc (UnifyRules::go): Handle a const type unify
(UnifyRules::expect_inference_variable): likewise
(UnifyRules::expect_adt): likewise
(UnifyRules::expect_str): likewise
(UnifyRules::expect_reference): likewise
(UnifyRules::expect_pointer): likewise
(UnifyRules::expect_param): likewise
(UnifyRules::expect_array): likewise
(UnifyRules::expect_slice): likewise
(UnifyRules::expect_fndef): likewise
(UnifyRules::expect_fnptr): likewise
(UnifyRules::expect_tuple): likewise
(UnifyRules::expect_bool): likewise
(UnifyRules::expect_char): likewise
(UnifyRules::expect_int): likewise
(UnifyRules::expect_uint): likewise
(UnifyRules::expect_float): likewise
(UnifyRules::expect_isize): likewise
(UnifyRules::expect_usize): likewise
(UnifyRules::expect_placeholder): likewise
(UnifyRules::expect_projection): likewise
(UnifyRules::expect_dyn): likewise
(UnifyRules::expect_closure): likewise
(UnifyRules::expect_const): likewise
* typecheck/rust-unify.h: new expect_const_type handler

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Refactor the ParamType to a BaseGeneric base-type
Philip Herron [Fri, 25 Jul 2025 16:49:15 +0000 (17:49 +0100)] 
gccrs: Refactor the ParamType to a BaseGeneric base-type

In order to support const generics we need to abstract away some of the specific
ParamType to a BaseGeneric type so we can easily reuse our existing substitution
bits for const generics which will mean creating a TyTy::ConstType to wrap up
the gcc tree but also represent a const inference and the const decl.

gcc/rust/ChangeLog:

* backend/rust-compile-intrinsic.cc (sizeof_handler): refactor types
(op_with_overflow_inner): likewise
(uninit_handler): likewise
(move_val_init_handler): likewise
* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): likewise
* typecheck/rust-hir-trait-resolve.cc: likewise
* typecheck/rust-hir-type-check-base.cc: likewise
* typecheck/rust-hir-type-check-item.cc: likewise
* typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::is_equal): likewise
* typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::get_param_ty): likewise
(SubstitutionArg::get_param_mapping): likewise
(SubstitutionRef::prepare_higher_ranked_bounds): likewise
(SubstitutionRef::monomorphize): likewise
* typecheck/rust-tyty-subst.h (class BaseGeneric): new generic base
* typecheck/rust-tyty.cc (VariantDef::clone): likewise
(VariantDef::monomorphized_clone): refactor
(ADTType::is_equal): likewise
(FnType::is_equal): likewise
(ParamType::ParamType): likewise
* typecheck/rust-tyty.h (class ParamType): likewise
(class BaseGeneric): new base class impl

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: simplify the is_eq on ADTType
Philip Herron [Fri, 25 Jul 2025 16:19:14 +0000 (17:19 +0100)] 
gccrs: simplify the is_eq on ADTType

We dont need to do this resolve we can just let the ParamType's sort this
out.

gcc/rust/ChangeLog:

* typecheck/rust-tyty.cc (ADTType::is_equal): let param::is_eq do this
(FnType::is_equal): remove whitespace

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Refactor substitution param mapping to be more abstract
Philip Herron [Fri, 25 Jul 2025 15:56:17 +0000 (16:56 +0100)] 
gccrs: Refactor substitution param mapping to be more abstract

This is an initial patch required to refactor our generics code to be
simpler and more abstract so we return the HIR::GenericParam in ParamMappings
instead of assuming its a TypeParam so we can slowly introduce ConstGenericParam
into this same flow.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-base.cc: check for type param
* typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::SubstitutionParamMapping):
return HIR::GenericParam base class
(SubstitutionParamMapping::get_generic_param): likewise
(SubstitutionParamMapping::get_type_representation): new helper
(SubstitutionParamMapping::param_has_default_ty): check for param type
(SubstitutionParamMapping::get_default_ty): likewise
* typecheck/rust-tyty-subst.h: get the locus from the subst HIR::GenericParam now
* typecheck/rust-tyty-variance-analysis.cc (GenericTyPerCrateCtx::debug_print_solutions):
likwise
(GenericTyVisitorCtx::process_type): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix ICE during const eval of const capacity
Philip Herron [Thu, 31 Jul 2025 20:46:42 +0000 (21:46 +0100)] 
gccrs: Fix ICE during const eval of const capacity

We assert that struct expressions during code-gen must be of TyTy::ADTType
but we can simply just check for this and return error_mark_node to make
this safe.

Fixes Rust-GCC#3960

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): check for ADTType instead of assert

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Remove warning for unused `self` parameter
Ryutaro Okada [Thu, 31 Jul 2025 10:57:11 +0000 (03:57 -0700)] 
gccrs: Remove warning for unused `self` parameter

gcc/testsuite/ChangeLog:

* rust/compile/auto_traits2.rs:
emove warning for unused `self` parameter
* rust/compile/derive-debug1.rs:
emove warning for unused `self` parameter
* rust/compile/derive_macro1.rs:
Remove warning for unused `self` parameter
* rust/compile/format_args_basic_expansion.rs:
Remove warning for unused `self` parameter
* rust/compile/format_args_extra_comma.rs:
Remove warning for unused `self` parameter
* rust/compile/issue-2043.rs:
Remove warning for unused `self` parameter
* rust/compile/issue-2166.rs:
Remove warning for unused `self` parameter
* rust/compile/issue-2238.rs:
Remove warning for unused `self` parameter
* rust/compile/issue-2907.rs:
Remove warning for unused `self` parameter
* rust/compile/min_specialization1.rs:
Remove warning for unused `self` parameter
* rust/compile/name_resolution2.rs:
Remove warning for unused `self` parameter
* rust/compile/name_resolution4.rs:
Remove warning for unused `self` parameter
* rust/compile/torture/generics29.rs:
Remove warning for unused `self` parameter
* rust/compile/torture/generics30.rs:
Remove warning for unused `self` parameter
* rust/compile/torture/traits3.rs:
Remove warning for unused `self` parameter
* rust/compile/torture/traits7.rs:
Remove warning for unused `self` parameter
* rust/execute/torture/impl_trait3.rs:
Remove warning for unused `self` parameter
* rust/execute/torture/min_specialization2.rs:
Remove warning for unused `self` parameter
* rust/execute/torture/trait10.rs:
Remove warning for unused `self` parameter
* rust/execute/torture/trait11.rs:
Remove warning for unused `self` parameter
* rust/execute/torture/trait12.rs:
Remove warning for unused `self` parameter
* rust/execute/torture/trait13.rs:
Remove warning for unused `self` parameter
* rust/execute/torture/trait9.rs:
Remove warning for unused `self` parameter

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
6 days agogccrs: Do not warn about unused `self` parameter
Ryutaro Okada [Thu, 31 Jul 2025 10:54:49 +0000 (03:54 -0700)] 
gccrs: Do not warn about unused `self` parameter

gcc/rust/ChangeLog:

* checks/lints/rust-lint-unused-var.cc (check_decl):
Do not warn about unused `self` parameter.

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
6 days agogccrs: desugar: Add base for desugaring while-let loops
Arthur Cohen [Tue, 22 Jul 2025 15:16:02 +0000 (17:16 +0200)] 
gccrs: desugar: Add base for desugaring while-let loops

gcc/rust/ChangeLog:

* Make-lang.in:
* ast/rust-expression-yeast.cc (ExpressionYeast::dispatch_loops): Call DesugarWhileLet.
* ast/rust-desugar-while-let.cc: New file.
* ast/rust-desugar-while-let.h: New file.

gcc/testsuite/ChangeLog:

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

6 days agogccrs: Fix AttrInputMacro operator= overloading.
Pierre-Emmanuel Patry [Thu, 31 Jul 2025 11:19:22 +0000 (13:19 +0200)] 
gccrs: Fix AttrInputMacro operator= overloading.

gcc/rust/ChangeLog:

* ast/rust-ast.cc (AttrInputMacro::operator=): Add return type.
* ast/rust-expr.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: desugar: Cleanup for-loop desugar implementation.
Arthur Cohen [Tue, 22 Jul 2025 15:16:29 +0000 (17:16 +0200)] 
gccrs: desugar: Cleanup for-loop desugar implementation.

gcc/rust/ChangeLog:

* ast/rust-desugar-for-loops.cc: Remove functions implemented in AST::Builder.
* ast/rust-desugar-for-loops.h: Likewise.

6 days agogccrs: lower: Add assertions for desugared nodes
Arthur Cohen [Tue, 22 Jul 2025 14:47:55 +0000 (16:47 +0200)] 
gccrs: lower: Add assertions for desugared nodes

gcc/rust/ChangeLog:

* hir/rust-ast-lower-base.cc: Add rust_unreachable() when lowering desugared exprs.
* hir/rust-ast-lower-base.h: Mention this.
* hir/rust-ast-lower-block.h: Remove existing definitions.
* hir/rust-ast-lower-expr.cc: Likewise.
* hir/rust-ast-lower-expr.h: Likewise.
* hir/rust-ast-lower.cc: Likewise.

6 days agogccrs: desugar: Add assertions for try-blocks and question-mark
Arthur Cohen [Tue, 22 Jul 2025 14:29:09 +0000 (16:29 +0200)] 
gccrs: desugar: Add assertions for try-blocks and question-mark

gcc/rust/ChangeLog:

* ast/rust-desugar-question-mark.cc (DesugarQuestionMark::go): Add assertion for the
expr's type.
* ast/rust-desugar-try-block.cc (DesugarTryBlock::go): Likewise.

6 days agogccrs: desugar: Add for-loop desugar to ExpressionYeast
Arthur Cohen [Tue, 22 Jul 2025 13:00:32 +0000 (15:00 +0200)] 
gccrs: desugar: Add for-loop desugar to ExpressionYeast

gcc/rust/ChangeLog:

* ast/rust-desugar-for-loops.h: Adapt API and remove visitor.
* ast/rust-desugar-for-loops.cc: Likewise.
* ast/rust-expression-yeast.cc: Call DesugarForLoop.
* ast/rust-expression-yeast.h: Declare dispatch_loops function.
* rust-session-manager.cc (Session::expansion): Do not call for-loop desugar.

6 days agogccrs: hir: Add OffsetOf node
Arthur Cohen [Mon, 28 Jul 2025 08:05:04 +0000 (10:05 +0200)] 
gccrs: hir: Add OffsetOf node

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.h (class OffsetOf): New.
* hir/tree/rust-hir-expr.cc: Define its methods.
* hir/tree/rust-hir-expr-abstract.h: Add ExprType::OffsetOf.
* hir/tree/rust-hir-full-decls.h (class OffsetOf): Declare it.
* backend/rust-compile-block.h: Add handling for OffsetOf.
* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
* backend/rust-compile-expr.h: Likewise.
* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit): Likewise.
* checks/errors/borrowck/rust-bir-builder-expr-stmt.h (RUST_BIR_BUILDER_EXPR_H): Likewise.
* checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h: Likewise.
* checks/errors/borrowck/rust-bir-builder-struct.h: Likewise.
* checks/errors/borrowck/rust-function-collector.h: Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h (RUST_PRIVACY_REPORTER_H): Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-const-checker.h: Likewise.
* checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): Likewise.
* checks/errors/rust-hir-pattern-analysis.h: Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.h: Likewise.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise.
* hir/rust-hir-dump.cc (Dump::visit): Likewise.
* hir/rust-hir-dump.h: Likewise.
* hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk): Likewise.
* hir/tree/rust-hir-visitor.h: Likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.
* typecheck/rust-hir-type-check-expr.h (RUST_HIR_TYPE_CHECK_EXPR): Likewise.

gcc/testsuite/ChangeLog:

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

6 days agogccrs: expand: Add parser for offset_of!() and builtin resolution.
Arthur Cohen [Thu, 24 Jul 2025 09:32:43 +0000 (11:32 +0200)] 
gccrs: expand: Add parser for offset_of!() and builtin resolution.

gcc/rust/ChangeLog:

* Make-lang.in: Compile the offset_of handler.
* lang.opt: Add -frust-assume-builtin-offset-of option.
* ast/rust-ast.h: Add has_str() for const_TokenPtr.
* expand/rust-macro-builtins.cc: Map offset_of as builtin.
* expand/rust-macro-builtins.h: Declare it.
* expand/rust-macro-expand.cc (MacroExpander::expand_invoc): Add hack for calling builtin
offset_of!().
* resolve/rust-early-name-resolver-2.0.cc (Early::visit):  Likewise.
* expand/rust-macro-builtins-offset-of.cc: New file.

gcc/testsuite/ChangeLog:

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

6 days agogccrs: ast: Add OffsetOf node
Arthur Cohen [Thu, 24 Jul 2025 09:03:35 +0000 (11:03 +0200)] 
gccrs: ast: Add OffsetOf node

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add OffsetOf expression kind.
* ast/rust-builtin-ast-nodes.h (class OffsetOf): Add node.
* ast/rust-ast.cc: Define it.
* ast/rust-ast-collector.cc: Add visitor for OffsetOf.
* ast/rust-ast-collector.h: Likewise.
* ast/rust-ast-visitor.cc: Likewise.
* ast/rust-ast-visitor.h: Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h: Likewise.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise.
* hir/rust-ast-lower-expr.h: Likewise.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise.
* resolve/rust-ast-resolve-base.h: Likewise.
* resolve/rust-early-name-resolver-2.0.cc: Likewise.
* expand/rust-derive.h:

6 days agogccrs: Error message field member was not properly updated
Pierre-Emmanuel Patry [Wed, 30 Jul 2025 17:37:45 +0000 (19:37 +0200)] 
gccrs: Error message field member was not properly updated

gcc/rust/ChangeLog:

* rust-diagnostics.h (struct Error): Add disambiguation.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Fix infinite loop with missing comma
Pierre-Emmanuel Patry [Wed, 30 Jul 2025 11:11:52 +0000 (13:11 +0200)] 
gccrs: Fix infinite loop with missing comma

A missing comma between inline assembly templates did not throw an error
and looped indefinitely.

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_format_strings): Emit an
error when expecting a comma.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Fix semicolon after some namespace
Pierre-Emmanuel Patry [Tue, 29 Jul 2025 15:33:18 +0000 (17:33 +0200)] 
gccrs: Fix semicolon after some namespace

Remove namespace comment after classes and structs.

gcc/rust/ChangeLog:

* checks/errors/borrowck/rust-bir-fact-collector.h: Remove spurious
comment.
* checks/errors/rust-feature.cc: Likewise.
* util/optional.h: Likewise.
* expand/rust-token-tree-desugar.cc (TokenTreeDesugar::visit): Remove
semicolons on namespace.
* expand/rust-token-tree-desugar.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: nr1.0: Remove rust/typecheck support
Owen Avery [Thu, 3 Jul 2025 21:48:22 +0000 (17:48 -0400)] 
gccrs: nr1.0: Remove rust/typecheck support

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-base.cc
(TypeCheckBase::TypeCheckBase): Remove initialization of
resolver field.
* typecheck/rust-hir-type-check-base.h
(TypeCheckBase::resolver): Remove field.
* typecheck/rust-hir-trait-resolve.cc: Remove "options.h"
include.
(TraitResolver::resolve_path_to_trait): Assume name resolution
2.0 is always enabled.
* typecheck/rust-hir-type-check-enumitem.cc: Remove "options.h"
include.
(TypeCheckEnumItem::visit): Assume name resolution 2.0 is always
enabled.
* typecheck/rust-hir-type-check-expr.cc: Remove "options.h"
include.
(TypeCheckExpr::visit): Assume name resolution 2.0 is always
enabled.
(TypeCheckExpr::resolve_operator_overload): Likewise.
(TypeCheckExpr::resolve_fn_trait_call): Likewise.
* typecheck/rust-hir-type-check-implitem.cc: Remove "options.h"
include.
(TypeCheckImplItem::visit): Assume name resolution 2.0 is always
enabled.
* typecheck/rust-hir-type-check-item.cc: Remove "options.h"
include.
(TypeCheckItem::visit): Assume name resolution 2.0 is always
enabled.
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit):
Likewise.
(TypeCheckExpr::resolve_root_path): Likewise.
(TypeCheckExpr::resolve_segments): Likewise.
* typecheck/rust-hir-type-check-pattern.cc: Remove "options.h"
include.
(TypeCheckPattern::visit): Assume name resolution 2.0 is always
enabled.
* typecheck/rust-hir-type-check-type.cc
(TypeCheckType::resolve_root_path): Likewise.
(ResolveWhereClauseItem::visit): Likewise.
* typecheck/rust-hir-type-check.cc: Remove "options.h" include.
(TraitItemReference::get_type_from_fn): Assume name resolution
2.0 is always enabled.
* typecheck/rust-type-util.cc (query_type): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Add execute test for variable and identifiers
Pierre-Emmanuel Patry [Tue, 29 Jul 2025 13:15:31 +0000 (15:15 +0200)] 
gccrs: Add execute test for variable and identifiers

gcc/testsuite/ChangeLog:

* rust/execute/inline_asm_inout_ident.rs: New test.
* rust/execute/inline_asm_inout_var.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Add input/output from inout and split in out
Pierre-Emmanuel Patry [Tue, 29 Jul 2025 11:48:17 +0000 (13:48 +0200)] 
gccrs: Add input/output from inout and split in out

Inline assembly was incomplete and input/output from inout or split in
out were not handled.

gcc/rust/ChangeLog:

* backend/rust-compile-asm.cc (get_out_expr): Return valid output from
an operand.
(CompileAsm::asm_construct_outputs): Handle every output
(get_in_expr): Return valid input from an operand.
(CompileAsm::asm_construct_inputs): Handle every input

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Parse input and output expression
Pierre-Emmanuel Patry [Tue, 29 Jul 2025 11:53:58 +0000 (13:53 +0200)] 
gccrs: Parse input and output expression

Previously inline assembly expected identifiers instead of expression.

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (parse_reg_operand_inout): Parse
expressions and build split in out.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Add pretty hir dump for inline assembly
Pierre-Emmanuel Patry [Tue, 29 Jul 2025 11:42:14 +0000 (13:42 +0200)] 
gccrs: Add pretty hir dump for inline assembly

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::visit): Dump inline assembly fields
* hir/tree/rust-hir-expr.h: Add non const getter and avoid operand copy
from getters.
* hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk): Use non const
reference.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Add indexed positional argument support in asm
Pierre-Emmanuel Patry [Wed, 23 Jul 2025 12:40:46 +0000 (14:40 +0200)] 
gccrs: Add indexed positional argument support in asm

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-asm.cc (expand_inline_asm_strings): Handle
transformation for indexed positional arguments.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Implement compilation for SlicePattern against SliceType scrutinee
Yap Zhi Heng [Wed, 23 Jul 2025 00:24:18 +0000 (08:24 +0800)] 
gccrs: Implement compilation for SlicePattern against SliceType scrutinee

006t.original output from compiling testsuite/rust/compile/match-slicepattern-slice.rs:

...
  RUSTTMP.3 = slice;
  if (RUSTTMP.3.len == 1 && *(RUSTTMP.3.data + 0 * 4) == 1)
    {
      {
                struct () RUSTTMP.4;
        {

        }
        goto <D.129>;
      }
    }
  if (RUSTTMP.3.len == 2 && *(RUSTTMP.3.data + 1 * 4) == 2)
    {
      {
                struct () RUSTTMP.5;
        {

        }
        goto <D.129>;
      }
    }
  if (1)
    {
      {
                struct () RUSTTMP.6;
        {

        }
        goto <D.129>;
      }
    }
  <D.129>:;
...

gcc/rust/ChangeLog:

* rust-backend.h: New slice_index_expression function.
* rust-gcc.cc: Implementation of slice_index_expression to generate tree node for
accessing slice elements.
* backend/rust-compile-pattern.cc: Implement SlicePattern check expression & binding
compilation against SliceType scrutinee.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: Update SlicePattern typechecking against slice reference parents
Yap Zhi Heng [Sun, 20 Jul 2025 07:55:51 +0000 (15:55 +0800)] 
gccrs: Update SlicePattern typechecking against slice reference parents

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit(SlicePattern)):
Add new type check case for SliceType wrapped in ReferenceType.
* backend/rust-compile-pattern.cc: Adjusted the asserts accordingly for
CompilePatternCheckExpr(SlicePattern) & CompilePatternBindings(SlicePattern).

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: desugar: Handle try-blocks
Arthur Cohen [Tue, 22 Jul 2025 11:30:11 +0000 (13:30 +0200)] 
gccrs: desugar: Handle try-blocks

gcc/rust/ChangeLog:

* Make-lang.in: Compile it.
* ast/rust-expression-yeast.cc (ExpressionYeast::dispatch): Dispatch to try-block
desugar.
* ast/rust-desugar-try-block.cc: New file.
* ast/rust-desugar-try-block.h: New file.

gcc/testsuite/ChangeLog:

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

6 days agogccrs: ast: Add Expr::Kind::Try
Arthur Cohen [Tue, 22 Jul 2025 11:38:26 +0000 (13:38 +0200)] 
gccrs: ast: Add Expr::Kind::Try

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add the new variant.
* ast/rust-expr.h: Use it for TryExpr class.

6 days agogccrs: Handle IfLetExprConseqElse in DefaultResolver
Owen Avery [Tue, 22 Jul 2025 02:32:29 +0000 (22:32 -0400)] 
gccrs: Handle IfLetExprConseqElse in DefaultResolver

This relies on the DefaultASTVisitor visitor for IfLetExprConseqElse
performing a virtual call of the visitor for IfLetExpr, which doesn't
hold when DefaultASTVisitor is generated by the X-macro-DSL-system I
have in another patch.

gcc/rust/ChangeLog:

* resolve/rust-default-resolver.cc (DefaultResolver::visit): Add
visitor for IfLetExprConseqElse.
* resolve/rust-default-resolver.h (DefaultResolver::visit):
Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: ast: Visit block labels if they are present
Arthur Cohen [Mon, 21 Jul 2025 08:12:13 +0000 (10:12 +0200)] 
gccrs: ast: Visit block labels if they are present

gcc/rust/ChangeLog:

* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Visit a block's loop label if it
exists.

6 days agogccrs: desugar: Add desugar dispatch for all desugars
Arthur Cohen [Mon, 21 Jul 2025 09:27:01 +0000 (11:27 +0200)] 
gccrs: desugar: Add desugar dispatch for all desugars

Since we are doing more and more "external" desugars, as in desugars
that take a pointer and replace it with another one, rather than
modifying it from within, having an external visitor dispatch to the
proper desugar helps with code clarity.

gcc/rust/ChangeLog:

* Make-lang.in: Compile it.
* rust-session-manager.cc: Call the expression desugar dispatcher.
* ast/rust-desugar-question-mark.cc: Rework class API.
* ast/rust-desugar-question-mark.h: Likewise.
* ast/rust-expression-yeast.cc: New file.
* ast/rust-expression-yeast.h: New file.

6 days agogccrs: chore: ast: Fix formatting in rust-expr.h
Arthur Cohen [Mon, 21 Jul 2025 07:38:44 +0000 (09:38 +0200)] 
gccrs: chore: ast: Fix formatting in rust-expr.h

gcc/rust/ChangeLog:

* ast/rust-expr.h: Fix formatting.

6 days agogccrs: hir: Handle deferred const inference variables
Arthur Cohen [Tue, 8 Jul 2025 12:34:04 +0000 (14:34 +0200)] 
gccrs: hir: Handle deferred const inference variables

gcc/rust/ChangeLog:

* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Handle defered consts.
* hir/tree/rust-hir-expr.cc (AnonConst::AnonConst): Likewise.
(AnonConst::operator=): Likewise.
* hir/tree/rust-hir-expr.h: Likewise.
* hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk): Likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.

gcc/testsuite/ChangeLog:

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

6 days agogccrs: ast: Use AnonConst for array type sizes
Arthur Cohen [Mon, 21 Jul 2025 07:36:16 +0000 (09:36 +0200)] 
gccrs: ast: Use AnonConst for array type sizes

gcc/rust/ChangeLog:

* ast/rust-expr.h: Add handling for deferred consts.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
* ast/rust-ast.cc (AnonConst::as_string): Likewise.
(ArrayType::as_string): Likewise.
* ast/rust-type.h (class ArrayType): Use AnonConst for sizes.
* parse/rust-parse-impl.h (Parser::parse_anon_const): New function.
(Parser::parse_slice_or_array_type): Call it.
* parse/rust-parse.h: Declare it.

6 days agogccrs: nr2.0: Handle glob imports of enum variants.
Arthur Cohen [Wed, 9 Jul 2025 13:52:04 +0000 (15:52 +0200)] 
gccrs: nr2.0: Handle glob imports of enum variants.

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc (Early::resolve_glob_import): Adapt for enums.
(Early::finalize_glob_import): Likewise.
* resolve/rust-early-name-resolver-2.0.h: Likewise.
* resolve/rust-finalize-imports-2.0.cc (GlobbingVisitor::go): Likewise.
(GlobbingVisitor::visit_module_container): New function.
(GlobbingVisitor::visit_enum_container): New function.
* resolve/rust-finalize-imports-2.0.h: Declare them.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Insert enums as potential
containers.

gcc/testsuite/ChangeLog:

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

6 days agogccrs: mappings: Change mappings to allow other items as item containers.
Arthur Cohen [Wed, 9 Jul 2025 13:50:01 +0000 (15:50 +0200)] 
gccrs: mappings: Change mappings to allow other items as item containers.

This is important for importing enum variants as items.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_ast_module): Rename to...
(Mappings::insert_glob_container): ...this.
(Mappings::lookup_ast_module): Rename to...
(Mappings::lookup_glob_container): ...this.
* util/rust-hir-map.h: Change declarations.

6 days agogccrs: Load modules during CfgStrip phase
Owen Avery [Thu, 3 Jul 2025 01:38:56 +0000 (21:38 -0400)] 
gccrs: Load modules during CfgStrip phase

TopLevel would ignore just-loaded modules but Early and ExpandVisitor
wouldn't. The latter would produce errors when it hit attributes which
should have been indirectly CfgStrip'd away.

gcc/rust/ChangeLog:

* expand/rust-cfg-strip.cc (CfgStrip::visit): Load unloaded
modules.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
Assume modules have been loaded by CfgStrip.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Specialize ExpandVisitor::expand_macro_children
Owen Avery [Sat, 19 Jul 2025 01:44:09 +0000 (21:44 -0400)] 
gccrs: Specialize ExpandVisitor::expand_macro_children

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc
(ExpandVisitor::expand_inner_items): Adjust call to
expand_macro_children.
(ExpandVisitor::expand_inner_stmts): Likewise.
(ExpandVisitor::visit): Likewise.
* expand/rust-expand-visitor.h
(ExpandVisitor::expand_macro_children): Take a pointer to member
function instead of a std::function.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: fix bad monomophization of generic paths
Philip Herron [Sun, 20 Jul 2025 20:48:18 +0000 (21:48 +0100)] 
gccrs: fix bad monomophization of generic paths

When we have generic paths like T::foobar during codegen sometimes we need
to enforce an extra lookup for this generic parameter type to the mono
morphized underlying type.

Fixes Rust-GCC#3915
Fixes Rust-GCC#1247

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): do another lookup

gcc/testsuite/ChangeLog:

* rust/compile/issue-3915.rs: New test.
* rust/execute/torture/sip-hasher.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Use MacroInvocLexer in AttributeParser
Owen Avery [Tue, 8 Jul 2025 21:03:49 +0000 (17:03 -0400)] 
gccrs: Use MacroInvocLexer in AttributeParser

This should make it easier for us to handle attribute meta items of the
form <SimplePath> '=' <Expression> where the expression isn't a literal.
Some low hanging fruit remains here, but I think I should keep this
patch small as I had some trouble debugging it as-is (see:
Rust::Token::as_string vs Rust::Token::get_str vs
Rust::AST::Token::as_string).

gcc/rust/ChangeLog:

* ast/rust-ast.cc: Include "rust-macro-invoc-lexer.h".
(AttributeParser::~AttributeParser): Move function definition
here.
(AttributeParser::AttributeParser): Likewise and adjust member
initialization.
(AttributeParser::parse_meta_item_inner): Handle changes to
peek_token.
(AttributeParser::parse_literal): Likewise.
(AttributeParser::parse_simple_path_segment): Likewise.
(AttributeParser::parse_meta_item_seq): Handle changes to
AttributeParser fields.
(AttributeParser::peek_token): Move function definition here and
wrap MacroInvocLexer.
(AttributeParser::skip_token): Likewise.
* ast/rust-macro.h (class MacroInvocLexer): Forward declare.
(class Parser): Likewise.
(AttributeParser::token_stream): Remove field.
(AttributeParser::stream_pos): Likewise.
(AttributeParser::lexer): New field.
(AttributeParser::parser): Likewise.
(AttributeParser::AttributeParser): Move definition to
"rust-ast.cc".
(AttributeParser::~AttributeParser): Likewise.
(AttributeParser::peek_token): Likewise.
(AttributeParser::skip_token): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: return error node when this fails during constexpr case
Philip Herron [Fri, 18 Jul 2025 15:44:36 +0000 (16:44 +0100)] 
gccrs: return error node when this fails during constexpr case

Not adding the test case here we emit more errors than rustc for the error
type node so its just noisy and dejagnu is being a pain.

Fixes Rust-GCC#3933

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): return error_mark_node

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Add initial support for deffered operator overload resolution
Philip Herron [Fri, 18 Jul 2025 14:46:59 +0000 (15:46 +0100)] 
gccrs: Add initial support for deffered operator overload resolution

In the test case:

  fn test (len: usize) -> u64 {
     let mut i = 0;
     let mut out = 0;
     if i + 3 < len {
        out = 123;
     }
     out
  }

The issue is to determine the correct type of 'i', out is simple because it hits a
coercion site in the resturn position for u64. But 'i + 3', 'i' is an integer infer
variable and the same for the literal '3'. So when it comes to resolving the type for
the Add expression we hit the resolve the operator overload code and because of this:

  macro_rules! add_impl {
      ($($t:ty)*) => ($(
          impl Add for $t {
              type Output = $t;

              #[inline]
              #[rustc_inherit_overflow_checks]
              fn add(self, other: $t) -> $t { self + other }
          }
      )*)
  }

  add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }

This means the resolution for 'i + 3' is ambigious because it could be any of these Add
implementations. But because we unify against the '< len' where len is defined as usize
later in the resolution we determine 'i' is actually a usize. Which means if we defer the
resolution of this operator overload in the ambigious case we can simply resolve it at the
end.

Fixes Rust-GCC#3916

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.cc (OperatorExprMeta::OperatorExprMeta): track the rhs
* hir/tree/rust-hir-expr.h: likewise
* hir/tree/rust-hir-path.h: get rid of old comments
* typecheck/rust-hir-trait-reference.cc (TraitReference::get_trait_substs): return
references instead of copy
* typecheck/rust-hir-trait-reference.h: update header
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::ResolveOpOverload): write ambigious
operator overloads to a table and try to resolve it at the end
* typecheck/rust-hir-type-check-expr.h: new static helper
* typecheck/rust-hir-type-check.h (struct DeferredOpOverload): new model to defer resolution
* typecheck/rust-typecheck-context.cc (TypeCheckContext::lookup_operator_overload): new
(TypeCheckContext::compute_ambigious_op_overload): likewise
(TypeCheckContext::compute_inference_variables): likewise

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix ICE with duplicate root item main function
Philip Herron [Fri, 18 Jul 2025 15:22:44 +0000 (16:22 +0100)] 
gccrs: Fix ICE with duplicate root item main function

Rust seems to allow duplicate HIR::Item 'main' functions but it needs
to be a root item to be the true main entry point. This means we can
use the canonical path to determine if this is a root one where
its CrateName::main or CrateName::Module::main.

Fixes Rust-GCC#3978

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc: check the canonical path

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Improve parsing of simple paths
Owen Avery [Tue, 15 Jul 2025 02:47:07 +0000 (22:47 -0400)] 
gccrs: Improve parsing of simple paths

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h (Parser::parse_simple_path): Be more
careful about skipping SCOPE_RESOLUTION tokens.
(Parser::parse_simple_path_segment): Allow parsing from a
starting offset.
(Parser::parse_use_tree): Handle a non-skipped SCOPE_RESOLUTION
token.
* parse/rust-parse.h (Parser::parse_simple_path_segment): Add
parameter for parsing from a starting offset.

gcc/testsuite/ChangeLog:

* rust/compile/parse_simple_path_fail_1.rs: New test.
* rust/compile/parse_simple_path_fail_2.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Add test case to show issue is fixed
Philip Herron [Fri, 18 Jul 2025 15:11:59 +0000 (16:11 +0100)] 
gccrs: Add test case to show issue is fixed

Fixes Rust-GCC#3524

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Reject loop in const/static context
lishin [Wed, 16 Jul 2025 00:39:48 +0000 (01:39 +0100)] 
gccrs: Reject loop in const/static context

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): Add a catch for const/static.

gcc/testsuite/ChangeLog:

* rust/compile/loop_constant_context.rs: New test.
* rust/compile/issue-3618.rs:

Signed-off-by: lishin <lishin1008@gmail.com>
6 days agogccrs: Implement compilation for SlicePattern matching against ArrayType scrutinee
Yap Zhi Heng [Thu, 17 Jul 2025 14:13:32 +0000 (22:13 +0800)] 
gccrs: Implement compilation for SlicePattern matching against ArrayType scrutinee

Example GIMPLE output from compiling testsuite/rust/compile/match-pattern-array.rs:

  ...
  a[0] = 0;
  a[1] = 1;
  RUSTTMP.3 = a;
  _1 = RUSTTMP.3[0];
  _2 = _1 == 0;
  _3 = RUSTTMP.3[1];
  _4 = _3 == 1;
  _5 = _2 & _4;
  if (_5 != 0) goto <D.122>; else goto <D.123>;
  <D.122>:
  {
    {

}
}
  goto <D.117>;
  }
  <D.123>:
...

gcc/rust/ChangeLog:

* rust-backend.h: New size_constant_expression function.
* rust-gcc.cc: Implementation of size_constant_expression function to generate tree node
for array access.
* backend/rust-compile-pattern.h: Remove empty visits for SlicePattern.
* backend/rust-compile-pattern.cc: Implement SlicePattern check expression & binding
compilation against ArrayType scrutinee.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: Add size checking to SlicePattern
Yap Zhi Heng [Fri, 11 Jul 2025 14:29:31 +0000 (22:29 +0800)] 
gccrs: Add size checking to SlicePattern

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc(TypeCheckPattern::visit(SlicePattern)):
Implement size checking for SlicePattern when type checking against array parent

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: Add test case showing all derives working on enum
Philip Herron [Fri, 11 Jul 2025 12:51:10 +0000 (13:51 +0100)] 
gccrs: Add test case showing all derives working on enum

We have more complex test cases already but this will close out this issue.

Fixes Rust-GCC#2005

gcc/testsuite/ChangeLog:

* rust/execute/torture/issue-2005.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: attributes: Add #[test] and #[simd_test]
Arthur Cohen [Fri, 4 Jul 2025 14:19:44 +0000 (16:19 +0200)] 
gccrs: attributes: Add #[test] and #[simd_test]

gcc/rust/ChangeLog:

* util/rust-attribute-values.h: Add declarations for them.
* util/rust-attributes.cc: Add definitions.

6 days agogccrs: Add test case to show issue is fixed
Philip Herron [Fri, 11 Jul 2025 11:20:04 +0000 (12:20 +0100)] 
gccrs: Add test case to show issue is fixed

Fixes Rust-GCC#1048

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Add test case to show we emit better errors now
Philip Herron [Fri, 11 Jul 2025 09:35:38 +0000 (10:35 +0100)] 
gccrs: Add test case to show we emit better errors now

Fixes Rust-GCC#3144

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: add test case to show issue is fixed
Philip Herron [Fri, 11 Jul 2025 09:16:36 +0000 (10:16 +0100)] 
gccrs: add test case to show issue is fixed

Fixes Rust-GCC#3599

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix ICE when handling bad constructor
Philip Herron [Thu, 10 Jul 2025 21:58:21 +0000 (22:58 +0100)] 
gccrs: Fix ICE when handling bad constructor

We just had a typo returning ok true when it should have been false.

Fixes Rust-GCC#3876

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::visit): fix typo

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix cast rules logic to try simple casts then fall back to coercions
Philip Herron [Thu, 10 Jul 2025 18:24:37 +0000 (19:24 +0100)] 
gccrs: Fix cast rules logic to try simple casts then fall back to coercions

This case:

    let i = 1;
    let j = i as i64;

'i' is meant to default to i32 but the inference was making both of these
i64 because the code was prefering coercion logic which can end up with a
default unify which causes the ?integer to unify with i64 making them both
i64.

But all we need to do is allow the simple cast rules to run first then
fallback to coercions but special consideration has to be made to ensure
that if there are dyn objects needed then this needs a unsize coercion, but
also we need to ensure the underlying types are a valid simple cast too
otherwise these also need to fallback to the coercion code.

Fixes Rust-GCC#2680

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::resolve): optional emit_error flag
(TypeCastRules::check): try the simple cast rules then fallback to coercions
(TypeCastRules::check_ptr_ptr_cast): ensure the underlying's
(TypeCastRules::emit_cast_error): make this a static helper
* typecheck/rust-casts.h: new emit_error prototype

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: nr2.0: Check before visiting a for-loop's label
Arthur Cohen [Wed, 9 Jul 2025 09:46:10 +0000 (11:46 +0200)] 
gccrs: nr2.0: Check before visiting a for-loop's label

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Check for a label
before visiting it.

6 days agogccrs: Fix bad bounds checking for PartialOrd
Philip Herron [Fri, 27 Jun 2025 16:13:29 +0000 (17:13 +0100)] 
gccrs: Fix bad bounds checking for PartialOrd

This was a nasty issue to debug, the issue was very eager type bounds
checking. So for example:

  pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs>

The super trait of PartialEq<Rhs> is a generic substitution and we reuse
our bounds code here for normal generic bounds and generics an invalid
bounds check was occuring when PartialEq<Rhs> was getting substituted becase
this is a trait doing proper bounds checking is not valid here because this
is telling us about the bounds in this case.

Fixes Rust-GCC#3836

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): track is super trait
* typecheck/rust-hir-type-bounds.h: refactor bounds scan
* typecheck/rust-hir-type-check-base.h: track from super trait
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): likewise
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::is_bound_satisfied_for_type): refactor
(TypeBoundsProbe::scan): likewise
(TypeBoundPredicate::apply_generic_arguments): likewise
* typecheck/rust-tyty-subst.cc: optional bounds checking on parm subst
* typecheck/rust-tyty-subst.h: likewise
* typecheck/rust-tyty.h: likewise

gcc/testsuite/ChangeLog:

* rust/compile/derive_partial_ord1.rs: this is now fully supported
* rust/execute/torture/basic_partial_ord1.rs: add missing i32 impl
* rust/execute/torture/basic_partial_ord2.rs: likewise
* rust/compile/issue-3836.rs: New test.
* rust/execute/torture/issue-3836.rs: New test.
* rust/execute/torture/partial-ord-6.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix narrowing of Loan (size_t) into LoanId (uint32)
Marc Poulhiès [Thu, 3 Jul 2025 21:29:48 +0000 (23:29 +0200)] 
gccrs: Fix narrowing of Loan (size_t) into LoanId (uint32)

Fix narrowing:

  -../../gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc:145:46:
  warning: narrowing conversion of â€˜loan’ from â€˜Rust::Polonius::Loan’ {aka
  â€˜long unsigned int’} to â€˜uint32_t’ {aka â€˜unsigned int’} [-Wnarrowing]

gcc/rust/ChangeLog:
* checks/errors/borrowck/rust-bir-place.h (LoanId::value): Make
it size_t to match Loan's base type.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
6 days agogccrs: Improve LiteralPattern type checking
Yap Zhi Heng [Tue, 8 Jul 2025 14:15:09 +0000 (22:15 +0800)] 
gccrs: Improve LiteralPattern type checking

This change is made to ensure that LiteralPatterns in SlicePattern are type-checked
against the scrutinee array/slice's element type properly.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc(TypeCheckPattern::visit(LiteralPattern)):
Check LiteralPattern's type against its parent.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: Add type checking for SlicePattern
Yap Zhi Heng [Tue, 8 Jul 2025 14:10:46 +0000 (22:10 +0800)] 
gccrs: Add type checking for SlicePattern

This commit implements basic type checking support for SlicePattern, based on rustc's
check_pat_slice function.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit(SlicePattern)):
Implement initial type checking for SlicePattern.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: Fix ice with invalid borrow expression
Philip Herron [Tue, 8 Jul 2025 21:33:02 +0000 (22:33 +0100)] 
gccrs: Fix ice with invalid borrow expression

This is an invalid test case but we just need a guard for the missing
borrow expression.

Fixes Rust-GCC#3874

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): check for missing borrow
* ast/rust-expr.h: add helper

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Do proper const folding during typechecking for array capacities
Philip Herron [Tue, 8 Jul 2025 20:13:48 +0000 (21:13 +0100)] 
gccrs: Do proper const folding during typechecking for array capacities

This patch adds proper folding to the const expression for array capacity we
already have the const folding mechanics and the query system needed to handle cases
where the capacity is a function call in a const context. This leverages and pulls the
gcc tree capacity into the TyTy::ArrayType so it can be used for more typechecking and
eventually doing more const generics work.

Addresses Rust-GCC#3885
Fixes Rust-GCC#3882

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::query_compile_const_expr): new wrapper
* backend/rust-compile-base.h: add prototype
* backend/rust-compile-context.cc (Context::get): singleton helper
* backend/rust-compile-context.h: likewise
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): handle infer's that can default
* rust-session-manager.cc (Session::compile_crate): create the gcc context earlier for tychk
* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::resolve_literal): const fold it
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): likewise
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): likewise
* typecheck/rust-tyty.cc (BaseType::monomorphized_clone): fix constructor call
(ArrayType::as_string): print capacity
(ArrayType::clone): fix constructor call
* typecheck/rust-tyty.h: track capacity
* typecheck/rust-unify.cc (UnifyRules::expect_array): check the capacities

gcc/testsuite/ChangeLog:

* rust/compile/all-cast.rs: shows array capacity now
* rust/compile/arrays2.rs: likewise
* rust/compile/const3.rs: fix error message
* rust/compile/const_generics_3.rs: disable until typecheck we get proper errors now!
* rust/compile/usize1.rs: proper capacity error message

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: nr2.0: Add proper handling for WhileLet loops.
Arthur Cohen [Fri, 4 Jul 2025 13:40:02 +0000 (15:40 +0200)] 
gccrs: nr2.0: Add proper handling for WhileLet loops.

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): New visitor.
* resolve/rust-late-name-resolver-2.0.h: Declare it.
* resolve/rust-name-resolution-context.h (enum class): New binding context.

6 days agogccrs: ast: Check before visiting a while-let's label
Arthur Cohen [Mon, 19 May 2025 10:27:17 +0000 (12:27 +0200)] 
gccrs: ast: Check before visiting a while-let's label

gcc/rust/ChangeLog:

* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Check that the WhileLet has a label
before visiting it.

gcc/testsuite/ChangeLog:

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

6 days agogccrs: Parse try expressions
Owen Avery [Wed, 2 Jul 2025 22:48:07 +0000 (18:48 -0400)] 
gccrs: Parse try expressions

This doesn't do anything beyond creating TryExpr and parsing them, so
try expressions shouldn't be able to make it past AST lowering yet.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Add visitor
for TryExpr.
* ast/rust-ast-collector.h (TokenCollector::visit): Likewise.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
* ast/rust-ast-visitor.h (ASTVisitor::visit): Likewise.
(DefaultASTVisitor::visit): Likewise.
* expand/rust-derive.h (DeriveVisitor::visit): Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h (ASTLoweringBase::visit): Likewise.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit):
Likewise.
* resolve/rust-ast-resolve-base.h (ResolverBase::visit):
Likewise.
* ast/rust-ast-full-decls.h (class TryExpr): New forward class
declaration.
* ast/rust-ast.cc (TryExpr::as_string): New function.
(TryExpr::accept_vis): Likewise.
* ast/rust-expr.h (class TryExpr): New class.
* parse/rust-parse.h (Parser::parse_try_expr): New function.
* parse/rust-parse-impl.h (Parser::parse_try_expr): Likewise.
(Parser::null_denotation_not_path): Use parse_try_expr to parse
try expressions.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Allow format_args to accept a raw string literal
Owen Avery [Tue, 1 Jul 2025 17:47:59 +0000 (13:47 -0400)] 
gccrs: Allow format_args to accept a raw string literal

gcc/rust/ChangeLog:

* expand/rust-macro-builtins-format-args.cc
(format_args_parse_arguments): Accept a RAW_STRING_LITERAL token
as the first argument.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Change enum to enum class
Pierre-Emmanuel Patry [Mon, 7 Jul 2025 11:03:10 +0000 (13:03 +0200)] 
gccrs: Change enum to enum class

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h: Add enum prefix.
* parse/rust-parse.h (enum ParseSelfError): Change from enum...
(enum class): To enum class.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: Remove reinterpret_cast usages in DefaultASTVisitor
Owen Avery [Tue, 1 Jul 2025 21:18:28 +0000 (17:18 -0400)] 
gccrs: Remove reinterpret_cast usages in DefaultASTVisitor

gcc/rust/ChangeLog:

* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Replace
usages of reinterpret_cast with static_cast.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Remove Late visitor override for StructStruct
Owen Avery [Tue, 1 Jul 2025 19:54:27 +0000 (15:54 -0400)] 
gccrs: Remove Late visitor override for StructStruct

DefaultResolver already handles StructStruct in a more correct fashion.

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Remove
override for StructStruct visitor.
* resolve/rust-late-name-resolver-2.0.h (Late::visit): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: nr1.0: Remove rust/backend support
Owen Avery [Thu, 3 Jul 2025 21:48:11 +0000 (17:48 -0400)] 
gccrs: nr1.0: Remove rust/backend support

This is the first patch in a set intended to fully remove name
resolution 1.0. As such, it should leave name resolution 1.0 technically
available but broken.

gcc/rust/ChangeLog:

* backend/rust-compile-context.cc (Context::Context): Remove
initialization of resolver field.
* backend/rust-compile-context.h (Context::get_resolver): Remove
function.
(Context::resolver): Remove field.
* backend/rust-compile-expr.cc (CompileExpr::visit): Assume name
resolution 2.0 is always enabled.
(CompileExpr::generate_closure_function): Likewise.
* backend/rust-compile-implitem.cc (CompileTraitItem::visit):
Likewise.
* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
* backend/rust-compile-resolve-path.cc
(ResolvePathRef::resolve): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Remove -frust-name-resolution-2.0 usage in tests
Owen Avery [Thu, 3 Jul 2025 21:43:55 +0000 (17:43 -0400)] 
gccrs: Remove -frust-name-resolution-2.0 usage in tests

gcc/testsuite/ChangeLog:

* rust/compile/additional-trait-bounds2nr2.rs: Remove
-frust-name-resolution-2.0 usage.
* rust/compile/const_generics_3.rs: Likewise.
* rust/compile/enum_variant_name.rs: Likewise.
* rust/compile/generics9.rs: Likewise.
* rust/compile/invalid_label_name.rs: Likewise.
* rust/compile/issue-3304.rs: Likewise.
* rust/compile/macros/mbe/macro-issue3708.rs: Likewise.
* rust/compile/macros/mbe/macro-issue3709-2.rs: Likewise.
* rust/compile/name_resolution10.rs: Likewise.
* rust/compile/name_resolution11.rs: Likewise.
* rust/compile/name_resolution12.rs: Likewise.
* rust/compile/name_resolution13.rs: Likewise.
* rust/compile/name_resolution14.rs: Likewise.
* rust/compile/name_resolution15.rs: Likewise.
* rust/compile/name_resolution16.rs: Likewise.
* rust/compile/name_resolution17.rs: Likewise.
* rust/compile/name_resolution18.rs: Likewise.
* rust/compile/name_resolution20.rs: Likewise.
* rust/compile/name_resolution22.rs: Likewise.
* rust/compile/name_resolution23.rs: Likewise.
* rust/compile/name_resolution24.rs: Likewise.
* rust/compile/name_resolution25.rs: Likewise.
* rust/compile/name_resolution6.rs: Likewise.
* rust/compile/name_resolution7.rs: Likewise.
* rust/compile/name_resolution8.rs: Likewise.
* rust/compile/name_resolution9.rs: Likewise.
* rust/compile/nested_macro_definition.rs: Likewise.
* rust/compile/pub_restricted_1.rs: Likewise.
* rust/compile/pub_restricted_2.rs: Likewise.
* rust/compile/self-in-impl.rs: Likewise.
* rust/compile/self_import_namespace.rs: Likewise.
* rust/compile/use_1.rs: Likewise.
* rust/compile/xfail/name_resolution21.rs: Likewise.
* rust/execute/torture/name_resolution.rs: Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: nr2.0: Enable by default
Owen Avery [Fri, 9 May 2025 21:50:22 +0000 (17:50 -0400)] 
gccrs: nr2.0: Enable by default

gcc/rust/ChangeLog:

* lang.opt (frust-name-resolution-2.0): Enable by default.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/compile.exp: Removed.
* rust/compile/nr2/exclude: Removed.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Implement compilation support for TuplePatternItems::RANGED
Zhi Heng [Sun, 29 Jun 2025 02:44:31 +0000 (10:44 +0800)] 
gccrs: Implement compilation support for TuplePatternItems::RANGED

Example GIMPLE output of the match statement for match-restpattern-tuple-1.rs:

...
RUSTTMP.2 = x;
_1 = RUSTTMP.2.__0;
_2 = _1 == 1;
_3 = RUSTTMP.2.__3;
_4 = _3 == 4;
_5 = _2 & _4;
if (_5 != 0) goto <D.109>; else goto <D.110>;
<D.109>:
{
    {

    }
    goto <D.104>;
}
<D.110>:
if (1 != 0) goto <D.111>; else goto <D.112>;
<D.111>:
{
    {

    }
    goto <D.104>;
}
<D.112>:
<D.104>:
...

gcc/rust/ChangeLog:

* backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit(TuplePattern)):
Implement check expression compilation for TuplePatternItems::RANGED.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: Fix type checking logic for TuplePattern
Zhi Heng [Sat, 28 Jun 2025 12:59:54 +0000 (20:59 +0800)] 
gccrs: Fix type checking logic for TuplePattern

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (visit(TuplePattern)): Fix
incorrect logic for field size checking.

gcc/testsuite/ChangeLog:

* rust/compile/tuple_mismatch.rs: Include RestPattern in test.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: chore: ast: Fix formatting and includes
Arthur Cohen [Fri, 13 Jun 2025 08:04:41 +0000 (10:04 +0200)] 
gccrs: chore: ast: Fix formatting and includes

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc: Remove extra include, fix new formatting.

6 days agogccrs: reconstruct_vec: Allocate size when creating the vector
Arthur Cohen [Mon, 26 May 2025 09:31:40 +0000 (11:31 +0200)] 
gccrs: reconstruct_vec: Allocate size when creating the vector

gcc/rust/ChangeLog:

* ast/rust-ast.h (reconstruct_vec): Pre-allocate size of vector.

6 days agogccrs: ast: builder: Remove ASTTypeBuilder
Arthur Cohen [Tue, 20 May 2025 13:08:06 +0000 (15:08 +0200)] 
gccrs: ast: builder: Remove ASTTypeBuilder

gcc/rust/ChangeLog:

* Make-lang.in: Remove object file for ASTTypeBuilder.
* ast/rust-ast-builder.h: Remove function.
* ast/rust-ast-builder.cc (Builder::new_type): Likewise.
(Builder::new_const_param): Use reconstruct_type() instead.
(Builder::new_generic_args): Likewise.
* expand/rust-derive-default.cc (DeriveDefault::visit_struct): Likewise.
(DeriveDefault::visit_tuple): Likewise.
* expand/rust-derive-eq.cc (DeriveEq::visit_tuple): Likewise.
(DeriveEq::visit_struct): Likewise.
(DeriveEq::visit_enum): Likewise.
(DeriveEq::visit_union): Likewise.
* ast/rust-ast-builder-type.cc: Removed.
* ast/rust-ast-builder-type.h: Removed.

6 days agogccrs: ast: Add reconstruct() method for Type nodes
Arthur Cohen [Tue, 20 May 2025 12:59:28 +0000 (14:59 +0200)] 
gccrs: ast: Add reconstruct() method for Type nodes

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add reconstruct() and reconstruct_impl() for Type nodes.
* ast/rust-type.h: Implement them.
* ast/rust-macro.h: Likewise.
* ast/rust-path.h: Likewise.

6 days agogccrs: ast: reconstruct: Add base for reconstructing and asserting different IDs
Arthur Cohen [Tue, 20 May 2025 12:25:07 +0000 (14:25 +0200)] 
gccrs: ast: reconstruct: Add base for reconstructing and asserting different IDs

gcc/rust/ChangeLog:

* ast/rust-ast.h (reconstruct): New function for calling the `reconstruct_*_impl` method
and asserting that the new NodeId is different, and then wrap it in a unique_ptr<T>.
(reconstruct_vec): Likewise, but for vectors of unique_ptr<T>

6 days agogccrs: nr2.0: Adjust resolution of modules
Owen Avery [Thu, 29 May 2025 21:04:46 +0000 (17:04 -0400)] 
gccrs: nr2.0: Adjust resolution of modules

This prioritizes resolution in the language prelude over resolution as a
module.

gcc/rust/ChangeLog:

* resolve/rust-forever-stack.hxx (ForeverStack::resolve_path):
Resolve final segments which point to modules.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
Avoid inserting module names into ribs in the type namespace.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove issue-3315-2.rs.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: nr2.0: Add more checks for alternate patterns
Owen Avery [Sat, 24 May 2025 15:51:29 +0000 (11:51 -0400)] 
gccrs: nr2.0: Add more checks for alternate patterns

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc
(visit_identifier_as_pattern): Handle is_ref and is_mut.
(Late::visit): Likewise.
* resolve/rust-name-resolution-context.cc
(BindingLayer::insert_ident): Likewise.
(BindingLayer::bind_test): Handle changes to BindingLayer
fields.
(BindingLayer::merge): Likewise and emit more error messages.
* resolve/rust-name-resolution-context.h
(struct IdentifierMode): New.
(Binding::has_expected_bindings): New field.
(Binding::set): Rename field to...
(Binding::idents): ...here and convert from a set to a map.
(Binding::Binding): Initialize has_expected_bindings.
(BindingLayer::insert_ident): Adjust parameters.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove torture/alt_patterns1.rs.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Add locus getters
Pierre-Emmanuel Patry [Fri, 27 Jun 2025 11:31:17 +0000 (13:31 +0200)] 
gccrs: Add locus getters

gcc/rust/ChangeLog:

* ast/rust-expr.h: Add getter to locus field.
* ast/rust-pattern.h (tokenid_to_rangekind): Likewise.
* hir/tree/rust-hir-item.h: Likewise.
* hir/tree/rust-hir-visibility.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
6 days agogccrs: nr2.0: Adjust resolution of external crates
Owen Avery [Mon, 16 Jun 2025 21:05:09 +0000 (17:05 -0400)] 
gccrs: nr2.0: Adjust resolution of external crates

This ensures Session::load_extern_crate doesn't try to use the old name
resolver when nr2.0 is enabled, while also ensuring that nr2.0 handles
external crates.

gcc/rust/ChangeLog:

* resolve/rust-default-resolver.cc
(DefaultResolver::visit_extern_crate): New function.
(DefaultResolver::visit): New visitor function for ExternCrate.
* resolve/rust-default-resolver.h
(DefaultResolver::visit_extern_crate): New function.
(DefaultResolver::visit): New visitor function for ExternCrate.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
Adjust ExternCrate visitor and rename to...
(TopLevel::visit_extern_crate): ...here.
* resolve/rust-toplevel-name-resolver-2.0.h (TopLevel::visit):
Remove ExternCrate visitor override.
(TopLevel::visit_extern_crate): New function.
* rust-session-manager.cc (Session::load_extern_crate): Only run
name resolution 1.0 if name resolution 2.0 is disabled.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Implement type checking for ItemType::RANGED in TuplePattern
Zhi Heng [Thu, 26 Jun 2025 14:33:15 +0000 (22:33 +0800)] 
gccrs: Implement type checking for ItemType::RANGED in TuplePattern

This patch implements the previously unimplemented type checking for RANGED item
type for TuplePattern, which serves as the start for implementing compilation of
RestPattern.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit(TuplePattern)):
Implement type checking for ItemType::RANGED.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: Update C++ version check in rust-lang.cc
Owen Avery [Tue, 24 Jun 2025 03:10:34 +0000 (23:10 -0400)] 
gccrs: Update C++ version check in rust-lang.cc

gcc/rust/ChangeLog:

* rust-lang.cc: Move version check from C++11 to C++14.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Implement default HIR visitor class
Ryutaro Okada [Tue, 24 Jun 2025 05:03:09 +0000 (22:03 -0700)] 
gccrs: Implement default HIR visitor class

gcc/rust/ChangeLog:

* Make-lang.in: Scaffolding new rust-hir-visitor files
* hir/tree/rust-hir-visitor.h (DefaultHIRVisitor): Declare default HIR visitor
* hir/tree/rust-hir-visitor.cc  (DefaultHIRVisitor): Define default HIR visitor

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
6 days agogccrs: Create Rust::GGC::Ident
Owen Avery [Tue, 24 Jun 2025 00:27:36 +0000 (20:27 -0400)] 
gccrs: Create Rust::GGC::Ident

This should make it easier for us to hand identifiers off to the
back end.

gcc/rust/ChangeLog:

* Make-lang.in (GRS_OBJS): Add rust-ggc.o.
* backend/rust-compile-base.cc
(HIRCompileBase::compile_function): Adjust call to
Backend::function.
(HIRCompileBase::compile_constant_item): Likewise and adjust
initialization of Backend::typed_identifier.
* backend/rust-compile-expr.cc (CompileExpr::visit): Adjust call
to Backend::label.
* backend/rust-compile-type.cc (TyTyResolveCompile::visit):
Adjust initialization of Backend::typed_identifier.
* rust-backend.h: Add includes.
(Backend::GGC::Ident): Use Rust::GGC::Ident.
(struct typed_identifier): Store name as a GGC::Ident rather
than a std::string and adjust constructors.
(named_type): Take GGC::Ident/tl::optional<GGC::Ident> rather
than std::string.
(global_variable): Likewise.
(local_variable): Likewise.
(parameter_variable): Likewise.
(static_chain_variable): Likewise.
(label): Likewise.
(function): Likewise.
* rust-gcc.cc (named_type): Likewise.
(global_variable): Likewise.
(local_variable): Likewise.
(parameter_variable): Likewise.
(static_chain_variable): Likewise.
(label): Likewise.
(function): Likewise.
(function_defer_statement): Adjust call to Backend::label.
(get_identifier_from_string): Remove function.
(fill_in_fields): Handle adjustments to typed_identifier.
* util/rust-ggc.cc: New file.
* util/rust-ggc.h: New file.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Add getter for non const lifetime object
Ryutaro Okada [Tue, 24 Jun 2025 05:28:59 +0000 (22:28 -0700)] 
gccrs: Add getter for non const lifetime object

gcc/rust/ChangeLog:

* hir/tree/rust-hir-item.h (SelfParam::get_lifetime): Add getter
for non const lifetime object

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
6 days agogccrs: Add getter for outer attributions
Ryutaro Okada [Tue, 24 Jun 2025 05:25:57 +0000 (22:25 -0700)] 
gccrs: Add getter for outer attributions

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.h (MatchArm::get_outer_attrs): Add getter for outer attributions

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
6 days agogccrs: Fix scan-assembler regexp in recurse2.rs
Owen Avery [Mon, 23 Jun 2025 16:33:54 +0000 (12:33 -0400)] 
gccrs: Fix scan-assembler regexp in recurse2.rs

gcc/testsuite/ChangeLog:

* rust/compile/macros/builtin/recurse2.rs: Match "abheyho\0" as
well as "abheyho", to handle slight differences in assembly
output for null-terminated strings.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Fix bug with non compiled const decl
Philip Herron [Mon, 23 Jun 2025 11:59:33 +0000 (12:59 +0100)] 
gccrs: Fix bug with non compiled const decl

There was a sily bug where if you reorder this test case to declare A before B
this test would work but its meant to work in any order. So this fixes the bug
during code gen to fall back to our query compile system if this is needed.

Fixes Rust-GCC#3525

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc: if this fails fall back to query compile

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: check for invalid const calls during code-gen
Philip Herron [Mon, 23 Jun 2025 10:21:02 +0000 (11:21 +0100)] 
gccrs: check for invalid const calls during code-gen

Closure calls are not const so this is invalid. This patch fixes two bugs

  1. Make the look at the parent context optional for generics
  2. Ensure we look for non const calls during call expr code-gen

Fixes Rust-GCC#3551

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): add const call check
* backend/rust-compile-item.cc (CompileItem::visit): ensure we upfront compile types where
possible
* backend/rust-compile-item.h: update header
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): make parent ctx optional

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix TupleStructPattern compilation throwing error
Zhi Heng [Sun, 22 Jun 2025 09:38:06 +0000 (17:38 +0800)] 
gccrs: Fix TupleStructPattern compilation throwing error

Code for TupleStructPattern compilation previously only assumes that it is derived from
an enum. This commit adds a check for that, and compiles non-enum TupleStructPatterns
similarly to TuplePatterns if it is not an enum.

gcc/rust/ChangeLog:

* backend/rust-compile-pattern.cc(CompilePatternCheckExpr::visit(TupleStructPattern)):
Fix error thrown when compiling non-enum TupleStructPattern.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
6 days agogccrs: nr2.0: Fix resolution of constant items
Owen Avery [Fri, 20 Jun 2025 16:04:29 +0000 (12:04 -0400)] 
gccrs: nr2.0: Fix resolution of constant items

gcc/rust/ChangeLog:

* resolve/rust-default-resolver.cc (DefaultResolver::visit):
Call DefaultASTVisitor::visit even on ConstantItem instances
without expressions.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove issue-3642.rs.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
6 days agogccrs: Add unify rules for fnptr and closures
Philip Herron [Sat, 21 Jun 2025 15:08:28 +0000 (16:08 +0100)] 
gccrs: Add unify rules for fnptr and closures

Its valid to unify a closure to an fnptr as we are working on the
fn traits. There are still other issues but this is part of the patch set.

gcc/rust/ChangeLog:

* typecheck/rust-unify.cc (UnifyRules::expect_fnptr): add unify rules

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix silly ordering bug in trait reference resolution
Philip Herron [Fri, 20 Jun 2025 17:21:30 +0000 (18:21 +0100)] 
gccrs: Fix silly ordering bug in trait reference resolution

Ensure proper ordering when resolving trait references to prevent
incorrect type resolution in certain contexts.

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-reference.cc (TraitReference::on_resolved): ensure associated
types are done first
* typecheck/rust-hir-type-check-type.cc: Update call site.

gcc/testsuite/ChangeLog:

* rust/compile/silly-order-bug.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
6 days agogccrs: Fix bug with bad type bindings not looking at super traits
Philip Herron [Sat, 21 Jun 2025 13:58:49 +0000 (14:58 +0100)] 
gccrs: Fix bug with bad type bindings not looking at super traits

When resolving type bounds, we need to examine super traits to properly
determine if type bindings are valid in the current context.

gcc/rust/ChangeLog:

* typecheck/rust-tyty-bounds.cc: Check super traits for type bindings.
* typecheck/rust-tyty.h: Add helper methods for bound checking.