]> git.ipfire.org Git - thirdparty/gcc.git/log
thirdparty/gcc.git
4 months agogccrs: derive(Copy): Use copy lang item when deriving Copy.
Arthur Cohen [Thu, 26 Dec 2024 22:09:11 +0000 (22:09 +0000)] 
gccrs: derive(Copy): Use copy lang item when deriving Copy.

gcc/rust/ChangeLog:

* expand/rust-derive-copy.cc: Use lang item path.

4 months agogccrs: derive(Clone): Mark PhantomData as a lang item
Arthur Cohen [Thu, 26 Dec 2024 10:57:07 +0000 (10:57 +0000)] 
gccrs: derive(Clone): Mark PhantomData as a lang item

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro4.rs: Make PhantomData a lang item.

4 months agogccrs: derive(Clone): Manually generate AssertParamIsCopy struct for unions
Arthur Cohen [Thu, 26 Dec 2024 10:50:13 +0000 (10:50 +0000)] 
gccrs: derive(Clone): Manually generate AssertParamIsCopy struct for unions

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::visit_union): Manually generate
the struct used for asserting a union implements Copy.

4 months agogccrs: builder: Allow generating struct statements
Arthur Cohen [Thu, 26 Dec 2024 10:49:16 +0000 (10:49 +0000)] 
gccrs: builder: Allow generating struct statements

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc (Builder::struct_struct): New function.
* ast/rust-ast-builder.h (vec): New function.

4 months agogccrs: lang-items: Mark Clone trait as a lang item in testsuite
Arthur Cohen [Fri, 3 Jan 2025 15:46:33 +0000 (15:46 +0000)] 
gccrs: lang-items: Mark Clone trait as a lang item in testsuite

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro1.rs: Add #[lang = "clone"] to Clone trait.
* rust/compile/derive_macro3.rs: Likewise.
* rust/compile/derive_macro6.rs: Likewise.
* rust/execute/torture/derive_macro3.rs: Likewise.

4 months agogccrs: ast-collector: Fix tuple struct pattern collection
Arthur Cohen [Fri, 3 Jan 2025 14:28:07 +0000 (14:28 +0000)] 
gccrs: ast-collector: Fix tuple struct pattern collection

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Visit tuple pattern items as
separated by commas.

4 months agogccrs: ast-collector: Adapt to lang item type path segments
Arthur Cohen [Tue, 31 Dec 2024 17:36:50 +0000 (17:36 +0000)] 
gccrs: ast-collector: Adapt to lang item type path segments

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Fix collector to better
handle lang item type path segments.

4 months agogccrs: mappings: Add get_lang_item_node
Arthur Cohen [Thu, 26 Dec 2024 23:00:08 +0000 (23:00 +0000)] 
gccrs: mappings: Add get_lang_item_node

This method errors out if the lang item has not been declared yet.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::get_lang_item_node): New.
* util/rust-hir-map.h: New function.

4 months agogccrs: lang-item: Add LangItem::PrettyString
Arthur Cohen [Thu, 26 Dec 2024 22:45:12 +0000 (22:45 +0000)] 
gccrs: lang-item: Add LangItem::PrettyString

Which formats a lang item as it appears in source code.

gcc/rust/ChangeLog:

* util/rust-lang-item.cc (LangItem::PrettyString): New.
* util/rust-lang-item.h: New.

4 months agogccrs: lang-items: Collect struct lang items.
Arthur Cohen [Thu, 26 Dec 2024 22:35:15 +0000 (22:35 +0000)] 
gccrs: lang-items: Collect struct lang items.

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (CollectLangItems::visit): New.
* ast/rust-collect-lang-items.h: New.

4 months agogccrs: lower: Properly lower non-generic lang item type path segments.
Arthur Cohen [Thu, 2 Jan 2025 10:41:44 +0000 (10:41 +0000)] 
gccrs: lower: Properly lower non-generic lang item type path segments.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-type.cc (ASTLowerTypePath::visit): Adapt code to lang item
type path segments.

4 months agogccrs: tychk: resolve lang item type paths properly
Arthur Cohen [Thu, 2 Jan 2025 10:37:00 +0000 (10:37 +0000)] 
gccrs: tychk: resolve lang item type paths properly

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Adapt
code to handle lang item type paths.

4 months agogccrs: ast: Refactor how lang item paths are handled.
Arthur Cohen [Thu, 26 Dec 2024 21:46:03 +0000 (21:46 +0000)] 
gccrs: ast: Refactor how lang item paths are handled.

Lang item typepaths were not handled properly, and required a complete overhaul.
All old classes that concerned lang item paths are now modified to use a simpler
version of `AST::LangItemPath`, which has been removed. TypePath segments can now
be lang items, as this is requied for having generic lang item paths such as
PhantomData<T>.

gcc/rust/ChangeLog:

* ast/rust-path.h: Rework how lang item paths are represented.
* ast/rust-path.cc: Likewise.
* ast/rust-item.h: Likewise.
* ast/rust-ast.cc: Likewise.
* ast/rust-ast-collector.cc: Adapt to new lang item path system.
* ast/rust-ast-collector.h: Likewise.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
* ast/rust-ast-visitor.h: Likewise.
* expand/rust-derive-copy.cc: Likewise.
* expand/rust-derive.h: Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h: Likewise.
* hir/rust-ast-lower-type.cc (ASTLowerTypePath::translate): Likewise.
(ASTLowerTypePath::visit): Likewise.
* hir/rust-ast-lower-type.h: Likewise.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise.
* resolve/rust-ast-resolve-base.h: Likewise.
* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise.
* resolve/rust-ast-resolve-type.h: Likewise.
* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): Likewise.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Likewise.
* resolve/rust-late-name-resolver-2.0.h: Likewise.
* hir/tree/rust-hir-path.cc (TypePathSegment::TypePathSegment): Likewise.
(TypePathSegmentGeneric::TypePathSegmentGeneric): Likewise.
* hir/tree/rust-hir-path.h: Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Likewise.
* ast/rust-ast-builder.cc: Likewise.
* ast/rust-ast-builder.h: Likewise.

4 months agogccrs: collect-lang-items: Display attribute upon error finding it
Arthur Cohen [Fri, 3 Jan 2025 15:45:39 +0000 (15:45 +0000)] 
gccrs: collect-lang-items: Display attribute upon error finding it

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (get_lang_item_attr): Show unknown attribute upon error.

4 months agogccrs: attributes: Add #[derive] as a built-in attribute
Arthur Cohen [Fri, 3 Jan 2025 15:45:57 +0000 (15:45 +0000)] 
gccrs: attributes: Add #[derive] as a built-in attribute

gcc/rust/ChangeLog:

* util/rust-attribute-values.h: Declare new attribute value.
* util/rust-attributes.cc: Use it.

4 months agogccrs: ast: Fix warning about copy elision for moved expr
Arthur Cohen [Thu, 26 Dec 2024 21:31:21 +0000 (21:31 +0000)] 
gccrs: ast: Fix warning about copy elision for moved expr

