]> gcc.gnu.org Git - gcc.git/log
gcc.git
9 months agogccrs: session: Add tokenstream dump option
Pierre-Emmanuel Patry [Mon, 27 Mar 2023 11:09:34 +0000 (13:09 +0200)]
gccrs: session: Add tokenstream dump option

Add an option to the command line interface to dump the ast back to a
tokenstream.

gcc/rust/ChangeLog:

* rust-session-manager.cc (Session::enable_dump): Enable new
dump.
(Session::compile_crate): CLI argument parsing.
(Session::dump_tokenstream): Dump the tokenstream as a string in
the specified file.
* rust-session-manager.h (struct CompileOptions): Add
tokenstream dump option.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: lex: Add source code token string representation
Pierre-Emmanuel Patry [Mon, 27 Mar 2023 10:54:53 +0000 (12:54 +0200)]
gccrs: lex: Add source code token string representation

Add a new representation for tokens which should reflect the string
token as it could be found in the original source.

gcc/rust/ChangeLog:

* lex/rust-token.cc (Token::as_string): Add as_string
implementation.
* lex/rust-token.h: Add as_string prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add missing constructor implementation
Pierre-Emmanuel Patry [Mon, 27 Mar 2023 10:53:29 +0000 (12:53 +0200)]
gccrs: ast: Add missing constructor implementation

Add a missing constructor implementation for TokenStream objects.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::TokenStream): Add
constructor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add TokenStream collect function
Pierre-Emmanuel Patry [Fri, 24 Mar 2023 11:39:48 +0000 (12:39 +0100)]
gccrs: ast: Add TokenStream collect function

Add a function to TokenStream to collect in a single vector all tokens.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::collect_tokens): Add
getter.
(TokenStream::go): Rename function.
(TokenStream::visit): Likewise.
* ast/rust-ast-tokenstream.h: Add collect prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Refactor SimplePath attribute name
Pierre-Emmanuel Patry [Fri, 24 Mar 2023 10:42:19 +0000 (11:42 +0100)]
gccrs: ast: Refactor SimplePath attribute name

The getter name was overly complicated and long, this could be
refactored to a shorter name.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Change
getter name.
* ast/rust-ast.cc (SimplePath::as_string): Change attribute
name.
* ast/rust-ast.h (class SimplePath): Refactor attribute name and
change getter name.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add multiple formatting stub functions
Pierre-Emmanuel Patry [Thu, 23 Mar 2023 19:30:53 +0000 (20:30 +0100)]
gccrs: ast: Add multiple formatting stub functions

Add multiple stub functions depending on the context. This will allow an
easier unification of ast dump.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit_as_line): Add
formatting calls.
(TokenStream::visit_items_as_block): Likewise.
(TokenStream::newline): Add stub.
(TokenStream::indentation): Add stub.
(TokenStream::increment_indentation): Add stub.
(TokenStream::decrement_indentation): Add stub.
(TokenStream::visit): Add formatting calls.
(TokenStream::visit_function_common): Likewise.
* ast/rust-ast-tokenstream.h: Add stub prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add AltPattern TokenStream visitor
Pierre-Emmanuel Patry [Thu, 23 Mar 2023 18:30:51 +0000 (19:30 +0100)]
gccrs: ast: Add AltPattern TokenStream visitor

Add TokenStream visitor implementation for AltPattern.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add visitor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add Meta items TokenStream visitor
Pierre-Emmanuel Patry [Thu, 23 Mar 2023 18:24:59 +0000 (19:24 +0100)]
gccrs: ast: Add Meta items TokenStream visitor

Add visitor implementation for all remaining meta items.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add visitor
implementation.
* ast/rust-ast-tokenstream.h: Add additional function prototype.
* ast/rust-item.h: Add some getters.
* ast/rust-macro.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add use declarations TokenStream visitors
Pierre-Emmanuel Patry [Thu, 23 Mar 2023 15:46:51 +0000 (16:46 +0100)]
gccrs: ast: Add use declarations TokenStream visitors

Add UseDeclaration (and it's childrens) visitor implementation.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add visitor.
* ast/rust-item.h: Add missing getters.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add ExternCrate TokenStream visitor
Pierre-Emmanuel Patry [Thu, 23 Mar 2023 15:45:35 +0000 (16:45 +0100)]
gccrs: ast: Add ExternCrate TokenStream visitor

Add a visitor implementation for ExternCrate declarations.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add visitor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Implement remaining expr TokenStream visitor
Pierre-Emmanuel Patry [Thu, 23 Mar 2023 10:02:47 +0000 (11:02 +0100)]
gccrs: ast: Implement remaining expr TokenStream visitor

Implement the missing expression functions visitors for TokenStream.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add
visitors.
(TokenStream::visit_closure_common): Merge common code for
closure visitors.
* ast/rust-ast-tokenstream.h: Add function prototype.
* ast/rust-expr.h: Add missing move attribute getter.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add Tokenstream visitors for loop expressions
Pierre-Emmanuel Patry [Wed, 22 Mar 2023 15:17:42 +0000 (16:17 +0100)]
gccrs: ast: Add Tokenstream visitors for loop expressions

Add the implementation of tokenstream dump for multiple loop
expressions.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add
visitors.
(TokenStream::visit_loop_common): Merge common loop code.
* ast/rust-ast-tokenstream.h: Add function prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add some expr TokenStream visitors
Pierre-Emmanuel Patry [Wed, 22 Mar 2023 13:44:05 +0000 (14:44 +0100)]
gccrs: ast: Add some expr TokenStream visitors

Implement some TokenStream expression visitor functions.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Implement
visitor.
* ast/rust-ast-tokenstream.h: Add function prototype for missing
component.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add trailing comma formatting option
Pierre-Emmanuel Patry [Wed, 22 Mar 2023 13:39:34 +0000 (14:39 +0100)]
gccrs: ast: Add trailing comma formatting option

Add an option to output trailing commas depending on the configuration
of the TokenStream.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::trailing_comma):
Output a trailing comma to the token stream according to the
configuration.
* ast/rust-ast-tokenstream.h: Add function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Implement TokenStream visitor for meta nodes
Pierre-Emmanuel Patry [Wed, 22 Mar 2023 12:27:50 +0000 (13:27 +0100)]
gccrs: ast: Implement TokenStream visitor for meta nodes

Implement TokenStream visitor on some Meta container nodes.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Implement
visitors.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Refactor and add some Path node visitors
Pierre-Emmanuel Patry [Wed, 22 Mar 2023 12:20:31 +0000 (13:20 +0100)]
gccrs: ast: Refactor and add some Path node visitors

Implement some functions for Path nodes and refactor existing ones by
merging some common code.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Implement
visitors.
* ast/rust-ast-tokenstream.h: Add function prototypes.
* ast/rust-ast.h: Add missing getters.
* ast/rust-expr.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: ast: Add conversion to token stream
Pierre-Emmanuel Patry [Tue, 21 Mar 2023 09:39:51 +0000 (10:39 +0100)]
gccrs: ast: Add conversion to token stream

Add several functions to convert an ast back to a token stream. This may
be used later either to unify the ast dumping and modifications on ast
nodes in procedural macros.

gcc/rust/ChangeLog:

* Make-lang.in: Add rust-as-tokenstream to compile list.
* ast/rust-item.h: Add missing getter for location.
* ast/rust-ast-tokenstream.cc: Add ast visitor implementation.
* ast/rust-ast-tokenstream.h: New file.
* ast/rust-pattern.h: Add getter.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: handle bare function types with no specified return type
Philip Herron [Wed, 29 Mar 2023 15:28:11 +0000 (16:28 +0100)]
gccrs: handle bare function types with no specified return type

When we have a function with no return type this defaults to (), but we
need to be sure we generate a new implicit HirId for it otherwise it will
end up in a recursive reference chain.

Fixes #2042

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit):
Add implicit unit type as the return type when not specified

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Add test to show multiple candidates for methods is working
Philip Herron [Wed, 29 Mar 2023 16:02:17 +0000 (17:02 +0100)]
gccrs: Add test to show multiple candidates for methods is working

Fixes #925

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Add missing known attribute stable
Philip Herron [Wed, 29 Mar 2023 15:49:40 +0000 (16:49 +0100)]
gccrs: Add missing known attribute stable

Fixes #2025

gcc/rust/ChangeLog:

* util/rust-attributes.cc: Add stable to the table of known attributes

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: move missed testcase
Philip Herron [Wed, 29 Mar 2023 15:43:32 +0000 (16:43 +0100)]
gccrs: move missed testcase

This testcase was missed and not actually added to the testsuite. This
moves it to be part of the simple compile tests.

gcc/testsuite/ChangeLog:

* rust/bounds1.rs: Moved to...
* rust/compile/bounds1.rs: ...here.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: fix ICE with bad match arm type
Philip Herron [Wed, 29 Mar 2023 14:57:56 +0000 (15:57 +0100)]
gccrs: fix ICE with bad match arm type

We expect to get some kind of ADT or Tuple type when computing this kind of
match arms this adds a new diagnostic to check for this case instead of
an assertion.

Fixes #2029

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): return early on bad type
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::TypeCheckPattern):
remove assertion in favor of check
(TypeCheckPattern::visit): likewise

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: libproc_macro: Add rust interface
Pierre-Emmanuel Patry [Fri, 24 Mar 2023 14:25:08 +0000 (15:25 +0100)]
gccrs: libproc_macro: Add rust interface

libgrust/ChangeLog:

