]> git.ipfire.org Git - thirdparty/gcc.git/log
thirdparty/gcc.git
18 months agogccrs: Support fully qualified calls to trait items from CallExpr's
Philip Herron [Tue, 4 Apr 2023 14:40:26 +0000 (15:40 +0100)] 
gccrs: Support fully qualified calls to trait items from CallExpr's

Fixes #2070

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
reuse resolve_method_address to monomorphize the call if required
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
allow matching against unimplemented trait item bounds

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: never re-setup trait item bound with a dyn object
Philip Herron [Tue, 4 Apr 2023 15:13:18 +0000 (16:13 +0100)] 
gccrs: never re-setup trait item bound with a dyn object

It results in a recursive generic Param cycle.

gcc/rust/ChangeLog:

* typecheck/rust-hir-path-probe.cc: dont subst with a dynamic object

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: refactor resolve_method_address to be inside base class
Philip Herron [Tue, 4 Apr 2023 14:49:20 +0000 (15:49 +0100)] 
gccrs: refactor resolve_method_address to be inside base class

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address): moved here
* backend/rust-compile-base.h: refactored prototype
* backend/rust-compile-expr.cc (CompileExpr::resolve_method_address): refactor
* backend/rust-compile-expr.h: likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: remove unused code in query_compile
Philip Herron [Tue, 4 Apr 2023 14:37:33 +0000 (15:37 +0100)] 
gccrs: remove unused code in query_compile

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): remove unused

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: cleanup query_compile to reuse destructure call
Philip Herron [Tue, 4 Apr 2023 14:34:03 +0000 (15:34 +0100)] 
gccrs: cleanup query_compile to reuse destructure call

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): call destructure

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: cleanup resolve method address
Philip Herron [Tue, 4 Apr 2023 14:24:01 +0000 (15:24 +0100)] 
gccrs: cleanup resolve method address

Calling PathProbeImplTrait resolves directly to the trait bound in question
to stop resolving to potentially multiple implementations of that bound

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::resolve_method_address):
call path probe impl trait

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: expand: Add outer attribute expansion on expr
Pierre-Emmanuel Patry [Mon, 3 Apr 2023 10:38:50 +0000 (12:38 +0200)] 
gccrs: expand: Add outer attribute expansion on expr

Add additional outer attribute expansion on some expressions.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Add call
to attribute expansion function.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add builtin attribute identification
Pierre-Emmanuel Patry [Mon, 3 Apr 2023 09:03:37 +0000 (11:03 +0200)] 
gccrs: expand: Add builtin attribute identification

Add a function to identify builtin attributes and skip those on proc
macro expansion phase.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit_outer_attrs):
Change condition order.
(ExpandVisitor::visit_inner_using_attrs): Likewise.
(ExpandVisitor::visit_attrs_with_derive): Likewise.
(ExpandVisitor::is_builtin): Add builtin identification
function.
* expand/rust-expand-visitor.h: Add function prototype.
* util/rust-attributes.cc: Add missing rustc_builtin_macro
attribute.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add call to outer attributes stubs
Pierre-Emmanuel Patry [Fri, 31 Mar 2023 10:17:04 +0000 (12:17 +0200)] 
gccrs: expand: Add call to outer attributes stubs

Add call to outer attribute expansion stub on required types.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::expand_struct_fields):
Add call to expand structu fields.
(ExpandVisitor::expand_function_params): Add call to expand
function parameters.
(ExpandVisitor::expand_closure_params): Add call to expand
closure parameters.
(ExpandVisitor::visit): Add calls for item outer attribute
expansions.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add stub function for attribute expansion
Pierre-Emmanuel Patry [Thu, 30 Mar 2023 18:40:07 +0000 (20:40 +0200)] 
gccrs: expand: Add stub function for attribute expansion

Add a stub function and utility functions that should be called on some
nodes to expand attribute procedural macros.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::expand_outer_attribute):
Stub for a single attribute expansion.
(ExpandVisitor::visit_outer_attrs): Visit the attributes to
expand on a given item.
* expand/rust-expand-visitor.h: Add function prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Add outer attribute getter to Expr class
Pierre-Emmanuel Patry [Thu, 30 Mar 2023 18:05:14 +0000 (20:05 +0200)] 
gccrs: ast: Add outer attribute getter to Expr class

We need to retrieve outer attributes from some Expressions depending on
their context. This means this should be retrieved from their parent
node. But expr did not have a getter for outer attributes since some
expr can't have any outer attribute.

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add getter to Expr class.
* ast/rust-expr.h: Add override attribute to existing getters.
Also implement it for RangeExpr, attempting to retrieve outer
attributes on those types will crash the compiler.
* ast/rust-macro.h: Add override attribute to existing getters.
* ast/rust-path.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add inner attribute expansion stubs
Pierre-Emmanuel Patry [Thu, 30 Mar 2023 14:35:53 +0000 (16:35 +0200)] 
gccrs: expand: Add inner attribute expansion stubs