gcc/rust/ChangeLog:

* ast/rust-ast.cc (BlockExpr::normalize_tail_expr): Remove overzealous
std::move

4 months agogccrs: Remove dead code related to external functions
Owen Avery [Fri, 10 Jan 2025 20:50:25 +0000 (15:50 -0500)] 
gccrs: Remove dead code related to external functions

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc
(TokenCollector::visit): Remove visitor for NamedFunctionParam.
* ast/rust-ast-collector.h
(TokenCollector::visit): Likewise.
* ast/rust-ast-full-decls.h
(class NamedFunctionParam): Remove forward declaration.
* ast/rust-ast-visitor.cc
(DefaultASTVisitor::visit): Remove visitor for
NamedFunctionParam.
* ast/rust-ast-visitor.h
(DefaultASTVisitor::visit): Likewise.
* ast/rust-ast.cc
(NamedFunctionParam::as_string): Remove.
* ast/rust-item.h
(class NamedFunctionParam): Remove.
(class ExternalFunctionItem): Remove.
* parse/rust-parse-impl.h
(Parser::parse_named_function_param): Remove.
(Parser::parse_named_function_params): Remove.
* parse/rust-parse.h
(Parser::parse_named_function_param): Remove.
(Parser::parse_named_function_params): Remove.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: nr2.0: Early resolve pending eager macro invocations
Owen Avery [Sat, 11 Jan 2025 04:55:29 +0000 (23:55 -0500)] 
gccrs: nr2.0: Early resolve pending eager macro invocations

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc
(Early::visit): Resolve the pending eager invocations inside
builtin macro invocations.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: Add missing name resolution to static items in blocks
Philip Herron [Mon, 13 Jan 2025 11:51:51 +0000 (11:51 +0000)] 
gccrs: Add missing name resolution to static items in blocks

We need to add name resolution and hir lowering for items as part of blocks
in order to typecheck and compile them correctly.

Fixes Rust-GCC#3350

gcc/rust/ChangeLog:

* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): hir lowering
* hir/rust-ast-lower-stmt.h: likewise
* resolve/rust-ast-resolve-stmt.cc (ResolveStmt::visit): name resolution
* resolve/rust-ast-resolve-stmt.h: likewise

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: Revert "gcc/rust/ChangeLog:"
CohenArthur [Fri, 10 Jan 2025 14:26:39 +0000 (15:26 +0100)] 
gccrs: Revert "gcc/rust/ChangeLog:"

This reverts commit 600fd806b8821ea24103ea0f31d666077245c6b7.

4 months agogccrs: Fix scan-gimple testcases on LE platforms.
Arthur Cohen [Tue, 23 Jan 2024 16:19:31 +0000 (17:19 +0100)] 
gccrs: Fix scan-gimple testcases on LE platforms.

gcc/testsuite/ChangeLog:

* rust/compile/macros/builtin/eager1.rs: Switch to scan-assembler directive as the
GIMPLE dump does not contain strings on LE.
* rust/compile/macros/builtin/recurse2.rs: Likewise.

4 months agogccrs: testsuite: Fix missing handling of little endian.
Arthur Cohen [Thu, 18 Jan 2024 16:24:01 +0000 (17:24 +0100)] 
gccrs: testsuite: Fix missing handling of little endian.

Some failures occur in the testsuite because we
did not account for the little-endian case.

gcc/testsuite/ChangeLog:

* rust/compile/issue-1446.rs: Add swap_bytes function.
* rust/compile/iterators1.rs: Remove unused {to, from}_le functions.

4 months agogccrs: Add ForeverStackStore
Owen Avery [Fri, 15 Nov 2024 00:57:42 +0000 (19:57 -0500)] 
gccrs: Add ForeverStackStore

ForeverStackStore is meant to partially unify the internal states of
per-namespace ForeverStack instances. This commit does not contain
modifications to ForeverStack which would allow it to rely on a
ForeverStackStore to store nodes, but a future commit should address
this.

gcc/rust/ChangeLog:

* Make-lang.in: Handle rust-forever-stack.cc.
* resolve/rust-forever-stack.h
(class ForeverStackStore): Add.
* resolve/rust-forever-stack.cc: New file, based on
rust-forever-stack.hxx.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: gcc/rust/ChangeLog:
Om Swaroop Nayak [Wed, 1 Jan 2025 17:02:02 +0000 (09:02 -0800)] 
gccrs: gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (get_lang_item_attr): "removed checker fn"
* util/rust-attributes.cc (Attributes::is_lang_item): "added fn"
* util/rust-attributes.h: "added fn"

Signed-off-by: Om Swaroop Nayak <96killerat96@gmail.com>
4 months agogccrs: improve mutability checks
Philip Herron [Mon, 16 Dec 2024 14:51:17 +0000 (14:51 +0000)] 
gccrs: improve mutability checks

This ensures that we handle var decls readonly checks much better

Addresses: Rust-GCC#807 Rust-GCC#3287

gcc/rust/ChangeLog:

* checks/errors/rust-readonly-check.cc (check_decl): improve mut check
(emit_error): helper
(check_modify_expr): likewise
(readonly_walk_fn): reuse helper
(ReadonlyCheck::Lint): cleanup context each run

gcc/testsuite/ChangeLog:

* rust/execute/torture/builtin_macro_include_bytes.rs: needs mut
* rust/compile/mutability_checks1.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: Visit the trait paths of trait implementations
Owen Avery [Sat, 28 Dec 2024 20:58:41 +0000 (15:58 -0500)] 
gccrs: Visit the trait paths of trait implementations

gcc/rust/ChangeLog:

* ast/rust-ast-visitor.cc
(DefaultASTVisitor::visit): When visiting a TraitImpl, visit its
trait path.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: add two more tests to test try-catch (unwind) code generation
liushuyu [Mon, 2 Dec 2024 21:52:35 +0000 (14:52 -0700)] 
gccrs: add two more tests to test try-catch (unwind) code generation

gcc/testsuite/ChangeLog:
* rust/compile/try-catch-unwind-old.rs: add a test to test the older
try intrinsics from plain old Rust to v1.78.0
* rust/compile/try-catch-unwind-new.rs: add a test to test the newer
catch_unwind instrinsics since Rust v1.78.0

4 months agogccrs: rust/intrinsic: add new "catch_unwind" variant of API
liushuyu [Mon, 2 Dec 2024 21:24:04 +0000 (14:24 -0700)] 
gccrs: rust/intrinsic: add new "catch_unwind" variant of API

gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc: add the new `catch_unwind` variant
of the `try` intrinsic: this variant can be seen on Rust 1.78+
and returns `()` instead of `i32`.

4 months agogccrs: rust/intrinsic: add try intrinsic and panic strategy options
liushuyu [Sat, 9 Sep 2023 20:19:06 +0000 (14:19 -0600)] 
gccrs: rust/intrinsic: add try intrinsic and panic strategy options

gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc: add `try` intrinsic handler.
* lang.opt: add `-frust-panic` option.
* rust-lang.cc: enable exception handler code generation.
* rust-session-manager.cc: add getter and setter for panic
strategy option.
* rust-session-manager.h: Likewise.

Signed-off-by: Zixing Liu <liushuyu011@gmail.com>
4 months agogccrs: match arms are a LUB
Philip Herron [Thu, 9 Jan 2025 16:47:47 +0000 (16:47 +0000)] 
gccrs: match arms are a LUB

Unify rules are not the same as coercion rules. The coercion of ! is
allowed to any type but not for a unify site which is different.

Match arms are another least upper bound coercion.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): implement coercion
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): this is an LUB
* typecheck/rust-unify.cc (UnifyRules::go): remove unify ! coercion

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: Allow float type to be casted as integer type
Nobel [Sat, 21 Dec 2024 18:11:39 +0000 (23:56 +0545)] 
gccrs: Allow float type to be casted as integer type

gccrs now should be able to cast float types as numeric.

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules): Add rule.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Nobel Singh <nobel2073@gmail.com>
4 months agogccrs: cleanup our enum type layout to be closer to rustc
Philip Herron [Tue, 7 Jan 2025 18:15:37 +0000 (18:15 +0000)] 
gccrs: cleanup our enum type layout to be closer to rustc

This changes our enum type layout so for example:

  enum Foo {
      A,
      B,
      C(char),
      D { x: i32, y: i32 },
  }

Used to get layed out like this in gccrs:

  union {
    struct A { int RUST$ENUM$DISR; };
    struct B { int RUST$ENUM$DISR; };
    struct C { int RUST$ENUM$DISR; char __0; };
    struct D { int RUST$ENUM$DISR; i64 x; i64 y; };
  }

This has some issues notably with the constexpr because this is just a
giant union it means its not simple to constify what enum variant we are
looking at because the discriminant is a mess.

This now gets layed out as:

  struct {
     int RUST$ENUM$DISR;
     union {
         struct A { };
         struct B { };
         struct C { char __0; };
         struct D { i64 x; i64 y; };
     } payload;
  }

This layout is much cleaner and allows for our constexpr to work properly.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): new layout
* backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit): likewise
(CompilePatternBindings::visit): likewise
* backend/rust-compile-resolve-path.cc: likewise
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): implement new layout
* rust-gcc.cc (constructor_expression): get rid of useless assert

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: nr2.0: Resolve Self inside impl blocks
Owen Avery [Tue, 7 Jan 2025 19:03:13 +0000 (14:03 -0500)] 
gccrs: nr2.0: Resolve Self inside impl blocks

gcc/rust/ChangeLog:

* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Insert a definition for Self when visiting
InherentImpl and TraitImpl instances.
* resolve/rust-toplevel-name-resolver-2.0.h
(TopLevel::visit): Add visitors for InherentImpl and TraitImpl.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: fix ICE with hir dump on closure
Philip Herron [Tue, 7 Jan 2025 18:43:32 +0000 (18:43 +0000)] 
gccrs: fix ICE with hir dump on closure

Return type and parameter types are optional on closures.

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::visit): add null guard

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: add support for lang_item eq and PartialEq trait
Philip Herron [Thu, 19 Dec 2024 16:43:49 +0000 (16:43 +0000)] 
gccrs: add support for lang_item eq and PartialEq trait

The Eq and Partial Ord are very similar to the operator overloads
we support for add/sub/etc... but they differ in that usually the
function call name matches the name of the lang item. This time
we need to have support to send in a new path for the method call
on the lang item we want instead of just the name of the lang item.

NOTE: this test case doesnt work correctly yet we need to support
the derive of partial eq on enums to generate the correct comparison
code for that.

Fixes Rust-GCC#3302

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): handle partial_eq possible call
* backend/rust-compile-expr.h: handle case where lang item calls differ from name
* hir/tree/rust-hir-expr.cc (OperatorExprMeta::OperatorExprMeta): new helper
* hir/tree/rust-hir-expr.h: likewise
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): handle partial_eq
(TypeCheckExpr::resolve_operator_overload): likewise
* typecheck/rust-hir-type-check-expr.h: likewise
* util/rust-lang-item.cc (LangItem::ComparisonToLangItem): map comparison to lang item
(LangItem::ComparisonToSegment): likewise
* util/rust-lang-item.h: new lang items PartialOrd and Eq
* util/rust-operators.h (enum class): likewise

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/cmp1.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: fix ICE in borrows to invalid expressions
Philip Herron [Tue, 7 Jan 2025 12:32:43 +0000 (12:32 +0000)] 
gccrs: fix ICE in borrows to invalid expressions

We need to check if the borrowed value is valid before creating the
reference type. Otherwise this will lead to an ICE.

Fixes Rust-GCC#3140

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): check for error
* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): likewise and remove debug error

gcc/testsuite/ChangeLog:

* rust/compile/issue-3046.rs: remove old error message
* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-3140.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: nr2.0: Improve default, top-level, and late resolvers
Owen Avery [Thu, 2 Jan 2025 10:55:38 +0000 (05:55 -0500)] 
gccrs: nr2.0: Improve default, top-level, and late resolvers

gcc/rust/ChangeLog:

* resolve/rust-default-resolver.cc
(DefaultResolver::visit): Make sure to scope visitation of the
children of type definition items.
* resolve/rust-default-resolver.h
(DefaultResolver::visit): Add overrides for TupleStruct, Union,
and TypeAlias.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Remove override for Enum.
* resolve/rust-late-name-resolver-2.0.h
(Late::visit): Likewise.
* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Rely more on DefaultResolver::visit.
* resolve/rust-toplevel-name-resolver-2.0.h
(TopLevel::visit): Remove override for BlockExpr.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: fix ICE during HIR dump
Philip Herron [Mon, 6 Jan 2025 11:02:51 +0000 (11:02 +0000)] 
gccrs: fix ICE during HIR dump

These hir nodes have optional expressions which need guarded

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::do_qualifiedpathtype): add guard
(Dump::do_traitfunctiondecl): likewise
(Dump::visit): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: use StackedContexts for block context
Prajwal S N [Mon, 16 Dec 2024 07:59:46 +0000 (13:29 +0530)] 
gccrs: use StackedContexts for block context

Replaces the DIY vector stack with a StackedContexts object for block
scopes in the type checker context.

Fixes #3284.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check.h (class TypeCheckContext): add
header file and use StackedContexts for blocks
* typecheck/rust-typecheck-context.cc: update methods
* typecheck/rust-hir-trait-resolve.cc: refactor function calls
* typecheck/rust-hir-type-check-implitem.cc: refactor function calls
* typecheck/rust-hir-type-check-type.cc: refactor function calls

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
4 months agogccrs: ast: Add new Kind enums for more precise downcasting
Arthur Cohen [Sat, 21 Dec 2024 22:56:52 +0000 (22:56 +0000)] 
gccrs: ast: Add new Kind enums for more precise downcasting