* libproc_macro/rust/bridge.rs: New file.
* libproc_macro/rust/bridge/group.rs: New file.
* libproc_macro/rust/bridge/ident.rs: New file.
* libproc_macro/rust/bridge/literal.rs: New file.
* libproc_macro/rust/bridge/punct.rs: New file.
* libproc_macro/rust/bridge/span.rs: New file.
* libproc_macro/rust/bridge/token_stream.rs: New file.
* libproc_macro/rust/group.rs: New file.
* libproc_macro/rust/ident.rs: New file.
* libproc_macro/rust/lib.rs: New file.
* libproc_macro/rust/literal.rs: New file.
* libproc_macro/rust/punct.rs: New file.
* libproc_macro/rust/span.rs: New file.
* libproc_macro/rust/token_stream.rs: New file.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: fix ICE when closure body is not a block
Philip Herron [Wed, 29 Mar 2023 15:14:04 +0000 (16:14 +0100)]
gccrs: fix ICE when closure body is not a block

Fixes: #2052
gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function):
when its not a block we dont have any ribs to generate locals from

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Fix parsing of array expressions.
Owen Avery [Mon, 27 Mar 2023 15:05:03 +0000 (11:05 -0400)]
gccrs: Fix parsing of array expressions.

Array expressions were being eagerly handled
outside of Pratt parsing.

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h
(Parser<ManagedTokenSource>::parse_expr_without_block):
Remove direct array expression handling.

gcc/testsuite/ChangeLog:

* rust/compile/array4.rs: New test.
* rust/execute/torture/arrays.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: privacy: Check for pub(crate) when resolving visibility path.
Arthur Cohen [Tue, 28 Mar 2023 12:22:21 +0000 (14:22 +0200)]
gccrs: privacy: Check for pub(crate) when resolving visibility path.

This causes the function to return true and the checks to pass, but it
requires more thinking - how do we deal with pub(crate) in the current system?
Should we simply treat it as a pub item in the current crate, but export it as
a private item in the metadata?

gcc/rust/ChangeLog:

* ast/rust-item.h: Fix `Visibility::has_path()` implementation.
* checks/errors/privacy/rust-visibility-resolver.cc
(VisibilityResolver::resolve_module_path): Check if we are dealing with pub(crate) properly.

gcc/testsuite/ChangeLog:

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

9 months agogccrs: resolver: Allow SimplePath to resolve to their root segment
Arthur Cohen [Tue, 28 Mar 2023 11:55:19 +0000 (13:55 +0200)]
gccrs: resolver: Allow SimplePath to resolve to their root segment

SimplePath are allowed to resolve to their root segment even if it is `crate`
or `super` when checking visibilities. We now have to make sure that this
is allowed everywhere a SimplePath is kept.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): Set
resolved_node_id when visiting root segment of SimplePath.

9 months agogccrs: ast: Fix location for pub(crate)
Arthur Cohen [Tue, 28 Mar 2023 09:34:27 +0000 (11:34 +0200)]
gccrs: ast: Fix location for pub(crate)

When creating a SimplePath from a string, we end up creating a path
which contains only one segment but no location. Take the location of
the one segment instead and use it as the simple path's location.

gcc/rust/ChangeLog:

* ast/rust-ast.h: Call SimplePath constructor differently in
SimplePath::from_str

9 months agogccrs: ast: Refactor rust-ast-lower-type.h to source file
Arthur Cohen [Tue, 28 Mar 2023 13:04:22 +0000 (15:04 +0200)]
gccrs: ast: Refactor rust-ast-lower-type.h to source file

gcc/rust/ChangeLog:

* hir/rust-ast-lower-type.h: Move all remaining definitions to source file.
* hir/rust-ast-lower-type.cc (ASTLoweringType::translate): Moved to source file.
(ASTLoweringType::visit): Likewise.
(ASTLowerGenericParam::translate): Likewise.
(ASTLowerGenericParam::visit): Likewise.
(ASTLoweringTypeBounds::translate): Likewise.
(ASTLoweringTypeBounds::visit): Likewise.
(ASTLowerWhereClauseItem::translate): Likewise.
(ASTLowerWhereClauseItem::visit): Likewise.

9 months agogccrs: Remove commented out TuplePatternItemsSingle
Owen Avery [Tue, 28 Feb 2023 05:28:55 +0000 (00:28 -0500)]
gccrs: Remove commented out TuplePatternItemsSingle

gcc/rust/ChangeLog:

* ast/rust-pattern.h: Remove commented out code.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: Fix bad cast error to bool
emanuele-em [Wed, 29 Mar 2023 01:45:01 +0000 (03:45 +0200)]
gccrs: Fix bad cast error to bool

In rust is not allowed to cast from int to bool. This patch handles the case when we cast a integer to bool with 'as bool'

Fixes #2026

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules): BOOL removed from switch cases

gcc/testsuite/ChangeLog:

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

Signed-off-by: Emanuele Micheletti <micheletti.emanuele@hotmail.com>
9 months agogccrs: expand: Rename AttrVisitor -> CfgStrip
Arthur Cohen [Tue, 21 Mar 2023 16:37:47 +0000 (17:37 +0100)]
gccrs: expand: Rename AttrVisitor -> CfgStrip

Since the expansion and stripping phase are now separated, it does not
make sense to keep AttrVisitor named AttrVisitor. Furthermore, the visitor
is already very complex, with a heavy mental load (erasing iterators,
rearranging them, performing cfg-expansion, etc) so further attribute
handling should probably happen in rust-attribute-checker.h

gcc/rust/ChangeLog:

* Make-lang.in: Rename rust-asttribute-visitor.o -> rust-cfg-strip.o
* expand/rust-attribute-visitor.cc: Moved to...
* expand/rust-cfg-strip.cc: ...here.
* expand/rust-attribute-visitor.h: Moved to...
* expand/rust-cfg-strip.h: ...here.
* expand/rust-macro-expand.cc: Fix include of rust-attribute-visitor.h
* expand/rust-macro-builtins.cc: Likewise.
* rust-session-manager.cc (Session::expansion): Call CfgStrip instead of
AttrVisitor.

9 months agogccrs: expand: Add new ExpandVisitor class
Arthur Cohen [Mon, 20 Mar 2023 03:17:04 +0000 (04:17 +0100)]
gccrs: expand: Add new ExpandVisitor class

This class takes care of actually performing the macro expansion by calling
into the MacroExpander for each node of a given AST, leaving the job of
cfg-stripping nodes to the AttrVisitor.

gcc/rust/ChangeLog:

* Make-lang.in: Add new object file.
* expand/rust-attribute-visitor.cc (AttrVisitor::go): Visit all items of a
crate.
(AttrVisitor::expand_struct_fields): Do not perform macro expansion anymore.
(AttrVisitor::expand_function_params): Likewise.
(AttrVisitor::expand_generic_args): Likewise.
(AttrVisitor::expand_qualified_path_type): Likewise.
(AttrVisitor::expand_self_param): Likewise.
(AttrVisitor::expand_trait_function_decl): Likewise.
(AttrVisitor::expand_trait_method_decl): Likewise.
(AttrVisitor::visit): Likewise.
(AttrVisitor::maybe_expand_expr): Remove function.
(AttrVisitor::maybe_expand_type): Likewise.
* expand/rust-attribute-visitor.h: Do not keep MacroExpander inside AttrVisitor
anymore.
* expand/rust-macro-expand.h (struct MacroExpander): Turn ContextType into an
enum class for more type safety.
* expand/rust-macro-expand.cc (MacroExpander::expand_crate): Use new ContextType
API.
* rust-session-manager.cc (Session::expansion): Call into ExpandVisitor.
* expand/rust-expand-visitor.cc: New file.
* expand/rust-expand-visitor.h: New file.

gcc/testsuite/ChangeLog:

* rust/compile/macro49.rs: New test.
* rust/compile/macro50.rs: New test.

9 months agogccrs: expand: Move cfg-attrs related functions out of MacroExpander
Arthur Cohen [Mon, 20 Mar 2023 02:10:26 +0000 (03:10 +0100)]
gccrs: expand: Move cfg-attrs related functions out of MacroExpander

The current situation where the MacroExpander and AttrVisitor recursively
call into each other is not great, and causes pains for macro imports. We
need to split this pass in two, with one pass being responsible for cfg-attr
checking and stripping, and one being responsible for macro expansion.

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc (MacroExpander::expand_crate): Do not cfg-attr
strip in MacroExpander
(MacroExpander::fails_cfg): Function moved...
(MacroExpander::fails_cfg_with_expand): Function moved...
(MacroExpander::expand_cfg_attrs): Function moved...
* expand/rust-attribute-visitor.cc (fails_cfg): ...here.
(fails_cfg_with_expand): ...here.
(expand_cfg_attrs): ...here.
(AttrVisitor::expand_struct_fields): Use new functions.
(AttrVisitor::expand_tuple_fields): Likewise.
(AttrVisitor::expand_function_params): Likewise.
(AttrVisitor::visit): Likewise.
(AttrVisitor::go): New function.
* expand/rust-attribute-visitor.h: Declare AttrVisitor::go.
* expand/rust-macro-expand.h (struct MacroExpander): Remove cfg-attr related
functions.

9 months agogccrs: lowering: Add lowering of exported macros
Arthur Cohen [Thu, 2 Mar 2023 13:41:54 +0000 (14:41 +0100)]
gccrs: lowering: Add lowering of exported macros

