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>
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>
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.
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.
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>
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.
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.
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.
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.
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.
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>
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.
Philip Herron [Sat, 21 Jun 2025 14:40:50 +0000 (15:40 +0100)]
gccrs: Refactor marker builtin trait assembly
Rename assemble_sized_builtin to assemble_marker_builtins and reorganize
the type matching to properly handle function pointers and closures with
their associated traits (Fn, FnMut, FnOnce).
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-bounds.h: Rename method.
* typecheck/rust-tyty-bounds.cc: Refactor marker trait assembly
and add proper Fn trait handling for function types.
Philip Herron [Wed, 18 Jun 2025 17:25:14 +0000 (18:25 +0100)]
gccrs: Fix ICE when constant is missing and expression
This is an invalid test case and doesnt work with rustc, we dont fully pick
up the errors. Nr2 does handle this and puts out an extra good diagnostic
but the old NR doesnt so for now i added this to the exclude list and then
when we remove old name resolver this issue goes away.
Owen Avery [Mon, 16 Jun 2025 21:04:22 +0000 (17:04 -0400)]
gccrs: Adjust external crate lowering and type checking
The 2.0 name resolver is provided through
ImmutableNameResolutionContext after it is done being mutated. The
typechecker attempts to use ImmutableNameResolutionContext, so it needs
to be run after ImmutableNameResolutionContext has been initialized
(after all name resolution has been completed). Additionally, although I
haven't seen any issues with lowering AST to HIR before name resolution
2.0 is complete, it makes sense to perform all lowering in lockstep as
well.
gcc/rust/ChangeLog:
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Add
visitor for ExternCrate.
* hir/rust-ast-lower-item.h (ASTLoweringItem::visit): Likewise.
* rust-session-manager.cc (Session::load_extern_crate): Avoid
lowering or type resolving external crates here.
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
Add visitor for ExternCrate.
* typecheck/rust-hir-type-check-item.h (TypeCheckItem::visit):
Replace empty definition with a declaration.
Philip Herron [Tue, 17 Jun 2025 10:32:21 +0000 (11:32 +0100)]
gccrs: Fix issues around PartialEq Eq Ord Partial Ord
There is still an issue with derive on PartialOrd but this adds good tests
and fixes a bug we had handling the default Rhs = Self generic type param
substitutions on the comparison traits.
gcc/rust/ChangeLog:
* typecheck/rust-tyty.cc (ParamType::handle_substitions): make this consistent
gcc/testsuite/ChangeLog:
* rust/compile/bug-with-default-generic.rs: New test.
* rust/execute/torture/partial-eq-1.rs: New test.
* rust/execute/torture/partial-eq-2.rs: New test.
* rust/execute/torture/partial-eq-3.rs: New test.
* rust/execute/torture/partial-eq-4.rs: New test.
* rust/execute/torture/partial-ord-1.rs: New test.
* rust/execute/torture/partial-ord-2.rs: New test.
* rust/execute/torture/partial-ord-3.rs: New test.
* rust/execute/torture/partial-ord-4.rs: New test.
* rust/execute/torture/partial-ord-5.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Zhi Heng [Fri, 13 Jun 2025 14:19:33 +0000 (22:19 +0800)]
gccrs: Implement name resolution for IdentifierPattern's subpattern
gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-pattern.cc: Implement name resolution for
IdentifierPattern's subpattern.
* resolve/rust-late-name-resolver-2.0.cc: Ditto, but for nr2.
Owen Avery [Fri, 9 May 2025 18:37:55 +0000 (14:37 -0400)]
gccrs: nr2.0: Adjust lookup of associated items
gcc/rust/ChangeLog:
* resolve/rust-default-resolver.cc (DefaultResolver::visit):
Adjust scoping of trait definitions and their generic
parameters.
* resolve/rust-forever-stack.hxx (ForeverStack::get): Prevent
lookups inside TraitOrImpl ribs.
(ForeverStack::resolve_segments): Prevent lookups of the first
segment inside TraitOrImpl ribs.
Arthur Cohen [Tue, 27 May 2025 12:23:39 +0000 (14:23 +0200)]
gccrs: derive(Ord): Handle unit structs properly
gcc/rust/ChangeLog:
* expand/rust-derive-ord.cc (DeriveOrd::make_cmp_arms): Use new make_equal function.
(DeriveOrd::make_equal): New function.
(DeriveOrd::recursive_match): Handle the unit struct/tuple case.
* expand/rust-derive-ord.h: Declare make_equal.
Arthur Cohen [Tue, 22 Apr 2025 20:17:18 +0000 (22:17 +0200)]
gccrs: derive-cmp: Add EnumMatchBuilder class
gcc/rust/ChangeLog:
* expand/rust-derive-cmp-common.h (class EnumMatchBuilder): New helper class.
* expand/rust-derive-cmp-common.cc (EnumMatchBuilder::tuple): New function.
(EnumMatchBuilder::strukt): New function.
Arthur Cohen [Tue, 22 Apr 2025 18:03:16 +0000 (20:03 +0200)]
gccrs: derive(Ord): Add handling for ordering of discriminant values
gcc/rust/ChangeLog:
* expand/rust-derive-ord.cc (DeriveOrd::cmp_call): New function.
(DeriveOrd::recursive_match): Use it.
(DeriveOrd::visit_enum): Likewise.
* expand/rust-derive-ord.h: Declare it.
Arthur Cohen [Fri, 18 Apr 2025 16:30:19 +0000 (18:30 +0200)]
gccrs: derive(PartialEq): chore: Refactor using new SelfOther APIs
gcc/rust/ChangeLog:
* expand/rust-derive-cmp-common.cc (SelfOther::indexes): Fix formatting.
(SelfOther::fields): Make iterator const.
* expand/rust-derive-cmp-common.h (struct SelfOther): New declaration for indexes.
* expand/rust-derive-partial-eq.cc (DerivePartialEq::visit_tuple): Use the new API.
(DerivePartialEq::visit_struct): Likewise.
Arthur Cohen [Wed, 9 Apr 2025 09:18:06 +0000 (11:18 +0200)]
gccrs: derive: Add const generics when deriving impls
gcc/rust/ChangeLog:
* ast/rust-ast-builder.cc (Builder::new_const_param): New function.
* ast/rust-ast-builder.h (vec): New function for creating 3 elts vector.
* expand/rust-derive.cc: Use the new_const_param builder.
* ast/rust-path.h: Add get_default_value() method.
Zhi Heng [Sat, 7 Jun 2025 14:24:03 +0000 (22:24 +0800)]
gccrs: Support compilation of IdentifierPattern's subpatterns
gcc/rust/ChangeLog:
* backend/rust-compile-pattern.cc: Add CheckExpr compilation for
IdentifierPattern with subpattern.
* backend/rust-compile-pattern.h: Modify IdentifierPattern's
visit func to support the above.
* typecheck/rust-hir-type-check-pattern.cc: Add typechecking
support for the changes above.
Owen Avery [Thu, 15 May 2025 01:40:04 +0000 (21:40 -0400)]
gccrs: nr2.0: Fix closure parameter scoping
gcc/rust/ChangeLog:
* resolve/rust-default-resolver.cc
(DefaultResolver::visit_closure_params): New member function
definition.
(DefaultResolver::visit): New visiting function definition for
ClosureExpr called from visiting functions for ClosureExprInner
and ClosureExprInnerTyped.
* resolve/rust-default-resolver.h
(DefaultResolver::visit_closure_params): New member function
declaration.
(DefaultResolver::visit): New visiting function declaration for
ClosureExpr.
* resolve/rust-late-name-resolver-2.0.cc (add_captures): Remove
function.
(Late::visit): New visiting function definition for ClosureExpr,
remove visiting function definitions for ClosureExprInner and
ClosureExprInnerTyped.
(Late::visit_closure_params): New member function definition.
* resolve/rust-late-name-resolver-2.0.h (Late::visit): New
visiting function declaration for ClosureExpr, remove visiting
function declarations for ClosureExprInner and
ClosureExprInnerTyped.
(Late::visit_closure_params): New member function declaration.
Owen Avery [Sun, 18 May 2025 02:26:20 +0000 (22:26 -0400)]
gccrs: nr2.0: Adjust pub_restricted tests
gcc/testsuite/ChangeLog:
* rust/compile/nr2/exclude: Remove entries.
* rust/compile/pub_restricted_1.rs: Adjust expected error
messages and only run with name resolution 2.0 enabled.
* rust/compile/pub_restricted_2.rs: Likewise.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Use
visit_identifier_as_pattern to handle IdentifierPattern and
StructPatternFieldIdent.
(visit_identifier_as_pattern): New function.
Owen Avery [Fri, 2 May 2025 22:38:50 +0000 (18:38 -0400)]
gccrs: nr2.0: Separate out canonical path handling
This should improve our canonical path handling, without requiring
further tweaks to ForeverStack. This may also help if, in the future, we
have to move canonical path calculation to later compilation phases for
proper handling of generics.
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc
(HIRCompileBase::compile_function): Since canonical paths
returned from nr2.0 now include the crate name, avoid prepending
the crate name again.
* resolve/rust-default-resolver.cc (DefaultResolver::visit): Add
Crate and EnumItem instance visitors, handle canonical path
context scoping.
* resolve/rust-default-resolver.h (DefaultResolver::visit): Add
Crate and EnumItem instance visitors.
* resolve/rust-early-name-resolver-2.0.cc (Early::go): Visit
instances of Crate using the virtual member function visit.
* resolve/rust-forever-stack.h
(ForeverStack::to_canonical_path): Remove function declaration.
* resolve/rust-forever-stack.hxx
(ForeverStack::to_canonical_path): Remove function definition.
* resolve/rust-late-name-resolver-2.0.cc (Late::go): Visit
instances of Crate using the virtual member function visit.
* resolve/rust-name-resolution-context.cc
(CanonicalPathRecordCrateRoot::as_path): New function definition.
(CanonicalPathRecordNormal::as_path): Likewise.
(CanonicalPathRecordLookup::as_path): Likewise.
(CanonicalPathRecordImpl::as_path): Likewise.
(CanonicalPathRecordTraitImpl::as_path): Likewise.
(NameResolutionContext::NameResolutionContext): Initialize
member variable canonical_ctx.
* resolve/rust-name-resolution-context.h: Include "rust-item.h".
(class NameResolutionContext): Forward declare class.
(class CanonicalPathRecord): New class.
(class CanonicalPathRecordWithParent): Likewise.
(class CanonicalPathRecordCrateRoot): Likewise.
(class CanonicalPathRecordNormal): Likewise.
(class CanonicalPathRecordLookup): Likewise.
(class CanonicalPathRecordImpl): Likewise.
(class CanonicalPathRecordTraitImpl): Likewise.
(class CanonicalPathCtx): Likewise.
(NameResolutionContext::canonical_ctx): New member variable.
(NameResolutionContext::to_canonical_path): New member function.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go):
Visit instances of Crate with the virtual member function visit.
(TopLevel::visit): Handle canonical path context scoping for
external crates, use DefaultResolver::visit when visiting
instances of StructStruct.
* util/rust-canonical-path.h (CanonicalPath::new_seg): Take path
parameter by-value, as a duplicate instance will be constructed
regardless.
Zhi Heng [Sat, 7 Jun 2025 14:30:25 +0000 (22:30 +0800)]
gccrs: Lower IdentifierPattern's to_bind to HIR
gcc/rust/ChangeLog:
* hir/rust-ast-lower-pattern.cc: Lower of IdentifierPattern's to_bind to HIR.
* hir/rust-hir-dump.cc: Update IdentifierPattern's dump to properly show to_bind's full
full properties.
Owen Avery [Fri, 16 May 2025 02:04:34 +0000 (22:04 -0400)]
gccrs: nr2.0: Catch Self in impl block self types
gcc/rust/ChangeLog:
* resolve/rust-default-resolver.cc (DefaultResolver::visit): Use
visit_impl_type to visit the self types of impl blocks.
* resolve/rust-default-resolver.h
(DefaultResolver::visit_impl_type): New member function
declaration.
* resolve/rust-late-name-resolver-2.0.cc (Late::Late):
Initialize member variable block_big_self.
(Late::visit_impl_type): New member function definition.
(Late::visit): Check for Self while inside impl block self
types.
* resolve/rust-late-name-resolver-2.0.h (Late::visit_impl_type):
New member function.
(Late::block_big_self): New member variable.
gcc/testsuite/ChangeLog:
* rust/compile/issue-3671.rs: Remove usage of Self.
* rust/compile/nr2/exclude: Remove issue-3671.rs.
* rust/compile/self-in-impl.rs: New test.