This commit adds things like Item::Kind, Expr::Kind, etc, and implements the associated `get_*_kind` functions.
It also removes the more generic AST::Kind enum we were using, which was incomplete and painful to use.

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add new Kind enums, remove Node class.
* ast/rust-builtin-ast-nodes.h: Use new Kind enums.
* ast/rust-expr.h (class LoopLabel): Likewise.
* ast/rust-item.h: Likewise.
* ast/rust-macro.h: Likewise.
* ast/rust-path.h: Likewise.
* expand/rust-macro-builtins-helpers.cc: Likewise.
* expand/rust-macro-builtins-utility.cc (MacroBuiltin::concat_handler): Likewise.
(MacroBuiltin::stringify_handler): Likewise.
* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Likewise.
* resolve/rust-early-name-resolver.cc: Likewise.
* hir/rust-ast-lower.cc (ASTLoweringBlock::visit): Likewise.

4 months agogccrs: nr2.0: Resolve type aliases inside trait definitions
Owen Avery [Thu, 2 Jan 2025 10:29:03 +0000 (05:29 -0500)] 
gccrs: nr2.0: Resolve type aliases inside trait definitions

gcc/rust/ChangeLog:

* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Add visitor for TraitItemType.
* resolve/rust-toplevel-name-resolver-2.0.h
(TopLevel::visit): Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: tychk: Add more support for additional trait bounds in functions
Arthur Cohen [Wed, 25 Dec 2024 18:13:43 +0000 (18:13 +0000)] 
gccrs: tychk: Add more support for additional trait bounds in functions

This commit correctly lowers and typechecks parenthesized types, which are used for trait objects with additional bounds.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-type.cc (ResolveType::visit): New visitor to handle
ParenthesizedType.
* resolve/rust-ast-resolve-type.h: Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): Likewise.
* typecheck/rust-hir-type-check-type.h: Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/auto_traits2.rs: New test.
* rust/compile/auto_traits3.rs: New test.
* rust/compile/nr2/exclude: Add auto_traits2 test.

4 months agogccrs: lower: Correctly lower parenthesized types
Arthur Cohen [Wed, 25 Dec 2024 17:55:09 +0000 (17:55 +0000)] 
gccrs: lower: Correctly lower parenthesized types

This is useful for handling multiple trait bounds, and required for better handling of auto traits.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-type.cc (ASTLoweringType::visit): Add implementation for
ParenthesizedType.
* hir/rust-ast-lower-type.h: Declare that new visitor.

gcc/testsuite/ChangeLog:

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

4 months agogccrs: Remove Rust::make_unique
Owen Avery [Wed, 4 Dec 2024 20:23:07 +0000 (15:23 -0500)] 
gccrs: Remove Rust::make_unique

Since our bootstrap requirement has been bumped to C++14, we don't need
a custom implementation of std::make_unique anymore.

gcc/rust/ChangeLog:

* ast/rust-ast-builder-type.cc: Remove inclusion of
rust-make-unique.h.
* ast/rust-ast-builder.cc: Likewise.
(Builder::array): Use std::make_unique instead of
Rust::make_unique.
* ast/rust-ast.cc (Attribute::get_traits_to_derive): Likewise.
* ast/rust-macro.h: Remove inclusion of rust-make-unique.h.
(MacroRulesDefinition::mbe): Use std::make_unique instead of
Rust::make_unique.
(MacroRulesDefinition::decl_macro): Likewise.
* ast/rust-path.h
(PathInExpression::PathInExpression): Likewise.
(QualifiedPathInExpression::QualifiedPathInExpression):
Likewise.
* backend/rust-compile-pattern.cc
(CompilePatternCheckExpr::visit): Likewise.
* expand/rust-derive-copy.cc
(DeriveCopy::copy_impl): Likewise.
* expand/rust-expand-format-args.cc
(expand_format_args): Likewise.
* expand/rust-macro-builtins-asm.cc: Remove inclusion of
rust-make-unique.h.
(parse_asm): Use std::make_unique instead of Rust::make_unique.
* hir/rust-ast-lower-expr.cc
(ASTLoweringExpr::visit): Likewise.
* hir/tree/rust-hir-expr.cc
(StructExprStructFields::StructExprStructFields): Likewise.
(StructExprStructFields::operator=): Likewise.
* hir/tree/rust-hir.cc
(TypePath::to_trait_bound): Likewise.
* lex/rust-token.h: Remove inclusion of rust-make-unique.h.
(Token::Token): Use std::make_unique instead of
Rust::make_unique.
* metadata/rust-import-archive.cc: Remove inclusion of
rust-make-unique.h.
(Import::find_archive_export_data): Use std::make_unique instead
of Rust::make_unique.
* metadata/rust-imports.cc: Remove inclusion of
rust-make-unique.h.
(Import::find_export_data): Use std::make_unique instead of
Rust::make_unique.
(Import::find_object_export_data): Likewise.
* parse/rust-parse-impl.h: Remove inclusion of
rust-make-unique.h.
(Parser::parse_function_param): Use std::make_unique instead of
Rust::make_unique.
(Parser::parse_self_param): Likewise.
(Parser::parse_array_expr): Likewise.
* typecheck/rust-hir-type-check-enumitem.cc
(TypeCheckEnumItem::visit): Likewise.
* typecheck/rust-hir-type-check-implitem.cc
(TypeCheckTopLevelExternItem::visit): Likewise.
(TypeCheckImplItem::visit): Likewise.
* typecheck/rust-hir-type-check-type.cc
(TypeResolveGenericParam::visit): Likewise.
* typecheck/rust-hir-type-check.cc: Remove inclusion of
rust-make-unique.h.
(TraitItemReference::get_type_from_fn): Use std::make_unique
instead of Rust::make_unique.
* typecheck/rust-tyty-bounds.cc
(TypeCheckBase::get_predicate_from_bound): Likewise.
* util/rust-make-unique.h: Removed.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: ast: Add EnumItem::Kind
Arthur Cohen [Thu, 2 Jan 2025 18:31:54 +0000 (18:31 +0000)] 
gccrs: ast: Add EnumItem::Kind

gcc/rust/ChangeLog:

* ast/rust-item.h: Add EnumItem::Kind for differentiating all variants that may be
used inside an enum declaration.

4 months agogccrs: nr2.0: Handle "Self" properly in trait definitions
Owen Avery [Tue, 24 Dec 2024 21:02:40 +0000 (16:02 -0500)] 
gccrs: nr2.0: Handle "Self" properly in trait definitions

gcc/rust/ChangeLog:

* ast/rust-ast-visitor.cc
(DefaultASTVisitor::visit): Visit implicit Self parameters of
traits.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Resolve implicit Self parameters of traits.
* resolve/rust-late-name-resolver-2.0.h:
(Late::visit): Add trait visitor.
* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Insert resolutions for Self type parameters
as well.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: Fix NR2.0 compiler ICE caused by Generics in Enums
Liam Naddell [Tue, 17 Dec 2024 16:48:03 +0000 (11:48 -0500)] 
gccrs: Fix NR2.0 compiler ICE caused by Generics in Enums

gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc:
Change the late name resolver to enter proper lexical scope during typechecking
* resolve/rust-late-name-resolver-2.0.h:
Add needed prototype to header
* resolve/rust-toplevel-name-resolver-2.0.cc:
Add generic parameters to enum's scoped RIB to allow for proper name resolution on types.

gcc/testsuite/ChangeLog:
* rust/compile/issue-3304.rs:
Add small test for generics+enums combination for NR2.0

Signed-off-by: Liam Naddell <liam.naddell@mail.utoronto.ca>
4 months agogccrs: ast-builder: Add more methods
Arthur Cohen [Sat, 21 Dec 2024 22:52:57 +0000 (22:52 +0000)] 
gccrs: ast-builder: Add more methods

This commit adds new methods for building pattern nodes, match expressions and more precise call expressions.

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc: Add new functions.
* ast/rust-ast-builder.h: Declare them.

4 months agogccrs: ast: Add new constructors for PathInExpression
Arthur Cohen [Sat, 21 Dec 2024 22:53:50 +0000 (22:53 +0000)] 
gccrs: ast: Add new constructors for PathInExpression

This commit adds two new constructors for AST::PathInExpression: One using a provided lang-item, and one with an already built std::unique_ptr<Path>

gcc/rust/ChangeLog:

* ast/rust-path.h: Add two new constructors.

4 months agogccrs: lang-items: Collect trait functions that are lang items
Arthur Cohen [Wed, 18 Dec 2024 11:20:27 +0000 (12:20 +0100)] 
gccrs: lang-items: Collect trait functions that are lang items

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (CollectLangItems::visit): Add visitor for collecting
functions that might be lang items.
* ast/rust-collect-lang-items.h: Likewise.

4 months agogccrs: lang-item: Add Option::{None, Some}, Iterator::next, IntoIter::into_iter
Arthur Cohen [Mon, 16 Dec 2024 13:17:29 +0000 (14:17 +0100)] 
gccrs: lang-item: Add Option::{None, Some}, Iterator::next, IntoIter::into_iter

gcc/rust/ChangeLog:

* util/rust-lang-item.h: Add new lang items.
* util/rust-lang-item.cc: Likewise.

4 months agogccrs: lang-item: Add Sync trait
Arthur Cohen [Wed, 25 Dec 2024 11:08:25 +0000 (11:08 +0000)] 
gccrs: lang-item: Add Sync trait

gcc/rust/ChangeLog:

* util/rust-lang-item.h: Add Sync marker trait.
* util/rust-lang-item.cc: Likewise.

4 months agogccrs: typecheck: Add note about erorring out on additional trait bounds.
Arthur Cohen [Wed, 25 Dec 2024 11:07:17 +0000 (11:07 +0000)] 
gccrs: typecheck: Add note about erorring out on additional trait bounds.

If additional trait bounds aren't auto traits, then the typechecker
must error out (Rust-GCC#3008)

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc: Add TODO note.

4 months agogccrs: resolve: Name resolve trait bounds properly
Arthur Cohen [Wed, 25 Dec 2024 11:03:44 +0000 (11:03 +0000)] 
gccrs: resolve: Name resolve trait bounds properly

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-type.cc (ResolveTypeToCanonicalPath::visit): Resolve additional
trait bounds.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Error out properly on unresolved
type-path instead of crashing.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Exclude additional-trait-bounds2 for different error message.
* rust/compile/additional-trait-bounds1.rs: New test.
* rust/compile/additional-trait-bounds2.rs: New test.
* rust/compile/additional-trait-bounds2nr2.rs: New test.

4 months agogccrs: parser: Add testcases for multiline strings
Arthur Cohen [Wed, 25 Dec 2024 17:02:38 +0000 (17:02 +0000)] 
gccrs: parser: Add testcases for multiline strings

Regression checks for Rust-GCC#1399

gcc/testsuite/ChangeLog:

* rust/compile/multiline-string.rs: New test.
* rust/execute/torture/multiline-string.rs: New test.

4 months agogccrs: typecheck-path: Fix typo (reciever -> receiver)
Arthur Cohen [Fri, 20 Dec 2024 11:29:32 +0000 (11:29 +0000)] 
gccrs: typecheck-path: Fix typo (reciever -> receiver)

gcc/rust/ChangeLog:

* typecheck/rust-hir-path-probe.cc: Fix typos.
* typecheck/rust-hir-path-probe.h: Likewise.
* typecheck/rust-hir-type-check-path.cc: Likewise.

4 months agogccrs: add ptr to int and int to ptr type cast rules
Nobel [Sun, 22 Dec 2024 22:19:44 +0000 (04:04 +0545)] 
gccrs: add ptr to int and int to ptr type cast rules

Added rules to allow type casting pointer as integer types (u*,i*)
and integer types to be casted as pointer.

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules): Add rule.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Nobel Singh <nobel2073@gmail.com>
4 months agogccrs: Made changes to AST::TraitImpl constructor for TypePath
Sri Ganesh Thota [Tue, 10 Dec 2024 13:08:27 +0000 (18:38 +0530)] 
gccrs: Made changes to AST::TraitImpl constructor for TypePath

gcc/rust/ChangeLog:

* ast/rust-item.h: I have changed helper constructor for typepath
to be a delegating constructor.

Signed-off-by: Sri Ganesh Thota <sriganeshthota12345@gmail.com>
4 months agogccrs: implement the TuplePattern and use it for function patterns
Philip Herron [Fri, 13 Dec 2024 15:51:55 +0000 (15:51 +0000)] 
gccrs: implement the TuplePattern and use it for function patterns

In order to handle the tuple pattern of: fn test ((x _) : (i32, i32)) -> i32 { x }
we need to recognize that ABI wise this function still takes a tuple as the parameter
to this function its just how we can address the "pattern" of the tuple changes.

So reall if this was C it would look like:

  void test (struct tuple_type __prameter)
  {
    return __parameter.0
  }

The code here reuses our existing pattern code so that we generate these implicit
bindings of the paramter with a field access so any time x is referenced it's really
just emplacing __parameter.0 for the field access into the struct which is a tuple.

Fixes Rust-GCC#2847

gcc/rust/ChangeLog:

* backend/rust-compile-fnparam.cc (CompileFnParam::visit): compile tuple patterns
(CompileSelfParam::compile): update return type
(CompileFnParam::create_tmp_param_var): return Bvariable not tree to stop ICE
* backend/rust-compile-fnparam.h: update prototype
* backend/rust-compile-pattern.cc (CompilePatternBindings::visit): implement TuplePattern
* backend/rust-compile-pattern.h: update prototype

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: fix bad not expression in rust
Philip Herron [Thu, 12 Dec 2024 14:56:14 +0000 (14:56 +0000)] 
gccrs: fix bad not expression in rust