Macros marked with #[macro_export] need to be lowered to HIR in order
to get exported to the relevant metadata files.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_macro_definition):
New function.
* hir/rust-ast-lower-base.h: Declare `lower_macro_definition`.
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Lower public
macro definitions.
* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Likewise.
* hir/rust-ast-lower-stmt.h: Add visitor for `AST::MacroRulesDefinition`.
* hir/rust-ast-lower.cc (ASTLowering::go): Formatting.
(ASTLoweringBlock::visit): Visit `AST::MacroRulesDefinition`
(ASTLoweringIfLetBlock::visit): Formatting.
(ASTLoweringExprWithBlock::visit): Formatting.

9 months agogccrs: mappings: Keep exported macro IDs
Arthur Cohen [Wed, 15 Mar 2023 16:30:25 +0000 (17:30 +0100)]
gccrs: mappings: Keep exported macro IDs

gcc/rust/ChangeLog:

* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Add new visitor
for AST::MacroRulesDefinition.
* hir/rust-ast-lower-item.h: Declare above mentioned visitor.
* metadata/rust-export-metadata.cc (ExportContext::emit_macro): New function.
* metadata/rust-export-metadata.h: Declare it.
(PublicInterface::gather_export_data): Go through each exported macro.
* util/rust-hir-map.cc (Mappings::insert_exported_macro): New function.
(Mappings::get_exported_macros): New function.
* util/rust-hir-map.h: Add new mappings for exported macros.

9 months agogccrs: Revert "hir: Add ExportedMacro node and handling."
Arthur Cohen [Wed, 15 Mar 2023 16:15:39 +0000 (17:15 +0100)]
gccrs: Revert "hir: Add ExportedMacro node and handling."

This reverts commit a1f940d193c6cdb13483690a4f4a7d501ad7040e. It is easier
and cleaner to store exported macros' NodeIds into our mappings rather than
create a new HIR kind of node.

gcc/rust/ChangeLog:

* backend/rust-compile-item.h: Revert 1c946687239b86a92839d57dfbc928ad7ce35eae.
* backend/rust-compile-stmt.h: Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
* checks/errors/privacy/rust-pub-restricted-visitor.cc
(PubRestrictedVisitor::visit): Likewise.
* checks/errors/privacy/rust-pub-restricted-visitor.h: Likewise.
* checks/errors/privacy/rust-reachability.cc
(ReachabilityVisitor::visit): Likewise.
* checks/errors/privacy/rust-reachability.h: Likewise.
* checks/errors/privacy/rust-visibility-resolver.cc
(VisibilityResolver::visit): Likewise.
* checks/errors/privacy/rust-visibility-resolver.h: Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-const-checker.h: Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.h: Likewise.
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise.
* hir/rust-ast-lower-item.h: Likewise.
* hir/rust-hir-dump.cc (Dump::visit): Likewise.
* hir/rust-hir-dump.h: Likewise.
* hir/tree/rust-hir-full-decls.h (class ExportedMacro): Likewise.
* hir/tree/rust-hir-item.h (class ExportedMacro): Likewise.
* hir/tree/rust-hir-visitor.h: Likewise.
* hir/tree/rust-hir.cc (ExportedMacro::accept_vis): Likewise.
(ExportedMacro::get_locus): Likewise.
(ExportedMacro::get_item_kind): Likewise.
(ExportedMacro::clone_item_impl): Likewise.
* hir/tree/rust-hir.h: Likewise.
* metadata/rust-export-metadata.cc: Likewise.
* typecheck/rust-hir-type-check-item.h: Likewise.
* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit): Likewise.
* typecheck/rust-hir-type-check-stmt.h: Likewise.
* typecheck/rust-tycheck-dump.h: Likewise.
* util/rust-attributes.cc: Likewise.

9 months agogccrs: Fix ICE on parsing trait object missing dyn keyword
Philip Herron [Fri, 24 Mar 2023 22:17:36 +0000 (22:17 +0000)]
gccrs: Fix ICE on parsing trait object missing dyn keyword

Trait objects are not required to use the 'dyn' keyword though it is
depreciated in later editions/version of Rustc. This patch handles the case
when we query the type for an HIR::Item which happens to be an HIR::Trait
and return a trait object or error.

Fixes #2037

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): return a TraitObject

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Fix bad type inference
Philip Herron [Thu, 23 Mar 2023 21:18:18 +0000 (21:18 +0000)]
gccrs: Fix bad type inference

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc: don't inject extra inference variables

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: ast: Dump each MacroRule properly without the extra semicolon
Arthur Cohen [Mon, 20 Mar 2023 01:58:55 +0000 (02:58 +0100)]
gccrs: ast: Dump each MacroRule properly without the extra semicolon

Having the extra semicolon causes parsing errors when importing macros
exported using the AST dump, as no rule expects multiple tokens after a
single macro rule definition.

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::visit): Remove extraneous semicolon when
dumping macro rules.

9 months agogccrs: rust-imports: Fix last few mentions of Go.
Arthur Cohen [Thu, 16 Mar 2023 12:13:22 +0000 (13:13 +0100)]
gccrs: rust-imports: Fix last few mentions of Go.

Some were still present from the original adaptation of the Go metadata
imports and exports.

gcc/rust/ChangeLog:

* metadata/rust-imports.cc (add_search_path): Change `Go` -> `Rust`.
(Import::try_package_in_directory): Likewise.
(Import::find_export_data): Likewise.

9 months agogccrs: Lower AltPattern
Owen Avery [Wed, 8 Mar 2023 05:43:11 +0000 (00:43 -0500)]
gccrs: Lower AltPattern

gcc/rust/ChangeLog:

* hir/rust-ast-lower-pattern.cc
(ASTLoweringPattern::visit): Lower AltPattern.
* hir/rust-ast-lower-pattern.h:
(ASTLoweringPattern::visit): Add AltPattern visitor.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: Fix `FeatureGate::gate` will crash on 32-bit x86.
Xiao Ma [Thu, 2 Mar 2023 02:31:36 +0000 (02:31 +0000)]
gccrs: Fix `FeatureGate::gate` will crash on 32-bit x86.

gcc/rust/ChangeLog:

* checks/errors/rust-feature-gate.cc: Adjust 'ld'->'u'.
* checks/errors/rust-feature.h: Adjust
the type of `m_issue`: 'uint64_t' -> 'unsigned'.

Signed-off-by: Xiao Ma <mxlol233@outlook.com>
Co-authored-by: Thomas Schwinge <thomas@codesourcery.com>
9 months agogccrs: support use declaration to write the type into the correct namespace
Philip Herron [Fri, 17 Mar 2023 22:46:37 +0000 (22:46 +0000)]
gccrs: support use declaration to write the type into the correct namespace

This builds upon the previous path resolution fixes so that it returns the
resolved_node_id or UNKNOWN_NODEID on failure to resolve the use-path.
It then exports the name to the current module namespace so it can be used.

Fixes #850 #855

gcc/rust/ChangeLog:

* ast/rust-ast.h: add const get_final_segment helper
* hir/rust-ast-lower-enumitem.h: dont add an item mapping for enumitems
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): add enum to enum-items mappings
* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): likewise
* hir/tree/rust-hir-item.h: add non const helper to get variants
* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): resolve the use declaration
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): handle self
* resolve/rust-ast-resolve-toplevel.h: add enum item mappings to module mappings
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path): ensure variant
(TypeCheckExpr::resolve_segments): likewise
* typecheck/rust-type-util.cc (query_type): lookup enum's
* util/rust-hir-map.cc (Mappings::insert_hir_enumitem): enum item mappings
(Mappings::lookup_hir_enumitem): likewise
* util/rust-hir-map.h: likewise

gcc/testsuite/ChangeLog:

* rust/compile/issue-850.rs: New test.
* rust/compile/issue-855.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Return resolved_node_id when possible
Philip Herron [Fri, 17 Mar 2023 22:46:20 +0000 (22:46 +0000)]
gccrs: Return resolved_node_id when possible

To enable use statement to export a name to a path such as the prelude
example: use option::Option::{self, None, Some} we need to get the resolved
node id of the self, None and Some in order to export the name of None and
Some to their respective NodeId.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-path.cc (ResolvePath::ResolvePath): return NodeId
(ResolvePath::go): update signiture
(ResolvePath::resolve_path): return resolved_node_id
* resolve/rust-ast-resolve-path.h: update prototypes

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Add name resolution and ast lowering for slice pattern
Mahmoud Mohamed [Mon, 20 Mar 2023 12:31:28 +0000 (15:31 +0300)]
gccrs: Add name resolution and ast lowering for slice pattern

gcc/rust/ChangeLog:

* hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit):
Add lowering for SlicePattern.
* hir/rust-ast-lower-pattern.h: Likewise.
* resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::visit):
Add name resolution visit for SlicePattern.
* resolve/rust-ast-resolve-pattern.h: Likewise.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: Cleanup unused headers
Philip Herron [Mon, 20 Mar 2023 14:39:44 +0000 (14:39 +0000)]
gccrs: Cleanup unused headers

gcc/rust/ChangeLog:

* hir/rust-ast-lower-implitem.h (RUST_AST_LOWER_IMPLITEM_H): cleanup header usage
* hir/rust-ast-lower-item.cc: likewise
* hir/rust-ast-lower-item.h (RUST_AST_LOWER_ITEM): likewise
* hir/rust-ast-lower-stmt.cc: likewise
* hir/rust-ast-lower-stmt.h (RUST_AST_LOWER_STMT): likewise
* hir/rust-ast-lower-type.h: likewise
* hir/rust-ast-lower.cc: likewise
* hir/rust-ast-lower.h: likewise
* resolve/rust-ast-resolve-expr.h: likewise
* resolve/rust-ast-resolve-item.cc: likewise
* resolve/rust-ast-resolve-item.h: likewise
* resolve/rust-ast-resolve-stmt.cc: likewise
* resolve/rust-ast-resolve-stmt.h: likewise
* resolve/rust-ast-resolve-struct-expr-field.h: likewise
* resolve/rust-ast-resolve-toplevel.h: likewise
* resolve/rust-ast-resolve-type.h: likewise
* resolve/rust-ast-resolve.h: likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Remove unused ResolvePattern visitor
Mahmoud Mohamed [Sun, 19 Mar 2023 21:48:53 +0000 (00:48 +0300)]
gccrs: Remove unused ResolvePattern visitor

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-pattern.h (class ResolvePattern): Removed.

9 months agogccrs: Add move_val_init intrinsic
Philip Herron [Thu, 16 Mar 2023 21:03:34 +0000 (21:03 +0000)]
gccrs: Add move_val_init intrinsic

This implements it as a builtin memcpy using the generic param T for the
size hint.

Fixes #1902

gcc/rust/ChangeLog:

* backend/rust-compile-intrinsic.cc (move_val_init_handler): new intrinsice
(uninit_handler): use a builtin memcpy

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Fix bad method resolution
Philip Herron [Fri, 17 Mar 2023 18:09:42 +0000 (18:09 +0000)]
gccrs: Fix bad method resolution

We should use the result of our attempted unify inference as this will
allow the direct unification of generic pointer types to concrete ones.

Fixes #1981

gcc/rust/ChangeLog:

* typecheck/rust-coercion.cc (TypeCoercionRules::select): use the result

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: add new apply_primtiive_type_hint to inference variables
Philip Herron [Fri, 17 Mar 2023 18:09:53 +0000 (18:09 +0000)]
gccrs: add new apply_primtiive_type_hint to inference variables

In the senario where you infer types via unify_site_and but choose to not
commit the result you can end up with coercion to infer the result later on
which does not get fully replaced resulting in a stray inference variable
that can be left alone as a general inference variable leading to missing
type context info. This patch gives support to add type hints to inference
variables so they can be defaulted correctly in more complex cases. The
old system relied on the unification result commiting and overriding the
inference variables so they dissapear out of the current typeing context.

This was needed to fix #1981 where it is valid to inject inference
variables here. This lead to a regression in a few of the complex generic
trait test cases such as execute/torture/traits9.rs which had the wrong
argument type and defaulted wrongly to i32 instead of isize.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::resolve_literal): fix ctor
* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit): likewise
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): likewise
* typecheck/rust-typecheck-context.cc (TypeCheckContext::push_new_loop_context): likewise
* typecheck/rust-tyty-util.cc (TyVar::get_implicit_infer_var): likewise
* typecheck/rust-tyty.cc (InferType::InferType): new ctor with type hint
(InferType::clone): fix ctor
(InferType::apply_primitive_type_hint): new function to apply possible hint
* typecheck/rust-tyty.h: update prototypes
* typecheck/rust-unify.cc (UnifyRules::expect_inference_variable): apply type hints
(UnifyRules::expect_bool): likewise
(UnifyRules::expect_char): likewise
(UnifyRules::expect_int): likewise
(UnifyRules::expect_uint): likewise
(UnifyRules::expect_float): likewise
(UnifyRules::expect_isize): likewise
(UnifyRules::expect_usize): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Dump inner attrs for Dump::visit(BlockExpr &)
Jiakun Fan [Sat, 11 Mar 2023 17:47:32 +0000 (17:47 +0000)]
gccrs: Dump inner attrs for Dump::visit(BlockExpr &)

Refer to `BlockExpr::as_string ()`, dump inner attrs for `Dump::visit`.
Add visit (AST::Attribute &).

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::go): fix format
(Dump::visit): impl `visit (AST::Attribute &)`, dump inner attrs and
`visit (Lifetime &)`
* hir/rust-hir-dump.h:add `visit (AST::Attribute &)`

Signed-off-by: Jiakun Fan <120090316@link.cuhk.edu.cn>
9 months agogccrs: resolve: Add name resolution for AltPattern
Mahmoud Mohamed [Wed, 15 Mar 2023 23:54:11 +0000 (02:54 +0300)]
gccrs: resolve: Add name resolution for AltPattern

The main changes in this commit can be logically split into two parts:

1) Pushing new pattern binding contexts in alt patterns. At the start
of each AltPattern we push an 'Or' context which represents the relation
between the bindings of different alts.

Before we resolve each alt arm we need to push a 'Product' context to represent
the relation between the bindings of this specific alt arm and each other.
This 'Product' context is removed after the alt arm visit and the its bindings
are pushed into the 'Or' context.

Eventually, the 'Or' context is removed as well after it fulfills its duty.
Its bindings are then pushed into the previous context similarly and so on.

2) Checking for consistent bindings between the alt arms which is handled
by check_bindings_consistency. The info necessary for this check is held
by binding_info_map inside PatternDeclaration class. The binding_info_map
only holds bindings for one alt arm at a time. After every alt arm visit,
these bindings info are pushed into a vec (that contains all alt arms info
and will eventually be passed to check_bindings_consistency) and emptied out
for the next alt arm visit. At the end, all the info from all the alt arms
are pushed again into binding_info in addition to the initial bindings that
were there before the alt arms visits, this way the binding_info will contain
all the binding_info present in the AltPattern (as it should).

In addition to that, some refactors were made (e.g. add_new_binding function)
and some errors emitted, no biggie.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::go):
Print out consistency errors.
(PatternDeclaration::visit): Implement visit for AltPattern.
(PatternDeclaration::add_new_binding): New helper function for
adding a binding to identifier.
* resolve/rust-ast-resolve-pattern.h (struct BindingInfo):
New struct to facilitate checking for inconsistencies between bindings.

gcc/testsuite/ChangeLog:

* rust/compile/torture/alt_patterns1.rs: New test.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: Unify HIR::IfLetExprConseqIf{,Let} into HIR::IfLetExprConseqElse
Owen Avery [Wed, 15 Mar 2023 20:44:07 +0000 (16:44 -0400)]
gccrs: Unify HIR::IfLetExprConseqIf{,Let} into HIR::IfLetExprConseqElse

This should allow for 'if let' expressions to be lowered more easily.

gcc/rust/ChangeLog:

* backend/rust-compile-block.h
(CompileConditionalBlocks::visit): Remove IfLetExprConseqIf{,Let} visitors.
(CompileExprWithBlock::visit): Remove IfLetExprConseqIf{,Let} visitors.
* backend/rust-compile-expr.h
(CompileExpr::visit): Remove IfLetExprConseqIf{,Let} visitors.
* checks/errors/rust-unsafe-checker.cc
(UnsafeChecker::visit): Remove IfLetExprConseqIf{,Let} visitors.
* checks/errors/rust-unsafe-checker.h
(UnsafeChecker::visit): Remove IfLetExprConseqIf{,Let} visitors.
* checks/errors/rust-const-checker.cc
(ConstChecker::visit): Remove IfLetExprConseqIf{,Let} visitors.
* checks/errors/rust-const-checker.h
(ConstChecker::visit): Remove IfLetExprConseqIf{,Let} visitors.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::visit): Remove IfLetExprConseqIf{,Let} visitors.
* checks/errors/privacy/rust-privacy-reporter.h
(PrivacyReporter::visit): Remove IfLetExprConseqIf{,Let} visitors.
* hir/tree/rust-hir-expr.h
(class IfLetExprConseqElse): Make else_block ExprWithBlock.
(class IfLetExprConseqIf): Remove.
(class IfLetExprConseqIfLet): Remove.
* hir/tree/rust-hir-full-decls.h
(class IfLetExprConseqIf): Remove.
(class IfLetExprConseqIfLet): Remove.
* hir/tree/rust-hir.cc
(IfLetExprConseqElse::as_string): Adjust output.
(IfLetExprConseqIf::as_string): Remove.
(IfLetExprConseqIfLet::as_string): Remove.
(IfLetExprConseqIf::accept_vis): Remove.
(IfLetExprConseqIfLet::accept_vis): Remove.
* hir/tree/rust-hir-visitor.h
(HIRFullVisitor::visit): Remove IfLetExprConseqIf{,Let} visitors.
(HIRFullVisitorBase::visit): Remove IfLetExprConseqIf{,Let} visitors.
(HIRExpressionVisitor::visit): Remove IfLetExprConseqIf{,Let} visitors.
* hir/rust-hir-dump.cc
(Dump::visit): Remove IfLetExprConseqIf{,Let} visitors.
* hir/rust-hir-dump.h
(Dump::visit): Remove IfLetExprConseqIf{,Let} visitors.
* typecheck/rust-hir-type-check-expr.h
(TypeCheckExpr::visit): Remove IfLetExprConseqIf{,Let} visitors.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: Unify HIR::IfExprConseqIf{,Let} into HIR::IfExprConseqElse
Owen Avery [Wed, 15 Mar 2023 20:35:47 +0000 (16:35 -0400)]
gccrs: Unify HIR::IfExprConseqIf{,Let} into HIR::IfExprConseqElse

