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.
Owen Avery [Fri, 9 May 2025 00:44:47 +0000 (20:44 -0400)]
gccrs: nr2.0: Improve visibility path handling
gcc/rust/ChangeLog:
* resolve/rust-forever-stack.h
(enum ResolutionMode): New.
(ForeverStack::get): Add a private overload that takes a
starting node as a parameter.
(ForeverStack::resolve_path): Replace boolean parameter
has_opening_scope_resolution with ResolutionMode parameter mode.
* resolve/rust-forever-stack.hxx
(ForeverStack::resolve_path): Likewise.
(ForeverStack::get): Add a private overload that takes a
starting node as a parameter.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Add Visibility visitor.
* resolve/rust-late-name-resolver-2.0.h
(Late::visit): Likewise.
* resolve/rust-name-resolution-context.h
(NameResolutionContext::resolve_path): Rework overloading a bit
and accept ResolutionMode parameter.
Owen Avery [Wed, 4 Jun 2025 05:06:06 +0000 (01:06 -0400)]
gccrs: Make ConstantItem use Identifier
The change to ASTLoweringExternItem is necessary, since with this patch
Identifier can be implicitly converted to std::string.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::visit): Handle
changed type of ConstantItem::identifier.
* ast/rust-ast.cc (ConstantItem::as_string): Likewise.
* ast/rust-ast.h (operator const std::string &): New member
function.
* ast/rust-item.h (ConstantItem::identifier): Change type from
std::string to Identifier.
(ConstantItem::ConstantItem): Handle changed type of identifier
field.
(ConstantItem::is_unnamed): Likewise.
(ConstantItem::get_identifier): Likewise.
* hir/rust-ast-lower-extern.h (ASTLoweringExternItem::visit):
Avoid discarding location of wildcard patterns.
* lex/rust-token.cc: Include "rust-ast.h".
(Token::make_identifier): Add overload accepting an Identifier
instance.
* lex/rust-token.h (class Identifier): Add forward declaration
in order to...
(Token::make_identifier): ...declare an overload for this static
member function.
Philip Herron [Tue, 20 May 2025 17:44:57 +0000 (18:44 +0100)]
gccrs: Always emit the error highest in the type hierarchy
The unify code was a bit dumb here where we always set emit_error to false for any
subtypes for example in comparing tuples we always emitted the whole tuple didnt
match the other tuple but really its much better to say expected bool got i32 because
the element types didn't match.
gcc/rust/ChangeLog:
* typecheck/rust-coercion.cc (TypeCoercionRules::coerce_unsized): dont emit error here
* typecheck/rust-unify.cc (UnifyRules::resolve_subtype): new helper to handle emit error
(UnifyRules::expect_adt): call resolve_subtype
(UnifyRules::expect_reference): likewise
(UnifyRules::expect_pointer): likewise
(UnifyRules::expect_array): likewise
(UnifyRules::expect_slice): likewise
(UnifyRules::expect_fndef): likewise
(UnifyRules::expect_fnptr): likewise
(UnifyRules::expect_tuple): likewise
(UnifyRules::expect_closure): likewise
(UnifyRules::expect_opaque): likeiwse
* typecheck/rust-unify.h: add new helper to header
gcc/testsuite/ChangeLog:
* rust/compile/traits9.rs: update errors
* rust/compile/unify-errors1.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Owen Avery [Thu, 15 May 2025 00:03:01 +0000 (20:03 -0400)]
gccrs: nr2.0: Fix IfLet pattern handling
gcc/rust/ChangeLog:
* resolve/rust-default-resolver.cc
(DefaultResolver::visit_if_let_patterns): New function
definition.
(DefaultResolver::visit): New IfLetExpr visitor definition.
* resolve/rust-default-resolver.h
(DefaultResolver::visit_if_let_patterns): New function
declaration.
(DefaultResolver::visit): New IfLetExpr visitor declaration.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Remove
IfLetExpr visitor definition.
(Late::visit_if_let_patterns): New function definition.
* resolve/rust-late-name-resolver-2.0.h (Late::visit): Remove
IfLetExpr visitor declaration.
(Late::visit_if_let_patterns): New function declaration.
* resolve/rust-name-resolution-context.h (BindingSource::IfLet):
New enumerator.
Philip Herron [Mon, 19 May 2025 17:02:13 +0000 (18:02 +0100)]
gccrs: Initial support for Return Position Impl Trait
This is the initial patch for RPIT, we can build on this to handle the
more complex cases but there are enough distinct changes going on here
that it should just get merged now.
RPIT is really a sneaky generic so for example:
fn foo() -> impl Bar {
Baz
}
This is represented as: fn () -> OpaqueType Bar. But when we handle the
coercion site for Baz on impl Bar when we type resolve the function we
know that the underlying type is Baz. Note this function is _not_ generic
so its using this special OpaqueType and keeping track of the underlying type
in its ty_ref reference hir-id which will resolve to Baz.
This also means if we have a case where maybe this was in an if statement:
fn foo(a: i32) -> impl Bar {
if a > 10 {
Baz
} else {
Qux
}
}
The rules of impl Bar is that Baz is handled but Baz and Qux are different
underlying types so this is not allowed. The reason is impl traits are not
generic and although from a programmer perspective the callers dont know what
the underlying type is, the compiler _knows_ what it is. So really when
you call a function and get its return position impl trait the compiler knows
what to do and does all whats nessecary to handle calling functions using that
type etc.
gcc/rust/ChangeLog:
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): we need to resolve the
underlying type
* typecheck/rust-substitution-mapper.cc (SubstMapperInternal::visit): just clone
* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit):
ensure we monomphize to get the underlying
* typecheck/rust-tyty.cc (BaseType::destructure): handle opaque types
(OpaqueType::resolve): this is much simpler now
(OpaqueType::handle_substitions): no longer needed
* typecheck/rust-tyty.h: update header
* typecheck/rust-unify.cc (UnifyRules::expect_opaque): unify rules for opaque
gcc/testsuite/ChangeLog:
* rust/compile/bad-rpit1.rs: New test.
* rust/execute/torture/impl_rpit1.rs: New test.
* rust/execute/torture/impl_rpit2.rs: New test.
* rust/execute/torture/impl_rpit3.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 26 May 2025 18:30:45 +0000 (19:30 +0100)]
gccrs: Fix non canonical type bug with tuples
When working on rpit we needed to change to use a monomorphized clone of
the result of function calls. This ended up causing a verify gimple issue
with tuples because:
fn test<A, B>(a: A, b: B) -> (A, B)
When passing for example:
let a = test::<i32, i32> (123, 456) -> (A=i32, B=i32)
The resulting gimple types became:
const struct (A=i32, B=i32) vs struct (i32, i32)
We removed the VIEW_CONVERT_EXPR support to auto fix this stuff a good
while ago because it hides these kinds of issues because the type hasher
included the A=i32, B=i32 vs the i32, i32 name so updating this to use
get_name instead keeps the naming the same as well as the fields meaning
these types are 100% equivilant and therefore no conversion is required.
This only occurs because tuples are not named types we should really add
more rust specific info on our gimple TYPES.
gcc/rust/ChangeLog:
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): use get_name
* typecheck/rust-tyty.cc (TupleType::get_name): likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 19 May 2025 16:42:17 +0000 (17:42 +0100)]
gccrs: Remove unneeded clones untill we have an arena allocator for these tmps
Cloning inference variables is very expensive because it means we are indirectly
creating an implicit new inference variable added to the reference chain.
gcc/rust/ChangeLog:
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_base_type_privacy):
no need for unreachable here
* typecheck/rust-unify.cc (UnifyRules::commit): dont clone infer vars
(UnifyRules::expect_inference_variable): likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 19 May 2025 16:55:53 +0000 (17:55 +0100)]
gccrs: refactor default infer vars to be its own function
This is just a simple refactor to pull all the logic outside of the
closure which makes it more readable.
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check.h: new function
* typecheck/rust-typecheck-context.cc (TypeCheckContext::compute_inference_variables):
call the new helper
(TypeCheckContext::compute_infer_var): refactored code
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Undefined attribute macros have no proc macro definition, which results
in a failing `rust_assert`. This changes that assert to an if statement,
that returns early if there is no proc macro definition. Fixes #3661.
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver-2.0.cc (Early::visit_attributes): rust_assert to if
gcc/testsuite/ChangeLog:
* rust/compile/issue-3661.rs: Test NR2 has expected behavior
Signed-off-by: Tom Schollenberger <tss2344@g.rit.edu>
Owen Avery [Thu, 8 May 2025 21:32:05 +0000 (17:32 -0400)]
gccrs: Small improvements to DefaultASTVisitor and nr2.0
gcc/rust/ChangeLog:
* ast/rust-ast-visitor.cc
(DefaultASTVisitor::visit): Only visit the path of an instance
of Visibility if the instance has a path.
* ast/rust-ast.h
(SimplePath::SimplePath): Make sure constructors are explicit.
* resolve/rust-early-name-resolver-2.0.cc
(Early::visit_attributes): Pass entire paths to
NameResolutionContext::resolve_path.
(Early::visit): Likewise and avoid copying a path.
* resolve/rust-forever-stack.hxx
(ForeverStack::resolve_path): Assert that at least one path
segment has been passed in.
Owen Avery [Sat, 10 May 2025 02:17:55 +0000 (22:17 -0400)]
gccrs: Fix Attr metavariable binding
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h
(Parser::parse_attr_input): Handle more delimeter tokens and the
END_OF_FILE token.
(Parser::skip_after_end_attribute): Handle the END_OF_FILE
token.
gcc/testsuite/ChangeLog:
* rust/compile/macros/mbe/meta-param.rs: New test.
Owen Avery [Fri, 9 May 2025 22:02:29 +0000 (18:02 -0400)]
gccrs: nr2.0: Fix borrow checking
gcc/rust/ChangeLog:
* checks/errors/borrowck/rust-bir-builder-internal.h: Include
"rust-immutable-name-resolution-context.h" and "options.h".
(AbstractBuilder::resolve_label): Use the 2.0 name resolver when
it's enabled.
(AbstractBuilder::resolve_variable): Likewise.
(AbstractBuilder::resolve_variable_or_fn): Likewise.
Philip Herron [Wed, 7 May 2025 15:07:43 +0000 (16:07 +0100)]
gccrs: Prevent passing generic arguments to impl traits in argument position
When using impl traits in argument position (APIT), they are desugared into generics,
and supplying explicit generic arguments is not allowed. This commit adds the error
diagnostic E0632 for attempting to pass generic arguments to impl traits, completing
the implementation of the APIT feature.
gcc/rust/ChangeLog:
* ast/rust-desugar-apit.cc: track if this is a impl-trait generic
* ast/rust-item.h (class TypeParam): add field to track if from impl trait
* hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit): likewise
* hir/tree/rust-hir-item.cc (TypeParam::TypeParam): upate hir as well
(TypeParam::operator=): likewise
* hir/tree/rust-hir-item.h (class TypeParam): likewise
* typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::get_generic_param): add error
* typecheck/rust-tyty-subst.h: add const getter for the associated TypeParm
gcc/testsuite/ChangeLog:
* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/impl_trait_generic_arg.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Owen Avery [Thu, 1 May 2025 01:54:53 +0000 (21:54 -0400)]
gccrs: Improve struct pattern compilation
gcc/rust/ChangeLog:
* backend/rust-compile-pattern.cc
(CompilePatternCheckExpr::visit): Fix GENERIC generation in
light of enum layout changes since this code was written.
(CompilePatternBindings::handle_struct_pattern_ident_pat):
Delegate handling of child patterns to another
CompilePatternBindings::Compile call.
(CompilePatternBindings::make_struct_access): Make field name
parameter const qualified.
* backend/rust-compile-pattern.h
(CompilePatternBindings::make_struct_access): Likewise.
gcc/testsuite/ChangeLog:
* rust/execute/torture/struct-pattern-match.rs: New test.
Owen Avery [Sat, 3 May 2025 00:28:15 +0000 (20:28 -0400)]
gccrs: Improve canonical path handling for impl items
gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-item.cc
(ResolveItem::visit): Use the return values of
CanonicalPath::inherent_impl_seg and
CanonicalPath::trait_impl_projection_seg more directly.
* util/rust-canonical-path.h
(CanonicalPath::trait_impl_projection_seg): Append "<impl "
instead of "<" to the beginning of the returned path segment.
(CanonicalPath::inherent_impl_seg): Likewise.
* Make-lang.in: new desugar file
* ast/rust-ast.cc (ImplTraitTypeOneBound::as_string): its a unique_ptr now
(FormatArgs::set_outer_attrs): reformat
* ast/rust-path.h: remove has_generic_args assertion (can be empty because of desugar)
* ast/rust-type.h (class ImplTraitTypeOneBound): add copy ctor and use unique_ptr
* hir/rust-ast-lower-type.cc (ASTLoweringType::visit): update to use unique_ptr
* parse/rust-parse-impl.h (Parser::parse_type): reuse the existing unique_ptr instead
(Parser::parse_type_no_bounds): likewise
(Parser::parse_pattern): likewise
* resolve/rust-ast-resolve-type.cc (ResolveType::visit): its a unique_ptr now
* rust-session-manager.cc (Session::compile_crate): call desugar
* ast/rust-desugar-apit.cc: New file.
* ast/rust-desugar-apit.h: New file.
gcc/testsuite/ChangeLog:
* rust/compile/issue-2015.rs: fully supported now
* rust/compile/nr2/exclude: nr2 cant handle some of these
* rust/compile/issue-1487.rs: New test.
* rust/compile/issue-3454.rs: New test.
* rust/execute/torture/impl_desugar-2.rs: New test.
* rust/execute/torture/impl_desugar.rs: New test.
* rust/execute/torture/impl_trait1.rs: New test.
* rust/execute/torture/impl_trait2.rs: New test.
* rust/execute/torture/impl_trait3.rs: New test.
* rust/execute/torture/impl_trait4.rs: New test.
* rust/execute/torture/issue-1482.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 5 May 2025 20:07:20 +0000 (21:07 +0100)]
gccrs: Emit error diagnostic for bad impl type usage
Rust only allows impl traits to be used in the return position of
functions.
Fixes Rust-GCC#1485
gcc/rust/ChangeLog:
* hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::visit): allow impl type
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): likewise
* hir/rust-ast-lower-type.cc (ASTLoweringType::ASTLoweringType): new flag for impl trait
(ASTLoweringType::translate): pass flag
(ASTLoweringType::visit): track impl trait tag
(ASTLoweringType::emit_impl_trait_error): new diagnostic
* hir/rust-ast-lower-type.h: add new field
gcc/testsuite/ChangeLog:
* rust/compile/impl_trait_diag.rs: New test.
* rust/compile/issue-1485.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Mikael Morin [Sun, 3 Aug 2025 13:21:08 +0000 (15:21 +0200)]
fortran: Use array descriptor offset setter when possible
Regression-tested on x86_64-pc-linux-gnu.
OK for master?
-- >8 --
In some places, a write to an array descriptor offset field was
generated simply by adding a modification of a reference to it that
was already available. This change uses the existing setter
function instead in those places, to generate the same code. It
makes it more explicit that in those areas a write to the field is
generated.
gcc/fortran/ChangeLog:
* trans-array.cc (gfc_alloc_allocatable_for_assignment): Use the
offset setter instead of generating a write to the offset.
(gfc_conv_array_parameter): Use the offset setter instead of
generating a write to the value returned by the offset getter.
* trans-expr.cc (gfc_trans_alloc_subarray_assign): Likewise.
Mikael Morin [Sun, 3 Aug 2025 13:21:07 +0000 (15:21 +0200)]
fortran: Remove array descriptor data address accessor function
Regression-tested on x86_64-pc-linux-gnu.
OK for master?
-- >8 --
The function gfc_conv_descriptor_data_addr generates an address to the
data field of an array descriptor. It is only used once, and in the
single place where it is used, the address is immediately dereferenced.
This change replaces the single usage with a plain access to the data
field, and removes the function. As the previous patch removed the
usage of the data field to write to it, the data getter can be used.
Mikael Morin [Sun, 3 Aug 2025 13:21:06 +0000 (15:21 +0200)]
fortran: Use array descriptor data setter when possible
Regression-tested on x86_64-pc-linux-gnu.
OK for master?
-- >8 --
In some places, a write to an array descriptor data field was generated
simply by adding a modification of a reference to it that was already
available. This change uses the existing setter function instead in
those places, to generate the same code. It makes it more explicit that
in those areas a write to the field is generated.
We have to be careful because in some of the places, the pointer that is
to be modified is an array descriptor field only under some conditions.
Add a conditional in those places to separate the cases, and keep
generating the reference modification as before if the conditions are
not met.
gcc/fortran/ChangeLog:
* trans.cc (gfc_finalize_tree_expr): Use the data setter instead
of writing to the value returned by the data getter.
* trans-decl.cc (gfc_trans_deferred_vars): Likewise.
* trans-stmt.cc (trans_associate_var): Use the data setter
instead of writing to the value dereferenced from the data
address.
to match an extraction of a high register such as AH from AX/EAX/RAX.
This construct is used in contexts where only the low 8 bits of the
value matter. This is either done explicitly using a (subreg:QI ... 0)
or implicitly by assigning to an 8-bit zero_extract destination.
extract_operator therefore matches both sign_extract and zero_extract,
since the signedness of the extension beyond 8 bits is irrelevant.
But the fact that only the low 8 bits of the value are significant
means that a shift right by 8 is as good as an extraction. Shifts
right would already be used for things like:
struct s {
long a:8;
long b:8;
long c:48;
};
struct s f(struct s x, long y, long z) {
x.b = (y & z) >> 8;
return x;
}
This patch therefore replaces extract_operator with a new predicate
called extract_high_operator that matches both extractions and shifts.
The predicate checks the extraction field and shift amount itself,
so that patterns only need to match the first operand.
Splitters used match_op_dup to preserve the choice of extraction.
But the fact that the extractions (and now shifts) are equivalent
means that we can just as easily canonicalise on one of them.
(In theory, canonicalisation would also promote CSE, although
that's unlikely in practice.) The patch goes for zero_extract,
for consistency with destinations.
gcc/
PR target/121306
* config/i386/predicates.md (extract_operator): Replace with...
(extract_high_operator): ...this new predicate.
* config/i386/i386.md (*cmpqi_ext<mode>_1, *cmpqi_ext<mode>_2)
(*cmpqi_ext<mode>_3, *cmpqi_ext<mode>_4, *movstrictqi_ext<mode>_1)
(*extzv<mode>, *insvqi_2, *extendqi<SWI24:mode>_ext_1)
(*addqi_ext<mode>_1_slp, *addqi_ext<mode>_1_slp, *addqi_ext<mode>_0)
(*addqi_ext2<mode>_0, *addqi_ext<mode>_1, *<insn>qi_ext<mode>_2)
(*subqi_ext<mode>_1_slp, *subqi_ext<mode>_2_slp, *subqi_ext<mode>_0)
(*subqi_ext2<mode>_0, *subqi_ext<mode>_1, *testqi_ext<mode>_1)
(*testqi_ext<mode>_2, *<code>qi_ext<mode>_1_slp)
(*<code>qi_ext<mode>_2_slp. *<code>qi_ext<mode>_0)
(*<code>qi_ext2<mode>_0, *<code>qi_ext<mode>_1)
(*<code>qi_ext<mode>_1_cc, *<code>qi_ext<mode>_1_cc)
(*<code>qi_ext<mode>_2, *<code>qi_ext<mode>_3, *negqi_ext<mode>_1)
(*one_cmplqi_ext<mode>_1, *ashlqi_ext<mode>_1, *<insn>qi_ext<mode>_1)
(define_peephole2): Replace uses of extract_operator with
extract_high_operator, matching only the first operand.
Use zero_extract rather than match_op_dup when splitting.