Fixes Rust-GCC#3229

gcc/rust/ChangeLog:

* rust-gcc.cc (operator_to_tree_code): ! expressions are BIT_NOT_EXPR

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: lang-item: Remove unused NodeId from LangItemPath
Arthur Cohen [Fri, 29 Nov 2024 12:44:59 +0000 (13:44 +0100)] 
gccrs: lang-item: Remove unused NodeId from LangItemPath

gcc/rust/ChangeLog:

* ast/rust-path.h: Adapt children of Path to fix some NodeId issues.

4 months agoaarch64: Add support for -mcpu=olympus
Dhruv Chawla [Wed, 19 Mar 2025 16:34:09 +0000 (09:34 -0700)] 
aarch64: Add support for -mcpu=olympus

This adds support for the NVIDIA Olympus core to the AArch64 backend. The
initial patch does not add any special tuning decisions, and those may come
later.

Bootstrapped and tested on aarch64-none-linux-gnu.

gcc/ChangeLog:

* config/aarch64/aarch64-cores.def (olympus): New entry.
* config/aarch64/aarch64-tune.md: Regenerate.
* doc/invoke.texi (AArch64 Options): Document the above.

Signed-off-by: Dhruv Chawla <dhruvc@nvidia.com>
4 months agogccrs: nr2.0: Resolve lang item paths properly.
Arthur Cohen [Fri, 29 Nov 2024 11:35:37 +0000 (12:35 +0100)] 
gccrs: nr2.0: Resolve lang item paths properly.

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): New.
* resolve/rust-late-name-resolver-2.0.h: New.

4 months agogccrs: hir: Lower lang-item paths
Arthur Cohen [Fri, 29 Nov 2024 10:06:25 +0000 (11:06 +0100)] 
gccrs: hir: Lower lang-item paths

gcc/rust/ChangeLog:

* hir/rust-ast-lower-type.cc (ASTLowerTypePath::translate): Adapt to
handle lang item paths.
(ASTLowerTypePath::visit): Likewise.
(ASTLowerTypePath::translate_type_path): New.
(ASTLowerTypePath::translate_lang_item_type_path): New.
* hir/rust-ast-lower-type.h: Adapt to handle lang item paths.
* resolve/rust-ast-resolve-type.h: Likewise.

4 months agogccrs: nr1.0: Resolve lang item paths properly.
Arthur Cohen [Wed, 4 Dec 2024 14:14:45 +0000 (15:14 +0100)] 
gccrs: nr1.0: Resolve lang item paths properly.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Adapt resolver
to lang item paths.
* resolve/rust-ast-resolve-type.h: Likewise.

4 months agogccrs: hir: Start adapting visitors to accept multiple kinds of Paths
Arthur Cohen [Wed, 6 Nov 2024 15:32:35 +0000 (16:32 +0100)] 
gccrs: hir: Start adapting visitors to accept multiple kinds of Paths

gcc/rust/ChangeLog:

* ast/rust-item.h: Add new method to specifically get a type-path.
* ast/rust-path.cc (LangItemPath::as_string): Implement properly.
* hir/rust-ast-lower-type.cc (ASTLowerTypePath::translate): Adapt
visitor to use the new LangItemPath.
* hir/rust-ast-lower-type.h: Likewise.
* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise.
* resolve/rust-ast-resolve-type.h: Likewise.

4 months agogccrs: derive(Copy): Use new LangItemPath
Arthur Cohen [Wed, 4 Dec 2024 14:10:10 +0000 (15:10 +0100)] 
gccrs: derive(Copy): Use new LangItemPath

gcc/rust/ChangeLog:

* expand/rust-derive-copy.cc: Use new LangItemPath for derive(Copy).

4 months agogccrs: ast: Add LangItemPath class
Arthur Cohen [Wed, 4 Dec 2024 14:09:58 +0000 (15:09 +0100)] 
gccrs: ast: Add LangItemPath class

This commit adds a new kind of Path, changes the base Path class and turns TypePath
into a child of the base Path class.

gcc/rust/ChangeLog:

* ast/rust-path.h (class LangItemPath): New.
(class TypePath): Adapt to accomodate LangItemPath.
* ast/rust-ast.cc (TraitImpl::as_string): Use new checks for lang items.
(QualifiedPathType::as_string): Likewise.
(FormatArgs::set_outer_attrs): Likewise.
* ast/rust-item.h (class TraitImpl): Likewise.

4 months agogccrs: Fix ForeverStack::find_starting_point output parameter
Owen Avery [Mon, 11 Nov 2024 21:04:58 +0000 (16:04 -0500)] 
gccrs: Fix ForeverStack::find_starting_point output parameter

gcc/rust/ChangeLog:

* resolve/rust-forever-stack.h
(ForeverStack::find_starting_point): Use type
'std::reference_wrapper<Node> &' instead of 'Node &' for
parameter starting_point.
* resolve/rust-forever-stack.hxx
(ForeverStack::find_starting_point): Likewise.
(ForeverStack::resolve_path): Handle change to
ForeverStack::find_starting_point.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: type-check: Remove unused capture in nr2.0
Arthur Cohen [Fri, 29 Nov 2024 11:03:59 +0000 (12:03 +0100)] 
gccrs: type-check: Remove unused capture in nr2.0

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path):
Remove unused capture in lambda.

4 months agogccrs: attributes: Add class for sharing methods on attributes.
Arthur Cohen [Wed, 4 Dec 2024 13:53:11 +0000 (13:53 +0000)] 
gccrs: attributes: Add class for sharing methods on attributes.

gcc/rust/ChangeLog:

* util/rust-attributes.h (class Attributes): New.
* util/rust-attributes.cc: Implement Attributes::is_known().
* ast/rust-collect-lang-items.cc (is_known_attribute): Remove.
(get_lang_item_attr): Call Attributes::is_known() instead.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_outer_attributes): Likewise.
(ASTLoweringBase::is_known_attribute): Remove.

4 months agogccrs: lang-items: Add lang-items AST collector
Arthur Cohen [Mon, 25 Nov 2024 13:30:39 +0000 (14:30 +0100)] 
gccrs: lang-items: Add lang-items AST collector

gcc/rust/ChangeLog:

* Make-lang.in: Add new object file.
* rust-session-manager.cc (Session::compile_crate): Call CollectLangItems.
* ast/rust-collect-lang-items.cc: New file.
* ast/rust-collect-lang-items.h: New file.

4 months agogccrs: lang-items: Store NodeId mappings for lang items
Arthur Cohen [Mon, 25 Nov 2024 14:05:32 +0000 (15:05 +0100)] 
gccrs: lang-items: Store NodeId mappings for lang items

gcc/rust/ChangeLog:

* util/rust-hir-map.h: Keep a NodeId mappings for lang items.
* util/rust-hir-map.cc (Mappings::insert_lang_item_node): New function.
(Mappings::lookup_lang_item_node): Likewise.