This should simplify 'if' expression handling to match
future simplifications to 'if let' expression handling.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc
(TypeCheckExpr::visit): Remove IfExprConseqIf visitor.
* typecheck/rust-hir-type-check-expr.h
(TypeCheckExpr::visit): Remove IfExprConseqIf{,Let} visitor.
* backend/rust-compile-block.cc
(CompileConditionalBlocks::visit): Remove IfExprConseqIf visitor.
* backend/rust-compile-block.h
(CompileConditionalBlocks::visit): Remove IfExprConseqIf{,Let} visitors.
(CompileExprWithBlock::visit):
Remove IfExprConseqIf{,Let} visitors, implement BlockExpr visitor.
* backend/rust-compile-expr.cc
(CompileExpr::visit): Remove IfExprConseqIf visitor.
* backend/rust-compile-expr.h
(CompileExpr::visit): Remove IfExprConseqIf{,Let} visitors.
* checks/lints/rust-lint-marklive.h
(MarkLive::visit): Remove IfExprConseqIf visitor.
* checks/errors/rust-const-checker.cc
(ConstChecker::visit): Remove IfExprConseqIf{,Let} visitors.
* checks/errors/rust-const-checker.h
(ConstChecker::visit): Remove IfExprConseqIf{,Let} visitors.
* checks/errors/rust-unsafe-checker.cc
(UnsafeChecker::visit): Remove IfExprConseqIf{,Let} visitors.
* checks/errors/rust-unsafe-checker.h
(UnsafeChecker::visit): Remove IfExprConseqIf{,Let} visitors.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::visit): Remove IfExprConseqIf{,Let} visitors.
* checks/errors/privacy/rust-privacy-reporter.h
(PrivacyReporter::visit): Remove IfExprConseqIf{,Let} visitors.
* hir/tree/rust-hir-expr.h
(class IfExprConseqElse): Make else_block ExprWithBlock.
(class IfExprConseqIf): Remove.
(class IfExprConseqIfLet): Remove.
* hir/tree/rust-hir-full-decls.h
(class IfExprConseqIf): Remove.
(class IfExprConseqIfLet): Remove.
* hir/tree/rust-hir.cc
(IfExprConseqElse::as_string): Adjust output.
(IfExprConseqIf::as_string): Remove.
(IfExprConseqIfLet::as_string): Remove.
(IfExprConseqIf::accept_vis): Remove.
(IfExprConseqIfLet::accept_vis): Remove.
* hir/tree/rust-hir-visitor.h
(HIRFullVisitor::visit): Remove IfExprConseqIf{,Let} visitors.
(HIRFullVisitorBase::visit): Remove IfExprConseqIf{,Let} visitors.
(HIRExpressionVisitor::visit): Remove IfExprConseqIf{,Let} visitors.
* hir/rust-hir-dump.cc
(Dump::visit): Remove IfExprConseqIf{,Let} visitors.
* hir/rust-hir-dump.h
(Dump::visit): Remove IfExprConseqIf{,Let} visitors.
* hir/rust-ast-lower.cc
(ASTLoweringIfBlock::visit): Replace HIR::IfExprConseqIf with HIR::IfExprConseqElse.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: resolve: Fix multiple bindings handling in match
Mahmoud Mohamed [Thu, 16 Mar 2023 11:53:44 +0000 (14:53 +0300)]
gccrs: resolve: Fix multiple bindings handling in match

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit):
Push a Product context instead of an Or context.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: remove proxy class to use virtual method to get impl_item name
Philip Herron [Wed, 15 Mar 2023 16:50:03 +0000 (16:50 +0000)]
gccrs: remove proxy class to use virtual method to get impl_item name

gcc/rust/ChangeLog:

* typecheck/rust-hir-inherent-impl-overlap.h (class ImplItemToName): remove

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: cleanup resolve method address code generation
Philip Herron [Mon, 13 Mar 2023 21:43:27 +0000 (21:43 +0000)]
gccrs: cleanup resolve method address code generation

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::resolve_method_address): remove unused code

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Only infer when no generic arguments have been specified
Philip Herron [Mon, 13 Mar 2023 17:02:56 +0000 (17:02 +0000)]
gccrs: Only infer when no generic arguments have been specified

On Paths such as: mem::size_of<T>() we always specified to infer the
generics which is not always the case and can cause stay inference
variables.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): remove infer call
(TypeCheckExpr::resolve_root_path): only infer when we need to

gcc/testsuite/ChangeLog:

* rust/compile/sizeof-stray-infer-var-bug.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: add extra debug line for method resolve select
Philip Herron [Mon, 13 Mar 2023 16:58:59 +0000 (16:58 +0000)]
gccrs: add extra debug line for method resolve select

gcc/rust/ChangeLog:

* typecheck/rust-hir-dot-operator.cc (MethodResolver::select): add debug

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Remove solve_missing_mappings_from_this to handle covariants
Philip Herron [Mon, 13 Mar 2023 16:49:36 +0000 (16:49 +0000)]
gccrs: Remove solve_missing_mappings_from_this to handle covariants

change how we monomorphize dyn-items when we need to compute the generics

We might have a trait item such as:

  impl<'a, T> FnLike<&'a T, &'a T> for Identity {
      fn call(&self, arg: &'a T) -> &'a T { ... }
  }

Which ended up monomorphized badly to:

  const isize & const &
    <example::Identity as example::FnLike::<& T, & T>>::call<& isize>
    (const struct example::Identity & const self,
     const isize & const & const arg)

This is wrong because it turned into a double reference type becasuse this
bug was consistent bugs were not picked up but this is not correct. We now
reuse our type inference infrastructure to solve the parameters instead.

Fixes #1984

gcc/rust/ChangeLog:

* backend/rust-compile.cc: use unify_and instead
* typecheck/rust-tyty-subst.cc (SubstitutionRef::solve_missing_mappings_from_this): remove
* typecheck/rust-tyty-subst.h: update header

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: get rid of can_eq in CoerceionRules selection
Philip Herron [Mon, 13 Mar 2023 14:08:07 +0000 (14:08 +0000)]
gccrs: get rid of can_eq in CoerceionRules selection

The can_eq interface is very inconsistent so this begins the journey to
remove this and also address part of #1981

gcc/rust/ChangeLog:

* typecheck/rust-coercion.cc (TypeCoercionRules::select): use new unify_and interface
* typecheck/rust-hir-dot-operator.cc (MethodResolver::try_hook): grab bounds behind refs

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: cleanup header usage to decouple includes
Philip Herron [Sun, 12 Mar 2023 18:38:08 +0000 (18:38 +0000)]
gccrs: cleanup header usage to decouple includes

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc: fix headers
* backend/rust-compile-base.h: likewise
* backend/rust-compile-expr.cc: likewise
* backend/rust-compile-extern.h: likewise
* backend/rust-compile-pattern.cc: likewise
* backend/rust-compile.cc: likewise
* typecheck/rust-autoderef.cc: likewise
* typecheck/rust-hir-dot-operator.cc: likewise
* typecheck/rust-hir-inherent-impl-overlap.h: likewise
* typecheck/rust-hir-path-probe.cc: likewise
* typecheck/rust-hir-trait-resolve.cc: likewise
* typecheck/rust-hir-type-check-base.cc: likewise
* typecheck/rust-hir-type-check-base.h (RUST_HIR_TYPE_CHECK_BASE): likewise
* typecheck/rust-hir-type-check-enumitem.cc: likewise
* typecheck/rust-hir-type-check-expr.cc: likewise
* typecheck/rust-hir-type-check-implitem.cc: likewise
* typecheck/rust-hir-type-check-item.cc: likewise
* typecheck/rust-hir-type-check-path.cc: likewise
* typecheck/rust-hir-type-check-pattern.cc: likewise
* typecheck/rust-hir-type-check-stmt.cc: likewise
* typecheck/rust-hir-type-check-struct.cc: likewise
* typecheck/rust-hir-type-check-type.cc: likewise
* typecheck/rust-hir-type-check-type.h: likewise
* typecheck/rust-hir-type-check.h (RUST_HIR_TYPE_CHECK): likewise
* typecheck/rust-tyty-bounds.cc: likewise
* typecheck/rust-tyty-call.cc: likewise
* typecheck/rust-tyty-subst.cc: likewise
* typecheck/rust-tyty.cc: likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: get rid of virtual dispatch for substitution proxys
Philip Herron [Sat, 11 Mar 2023 12:55:30 +0000 (12:55 +0000)]
gccrs: get rid of virtual dispatch for substitution proxys

gcc/rust/ChangeLog:

* typecheck/rust-tyty.cc (BaseType::has_subsititions_defined): new implementation
(BaseType::needs_generic_substitutions): likewise
(ProjectionType::needs_generic_substitutions): remove
(ProjectionType::has_subsititions_defined): remove
* typecheck/rust-tyty.h: update header

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: tyty get rid of useless virtuals
Philip Herron [Fri, 10 Mar 2023 18:13:01 +0000 (18:13 +0000)]
gccrs: tyty get rid of useless virtuals

This removes can_substitute and contains_type_parameters which were
confusing interfaces to act as a proxy to the SubstitionRef types. This
replaces them with a single base implementation which is much easier to
debug and follow.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): update to use new interface
(TypeCheckExpr::resolve_root_path): likewise
(TypeCheckExpr::resolve_segments): likewise
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): likewise
(TypeCheckType::resolve_root_path): likewise
* typecheck/rust-tyty-subst.cc (SubstitutionRef::get_mappings_from_generic_args): likewise
* typecheck/rust-tyty.cc (BaseType::supports_substitutions): likewise
(BaseType::can_substitute): remove
(BaseType::contains_type_parameters): remove
(handle_substitions): cleanup
(TupleType::handle_substitions): update
(FnType::handle_substitions): update
(ProjectionType::supports_substitutions): update
* typecheck/rust-tyty.h: update header

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: group basetype virtuals together and remove unused virtual
Philip Herron [Fri, 10 Mar 2023 18:08:18 +0000 (18:08 +0000)]
gccrs: group basetype virtuals together and remove unused virtual

