Owen Avery [Wed, 19 Feb 2025 02:20:45 +0000 (21:20 -0500)]
gccrs: nr2.0: Implement macro_use for modules
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver-2.0.cc:
Include rust-attribute-values.h.
(Early::visit): If a module has a macro_use attribute, avoid
pushing a new textual macro scope.
Arthur Cohen [Fri, 20 Dec 2024 18:00:48 +0000 (18:00 +0000)]
gccrs: hir-dump: Fix more segfaults in the HIR dump
gcc/rust/ChangeLog:
* hir/rust-hir-dump.cc: Check unique_ptr members are present before
visiting them.
* hir/tree/rust-hir-path.h: Add `has_{type, trait}` methods to
QualifiedPathInType.
Arthur Cohen [Thu, 20 Feb 2025 17:01:28 +0000 (17:01 +0000)]
gccrs: derive(Hash): Add implementation.
gcc/rust/ChangeLog:
* Make-lang.in: Compile it.
* expand/rust-derive.cc (DeriveVisitor::derive): Call it.
* expand/rust-derive-hash.cc: New file.
* expand/rust-derive-hash.h: New file.
gcc/testsuite/ChangeLog:
* rust/compile/derive-hash1.rs: New test.
* rust/compile/nr2/exclude: Add testcase to exclusion list.
Owen Avery [Sat, 22 Feb 2025 00:07:59 +0000 (19:07 -0500)]
gccrs: Fix expansion of macros inside modules
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc
(ExpandVisitor::visit): Override DefaultASTVisitor in order to
expand a module's items, rather than directly visit them.
* expand/rust-expand-visitor.h
(ExpandVisitor::visit): Add override.
gcc/testsuite/ChangeLog:
* rust/compile/macros/mbe/macro-expand-module.rs: New test.
Arthur Cohen [Tue, 4 Feb 2025 08:58:14 +0000 (09:58 +0100)]
gccrs: expand: Allow built-in derives to generate multiple items
gcc/rust/ChangeLog:
* expand/rust-derive.cc (DeriveVisitor::derive): Return a vector of items.
* expand/rust-derive.h: Change return type.
* expand/rust-expand-visitor.cc: Insert all generated items into the AST.
We are still missing some deriving for enums, as part of our codegen and nameres for rebinding struct
field patterns is missing.
gcc/rust/ChangeLog:
* expand/rust-derive-partial-eq.cc: New file.
* expand/rust-derive-partial-eq.h: New file.
* expand/rust-derive.cc (DeriveVisitor::derive): Call them.
* Make-lang.in: Compile them.
gcc/testsuite/ChangeLog:
* rust/compile/derive-eq-invalid.rs: Mark PartialEq def as a lang item.
* rust/compile/derive-partialeq1.rs: New test.
* rust/execute/torture/derive-partialeq1.rs: New test.
* rust/compile/nr2/exclude: Exclude all of them.
Arthur Cohen [Thu, 30 Jan 2025 13:56:37 +0000 (14:56 +0100)]
gccrs: ast-builder: Improve function generation.
gcc/rust/ChangeLog:
* ast/rust-ast-builder.cc (Builder::block): Change return type.
(Builder::loop): Use new APIs.
* ast/rust-ast-builder.h: Change return type of block functions.
Owen Avery [Tue, 4 Feb 2025 10:53:10 +0000 (05:53 -0500)]
gccrs: nr2.0: Run DefaultResolver::visit on PathInExpression
gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): When visiting a PathInExpression instance, call
into DefaultResolver::visit, ensuring generic arguments are
visited.
Owen Avery [Tue, 18 Feb 2025 07:03:56 +0000 (02:03 -0500)]
gccrs: nr2.0: Fix issue with external crates
gcc/rust/ChangeLog:
* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): When visiting an external crate declaration,
handle failed crate name lookups. This can happen when
Session::load_extern_crate fails to load a crate during the
CfgStrip phase.
Owen Avery [Tue, 4 Feb 2025 10:02:38 +0000 (05:02 -0500)]
gccrs: nr2.0: Resolve paths which start with Self
gcc/rust/ChangeLog:
* resolve/rust-forever-stack.hxx
(ForeverStack::find_starting_point): Be more careful about
applying ForeverStack::find_closest_module.
(ForeverStack::resolve_segments): Allow traversal into parent
nodes when not in a module node or root node, which
ForeverStack::find_starting_point previously made moot through
use of ForeverStack::find_closest_module. Also, when a child
node lookup fails when resolving in the type namespace, attempt
a rib lookup as a fallback.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Avoid throwing a resolution error for type paths
when the typechecker may be able to finish the resolution. Also,
throw an error when a resolution is ambiguous.
Philip Herron [Sat, 15 Feb 2025 21:22:16 +0000 (21:22 +0000)]
gccrs: Add name resolution and HIR lowering for ImplTraitType's
Our AST has ImplTraitType for multiple bounds and a singular
ImplTraitTypeOneBound, this patch desugars these into a simple
HIR::ImplTraitType. It also does the name resolution for this.
Philip Herron [Thu, 13 Feb 2025 17:45:09 +0000 (17:45 +0000)]
gccrs: self paths are patterns but we dont have mappings for it
With simple patterns we dont introduce any more inference varaibles as
they are already declared in a specific way. This would only lead to
more unconstrained inference varaibles than is required.
Philip Herron [Thu, 13 Feb 2025 17:01:31 +0000 (17:01 +0000)]
gccrs: fix name resolution for generics where type param is declared later
Rust allows you to use generics within type bounds when they are declared
later on. This changes the name resolution to walk the genric params
in two passes to ensure the type parameters are setup before drilling down
into the type parameters
This issue has exposed another type checking issue which needs fixed in
a subseqent patch.
Addresses Rust-GCC#3022
gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): use new api
(ResolveItem::visit): likewise
(ResolveExternItem::visit): likewise
* resolve/rust-ast-resolve-stmt.h: likewise
* resolve/rust-ast-resolve-type.h (class ResolveGenericParam): remove
(class ResolveGenericParams): added new api
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Wed, 12 Feb 2025 16:44:55 +0000 (16:44 +0000)]
gccrs: remove name resolution inserts from type-path
We resolve path segments and inserted their resolution into the name
resolution space which was an old hack to use this as information in
code-gen/check-passes in order to help things move forward but this is
not nessecary
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): remove name resolution info
(TypeCheckType::resolve_root_path): likewise
* typecheck/rust-hir-type-check-type.h: likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 10 Feb 2025 16:28:30 +0000 (16:28 +0000)]
gccrs: add discriminant value intrinsic
This is pretty nasty piece of rust in my opinion the return type of this
intrinsic results into a lang item associated type:
<T as DiscriminantKind>::Discriminant
This is a special case which needs to support mapping onto the repr type
of the associated ADT that is passed in, but defaults to iszie otherwise.
This patch assumes we only come accross this case in a HIR::CALL_EXPR, so
and makes assumutions that its always of this function signiture. I will
do some checking in libcore to verify that assumption. More work is needed
to parse the repr type on enums but the code is there to support this when
its in to change the types etc.
Addresses Rust-GCC#3348
gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc (discriminant_value_handler): new handler
* typecheck/rust-hir-trait-resolve.cc (TraitItemReference::resolve_item): track the defid
* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options): default isize
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): special case CallExpr
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): parse repr options enum
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): remove bad diagnostic
* typecheck/rust-tyty.cc (PlaceholderType::PlaceholderType): track defid
(PlaceholderType::clone): likewise
(PlaceholderType::get_def_id): likeiwse
* typecheck/rust-tyty.h: placeholder track defid
* util/rust-lang-item.cc: add new lang items
* util/rust-lang-item.h: likewise
gcc/testsuite/ChangeLog:
* rust/execute/torture/enum_intrinsics1.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Tue, 4 Feb 2025 17:15:15 +0000 (17:15 +0000)]
gccrs: remove bad type checking diagnostic
This was a bad diagnostic added when I was working on slices and iterators
also the name of the function is also bad. This is all about setting up
associated types based on the current context of the bounds assocated with
the associated trait bounds on this function.
The tell tale is that this didnt have an associated rust error code so
this is most definetly not the correct error diagnostic.
Arthur Cohen [Sun, 22 Dec 2024 15:59:27 +0000 (15:59 +0000)]
gccrs: ast: Add DesugarForLoop class
gcc/rust/ChangeLog:
* ast/rust-desugar-for-loops.cc: New file.
* ast/rust-desugar-for-loops.h: New file.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Make lowering of for-loops an
unreachable.
* Make-lang.in: Compile it.
gcc/testsuite/ChangeLog:
* rust/compile/for-loop1.rs: New test.
* rust/compile/for-loop2.rs: New test.
* rust/execute/torture/for-loop1.rs: New test.
* rust/execute/torture/for-loop2.rs: New test.
* rust/compile/nr2/exclude: Exclude for-loop1.rs
Owen Avery [Thu, 23 Jan 2025 01:12:46 +0000 (20:12 -0500)]
gccrs: nr2.0: Resolve instances of TypePath more accurately
gcc/rust/ChangeLog:
* util/rust-unwrap-segment.cc: New file.
* util/rust-unwrap-segment.h: New file.
* Make-lang.in: Add rust-unwrap-segment.o to the object list.
* resolve/rust-forever-stack.hxx: Include rust-unwrap-segment.h.
(ForeverStack::find_starting_point): Use unwrap_type_segment.
(ForeverStack::resolve_segments): Likewise.
(ForeverStack::resolve_path): Likewise.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Resolve type paths using
NameResolutionContext::resolve_path.
* resolve/rust-name-resolution-context.h
(NameResolutionContext::resolve_path): Use
unwrap_segment_node_id.
Arthur Cohen [Wed, 29 Jan 2025 14:41:19 +0000 (14:41 +0000)]
gccrs: inline-asm: Fix some warnings
gcc/rust/ChangeLog:
* expand/rust-macro-builtins-asm.cc (strip_double_quotes): Special case empty
strings ("\"\"").
(parse_reg_operand): Remove use of the `struct` keyword.
(parse_reg_operand_in): Likewise.
(parse_reg_operand_out): Likewise.
* expand/rust-macro-builtins.cc: Add llvm_asm! built-in macro as an alias to asm!.
Philip Herron [Mon, 3 Feb 2025 16:19:30 +0000 (16:19 +0000)]
gccrs: Fix compilation of trait-items which map to impl items
When we have paths such as Try::from_error the Try references the Trait
and then from_error references the from_error trait item. So this resolves
directly to a trait implementation which has the type:
fn <Self> (v: placeholder) -> Self
Remember that a path such as: Try::from_error gets handled by doing a
clever substitution: <T? as Try>::from_error
The main piece here is that we resolve this function type and for
monomoprhization we know this is a trait call but we know that all trait's
have an implicit Self type param which in this case is Result<i32, i32>
so when it comes to knowing which impl block this is we got rid of the old
bad insert/lookup receiver hack and use the specified type to know which
impl block we are talking about to generate the function.
The hard part here is inside rust-compil-item.cc, where we have the
'concete' type which is the trait item fntype of:
fn <Result<i32, i32>> (v : i32) -> Result<i32,i32>
This does not really match the signiture of the impl item for Result
which is:
fn <T, E> (v: i32) -> Result<T, E>
So what we need to do is actually infer this by inputing inference
variables on the impl fntype then unify'ing the trait object to this to
compute the types of this to monomorphize this.
Philip Herron [Mon, 3 Feb 2025 15:25:50 +0000 (15:25 +0000)]
gccrs: Fix bad generic substitution error on fn/adt types
When passing generics around we try to adjust them because there are cases
where the names are adjusted from other generics this can fail for traits
because of the implicit Self and we just need to continue on without
adjustment.
Fxies Rust-GCC#3382
gcc/rust/ChangeLog:
* typecheck/rust-substitution-mapper.cc (SubstMapperInternal::visit):
continue on for trait item mode.
gcc/testsuite/ChangeLog:
* rust/compile/nr2/exclude: nr2 cant handle this.
* rust/compile/issue-3382.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 3 Feb 2025 15:14:48 +0000 (15:14 +0000)]
gccrs: Fix crash in privay reporter for placeholder types
This guards against a crash but i think this should actually be treated
as if its a generic type like below. But for now this addresses a crash which can occur.
gcc/rust/ChangeLog:
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_base_type_privacy):
Add guard for placeholder
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Owen Avery [Sat, 4 Jan 2025 19:59:54 +0000 (14:59 -0500)]
gccrs: Fix bug in type resolution of paths
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver-2.0.cc
(Early::resolve_glob_import): Use
NameResolutionContext::resolve_path instead of
ForeverStack::resolve_path.
(Early::visit): Likewise.
(Early::visit_attributes): Likewise.
* resolve/rust-early-name-resolver-2.0.h
(Early::resolve_path_in_all_ns): Likewise.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Likewise, insert segment resolutions not
handled by NameResolutionContext::resolve_path, and avoid throwing
an error when path resolution could be finished by the typechecker.
* resolve/rust-name-resolution-context.h
(NameResolutionContext::resolve_path): Add.
* typecheck/rust-hir-type-check-path.cc
(TypeCheckExpr::resolve_root_path): Use segment node ids instead
of the path node id to look up segment resolutions when using
the 2.0 resolver, as is done with the 1.0 resolver.
* typecheck/rust-hir-type-check-type.cc
(TypeCheckType::resolve_root_path): Likewise.
* resolve/rust-forever-stack.h
(ForeverStack::resolve_path): Add callback parameter for
inserting segment resolutions.
(ForeverStack::find_starting_point): Likewise.
(ForeverStack::resolve_segments): Likewise.
* resolve/rust-forever-stack.hxx
(ForeverStack::find_starting_point): Likewise.
(ForeverStack::resolve_segments): Likewise.
(ForeverStack::resolve_path): Likewise and avoid resolving
inside TraitOrImpl ribs.
Liam Naddell [Fri, 31 Jan 2025 01:51:21 +0000 (20:51 -0500)]
gccrs: Add option_env! support
gcc/rust/ChangeLog:
* expand/rust-macro-builtins-utility.cc: Add macro expansion for
option_env with eager expansion
* expand/rust-macro-builtins.cc: Add option_env to builtin list
* expand/rust-macro-builtins.h: Add option_env handler to header
file
* resolve/rust-late-name-resolver-2.0.cc: Prevent NR2.0 from
recursing into lang-item segments
gcc/testsuite/ChangeLog:
* rust/compile/macros/builtin/option_env1.rs: Add success case for option_env
* rust/compile/macros/builtin/option_env2.rs: Add failure case for option_env
* rust/execute/torture/builtin_macro_option_env.rs: Add
execution case for option_env
Query mode was a hack to catch up some compile errors early, it was
deemed to be removed at some time. Recent changes to NR1 highlighted
an incompatibility with it hence it's removal.
Arthur Cohen [Mon, 16 Dec 2024 12:01:13 +0000 (13:01 +0100)]
gccrs: hir: Add LangItem paths to PathPattern class
gcc/rust/ChangeLog:
* hir/tree/rust-hir-path.h: Adapt PathPattern to accept lang-item paths.
* hir/tree/rust-hir-path.cc: Assert we are dealing with a segmented path, create lang-item
constructors.
* hir/tree/rust-hir.cc (PathPattern::convert_to_simple_path): Likewise.
Dylan Gardner [Thu, 29 Aug 2024 11:43:42 +0000 (04:43 -0700)]
gccrs: Infer crate name after file opening
Fixes #3129.
gcc/rust/ChangeLog:
* rust-session-manager.cc (Session::handle_crate_name): Remove
crate name inference
(Session::compile_crate): Add crate name inference and error if
inferred name is empty. Remove CompileOptions::get_instance ()
that returned a local copy of the options. Rename
crate_name_changed to crate_name_found to match semantics.
(rust_crate_name_validation_test): Test inferring ".rs" name
* rust-session-manager.h: Modify handle_crate_name definition to
include filename.
gccrs: Add captures for ClosureExprInnerTyped with nr2
Captures were only processed for regular ClosureExprInner.
gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Add
ClosureExprInnerTyped visit implementation.
(add_captures): Add a function to avoid code duplication.
* resolve/rust-late-name-resolver-2.0.h: Add function prototype.
The compiler was still relying on NR1 for closure captures when using nr2
even though the resolver was not used and thus it's state empty.
gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Add environment
collection.
* resolve/rust-late-name-resolver-2.0.h: Add function prototype.
* resolve/rust-name-resolver.cc (Resolver::get_captures): Add assertion
to prevent NR2 usage with nr1 capture functions.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Use
nr2 captures.
* util/rust-hir-map.cc (Mappings::add_capture): Add function to
register capture for a given closure.
(Mappings::lookup_captures): Add a function to lookup all captures
available for a given closure.
* util/rust-hir-map.h: Add function prototypes.
Owen Avery [Tue, 21 Jan 2025 22:02:35 +0000 (17:02 -0500)]
gccrs: Check for type paths nr2.0 can't handle yet
Some of our tests only work with name resolution 2.0 because the latter
misinterprets type paths. This change should cause the compiler to error out
if it would otherwise misinterpret a type path. A fix for type path
resolution isn't included in this comment, since doing so would make it
harder to track the meaningfulness of test regressions.
gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Error out if a type path has multiple segments,
as we currently ignore every segment except the last.
Nr2 did not emit the correct error message for break identifier "rust".
gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Add "rust"
identifier detection akin to nr1.
(funny_ice_finalizer): Copy ICE finalizer from nr1.
* resolve/rust-late-name-resolver-2.0.h: Add funny_error member
context state.
* Make-lang.in: Add new translation unit for new ice finalizer.
* resolve/rust-ast-resolve-expr.cc: Move ice
finalizer to it's own file.
* resolve/rust-ice-finalizer.cc: New file.
* resolve/rust-ice-finalizer.h: New file.
gcc/testsuite/ChangeLog:
* rust/compile/nr2/exclude: Remove break-rust3.rs from exclude list.