4 months agogccrs: fix crash in hir dump
Philip Herron [Mon, 2 Dec 2024 17:38:55 +0000 (17:38 +0000)] 
gccrs: fix crash in hir dump

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::visit): add missing check for no return value

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: add test case to show issue is fixed
Philip Herron [Mon, 2 Dec 2024 17:02:25 +0000 (17:02 +0000)] 
gccrs: add test case to show issue is fixed

Fixes Rust-GCC#266

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-266.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: add checks for division by zero and left shift overflow
Philip Herron [Mon, 2 Dec 2024 13:54:29 +0000 (13:54 +0000)] 
gccrs: add checks for division by zero and left shift overflow

These are ported from the c-family code c-warn.cc and c/c-typchk.cc

Fixes Rust-GCC#2394

gcc/rust/ChangeLog:

* backend/rust-constexpr.cc (eval_store_expression): check for null
(eval_call_expression): remove bad warning
* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: constant evaluation like these are coercion sites
Philip Herron [Mon, 2 Dec 2024 16:17:54 +0000 (16:17 +0000)] 
gccrs: constant evaluation like these are coercion sites

The code here was wrongly assuming the decl type from the folding of the
expression would be the type of the constant decl. This is not the case for
unsized coercions for slices, where the expression here is a reference to
an array then we require the coercion to fix the result up to the expected
type.

Fixes Rust-GCC#1525

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc: apply coercion site to result
* backend/rust-compile-base.h: update prototype
* backend/rust-compile-implitem.cc (CompileTraitItem::visit): send in coercion info
* backend/rust-compile-item.cc (CompileItem::visit): likewise

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: Remove bad assertion in name resolution
Philip Herron [Mon, 2 Dec 2024 14:10:11 +0000 (14:10 +0000)] 
gccrs: Remove bad assertion in name resolution

This was a handy debug assertion but only works for valid rust code. This
needs to handle the case where the type is not resolved which is a valid
case.

Fixes Rust-GCC#2423

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): remove assertions

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 can't handle this
* rust/compile/issue-2423.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: ast: Use StackedContexts class in ContextualASTVisitor
Arthur Cohen [Tue, 26 Nov 2024 16:39:04 +0000 (16:39 +0000)] 
gccrs: ast: Use StackedContexts class in ContextualASTVisitor

gcc/rust/ChangeLog:

* ast/rust-ast-visitor.h: Replace context with StackedContexts.
* ast/rust-ast-visitor.cc (ContextualASTVisitor::visit): Use new APIs.
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Likewise.

4 months agogccrs: stacked-contexts: Add peek() method
Arthur Cohen [Tue, 26 Nov 2024 16:34:37 +0000 (16:34 +0000)] 
gccrs: stacked-contexts: Add peek() method

gcc/rust/ChangeLog:

* util/rust-stacked-contexts.h: Add new method to see what context we are currently in.

4 months agogccrs: hir: Remove duplicate function in TraitItemFunc
Arthur Cohen [Fri, 29 Nov 2024 10:05:29 +0000 (11:05 +0100)] 
gccrs: hir: Remove duplicate function in TraitItemFunc

Both TraitItemFunc::has_definition() and TraitItemFunc::has_block_defined()
were exactly the same implementation, so remove one.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-item.h: Remove TraitItemFunc::has_block_defined()
* backend/rust-compile-implitem.cc (CompileTraitItem::visit):
Call TraitItemFunc::has_definition() instead.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
* typecheck/rust-hir-trait-resolve.cc (ResolveTraitItemToRef::visit): Likewise.

4 months agogccrs: mappings: Move lang_item definitions to .cc
Arthur Cohen [Mon, 25 Nov 2024 11:37:12 +0000 (12:37 +0100)] 
gccrs: mappings: Move lang_item definitions to .cc

gcc/rust/ChangeLog:

* util/rust-hir-map.h: Move definitions from header...
* util/rust-hir-map.cc: ...to source file.

4 months agogccrs: lang-items: Move comment about arithmetic lang items
Arthur Cohen [Mon, 25 Nov 2024 11:21:05 +0000 (12:21 +0100)] 
gccrs: lang-items: Move comment about arithmetic lang items

gcc/rust/ChangeLog:

* util/rust-lang-item.h: Fix comment location to align with other comments.

4 months agogccrs: ensure packed and aligned is applied properly
Philip Herron [Tue, 26 Nov 2024 18:05:25 +0000 (18:05 +0000)] 
gccrs: ensure packed and aligned is applied properly

We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes Rust-GCC#3260

gcc/rust/ChangeLog:

* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
* rust-backend.h (struct_type): add optional layout parameter
(union_type): likewise
(fill_in_fields): likewise
* rust-gcc.cc (struct_type): likewise
(union_type): likewise
(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: allow casts from numeric types to floats
Philip Herron [Tue, 26 Nov 2024 15:33:35 +0000 (15:33 +0000)] 
gccrs: allow casts from numeric types to floats

Fixes Rust-GCC#3261

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules): allow casts to float

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: improve handling of Self Type paths
Philip Herron [Mon, 25 Nov 2024 21:37:19 +0000 (21:37 +0000)] 
gccrs: improve handling of Self Type paths

TypePaths have special handling for Self where we can look at the current ctx
for more acurate TypeAlias information if required. We cant do this for Impl
contexts but not for Traits as we might as well fall back to the TypePathProbe.

The other issue was the dyn type comming in because Foo::foo and Foo is a trait
reference we represent this as a dyn type as the root resolved path but then find
the associated impl block for this but we cannot do this when we have resolved to
a Dyn because this is just a representation that we know we are talking about a
trait not because we are actually working with a real Dyn type.

Fixes Rust-GCC#2907

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): track trait
* typecheck/rust-hir-type-check-implitem.cc: trait block
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): dont when dyn
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): look at Self contenxt
(TypeCheckType::resolve_root_path): track if Self
(TypeCheckType::resolve_associated_type): look at current context for associated types
* typecheck/rust-hir-type-check-type.h: change prototype
* typecheck/rust-hir-type-check.h (class TypeCheckBlockContextItem):
new context system to track current state
* typecheck/rust-typecheck-context.cc (TypeCheckContext::have_block_context): likewise
(TypeCheckContext::peek_block_context): likewise
(TypeCheckContext::push_block_context): likewise
(TypeCheckContext::pop_block_context): likewise
(TypeCheckBlockContextItem::Item::Item): likewise
(TypeCheckBlockContextItem::TypeCheckBlockContextItem): likewise
(TypeCheckBlockContextItem::is_impl_block): likewise
(TypeCheckBlockContextItem::is_trait_block): likewise
(TypeCheckBlockContextItem::get_impl_block): likewise
(TypeCheckBlockContextItem::get_trait): likewise

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-2907.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: fix crash in hir dump with missing guards
Philip Herron [Mon, 25 Nov 2024 20:16:42 +0000 (20:16 +0000)] 
gccrs: fix crash in hir dump with missing guards

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::visit): add missing null checks

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: Prepend crate name to functions with nr2
Pierre-Emmanuel Patry [Mon, 25 Nov 2024 13:24:21 +0000 (14:24 +0100)] 
gccrs: Prepend crate name to functions with nr2

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc: Prepend crate name to function's ir
name.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove passing tests from exclude list.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
4 months agogccrs: Prevent execution of some nr1.0 functions with nr2.0
Owen Avery [Thu, 21 Nov 2024 20:28:46 +0000 (15:28 -0500)] 
gccrs: Prevent execution of some nr1.0 functions with nr2.0