gcc/rust/ChangeLog:

* typecheck/rust-tyty.h: cleanup ordering of header

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: refactor monomoprhized_clone to not use virtual dispatch
Philip Herron [Fri, 10 Mar 2023 16:31:14 +0000 (16:31 +0000)]
gccrs: refactor monomoprhized_clone to not use virtual dispatch

gcc/rust/ChangeLog:

* typecheck/rust-tyty.cc (BaseType::monomorphized_clone): new impl
(InferType::monomorphized_clone): remove
(ErrorType::monomorphized_clone): likewise
(ADTType::monomorphized_clone): likewise
(TupleType::monomorphized_clone): likewise
(FnType::monomorphized_clone): likewise
(FnPtr::monomorphized_clone): likewise
(ClosureType::monomorphized_clone): likewise
(ArrayType::clone): likewise
(ArrayType::get_var_element_type): likewise
(ArrayType::monomorphized_clone): likewise
(SliceType::clone): likewise
(SliceType::get_var_element_type): likewise
(SliceType::monomorphized_clone): likewise
(BoolType::monomorphized_clone): likewise
(IntType::monomorphized_clone): likewise
(UintType::monomorphized_clone): likewise
(FloatType::monomorphized_clone): likewise
(USizeType::monomorphized_clone): likewise
(ISizeType::monomorphized_clone): likewise
(CharType::monomorphized_clone): likewise
(ReferenceType::clone): likewise
(ReferenceType::get_var_element_type): likewise
(ReferenceType::monomorphized_clone): likewise
(PointerType::clone): likewise
(PointerType::get_var_element_type): likewise
(PointerType::monomorphized_clone): likewise
(ParamType::monomorphized_clone): likewise
(StrType::monomorphized_clone): likewise
(NeverType::monomorphized_clone): likewise
(PlaceholderType::monomorphized_clone): likewise
(ProjectionType::monomorphized_clone): likewise
(DynamicObjectType::monomorphized_clone): likewise
* typecheck/rust-tyty.h: update header

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: refactor TyTy::BaseType::is_unit to not use virtual dispatch
Philip Herron [Fri, 10 Mar 2023 14:31:50 +0000 (14:31 +0000)]
gccrs: refactor TyTy::BaseType::is_unit to not use virtual dispatch

gcc/rust/ChangeLog:

* typecheck/rust-tyty.cc (BaseType::is_unit): new implementation
(ErrorType::is_unit): remove
(TupleType::is_unit): likewise
(NeverType::is_unit): likewise
(PlaceholderType::is_unit): likewise
(ProjectionType::is_unit): likewise
* typecheck/rust-tyty.h: update header

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: refactor is_concrete to not just virtual dispatch
Philip Herron [Fri, 10 Mar 2023 14:10:54 +0000 (14:10 +0000)]
gccrs: refactor is_concrete to not just virtual dispatch

Its easier to debug and maintain one base function for this than relying
on virtual dispatch

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* typecheck/rust-tyty.cc (BaseType::is_concrete): new implementation
(InferType::is_concrete): remove
(ErrorType::is_concrete): likewise
(StructFieldType::is_concrete): likewise
(ADTType::is_concrete): likewise
(TupleType::is_concrete): likewise
(BoolType::is_concrete): likewise
(IntType::is_concrete): likewise
(UintType::is_concrete): likewise
(FloatType::is_concrete): likewise
(USizeType::is_concrete): likewise
(ISizeType::is_concrete): likewise
(CharType::is_concrete): likewise
(ReferenceType::is_concrete): likewise
(PointerType::is_concrete): likewise
(ParamType::is_concrete): likewise
(StrType::is_concrete): likewise
(NeverType::is_concrete): likewise
(PlaceholderType::is_concrete): likewise
(ProjectionType::is_concrete): likewise
(DynamicObjectType::is_concrete): likewise
* typecheck/rust-tyty.h: update header

9 months agogccrs: reuse destructure code in compilation of types
Philip Herron [Fri, 10 Mar 2023 13:10:11 +0000 (13:10 +0000)]
gccrs: reuse destructure code in compilation of types

We can just return error_mark_node instead of our own custom
recursion limit checker code. This makes the backend more reuseable
in error states.

gcc/rust/ChangeLog:

* backend/rust-compile-type.cc (TyTyResolveCompile::TyTyResolveCompile): call destructure
(TyTyResolveCompile::compile): use error_mark_node
(TyTyResolveCompile::visit): use error_mark_node
* backend/rust-compile-type.h: remove recursive ops

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
9 months agogccrs: Add all rust keywords (except priv) to the follow-set of `:vis` when parsing...
Tage Johansson [Sat, 4 Mar 2023 18:43:02 +0000 (19:43 +0100)]
gccrs: Add all rust keywords (except priv) to the follow-set of `:vis` when parsing macro rules

Previously, the following macro rules were rejected by gccrs:
```Rust
macro_rules! {
    ($v:vis <KEY_WORD>) => { ... };
}
```

This PR fixes so the above code is accepted by the compiler for all key words like `async` or `unsafe`.
The only exception is the keyword `priv` which is not allowed.
See [this page](https://doc.rust-lang.org/reference/macro-ambiguity.html) for reference. Especially the following excerpt:
> FOLLOW(vis) = {,l any keyword or identifier except a non-raw priv; any token that can begin a type; ident, ty, and path nonterminals}.

Fixes #1060

gcc/rust/ChangeLog:

* parse/rust-parse.cc: fix follow-sets

gcc/testsuite/ChangeLog:

* rust/compile/macro47.rs: Test that :vis can be followed by some keywords
* rust/compile/macro48.rs: Test that :vis cannot be followed by the keyword priv

Signed-off-by: Tage Johansson <frans.tage@gmail.com>
9 months agogccrs: resolve: Handle multiple bindings to the same identifier
Mahmoud Mohamed [Mon, 13 Mar 2023 14:18:11 +0000 (17:18 +0300)]
gccrs: resolve: Handle multiple bindings to the same identifier

https://github.com/rust-lang/rust/blob/master/compiler/rustc_resolve/src/late.rs#L3168
This commit follows rustc's implementation of handling multiple bindings
to the same identifier in parameters.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit) :declare and
pass bindings to PatternDeclaration::go.
(ResolveExpr::resolve_closure_param): Likewise.
* resolve/rust-ast-resolve-expr.h: Likewise.
* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise.
(ResolveItem::visit): Likewise.
* resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::go): Likewise.
(PatternDeclaration::visit): check for previous identifier bindings
before inserting the new one.
* resolve/rust-ast-resolve-pattern.h (enum PatternBoundCtx): New enum.
* resolve/rust-ast-resolve-stmt.h: pass bindings to PatterDeclaration::go.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: hir: Improve pattern bindings handling in closure parameters
Mahmoud Mohamed [Tue, 14 Mar 2023 22:06:46 +0000 (01:06 +0300)]
gccrs: hir: Improve pattern bindings handling in closure parameters

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function):
handle closure parameters pattern bindings using CompilePatternBindings visitor

gcc/testsuite/ChangeLog:

* rust/execute/torture/closure4.rs: New test.
* rust/execute/torture/ref-pattern2.rs: New test.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: Rename compiler proper
Arthur Cohen [Tue, 14 Mar 2023 16:28:54 +0000 (17:28 +0100)]
gccrs: Rename compiler proper

gcc/rust/ChangeLog:

* README.md (process): Rename `rust1` to `crab1` in examples.
* Make-lang.in: Rename `rust1` to `crab1`.
* config-lang.in: Likewise.
* lang-specs.h: Likewise.
* rustspec.cc (lang_specific_driver): Likewise.

9 months agogccrs: Add method to access SlicePattern items
Owen Avery [Tue, 14 Mar 2023 23:17:26 +0000 (19:17 -0400)]
gccrs: Add method to access SlicePattern items

gcc/rust/ChangeLog:

* hir/tree/rust-hir-pattern.h
(SlicePattern::get_items): Add.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: Convert structs to classes
Owen Avery [Tue, 14 Mar 2023 22:52:50 +0000 (18:52 -0400)]
gccrs: Convert structs to classes

gcc/rust/ChangeLog:

* hir/tree/rust-hir-full-decls.h
(struct GenericArgsBinding): Convert to class.
(class GenericArgsBinding): Convert from struct.
(struct TypePathFunction): See above.
(class TypePathFunction): See above.
(struct QualifiedPathType): See above.
(class QualifiedPathType): See above.
* ast/rust-ast-full-decls.h
(struct WhereClause): See above.
(class WhereClause): See above.
(struct SelfParam): See above.
(class SelfParam): See above.
(struct FunctionQualifiers): See above.
(class FunctionQualifiers): See above.
(struct FunctionParam): See above.
(class FunctionParam): See above.
(struct StructField): See above.
(class StructField): See above.
(struct TupleField): See above.
(class TupleField): See above.
(struct TraitFunctionDecl): See above.
(class TraitFunctionDecl): See above.
(struct TraitMethodDecl): See above.
(class TraitMethodDecl): See above.
(struct NamedFunctionParam): See above.
(class NamedFunctionParam): See above.
* hir/tree/rust-hir-path.h
(struct GenericArgsBinding): See above.
(class GenericArgsBinding): See above.
(struct TypePathFunction): See above.
(class TypePathFunction): See above.
(struct QualifiedPathType): See above.
(class QualifiedPathType): See above.
* ast/rust-item.h
(struct WhereClause): See above.
(class WhereClause): See above.
(struct SelfParam): See above.
(class SelfParam): See above.
(struct FunctionQualifiers): See above.
(class FunctionQualifiers): See above.
(struct FunctionParam): See above.
(class FunctionParam): See above.
(struct StructField): See above.
(class StructField): See above.
(struct TupleField): See above.
(class TupleField): See above.
(struct TraitFunctionDecl): See above.
(class TraitFunctionDecl): See above.
(struct TraitMethodDecl): See above.
(class TraitMethodDecl): See above.
(struct NamedFunctionParam): See above.
(class NamedFunctionParam): See above.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: HIR::AltPattern fixes
Owen Avery [Tue, 14 Mar 2023 21:35:16 +0000 (17:35 -0400)]
gccrs: HIR::AltPattern fixes

gcc/rust/ChangeLog:

* hir/tree/rust-hir-pattern.h
(class AltPattern): Remove duplicate access specifier.
(AltPattern::get_alts): Add.
* hir/tree/rust-hir.cc
(AltPattern::as_string): Add.
(AltPattern::accept_vis): Add.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: hir: Add ExportedMacro node and handling.
Arthur Cohen [Wed, 1 Mar 2023 11:35:18 +0000 (12:35 +0100)]
gccrs: hir: Add ExportedMacro node and handling.

This HIR node represents macros which should be exported into the final
Rust metadata files. Because our metadata exporter operates on the HIR,
while macros are inherently tied to the AST, we need a way to propagate
exported macros up until the metadata export pass on the HIR. Hence the
existence of this class, whose sole purpose is to keep enough information
for the metadata exporter to retrieve the original AST::MacroRulesDefinition.
Handling for actually exporting these macros will come later.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-item.h (class ExportedMacro): Add new ExportedMacro class.
* hir/tree/rust-hir.cc (ExportedMacro::accept_vis): New function.
(ExportedMacro::get_locus): Likewise.
(ExportedMacro::get_item_kind): Likewise.
(ExportedMacro::clone_item_impl): Likewise.
* hir/tree/rust-hir-full-decls.h (class ExportedMacro): Forward declare class.
* backend/rust-compile-item.h: Add visitor for ExportedMacro.
* backend/rust-compile-stmt.h: Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
* checks/errors/privacy/rust-pub-restricted-visitor.cc (PubRestrictedVisitor::visit):
Likewise.
* checks/errors/privacy/rust-pub-restricted-visitor.h: Likewise.
* checks/errors/privacy/rust-reachability.cc (ReachabilityVisitor::visit): Likewise.
* checks/errors/privacy/rust-reachability.h: Likewise.
* checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::visit): Likewise.
* checks/errors/privacy/rust-visibility-resolver.h: Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-const-checker.h: Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.h: Likewise.
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise.
* hir/rust-ast-lower-item.h: Likewise.
* hir/rust-hir-dump.cc (Dump::visit): Likewise.
* hir/rust-hir-dump.h: Likewise.
* hir/tree/rust-hir-visitor.h: Likewise.
* metadata/rust-export-metadata.cc: Likewise.
* typecheck/rust-hir-type-check-item.h: Likewise.
* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit): Likewise.
* typecheck/rust-hir-type-check-stmt.h: Likewise.
* typecheck/rust-tycheck-dump.h: Likewise.
* hir/tree/rust-hir.h: Add new ItemKind::MacroExport variant.
* util/rust-attributes.cc: Add #[macro_export] attribute.

9 months agogccrs: hir: Provide basic handling for ReferencePattern in function parameter
Mahmoud Mohamed [Tue, 7 Mar 2023 23:57:24 +0000 (02:57 +0300)]
gccrs: hir: Provide basic handling for ReferencePattern in function parameter

Added an implementation for
`CompilePatternBindings::visit (HIR::ReferencePattern)` where we
dereference the initial expression and recurse.
Added an implementation for
`CompilePatternBindings::visit (HIR::IdentifierPattern)` as well since it's
the simplest base case.

In addition to this, a small refactor for the shared code in
`StructPattern` and `TupleStructPattern` visits was added as a helper
function called `create_tmp_param_var`.

gcc/rust/ChangeLog:

* backend/rust-compile-fnparam.cc (CompileFnParam::visit):
Added visit implementation for ReferencePattern.
(CompileFnParam::create_tmp_param_var):
Refactored duplicated code into a helper function.
* backend/rust-compile-fnparam.h: Added visit implementation for
ReferencePattern.
* backend/rust-compile-pattern.cc (CompilePatternBindings::visit):
Added visit implementation for ReferencePattern and
IdentifierPattern.
* backend/rust-compile-pattern.h: Added visit implementation for
ReferencePattern and IdentifierPattern.

gcc/testsuite/ChangeLog:

* rust/compile/ref_pattern_fn_param.rs: Moved to...
* rust/compile/ref_pattern_fn_param1.rs: ...here.
* rust/compile/ref_pattern_fn_param2.rs: New test.
* rust/execute/torture/ref-pattern1.rs: New test.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: enr: Fetch module items during early name resolution
Arthur Cohen [Thu, 2 Mar 2023 15:56:33 +0000 (16:56 +0100)]
gccrs: enr: Fetch module items during early name resolution

This is important as public macros can be present in other modules,
which would otherwise not be loaded until the expansion phase
happening right after the early name resolution.

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Move
unloaded module item loading to...
* expand/rust-attribute-visitor.cc (AttrVisitor::visit): ...here.

9 months agogccrs: hir: Simplify indentation to single line
Jakub Dupak [Sat, 11 Mar 2023 19:50:09 +0000 (20:50 +0100)]
gccrs: hir: Simplify indentation to single line

Improves readability by better corresponding to resulting output.

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::visit): Simplify indentation dump to single line.

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
9 months agogccrs: hir: Unify indentation approach with ast
Jakub Dupak [Sat, 11 Mar 2023 19:36:11 +0000 (20:36 +0100)]
gccrs: hir: Unify indentation approach with ast

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Indent::Indent): Move to separate file.
(operator<<): Move to separate file.
(Indent::increment): Move to separate file.
(Indent::decrement): Move to separate file.
* ast/rust-ast-dump.h (class Indent): Move to separate file.
* hir/rust-hir-dump.cc (Dump::Dump): Use new indentation object.
(Dump::go): Use new indentation object.
(Dump::visit): Use new indention object.
* hir/rust-hir-dump.h: Use new indentation object.
* util/rust-dump.h: New file. Moved Indentation from rust-ast-dump.cc

Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
9 months agogccrs: hir: Fix ReferencePattern typechecking
Mahmoud Mohamed [Sun, 12 Mar 2023 23:26:13 +0000 (02:26 +0300)]
gccrs: hir: Fix ReferencePattern typechecking

The visit for TypeCheckPattern::visit (HIR::ReferencePattern) was missing
an assignment to infered

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit):
Add the missing infered type assignment

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: fix some clang warnings
Marc Poulhiès [Sat, 17 Dec 2022 15:23:05 +0000 (16:23 +0100)]
gccrs: fix some clang warnings

This fixes some extra warnings reported by clang.

gcc/rust/ChangeLog:
PR rust/108111
* ast/rust-ast-full-decls.h (StructPatternElements): Declare as a
class.
* ast/rust-item.h (EnumItem): Mark several method as being
overrides.
* ast/rust-pattern.h (StructPattern::get_locus): Add override.
* lex/rust-lex.h (BufferInputSource): Use reference_wrapper
instead of bare reference.
(TokenSource::get): Add method to implement the reference_wrapper
interface.
* typecheck/rust-tyty.h (TypeBoundPredicate): Add empty dtor.
* util/rust-buffered-queue.h (peek): Source token stream is now
using a reference_wrapper, use .get()

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
9 months agogccrs: hir: add a helper function for visit
vincent [Tue, 7 Mar 2023 20:32:04 +0000 (20:32 +0000)]
gccrs: hir: add a helper function for visit

This commit adds a helper function for TypeCheckPattern::visit
(HIR::RangePattern &pattern) to remove redundancy

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc
(TypeCheckPattern::visit): rewrite part code to helper function
(TypeCheckPattern::typecheck_range_pattern_bound): helper function
* typecheck/rust-hir-type-check-pattern.h
(TypeCheckPattern::typecheck_range_pattern_bound):
change the parameter of the function

Signed-off-by: Jiakun Fan <jfan30@u.rochester.edu>
9 months agogccrs: typecheck: Add basic typechecking for ReferenceType
goar5670 [Mon, 6 Mar 2023 22:25:39 +0000 (01:25 +0300)]
gccrs: typecheck: Add basic typechecking for ReferenceType

gcc/rust/ChangeLog:
* hir/tree/rust-hir-pattern.h (HIR::ReferencePattern):
added get_referenced_pattern function.
* resolve/rust-ast-resolve-pattern.h
(Resolve::PatternDeclaration): add visit function for
AST::ReferencePattern
* typecheck/rust-hir-type-check-pattern.cc
(Resolver::TypeCheckPattern): add visit function for
HIR::ReferencePattern