Add function stubs for inner attribute proc macros expansion.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Add call
to inner attribute stub.
(ExpandVisitor::expand_inner_attribute): Expand one single
attribute.
(ExpandVisitor::visit_inner_using_attrs): Expand a given item
using a vector of attributes.
(ExpandVisitor::visit_inner_attrs): Visit inner attributes of a
given item.
* expand/rust-expand-visitor.h: Add function prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add derive expansion stubs
Pierre-Emmanuel Patry [Thu, 30 Mar 2023 14:28:12 +0000 (16:28 +0200)] 
gccrs: expand: Add derive expansion stubs

Add derive macros expansion stub functions.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Add call
to derive expander.
(ExpandVisitor::expand_derive): Expand a single derive.
(ExpandVisitor::visit_attrs_with_derive): Visit an item with
derive attributes.
(ExpandVisitor::is_derive): Identify a derive attribute.
* expand/rust-expand-visitor.h: Add function prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Fix ICE on generic args path
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 17:52:38 +0000 (19:52 +0200)] 
gccrs: ast: Fix ICE on generic args path

The path should be retrieved only when a generic arg has either type.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Fix ICE.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Fix macros in tokenstreams
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 16:22:56 +0000 (18:22 +0200)] 
gccrs: ast: Fix macros in tokenstreams

A default token was used as a separator, wich resulted in a wrong token
stream.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Remove
default separator.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Visit crate inner attributes for tokenstreams
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 14:56:00 +0000 (16:56 +0200)] 
gccrs: ast: Visit crate inner attributes for tokenstreams

Crate level inner attributes were not visited.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Visit inner
attributes.

18 months agogccrs: ast: Change visibility in token type
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 14:19:51 +0000 (16:19 +0200)] 
gccrs: ast: Change visibility in token type

Change the token type from a custom identifier to the reserved token
keyword "in".

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Change token
type.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Fix attribute tokenstreams
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 13:56:02 +0000 (15:56 +0200)] 
gccrs: ast: Fix attribute tokenstreams

Attributes were not converted to a correct tokenstream, furthermore meta
items containers and token trees attribute arguments were not even
implemented. This commit fix the literal attribute type and implement
both unimplemented types.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Fix existing
and implement remaining attribute visitors.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Output union's outer attributes
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 11:53:34 +0000 (13:53 +0200)] 
gccrs: ast: Output union's outer attributes

Output a union's outer attributes in a tokenstream.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add visitor
to the union's outer attributes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Output inner attributes in tokenstreams
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 10:50:25 +0000 (12:50 +0200)] 
gccrs: ast: Output inner attributes in tokenstreams

Inner attributes were not correctly visited.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Add call to
inner attribute visitor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Fix char literal ICE
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 09:54:59 +0000 (11:54 +0200)] 
gccrs: ast: Fix char literal ICE

The code was attempting to convert a char to an integer by parsing it
instead of taking it's raw value.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Fix ICE.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Fix match case comma output
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 09:26:51 +0000 (11:26 +0200)] 
gccrs: ast: Fix match case comma output

Even though the comma may be optional with blocks of expressions, it is
mandatory for a single expression alone. By changing this behavior we
ensure a working behavior with any kind of expressions.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Make comma
mandatory.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Fix tokenstream extern block statements
Pierre-Emmanuel Patry [Tue, 4 Apr 2023 09:24:44 +0000 (11:24 +0200)] 
gccrs: ast: Fix tokenstream extern block statements

The visitor created some additional semicolon between statements in
extern blocks, this lead to empty statement.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit): Remove
additional semicolon output.
* ast/rust-ast-tokenstream.h: Change block visitor prototype
with a default value for trailing tokens.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Move pattern implementation out of rust-ast
Pierre-Emmanuel Patry [Mon, 3 Apr 2023 18:44:45 +0000 (20:44 +0200)] 
gccrs: ast: Move pattern implementation out of rust-ast

Move rust-pattern implementation out of rust-ast.cc in their own file.

gcc/rust/ChangeLog:

* Make-lang.in: Add two new files to the list of sources.
* ast/rust-ast-full.h: Add new ast formatting header.
* ast/rust-ast.cc (enum indent_mode): Move formatting function
to it's own file.
(indent_spaces): Likewise.
(get_string_in_delims): Likewise.
(enum AttrMode): Likewise.
(get_mode_dump_desc): Likewise.
(append_attributes): Likewise.
(unquote_string): Likewise.
(GroupedExpr::as_string): Move pattern implementation to it's
own file.
(RangePattern::as_string): Likewise.
(RangePatternBoundLiteral::as_string): Likewise.
(SlicePattern::as_string): Likewise.
(AltPattern::as_string): Likewise.
(TuplePatternItemsMultiple::as_string): Likewise.
(TuplePatternItemsRanged::as_string): Likewise.
(TuplePattern::as_string): Likewise.
(StructPatternField::as_string): Likewise.
(StructPatternFieldIdent::as_string): Likewise.
(StructPatternFieldTuplePat::as_string): Likewise.
(StructPatternFieldIdentPat::as_string): Likewise.
(StructPatternElements::as_string): Likewise.
(StructPattern::as_string): Likewise.
(LiteralPattern::as_string): Likewise.
(ReferencePattern::as_string): Likewise.
(IdentifierPattern::as_string): Likewise.
(TupleStructItemsNoRange::as_string): Likewise.
(TupleStructItemsRange::as_string): Likewise.
(TupleStructPattern::as_string): Likewise.
(GroupedExpr::accept_vis): Likewise.
(LiteralPattern::accept_vis): Likewise.
(IdentifierPattern::accept_vis): Likewise.
(WildcardPattern::accept_vis): Likewise.
(RestPattern::accept_vis): Likewise.
(RangePatternBoundLiteral::accept_vis): Likewise.
(RangePatternBoundPath::accept_vis): Likewise.
(RangePatternBoundQualPath::accept_vis): Likewise.
(RangePattern::accept_vis): Likewise.
(ReferencePattern::accept_vis): Likewise.
(StructPatternFieldTuplePat::accept_vis): Likewise.
(StructPatternFieldIdentPat::accept_vis): Likewise.
(StructPatternFieldIdent::accept_vis): Likewise.
(StructPattern::accept_vis): Likewise.
(TupleStructItemsNoRange::accept_vis): Likewise.
(TupleStructItemsRange::accept_vis): Likewise.
(TupleStructPattern::accept_vis): Likewise.
(TuplePatternItemsMultiple::accept_vis): Likewise.
(TuplePatternItemsRanged::accept_vis): Likewise.
(TuplePattern::accept_vis): Likewise.
(GroupedPattern::accept_vis): Likewise.
(SlicePattern::accept_vis): Likewise.
(AltPattern::accept_vis): Likewise.
* ast/rust-ast-formatting.cc: New file.
* ast/rust-ast-formatting.h: New file.
* ast/rust-pattern.cc: New file.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Move rust-path implementation out of rust-ast
Pierre-Emmanuel Patry [Mon, 3 Apr 2023 15:22:57 +0000 (17:22 +0200)] 
gccrs: ast: Move rust-path implementation out of rust-ast

Move rust-path implementation details out of rust-ast.cc file. This will
hopefully decrease compile time and make things clearer.

gcc/rust/ChangeLog:

* Make-lang.in: Add new file for rust-path implementation.
* ast/rust-ast.cc (PathInExpression::as_string): Move function
to the new file.
(PathPattern::as_string): Likewise.
(QualifiedPathInExpression::as_string): Likewise.
(ConstGenericParam::as_string): Likewise.
(QualifiedPathInType::as_string): Likewise.
(TypePath::as_string): Likewise.
(PathPattern::convert_to_simple_path): Likewise.
(TypePath::as_simple_path): Likewise.
(PathExprSegment::as_string): Likewise.
(GenericArgs::as_string): Likewise.
(GenericArgsBinding::as_string): Likewise.
(TypePath::to_trait_bound): Likewise.
(TypePathSegmentGeneric::as_string): Likewise.
(TypePathFunction::as_string): Likewise.
(TypePathSegmentFunction::as_string): Likewise.
(ConstGenericParam::accept_vis): Likewise.
(PathInExpression::accept_vis): Likewise.
(TypePathSegment::accept_vis): Likewise.
(TypePathSegmentGeneric::accept_vis): Likewise.
(TypePathSegmentFunction::accept_vis): Likewise.
(TypePath::accept_vis): Likewise.
(QualifiedPathInExpression::accept_vis): Likewise.
(QualifiedPathInType::accept_vis): Likewise.
(GenericArg::disambiguate_to_const): Likewise.
(GenericArg::disambiguate_to_type): Likewise.
* ast/rust-path.cc: New file.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: Fix ICE using constructors for intilizers in statics
Philip Herron [Sun, 2 Apr 2023 20:59:50 +0000 (21:59 +0100)] 
gccrs: Fix ICE using constructors for intilizers in statics

We are getting constant expressions for the initilizers for static items
this hits an assertion in the GCC middle-end which is looking for a
constructor so we need to unwrap the constant expression using DECL_INITIAL
as the initilizer to the global static.

Fixes #2080

gcc/rust/ChangeLog:

* backend/rust-compile-item.cc (CompileItem::visit): unwrap the constant expression

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: Handle "fn" and "fn_mut" lang items
Owen Avery [Mon, 3 Apr 2023 22:52:23 +0000 (18:52 -0400)] 
gccrs: Handle "fn" and "fn_mut" lang items

gcc/rust/ChangeLog:

* util/rust-lang-item.h
(RustLangItem::ItemType::FN): Add.
(RustLangItem::ItemType::FN_MUT): Add.
(RustLangItem::Parse): Handle FN and FN_MUT.
(RustLangItem::ToString): Handle FN and FN_MUT.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: Fix bad cast as a char
Emanuele Micheletti [Fri, 31 Mar 2023 10:50:36 +0000 (12:50 +0200)] 
gccrs: Fix bad cast as a char

In rust cast to char is allowed only from u8 type.
This patch handles fix the case when the type is infered from an integer value,
allowing only the u8 case'

Fixes #2027

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules):
case INTEGRAL handles TypeKind::CHAR

gcc/testsuite/ChangeLog:

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

Signed-off-by: Emanuele Micheletti <micheletti.emanuele@hotmail.com>
18 months agogccrs: Fix handling of generic arguments for lifetimes only
Philip Herron [Wed, 29 Mar 2023 12:35:53 +0000 (13:35 +0100)] 
gccrs: Fix handling of generic arguments for lifetimes only

We might have generics that are only for lifetimes this is a bad error
check. We can simply rely on the function to marshall the HIR generics
arguements for the type system to do the error handling for us which will
be more acurate anyway.

Fixes #2043 #2039

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit):
add check for reference marker and type's in self params
(ResolveItem::visit): likewise
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path):
remove bad generics check
(TypeCheckExpr::resolve_segments): likewise
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): likewise

gcc/testsuite/ChangeLog:

* rust/compile/const_generics_5.rs: update test case
* rust/compile/issue-2043.rs: New test.
* rust/compile/issue-2039.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: add missing name resolution to self params with specified types
Philip Herron [Wed, 29 Mar 2023 13:24:12 +0000 (14:24 +0100)] 
gccrs: add missing name resolution to self params with specified types

In rust it is possible to write method where you explicitly specify the
type of a self param. It does not however allow for you to use reference
destructuring in this senario.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-item.cc
(ResolveTraitItems::visit): add name resolution self param
(ResolveItem::visit): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: make distinction between error and empty for substution args
Philip Herron [Wed, 29 Mar 2023 14:00:39 +0000 (15:00 +0100)] 
gccrs: make distinction between error and empty for substution args

When handling generics of only lifetimes we have an empty
SubstitutionArgumentMappings which was being detected as an error but this
is not the case as we don't currently handle const generics or generic
lifetimes so this makes the distinction between an error occurred or its
simply empty because it was all only lifetime arguments and we don't care
about it.

Addresses #2043 #2039

gcc/rust/ChangeLog:

* typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::TypeBoundPredicate): this is an empty
(TypeBoundPredicate::operator=): likewise
* typecheck/rust-tyty-subst.cc (SubstitutionArgumentMappings::empty): new interface
(SubstitutionArgumentMappings::is_error): this marks it as an error
* typecheck/rust-tyty-subst.h: update prototypes

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: privacy reporter should be permissive of error types
Philip Herron [Wed, 29 Mar 2023 13:01:34 +0000 (14:01 +0100)] 
gccrs: privacy reporter should be permissive of error types

gcc/rust/ChangeLog:

* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::check_base_type_privacy): allow error types to be permissive

Signed-off-by: Philip Herron <herron.philip@googlemail.com>a
18 months agogccrs: ast: Fix various TokenStream visitors
Pierre-Emmanuel Patry [Tue, 28 Mar 2023 07:56:52 +0000 (09:56 +0200)] 
gccrs: ast: Fix various TokenStream visitors

TokenStream visitors still have many issues, this commit fixes some of
them.

gcc/rust/ChangeLog:

* ast/rust-ast-tokenstream.cc (TokenStream::visit_items_as_lines):
Handle trailing chars.
(TokenStream::visit_items_as_block): Likewise.
(TokenStream::visit): Fix visitor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: token: Add type hints to string dump
Pierre-Emmanuel Patry [Mon, 27 Mar 2023 13:41:47 +0000 (15:41 +0200)] 
gccrs: token: Add type hints to string dump

The conversion to string of any known type literal was not giving back
any type hint, not even quotes for string. This commit fix this.

gcc/rust/ChangeLog:

* lex/rust-token.cc (Token::as_string): Add type hint output.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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.

18 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.

18 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

18 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.

18 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>
18 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>
18 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.

18 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.

18 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.

18 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.

18 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.

18 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.

18 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>
18 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>
18 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.

18 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.

18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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.

18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>
18 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>