gcc/rust/ChangeLog:

* resolve/rust-name-resolver.cc: Include options.txt.
(Resolver::insert_resolved_name): Assert that name resolution
2.0 is disabled.
(Resolver::lookup_resolved_name): Likewise.
(Resolver::insert_resolved_type): Likewise.
(Resolver::lookup_resolved_type): Likewise.
(Resolver::insert_resolved_label): Likewise.
(Resolver::lookup_resolved_label): Likewise.
(Resolver::insert_resolved_macro): Likewise.
(Resolver::lookup_resolved_macro): Likewise.
(Resolver::insert_resolved_misc): Likewise.
(Resolver::lookup_resolved_misc): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: empty match expressions should resolve to !
Philip Herron [Thu, 21 Nov 2024 17:36:34 +0000 (17:36 +0000)] 
gccrs: empty match expressions should resolve to !

This is a special case in Rust and the ! type can unify with pretty much
anything its almost a inference variable and a unit-type for special cases.

Fixes Rust-GCC/gccrs#3231
Fixes Rust-GCC/gccrs#2567

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (check_match_scrutinee): check for empty match
(CompileExpr::visit): fix assertion
* checks/errors/rust-hir-pattern-analysis.cc (check_match_usefulness): check for empty
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): resolve to !

gcc/testsuite/ChangeLog:

* rust/compile/exhaustiveness1.rs: remove bad check
* rust/compile/issue-2567-1.rs: New test.
* rust/compile/issue-2567-2.rs: New test.
* rust/compile/issue-2567-3.rs: New test.
* rust/compile/issue-3231.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: fix crashes in hir dump since move to references
Philip Herron [Thu, 21 Nov 2024 17:27:31 +0000 (17:27 +0000)] 
gccrs: fix crashes in hir dump since move to references

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::visit): add guards

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
4 months agogccrs: Clean up some system includes
Pierre-Emmanuel Patry [Fri, 22 Nov 2024 11:20:09 +0000 (12:20 +0100)] 
gccrs: Clean up some system includes

System includes shall use rust-system header instead.

gcc/rust/ChangeLog:

* ast/rust-stmt.h: Remove stdlib include and use rust-system instead.
* backend/rust-compile-expr.cc: Likewise.
* backend/rust-mangle-legacy.cc: Likewise.
* backend/rust-mangle-v0.cc: Likewise.
* hir/rust-hir-dump.cc: Likewise.
* typecheck/rust-hir-type-check-type.cc: Likewise.
* typecheck/rust-tyty.cc: Likewise.
* typecheck/rust-tyty.h: Likewise.
* util/rust-common.h: Likewise.
* util/rust-token-converter.cc: Likewise.
* util/rust-token-converter.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
4 months agogccrs: Use nr2.0 in typechecker
Owen Avery [Tue, 19 Nov 2024 20:10:09 +0000 (15:10 -0500)] 
gccrs: Use nr2.0 in typechecker

I probably missed a few spots, but this should cover most of the type
checker.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc: Add includes.
(TypeCheckExpr::visit): Use name resolver 2.0.
(TypeCheckExpr::resolve_operator_overload): Likewise.
(TypeCheckExpr::resolve_fn_trait_call): Likewise.
* typecheck/rust-hir-type-check-path.cc
(TypeCheckExpr::visit): Likewise.
(TypeCheckExpr::resolve_segments): Likewise.
* typecheck/rust-hir-type-check-type.cc
(TypeCheckType::resolve_segments): Likewise.
(ResolveWhereClauseItem::visit): Likewise.
(TypeCheckType::visit): Avoid usage of
Resolver::get_unit_type_node_id when handling TupleType, use
name resolver 2.0 when handling QualifiedPathInType.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: Handle type path segments during late resolution 2.0
Owen Avery [Tue, 19 Nov 2024 21:09:01 +0000 (16:09 -0500)] 
gccrs: Handle type path segments during late resolution 2.0

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Call DefaultResolver::visit when visiting
TypePath.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: Use nr2.0 in PrivacyReporter
Owen Avery [Tue, 19 Nov 2024 18:57:34 +0000 (13:57 -0500)] 
gccrs: Use nr2.0 in PrivacyReporter

gcc/rust/ChangeLog:

* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::check_for_privacy_violation): Use name
resolver 2.0.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
4 months agogccrs: Use a reference wrapper to please GCC 4.8
Pierre-Emmanuel Patry [Wed, 20 Nov 2024 12:35:22 +0000 (13:35 +0100)] 
gccrs: Use a reference wrapper to please GCC 4.8

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): Change call.
(CompileExpr::resolve_operator_overload): Update function arguments.
* backend/rust-compile-expr.h: Change the function's prototype to use
a reference wrapper instead of a reference within the optional.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
4 months agogccrs: Use default constructor for default arguments
Pierre-Emmanuel Patry [Wed, 20 Nov 2024 12:19:15 +0000 (13:19 +0100)] 
gccrs: Use default constructor for default arguments

GCC 4.8 complains about the initializer list.

gcc/rust/ChangeLog:

* typecheck/rust-tyty.h: Change initializer list to default constructor
call.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
4 months agogccrs: Add optional template arguments to please GCC4.8
Pierre-Emmanuel Patry [Wed, 20 Nov 2024 00:44:28 +0000 (01:44 +0100)] 
gccrs: Add optional template arguments to please GCC4.8

Clang on macos as well as GCC 4.8 complains when those templates are
missing.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Add template
to tl::optional.
* hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit): Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeResolveGenericParam::visit):
Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
4 months agogccrs: Fix FnParam pattern location ternary logic
Pierre-Emmanuel Patry [Wed, 20 Nov 2024 00:28:04 +0000 (01:28 +0100)] 
gccrs: Fix FnParam pattern location ternary logic

Condition was inverted, we should retrieve the locus only if we have a
pattern.

gcc/rust/ChangeLog:

* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Do not
get a reference if the pattern does not exist.
(TypeCheckMethodCallExpr::check): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
4 months agogccrs: Refactor optional initializers
Pierre-Emmanuel Patry [Tue, 19 Nov 2024 16:24:59 +0000 (17:24 +0100)] 
gccrs: Refactor optional initializers

Refactor some optional initializer in the lowering stage to make them
more readable.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Change the
ternary expression with a more readable if.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>