gcc/testsuite/ChangeLog:
* rust/compile/ref_pattern_fn_param.rs: New test.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: Add coherence related lang_items
Owen Avery [Tue, 7 Mar 2023 18:37:08 +0000 (13:37 -0500)]
gccrs: Add coherence related lang_items

gcc/rust/ChangeLog:

* util/rust-lang-item.h
(RustLangItem::ItemType): New enumerators.
(RustLangItem::Parse): Parse new enumerators.
(RustLangItem::ToString): Handle new enumerators.

gcc/testsuite/ChangeLog:

* rust/compile/lang-impl.rs: New test.

9 months agogccrs: Add AltPattern HIR node
Owen Avery [Mon, 20 Feb 2023 19:46:03 +0000 (14:46 -0500)]
gccrs: Add AltPattern HIR node

Example:
match x {
    0 | 1 => true,
    _ => false
}

gcc/rust/ChangeLog:

* backend/rust-compile-fnparam.h:
(CompileFnParam::visit): Add AltPattern visitor.
* backend/rust-compile-pattern.h:
(CompilePatternCaseLabelExpr::visit): Add AltPattern visitor.
(CompilePatternBindings::visit): Add AltPattern visitor.
(CompilePatternLet::visit): Add AltPattern visitor.
* backend/rust-compile-resolve-path.h:
(ResolvePathRef::visit): Add AltPattern visitor.
* backend/rust-compile-var-decl.h:
(CompileVarDecl::visit): Add AltPattern visitor.
* checks/errors/rust-const-checker.cc
(ConstChecker::visit): Add AltPattern visitor.
* checks/errors/rust-const-checker.h:
(ConstChecker::visit): Add AltPattern visitor.
* checks/errors/rust-unsafe-checker.cc
(UnsafeChecker::visit): Add AltPattern visitor.
* checks/errors/rust-unsafe-checker.h:
(UnsafeChecker::visit): Add AltPattern visitor.
* hir/rust-hir-dump.cc
(Dump::visit): Add AltPattern visitor.
* hir/rust-hir-dump.h:
(Dump::visit): Add AltPattern visitor.
* hir/tree/rust-hir-full-decls.h
(class AltPattern): Add forward declaration.
* hir/tree/rust-hir-pattern.h
(class AltPattern): New class.
* hir/tree/rust-hir-visitor.h:
(HIRFullVisitor::visit): Add AltPattern visitor.
(HIRFullVisitorBase::visit): Add AltPattern visitor.
(HIRPatternVisitor::visit): Add AltPattern visitor.
* hir/tree/rust-hir.h:
(Pattern::PatternType::ALT): New enumerator.
* typecheck/rust-hir-type-check-pattern.cc
(TypeCheckPattern::visit): Add AltPattern visitor.
* typecheck/rust-hir-type-check-pattern.h:
(TypeCheckPattern::visit): Add AltPattern visitor.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: expand: Add stringify macro
Pierre-Emmanuel Patry [Mon, 6 Mar 2023 08:58:02 +0000 (09:58 +0100)]
gccrs: expand: Add stringify macro

Add the stringify macro expansion as well as some tests.

gcc/rust/ChangeLog:

* ast/rust-macro.cc (builtin_macro_from_string): Add identifier
identification.
* ast/rust-macro.h (enum class): Add Stringify builtin macro
type.
* expand/rust-macro-builtins.cc (make_macro_path_str): Add path
for builtin stringify macro.
(MacroBuiltin::stringify_handler): Add handler for builtin
stringify macro.
* expand/rust-macro-builtins.h: Add stringify handler's
prototype.
* util/rust-hir-map.cc (Mappings::insert_macro_def): Add
stringify handler to builtin hir map.

gcc/testsuite/ChangeLog:

* rust/compile/stringify.rs: Add a basic test with some text.
* rust/execute/torture/builtin_macro_stringify.rs: Verify the
text is left as is without any other macro expansion.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
9 months agogccrs: Add length checking for tuple patterns
Nikos Alexandris [Fri, 3 Mar 2023 21:55:59 +0000 (23:55 +0200)]
gccrs: Add length checking for tuple patterns

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit):
Add length checking for tuple patterns.
(TypeCheckPattern::emit_pattern_size_error): New function.
* typecheck/rust-hir-type-check-pattern.h: New function
emit_pattern_size_error.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Nikos Alexandris <nikos-alexandris@protonmail.com>
9 months agogccrs: parser: Fix handling of multiple left angles in null denotation
goar5670 [Sat, 4 Mar 2023 19:13:18 +0000 (22:13 +0300)]
gccrs: parser: Fix handling of multiple left angles in null denotation

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h (Parser::parse_expr):
split LEFT_SHIFT before null_denotation.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
9 months agogccrs: Cleanup and Assertion
Parthib [Mon, 6 Mar 2023 02:04:43 +0000 (07:34 +0530)]
gccrs: Cleanup and Assertion

gcc/rust/ChangLog:

* rust-object-export.cc: Modified
* hir/rust-ast-lower-type.cc (rust_fatal_error): Removed
(rust_assert): Added

Signed-off-by: Parthib <parthibdutta02@gmail.com>
9 months agogccrs: Resolve closure parameters without given type
Owen Avery [Sat, 4 Mar 2023 01:05:59 +0000 (20:05 -0500)]
gccrs: Resolve closure parameters without given type

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver.cc
(EarlyNameResolver::visit):
Check for closure parameter without given type.

gcc/testsuite/ChangeLog:

* rust/compile/closure_no_type_anno.rs: Extend test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
9 months agogccrs: make predicate bounds overwrite-able
Philip Herron [Fri, 3 Mar 2023 18:17:50 +0000 (18:17 +0000)]
gccrs: make predicate bounds overwrite-able

When compiling types especially when using queries it needs to be
permissive and allow them to be overwritten and a predicate might have one
set of details in one senario and a new one with the same id later on but
with different types.

Fixes #1524

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* typecheck/rust-typecheck-context.cc (TypeCheckContext::insert_resolved_predicate): remove

gcc/testsuite/ChangeLog:

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

9 months agogccrs: Fix name-resolution to be permissive and carry on
Philip Herron [Fri, 3 Mar 2023 18:01:01 +0000 (18:01 +0000)]
gccrs: Fix name-resolution to be permissive and carry on

There are a few edge cases when resolving TypePaths that we cannot fully
resolve to an explicit node_id and this is expected. So for example

  <A as B>::foo

A and B are simple Type paths and thats 100% but the segment foo cannot be
100% resolved to an explicit node id as this requires type-resolution to
find the correct path. So when we have complex paths such as:

  <<A as B>::foo as C>

The ::foo part will return UNKNOWN_NODEId and we return early and think its
a failure case but its not necessarily a failure but we need to make sure
to name resolve C so when we do type-resolution we can resolve C properly.

Addresses #1524

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-type.cc (ResolveRelativeQualTypePath::resolve_qual_seg): fix

gcc/testsuite/ChangeLog:

* rust/compile/parse_associated_type_as_generic_arg3.rs: remove -fsyntax-only

9 months agogccrs: Add missing node_id copys in the copy constructors
Philip Herron [Fri, 3 Mar 2023 17:59:00 +0000 (17:59 +0000)]
gccrs: Add missing node_id copys in the copy constructors

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* ast/rust-path.h: add missing copy for node_id

9 months agogccrs: cleanup headers
Philip Herron [Fri, 3 Mar 2023 16:44:43 +0000 (16:44 +0000)]
gccrs: cleanup headers

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* rust-gcc.h: remove unused headers

9 months agogccrs: Fix missing move and copy constructors missing the associated-path
Philip Herron [Wed, 1 Mar 2023 22:09:47 +0000 (22:09 +0000)]
gccrs: Fix missing move and copy constructors missing the associated-path

Addresses #1524

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* ast/rust-ast.cc (QualifiedPathInType::as_string): add missing to string
* ast/rust-path.h: add missing copy+move constructors and assignment overloads
* hir/tree/rust-hir-path.h: likewise
* hir/tree/rust-hir.cc (QualifiedPathInType::as_string): add missing to string

gcc/testsuite/ChangeLog:

* rust/compile/parse_associated_type_as_generic_arg.rs: it now works without -fsyntax-only
* rust/compile/parse_associated_type_as_generic_arg2.rs: likewise

9 months agogccrs: destructure parameter names.
Philip Herron [Fri, 3 Mar 2023 16:12:46 +0000 (16:12 +0000)]
gccrs: destructure parameter names.

When we have complex generic code you can end up with situations where we
compile types:

  Maybe<<S as Foo>::A>

Into

  Maybe<<placeholder:<Projection=::()>>>

This calls destructure to cleanup the naming here and avoid making non
canonical TREE_TYPES hitting the verify_gimple code triggering non-trival
constructors.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* typecheck/rust-tyty.cc (ParamType::get_name): call destructure

9 months agogccrs: Take advantage of our new unify_and to inject inference
Philip Herron [Fri, 3 Mar 2023 18:42:21 +0000 (18:42 +0000)]
gccrs: Take advantage of our new unify_and to inject inference

Wen computing higher ranked trait bounds where there are multiple type
params and ones which are not bound entirely on the impl-type we need
to inject inference variables as required to compute the types. The
inference variables we inject are missing the callbacks that we can compute
the bounds properly so this is the first part of the fix.

Addresses #1893

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc: use unify_and infer

This page took 0.116871 seconds and 5 git commands to generate.