]> git.ipfire.org Git - thirdparty/gcc.git/log
thirdparty/gcc.git
18 months agogccrs: Add location info to `Rust::Identifier`
Raiki Tamura [Mon, 3 Jul 2023 07:34:59 +0000 (16:34 +0900)] 
gccrs: Add location info to `Rust::Identifier`

gcc/rust/ChangeLog:

* ast/rust-ast.h: Modify constructors of `Rust::Identifier`
* ast/rust-pattern.h: Likewise.
* hir/tree/rust-hir-item.h: Likewise.
* parse/rust-parse-impl.h (Parser::parse_macro_rules_def): Likewise.
(Parser::parse_decl_macro_def): Likewise.
(Parser::parse_macro_match_fragment): Likewise.
(Parser::parse_module): Likewise.
(Parser::parse_use_tree): Likewise.
(Parser::parse_function): Likewise.
(Parser::parse_type_param): Likewise.
(Parser::parse_type_alias): Likewise.
(Parser::parse_struct): Likewise.
(Parser::parse_struct_field): Likewise.
(Parser::parse_enum): Likewise.
(Parser::parse_enum_item): Likewise.
(Parser::parse_union): Likewise.
(Parser::parse_static_item): Likewise.
(Parser::parse_trait): Likewise.
(Parser::parse_trait_item): Likewise.
(Parser::parse_trait_type): Likewise.
(Parser::parse_trait_const): Likewise.
(Parser::parse_external_item): Likewise.
(Parser::parse_generic_args_binding): Likewise.
(Parser::parse_method): Likewise.
(Parser::parse_maybe_named_param): Likewise.
(Parser::parse_identifier_pattern): Likewise.
(Parser::parse_struct_expr_field): Likewise.
(ResolveItem::visit): Likewise.
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit):Add comments
* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise.
* checks/lints/rust-lint-scan-deadcode.h: Fix error location

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
18 months agogccrs: Change class Location into typedef
Owen Avery [Sun, 2 Jul 2023 23:59:29 +0000 (19:59 -0400)] 
gccrs: Change class Location into typedef

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc
(HIRCompileBase::address_expression): Remove gcc_location method call.
(HIRCompileBase::indirect_expression): Likewise.
(HIRCompileBase::compile_constant_item): Likewise.
(HIRCompileBase::named_constant_expression): Likewise.
* backend/rust-compile-expr.cc
(CompileExpr::visit): Remove gcc_location method call, use UNKNOWN_LOCATION to initialize.
(CompileExpr::get_fn_addr_from_dyn): Remove gcc_location method call.
(CompileExpr::type_cast_expression): Likewise.
* backend/rust-compile-intrinsic.cc
(transmute_handler): Replace "Location ().gcc_location ()" with UNKNOWN_LOCATION.
(copy_nonoverlapping_handler): Likewise.
(prefetch_data_handler): Likewise.
(atomic_store_handler_inner): Likewise.
(atomic_load_handler_inner): Likewise.
* resolve/rust-ast-resolve-expr.cc
(ResolveExpr::visit): Remove gcc_location method call.
* rust-diagnostics.cc
(rust_be_error_at): Likewise.
(rust_be_warning_at): Likewise.
(rust_be_fatal_error): Likewise.
(rust_be_inform): Likewise.
* rust-diagnostics.h
(rust_sorry_at): Likewise.
* rust-gcc.cc
(Bvariable::get_tree): Likewise.
(Gcc_backend::fill_in_fields): Likewise.
(Gcc_backend::named_type): Likewise.
(Gcc_backend::real_part_expression): Likewise.
(Gcc_backend::imag_part_expression): Likewise.
(Gcc_backend::complex_expression): Likewise.
(Gcc_backend::convert_expression): Likewise.
(Gcc_backend::struct_field_expression): Likewise.
(Gcc_backend::compound_expression): Likewise.
(Gcc_backend::conditional_expression): Likewise.
(Gcc_backend::negation_expression): Likewise.
(Gcc_backend::arithmetic_or_logical_expression): Likewise.
(Gcc_backend::arithmetic_or_logical_expression_checked): Likewise.
(Gcc_backend::comparison_expression): Likewise.
(Gcc_backend::lazy_boolean_expression): Likewise.
(Gcc_backend::constructor_expression): Likewise.
(Gcc_backend::array_constructor_expression): Likewise.
(Gcc_backend::array_initializer): Likewise.
(Gcc_backend::array_index_expression): Likewise.
(Gcc_backend::call_expression): Likewise.
(Gcc_backend::assignment_statement): Likewise.
(Gcc_backend::return_statement): Likewise.
(Gcc_backend::exception_handler_statement): Likewise.
(Gcc_backend::if_statement): Likewise.
(Gcc_backend::loop_expression): Likewise.
(Gcc_backend::exit_expression): Likewise.
(Gcc_backend::block): Likewise.
(Gcc_backend::convert_tree): Likewise.
(Gcc_backend::global_variable): Likewise.
(Gcc_backend::local_variable): Likewise.
(Gcc_backend::parameter_variable): Likewise.
(Gcc_backend::static_chain_variable): Likewise.
(Gcc_backend::temporary_variable): Likewise.
(Gcc_backend::label): Likewise.
(Gcc_backend::goto_statement): Likewise.
(Gcc_backend::label_address): Likewise.
(Gcc_backend::function): Likewise.
* rust-linemap.cc
(Gcc_linemap::to_string): Likewise.
(Gcc_linemap::location_file): Likewise.
(Gcc_linemap::location_line): Likewise.
(Gcc_linemap::location_column): Likewise.
(Gcc_linemap::is_predeclared): Likewise.
(Gcc_linemap::is_unknown): Likewise.
(RichLocation::RichLocation): Likewise.
(RichLocation::add_range): Likewise.
(RichLocation::add_fixit_insert_before): Likewise.
(RichLocation::add_fixit_insert_after): Likewise.
* rust-location.h
(class Location): Replace with typedef.
(operator<): Remove.
(operator==): Remove.
(operator+): Remove.
(operator-): Remove.
* typecheck/rust-hir-trait-resolve.cc
(AssociatedImplTrait::setup_associated_types): Initialize Location with UNKNOWN_LOCATION.
* typecheck/rust-hir-type-check-stmt.cc
(TypeCheckStmt::visit): Likewise.
* util/rust-token-converter.cc
(convert): Remove gcc_location method call.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: HIR cleanup
Marc Poulhiès [Sun, 2 Jul 2023 21:33:02 +0000 (23:33 +0200)] 
gccrs: HIR cleanup

Some more HIR cleanup.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.h (BorrowExpr::is_double_borrow): New.
(ArrayElemsCopied::get_elem_to_copy)
(ArrayElemsCopied::get_num_copies_expr): Return unique_ptr.
(CallExpr::get_fnexpr): Likewise.
(ReturnExpr::get_expr): Likewise.
(StructExprStructFields::get_struct_base): New.
(MethodCallExpr::has_params): New.
(ClosureExpr::has_params): New.
(BlockExpr::get_inner_attrs): New.
(WhileLoopExpr::get_patterns): New.
(ForLoopExpr::get_pattern): New.
(IfLetExpr::get_if_block): Return ref to unique_ptr.
(IfLetExprConseqElse::get_else_block): Likewise.
(MatchExpr::get_inner_attrs): New.
* hir/tree/rust-hir-item.h (Module::get_module_name): New.
(ExternCrate::get_referenced_crate)
(ExternCrate::get_as_clause_name): New.
(UseTreeGlob::get_glob_type, UseTreeGlob::get_path): New.
(UseTreeList::get_path_type, UseTreeList::get_path)
(UseTreeList::get_trees): New.
(TraitItemKind::get_expr): Remove assert. unique_ptr can
be "empty". Must be checked in caller.
* hir/tree/rust-hir-pattern.h (IdentifierPattern::get_is_ref)
(IdentifierPattern::get_to_bind): New.
(RangePatternBoundType::get_has_minus): New.
(RangePattern::get_has_ellipsis_syntax): New.
(StructPatternField::get_outer_attrs): New.
(StructPatternFieldTuplePat::get_index)
(StructPatternFieldTuplePat::get_tuple_pattern): New.
(StructPatternFieldIdent::get_has_ref): New.
* hir/tree/rust-hir-stmt.h (LetStmt::get_outer_attrs): New.
(LetStmt::get_type)
(LetStmt::get_init_expr, LetStmt::get_pattern): Return unique_ptr.
(ExprStmt::get_expr): Likewise.
* hir/tree/rust-hir-type.h (TraitBound::get_for_lifetimes): New.
(TraitBound::get_in_params): New.
(TraitBound::get_opening_question_mark): New.
(ImplTraitType::get_type_param_bounds): New.
(TraitObjectType::get_has_dyn): New.
(TraitBound::get_type_in_parens): New.
(ImplTraitTypeOneBound::get_trait_bound): New.
(BareFunctionType::get_for_lifetimes)
(BareFunctionType::get_is_variadic)
(BareFunctionType::get_function_qualifiers): New.
* hir/tree/rust-hir.h (class Expr): Virtual inherit from FullVisitable.
(class Pattern): Likewise.
(ConstGenericParam::get_name): New.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Adjust call.
(TypeCheckExpr::resolve_fn_trait_call): Likewise.
* backend/rust-compile-expr.cc (CompileExpr::visit): Adjust call.
(CompileExpr::array_copied_expr): Likewise.
(CompileExpr::generate_possible_fn_trait_call): Likewise.
* backend/rust-compile-stmt.cc (CompileStmt::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
* hir/rust-hir-dump.cc (Dump::visit): Likewise.
* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit): Likewise.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
18 months agogccrs: [E0034] Ambiguous Method Call Error
Muhammad Mahad [Mon, 3 Jul 2023 12:22:15 +0000 (17:22 +0500)] 
gccrs: [E0034] Ambiguous Method Call Error

Ambiguous Method Call Error - more than one method
has the same prototype

gcc/rust/ChangeLog:

* typecheck/rust-hir-path-probe.h: called error function

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: [E0229] associated type bindings error
Muhammad Mahad [Mon, 3 Jul 2023 16:08:33 +0000 (21:08 +0500)] 
gccrs: [E0229] associated type bindings error

Associated type binding outside of type
parameter Declaration and where Clause

gcc/rust/ChangeLog:

* typecheck/rust-tyty-subst.cc (SubstitutionRef::get_mappings_from_generic_args):
called error function

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: minor HIR interface cleanup
Marc Poulhiès [Wed, 28 Jun 2023 20:11:50 +0000 (22:11 +0200)] 
gccrs: minor HIR interface cleanup

Minor changes in HIR interface: add missing getters, make some classes
inherit from FullVisitable.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.h (class ArrayElems): Inherit from FullVisitable.
(class StructExprField): Likewise.
* hir/tree/rust-hir-item.h (class WhereClauseItem): Likewise.
(class UseTree): Likewise.
(UseTreeRebind::get_path, UseTreeRebind::get_identifier)
(UseTreeRebind::get_bind_type): New.
(UseDeclaration::get_use_tree): New.
(struct TraitFunctionDecl): Change struct to ...
(class TraitFunctionDecl): ... class.
(TraitFunctionDecl::get_where_clause): New.
(StructField::get_outer_attrs): New.
(struct TupleField): Change struct to ...
(class TupleField): ... class.
(TupleField::get_visibility, TupleField::get_outer_attrs): New.
* hir/tree/rust-hir-pattern.h (class TupleStructItems): Inherit
from FullVisitable.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
18 months agogccrs: Remove unnecessary methods of `Rust::Lexer`
Raiki Tamura [Thu, 29 Jun 2023 06:02:21 +0000 (15:02 +0900)] 
gccrs: Remove unnecessary methods of `Rust::Lexer`

gcc/rust/ChangeLog:

* lex/rust-lex.cc (Lexer::build_token): Replace function call.
(Lexer::parse_string): Likewise.
(Lexer::parse_identifier_or_keyword): Likewise.
(Lexer::parse_raw_string): Likewise.
(Lexer::parse_char_or_lifetime): Likewise.
(Lexer::get_input_codepoint_length): Deleted.
(Lexer::peek_codepoint_input): Deleted.
(Lexer::skip_codepoint_input): Deleted.
* lex/rust-lex.h: Remove some methods and fields

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
18 months agogccrs: Track fn_once output lang item properly
Philip Herron [Sun, 2 Jul 2023 19:24:43 +0000 (20:24 +0100)] 
gccrs: Track fn_once output lang item properly

In order to setup the Output assoicated type we can rely on using generic
argument bindings. So for example when we have the FnOnce trait:

  #[lang = "fn_once"]
  pub trait FnOnce<Args> {
      #[lang = "fn_once_output"]
      type Output;

      extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
  }

Thn we might have a function such as:

  pub fn map<R, F: FnOnce(T) -> R>(self, f: F) -> Option<R> { ... }

This trait bound predicate of FnOnce(T) -> R we setup generics for the
bound as:

  FnOnce<(T), Output=R>

This means we can reuse our generic arguments handling to get this support.

Fixes #2105

gcc/rust/ChangeLog:

* typecheck/rust-tyty-bounds.cc (TypeCheckBase::get_predicate_from_bound): track output
* util/rust-hir-map.cc (Mappings::lookup_trait_item_lang_item): new helper
* util/rust-hir-map.h: add prototype for helper

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: fix ICE with inserting autoderef mappings
Philip Herron [Fri, 30 Jun 2023 21:08:10 +0000 (22:08 +0100)] 
gccrs: fix ICE with inserting autoderef mappings

We were using the call-expr id for the autoderef mappings but this doesn't
work when the call expression itself is part of a coercion-site so this
changes the code to use the call-expression function path expression for
the id instead which will not be duplicated again.

Addresses #2105

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_possible_fn_trait_call): use fnexpr
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::resolve_fn_trait_call): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: [E0027] struct pattern fails to specify struct's fields
Muhammad Mahad [Tue, 27 Jun 2023 12:08:59 +0000 (17:08 +0500)] 
gccrs: [E0027] struct pattern fails to specify struct's fields

A pattern for a struct fails to specify a sub-pattern
for every one of the struct's fields. - pattern does
not mention fields `x`, `y`

gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): called rust_error_at

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: refactor: make crate.items private
Marc Poulhiès [Thu, 29 Jun 2023 22:44:52 +0000 (00:44 +0200)] 
gccrs: refactor: make crate.items private

Introduce Crate::get_items () and fixup all callers.

gcc/rust/ChangeLog:

* hir/tree/rust-hir.h (struct Crate): Rename struct into ...
(class Crate): ... class, and add get_items.
* backend/rust-compile.cc (CompileCrate::go): Adapt to visibility change of items.
* checks/errors/privacy/rust-privacy-check.cc (Resolver::resolve): Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::go): Likewise.
* checks/errors/privacy/rust-pub-restricted-visitor.cc (PubRestrictedVisitor::go): Likewise.
* checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::go): Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::go): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::go): Likewise.
* checks/lints/rust-lint-marklive.cc (FindEntryPoint::find): Likewise.
* checks/lints/rust-lint-scan-deadcode.h (ScanDeadCode::Scan): Likewise.
* metadata/rust-export-metadata.cc (PublicInterface::gather_export_data): Likewise.
* typecheck/rust-hir-type-check.cc (TypeResolution::Resolve): Likewise.
* hir/rust-hir-dump.cc (CompileCrate::go): Likewise.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
18 months agogccrs: Add new getters
Marc Poulhiès [Thu, 29 Jun 2023 18:47:16 +0000 (20:47 +0200)] 
gccrs: Add new getters

Some class members were not accessible from outside, which will be needed
for the new HIR dump.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.h (AwaitExpr::get_awaited_expr): New.
(AsyncBlockExpr::get_has_move, AsyncBlockExpr::get_block_expr): New.
* hir/tree/rust-hir-item.h (Enum::get_where_clause): New.
(Trait::get_where_clause, Trait::is_unsafe): New.
(ExternalItem::get_visibility): New.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
18 months agogccrs: FullVisitable abstract class
Marc Poulhiès [Thu, 29 Jun 2023 18:45:25 +0000 (20:45 +0200)] 
gccrs: FullVisitable abstract class

Introduce FullVisitable class. HIR nodes implementing the FullVisitor
interface should inherit from this class.

gcc/rust/ChangeLog:

* hir/tree/rust-hir.h (class FullVisitable): New.
(class Stmt): Adjust with new class.
(class Expr): Likewise.
(class Pattern): Likewise.
(class Type): Likewise.
(class TypeParamBound): Likewise.
(class GenericParam): Likewise.
(class TraitItem): Likewise.
(class ImplItem): Likewise.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
18 months agogccrs: factor out inner attributes in a class
Marc Poulhiès [Wed, 28 Jun 2023 20:02:37 +0000 (22:02 +0200)] 
gccrs: factor out inner attributes in a class

Introduce WithInnerAttrs class that can be inherited instead of
duplicating the same data/method in all HIR classes.

gcc/rust/ChangeLog:

* hir/tree/rust-hir.h (class WithInnerAttrs): New.
(class Item): Adjust to new WithInnerAttrs class.
(struct Crate): Likewise.
* hir/rust-hir-dump.cc (Dump::go): Likewise.
(Dump::visit): Likewise.
* hir/tree/rust-hir-expr.h (class GroupedExpr): Likewise.
(class ArrayExpr): Likewise.
(class TupleExpr): Likewise.
(class StructExprStruct): Likewise.
(class BlockExpr): Likewise.
(class MatchExpr): Likewise.
* hir/tree/rust-hir-item.h (TypeParam::get_outer_attribute): New.
(class Module): Adjust to new  WithInnerAttrs class.
(struct StructField): change struct to...
(class StructField): ... a class.
(class ImplBlock): Adjust to new  WithInnerAttrs class.
(class ExternBlock): Likewise.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
18 months agogccrs: cleanup getters to return &unique_ptr instead of pointer
Marc Poulhiès [Sun, 28 May 2023 20:10:35 +0000 (22:10 +0200)] 
gccrs: cleanup getters to return &unique_ptr instead of pointer

Make all getter methods in HIR classes return a unique_ptr reference
instead of a pointer and adjust all callers.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-type.h (ArrayType::get_element_type): Returns
unique_ptr.
* hir/tree/rust-hir-expr.h (ArithmeticOrLogicalExpr::get_lhs)
(ArithmeticOrLogicalExpr::get_rhs): Likewise.
(ComparisonExpr::get_lhs, ComparisonExpr::get_rhs): Likewise.
(LazyBooleanExpr::get_lhs, LazyBooleanExpr::get_rhs): Likewise.
(AssignmentExpr::get_lhs, AssignmentExpr::get_rhs): Likewise.
(ArrayExpr::get_internal_elements): Likewise.
(ArrayIndexExpr::get_index_expr, ArrayIndexExpr::get_array_expr):
Likewise.
(StructExprFieldWithVal::get_value): Likewise.
(IfExpr::get_if_condition, IfExpr::get_if_block): Likewise.
(ExprWithBlock::get_else_block): Likewise.
* hir/tree/rust-hir-item.h (FunctionParam::get_param_name)
(FunctionParam::get_type): Likewise.
(ConstantItem::get_type, ConstantItem::get_expr): Likewise.
(StaticItem::get_expr, StaticItem::get_type): Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit):
Adjust for previous change.
* backend/rust-compile-block.cc (CompileConditionalBlocks::visit):
Likewise.
* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
* backend/rust-compile-struct-field-expr.cc
(CompileStructExprField::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::visit): Likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Likewise.
* typecheck/rust-hir-type-check-implitem.cc
(TypeCheckImplItem::visit): Likewise.
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
Likewise.
* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit):
Likewise.
* typecheck/rust-hir-type-check-struct.cc
(TypeCheckStructExpr::visit): Likewise.
* typecheck/rust-hir-type-check.cc
(TraitItemReference::get_type_from_fn): Likewise.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
18 months agogccrs: abi: Add testcase for using "Rust" as ABI string
Arthur Cohen [Fri, 30 Jun 2023 14:40:17 +0000 (16:40 +0200)] 
gccrs: abi: Add testcase for using "Rust" as ABI string

gcc/testsuite/ChangeLog:

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

18 months agogccrs: parser: Propagate type hint value
Pierre-Emmanuel Patry [Wed, 28 Jun 2023 11:53:37 +0000 (13:53 +0200)] 
gccrs: parser: Propagate type hint value

Type hint value was not propagated correctly to LiteralPattern in ast.
This was defaulted to CORETYPE_STR instead, which introduced several
bugs with systems that did require any ast collection.

gcc/rust/ChangeLog:

* ast/rust-pattern.h: Change constructor to accept new parameter
instead of defaulting on string type.
* parse/rust-parse-impl.h (Parser::parse_literal_or_range_pattern):
Propagate type hint.
(Parser::parse_pattern_no_alt): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: fix lexing byte literal
Raiki Tamura [Thu, 29 Jun 2023 03:06:44 +0000 (12:06 +0900)] 
gccrs: fix lexing byte literal

gcc/rust/ChangeLog:

* lex/rust-lex.cc (Lexer::parse_byte_char):add check for range of codepoint

gcc/testsuite/ChangeLog:

* rust/compile/bytecharstring.rs:add test for it

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
18 months agogccrs: refactored field error function
Muhammad Mahad [Thu, 29 Jun 2023 14:24:00 +0000 (19:24 +0500)] 
gccrs: refactored field error function

Refactored field error function into one funciton
Fixes #2336

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (emit_invalid_field_error): Refactored Field
Error Function
(TypeCheckPattern::visit): called emit_invalid_field_error function

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: Improve macro repetition handling
Owen Avery [Sat, 3 Jun 2023 21:40:18 +0000 (17:40 -0400)] 
gccrs: Improve macro repetition handling

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::match_matcher): Remove append_fragment inside repetitions.
(MacroExpander::match_n_matches): Use sub_stack push/pop to handle repetitions.
(MatchedFragmentContainer::add_fragment): Add MatchedFragmentContainer accepting version.
* expand/rust-macro-expand.h
(MatchedFragmentContainer::add_fragment): Likewise.
(MatchedFragmentContainerRepetition::add_fragment): Likewise.
(SubstititionScope::append_fragment): Likewise.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: fix how we handle coercions and casts of unsafe pointers
Philip Herron [Thu, 29 Jun 2023 09:13:01 +0000 (10:13 +0100)] 
gccrs: fix how we handle coercions and casts of unsafe pointers

I copied swap_nonoverlapping badly from libcore which uses an unsafe
pointer case of x as *mut u8, I thought this was x as *mut T when i was
copying code form libcore this caused new infererence variables to be
introduced which actually automatically turned this swap_nonoverlapping
function to turn into a swap_nonoverlapping<u8> and lost the general
generic one in the type system context.

Rust allows for you to cast any pointer to another pointer of different
base type. The rules here are really subtle though because we need to
handle the case where we do really need to unify the types here there
are a few cases to consider but the main three are:

  *u32 vs *u32 -> valid pointers match = simple coercion

  *<?> vs *u8 -> inference variable unified with u8 which is valid

  *T vs *u8 -> invalid coercion as the element types don't match
            -> But this is a valid cast site

The code for casts try a coercion first then if that was sucsessful goes
on to perform a real coercion. Otherwise it follows the cast rules. The
bug here was that we saw the case of *T vs *u8 the try coercion used
inference variables to match T vs u8 but this will cause a cascase of
bad inference variables which we don't want when we perform the simple
coercion.

Fixes #2330

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::check): apply new argument
* typecheck/rust-coercion.cc (TypeCoercionRules::Coerce): track if this is a cast site
(TypeCoercionRules::TryCoerce): update ctor
(TypeCoercionRules::TypeCoercionRules): cleanup error handling
(TypeCoercionRules::do_coercion): likewise
(TypeCoercionRules::coerce_unsafe_ptr): only infer in try mode
(TypeCoercionRules::coerce_borrowed_pointer): cleanup
* typecheck/rust-coercion.h: update header

gcc/testsuite/ChangeLog:

* rust/compile/issue-1981.rs: Fix badly copied libcore code test
* rust/compile/issue-2330.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: Track associated type generics
Philip Herron [Thu, 29 Jun 2023 09:13:29 +0000 (10:13 +0100)] 
gccrs: Track associated type generics

This fixes an issue with our qualified type-paths if we point to a generic
associated type this needs to either use the placeholder type on the trait
reference or use the impl-type but apply the generics.

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-reference.h: update prototype
* typecheck/rust-hir-trait-resolve.cc: add generic args as an out param
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): use the generics

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: dump: Add formatting to dump
Pierre-Emmanuel Patry [Fri, 23 Jun 2023 12:33:53 +0000 (14:33 +0200)] 
gccrs: dump: Add formatting to dump

Add formatting character output to ast dump.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Add comment
after tail expressions.
* ast/rust-ast-dump.cc (Dump::require_spacing): Check for
missing previous token.
* ast/rust-ast-dump.h: Output formatting characters.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: collector: Implement formatting options
Pierre-Emmanuel Patry [Fri, 23 Jun 2023 11:59:51 +0000 (13:59 +0200)] 
gccrs: collector: Implement formatting options

The collector did only output the tokens without any formatting.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::collect): Collect
CollectItems once done.
(TokenCollector::newline): Add newline formatting
implementation.
(TokenCollector::indentation): Add indentation implementation.
(TokenCollector::increment_indentation): Add indentation
increment.
(TokenCollector::comment): Add new comment formatting option.
* ast/rust-ast-collector.h: Update prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: collector: Store CollectItem instead of TokenPtr
Pierre-Emmanuel Patry [Fri, 23 Jun 2023 10:49:48 +0000 (12:49 +0200)] 
gccrs: collector: Store CollectItem instead of TokenPtr

Store a custom type to differentiate formatting and metadata from data.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::collect_tokens):
Filter tokens.
(TokenCollector::visit_items_joined_by_separator): Change store
call.
(TokenCollector::visit_as_line): Likewise.
(TokenCollector::visit_items_as_block): Likewise.
(TokenCollector::trailing_comma): Likewise.
(TokenCollector::visit): Likewise.
(TokenCollector::visit_closure_common): Likewise.
(TokenCollector::visit_function_common): Likewise.
* ast/rust-ast-collector.h (class CollectItem): Add class to
differentiate between formatting and raw data (tokens).

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: collector: Remove external container
Pierre-Emmanuel Patry [Fri, 23 Jun 2023 09:44:49 +0000 (11:44 +0200)] 
gccrs: collector: Remove external container

The token collector was using an external container but it was later
revealed that this whole reuse thing was not necessary.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::TokenCollector):
Remove constructor, changing it to an implicit default
constructor.
* ast/rust-ast-collector.h: Change from container reference to
direct container.
* ast/rust-ast-dump.h: Change call to constructor.
* expand/rust-macro-expand.h (struct MacroExpander): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: dump: Output separating space under condition
Pierre-Emmanuel Patry [Wed, 21 Jun 2023 22:16:17 +0000 (00:16 +0200)] 
gccrs: dump: Output separating space under condition

Separating space shall not be output between every tokens to make the
dump clear and easy to read.

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::require_spacing): Add a function
to determine wether a space shall output.
* ast/rust-ast-dump.h: Add function prototype as well as
condition.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: dump: Replace ast dump internals
Pierre-Emmanuel Patry [Wed, 21 Jun 2023 21:47:14 +0000 (23:47 +0200)] 
gccrs: dump: Replace ast dump internals

Replace ast dump visitor with call to token collector.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Add a visit
function for visitable abstract class so we can call a dump on
any visitable item.
* ast/rust-ast-collector.h: Add new visit function prototype.
* ast/rust-ast-dump.cc (Dump::go): Add call to process function.
(Dump::visit): Remove functions.
(Dump::visit_items_joined_by_separator): Likewise.
(Dump::visit_as_line): Likewise.
(Dump::visit_items_as_lines): Likewise.
(Dump::visit_items_as_block): Likewise.
(Dump::visit_function_common): Likewise.
(get_delimiters): Likewise.
(Dump::debug): Update with call to tokencollector.
* ast/rust-ast-dump.h (RUST_AST_DUMP_H): Move include
directives.
(class Dump): Remove multiple prototypes and add generic process
function.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: collector: Fix missing semicolon output
Pierre-Emmanuel Patry [Thu, 22 Jun 2023 14:41:40 +0000 (16:41 +0200)] 
gccrs: collector: Fix missing semicolon output

A final semicolon was missing after every items in an extern block.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Add
semicolon in extern blocks.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: dump: Remove tokenstream dump
Pierre-Emmanuel Patry [Wed, 21 Jun 2023 21:11:19 +0000 (23:11 +0200)] 
gccrs: dump: Remove tokenstream dump

This dump will soon replace ast-pretty print and is hence no longer
required.

gcc/rust/ChangeLog:

* rust-session-manager.cc (Session::enable_dump): Remove
tokenstream dump.
(Session::compile_crate): Likewise.
(Session::dump_tokenstream): Likewise.
* rust-session-manager.h (struct CompileOptions): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: Add tests for Unicode identifiers
Raiki Tamura [Wed, 28 Jun 2023 09:34:05 +0000 (18:34 +0900)] 
gccrs: Add tests for Unicode identifiers

gcc/testsuite/ChangeLog:

* rust/compile/torture/raw_identifiers.rs: New test.
* rust/compile/torture/utf8_identifiers.rs: New test.

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
18 months agogccrs: fix tokenizing utf-8 whitespaces
Raiki Tamura [Wed, 28 Jun 2023 10:14:50 +0000 (19:14 +0900)] 
gccrs: fix tokenizing utf-8 whitespaces

gcc/rust/ChangeLog:

* lex/rust-lex.cc (Lexer::build_token):add check for all kinds of whitespaces

gcc/testsuite/ChangeLog:

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

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
18 months agogccrs: ast: Add explicit default copy constructor
Pierre-Emmanuel Patry [Tue, 27 Jun 2023 12:03:16 +0000 (14:03 +0200)] 
gccrs: ast: Add explicit default copy constructor

Add explicit default copy/move constructor to identifiers.

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add default constructors.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Change Identifier definition
Pierre-Emmanuel Patry [Tue, 27 Jun 2023 09:22:00 +0000 (11:22 +0200)] 
gccrs: ast: Change Identifier definition

Change Identifier type definition from a simple typedef to a whole class
with it's own node id.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Change with
call to getter.
* ast/rust-ast.cc (Module::as_string): Likewise.
(StaticItem::as_string): Likewise.
(TupleStruct::as_string): Likewise.
(Method::as_string): Likewise.
(StructStruct::as_string): Likewise.
(UseTreeRebind::as_string): Likewise.
(Enum::as_string): Likewise.
(Trait::as_string): Likewise.
(Union::as_string): Likewise.
(Function::as_string): Likewise.
(TypeAlias::as_string): Likewise.
(MacroRulesDefinition::as_string): Likewise.
(FieldAccessExpr::as_string): Likewise.
(MacroMatchFragment::as_string): Likewise.
(TypeParam::as_string): Likewise.
(StructExprFieldIdentifierValue::as_string): Likewise.
(EnumItem::as_string): Likewise.
(StructField::as_string): Likewise.
(ExternalTypeItem::as_string): Likewise.
(ExternalStaticItem::as_string): Likewise.
(ExternalFunctionItem::as_string): Likewise.
(TraitFunctionDecl::as_string): Likewise.
(TraitMethodDecl::as_string): Likewise.
(TraitItemConst::as_string): Likewise.
(TraitItemType::as_string): Likewise.
(MaybeNamedParam::as_string): Likewise.
(MetaListPaths::as_string): Likewise.
(MetaListNameValueStr::as_string): Likewise.
(Module::process_file_path): Likewise.
(MetaListNameValueStr::check_cfg_predicate): Likewise.
(MetaListPaths::check_cfg_predicate): Likewise.
(MetaWord::check_cfg_predicate): Likewise.
(MetaNameValueStr::check_cfg_predicate): Likewise.
(MetaNameValueStr::to_attribute): Likewise.
(MetaWord::to_attribute): Likewise.
(MetaListPaths::to_attribute): Likewise.
(MetaListNameValueStr::to_attribute): Likewise.
(operator<<): Change Identifier class <<
operator overload for standard output stream.
* ast/rust-ast.h (class Identifier): Change typedef to proper
class definition.
(operator<<): Add prototype for operator overload.
(class Token): Change getter identifier.
(class MetaListNameValueStr): Likewise.
(class PathExpr): Likewise.
* ast/rust-expr.h: Likewise.
* ast/rust-item.h: Likewise.
* ast/rust-macro.h: Likewise.
* ast/rust-path.cc (GenericArg::disambiguate_to_type): Likewise.
(GenericArgsBinding::as_string): Likewise.
(ConstGenericParam::as_string): Likewise.
* ast/rust-path.h: Likewise.
* ast/rust-pattern.cc (IdentifierPattern::as_string): Likewise.
(StructPatternFieldIdentPat::as_string): Likewise.
(StructPatternFieldIdent::as_string): Likewise.
* ast/rust-type.h: Likewise.
* backend/rust-compile-base.cc: Likewise.
* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
* backend/rust-compile-extern.h: Likewise.
* backend/rust-compile-fnparam.cc (CompileFnParam::visit):
Likewise.
* backend/rust-compile-implitem.cc (CompileTraitItem::visit):
Likewise.
* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
* backend/rust-compile-pattern.cc (CompilePatternBindings::visit): Likewise.
* backend/rust-compile-struct-field-expr.cc (CompileStructExprField::visit):
Likewise.
* backend/rust-compile-var-decl.h: Likewise.
* backend/rust-compile.cc: Likewise.
* checks/errors/rust-unsafe-checker.cc (check_extern_call):
Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::visit):
Likewise.
* checks/lints/rust-lint-scan-deadcode.h: Likewise.
* expand/rust-derive-clone.cc (DeriveClone::clone_fn): Likewise.
(DeriveClone::visit_tuple): Likewise.
(DeriveClone::visit_struct): Likewise.
(DeriveClone::visit_union): Likewise.
* expand/rust-derive-copy.cc (DeriveCopy::visit_struct):
Likewise.
(DeriveCopy::visit_tuple): Likewise.
(DeriveCopy::visit_enum): Likewise.
(DeriveCopy::visit_union): Likewise.
* expand/rust-macro-expand.cc (MacroExpander::match_matcher):
Likewise.
(MacroExpander::match_n_matches): Likewise.
(MacroExpander::match_repetition): Likewise.
(MacroExpander::match_repetition_skipped_metavars): Likewise.
* hir/rust-ast-lower-base.cc (struct_field_name_exists):
Likewise.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise.
* hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit):
Likewise.
* hir/rust-hir-dump.cc (Dump::visit): Likewise.
* hir/tree/rust-hir-expr.h: Likewise.
* hir/tree/rust-hir-item.h: Likewise.
* hir/tree/rust-hir-path.h: Likewise.
* hir/tree/rust-hir-type.h: Likewise.
* hir/tree/rust-hir.cc (Module::as_string): Likewise.
(StaticItem::as_string): Likewise.
(TupleStruct::as_string): Likewise.
(ConstantItem::as_string): Likewise.
(StructStruct::as_string): Likewise.
(UseTreeRebind::as_string): Likewise.
(Enum::as_string): Likewise.
(Trait::as_string): Likewise.
(Union::as_string): Likewise.
(Function::as_string): Likewise.
(TypeAlias::as_string): Likewise.
(FieldAccessExpr::as_string): Likewise.
(TypeParam::as_string): Likewise.
(GenericArgsBinding::as_string): Likewise.
(StructPatternFieldIdent::as_string): Likewise.
(StructPatternFieldIdentPat::as_string): Likewise.
(IdentifierPattern::as_string): Likewise.
(StructExprFieldIdentifierValue::as_string): Likewise.
(EnumItem::as_string): Likewise.
(StructField::as_string): Likewise.
(ExternalStaticItem::as_string): Likewise.
(ExternalFunctionItem::as_string): Likewise.
(NamedFunctionParam::as_string): Likewise.
(TraitFunctionDecl::as_string): Likewise.
(TraitItemConst::as_string): Likewise.
(TraitItemType::as_string): Likewise.
(MaybeNamedParam::as_string): Likewise.
* hir/tree/rust-hir.h: Likewise.
* parse/rust-parse-impl.h (Parser::parse_macro_match_fragment):
Likewise.
(Parser::parse_module): Likewise.
(Parser::parse_use_tree): Likewise.
(Parser::parse_maybe_named_param): Likewise.
* resolve/rust-ast-resolve-implitem.h: Likewise.
* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit):
Likewise.
(ResolveItem::visit): Likewise.
(flatten_rebind): Likewise.
(ResolveExternItem::visit): Likewise.
(rust_flatten_rebind): Likewise.
(rust_flatten_rebind_nested): Likewise.
* resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::go):
Likewise.
(PatternDeclaration::visit): Likewise.
(PatternDeclaration::add_new_binding): Likewise.
* resolve/rust-ast-resolve-stmt.h: Likewise.
* resolve/rust-ast-resolve-toplevel.h: Likewise.
* resolve/rust-ast-resolve-type.h: Likewise.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise.
* typecheck/rust-autoderef.cc: Likewise.
* typecheck/rust-hir-dot-operator.cc (MethodResolver::select):
Likewise.
* typecheck/rust-hir-path-probe.cc (PathProbeType::visit):
Likewise.
* typecheck/rust-hir-trait-reference.cc (TraitReference::get_name): Likewise.
* typecheck/rust-hir-trait-resolve.cc (ResolveTraitItemToRef::visit): Likewise.
(TraitResolver::resolve_trait): Likewise.
(TraitItemReference::resolve_item): Likewise.
(AssociatedImplTrait::setup_raw_associated_types): Likewise.
* typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::visit): Likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Likewise.
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit): Likewise.
(TypeCheckImplItem::visit): Likewise.
(TypeCheckImplItemWithTrait::visit): Likewise.
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
Likewise.
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Likewise.
* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::visit): Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeResolveGenericParam::visit): Likewise.
* typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise.
* typecheck/rust-tyty-subst.cc (SubstitutionRef::get_mappings_from_generic_args): Likewise.
* util/rust-attributes.cc (check_doc_attribute): Likewise.
* util/rust-hir-map.cc (Mappings::insert_macro_def): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: Refactor lexer to handle UTF-8
Raiki Tamura [Sun, 18 Jun 2023 14:25:31 +0000 (23:25 +0900)] 
gccrs: Refactor lexer to handle UTF-8

gcc/rust/ChangeLog:

* lex/rust-lex.cc (is_float_digit):Change types of param to `uint32_t`
(is_x_digit):Likewise
(is_octal_digit):Likewise
(is_bin_digit):Likewise
(check_valid_float_dot_end):Likewise
(is_whitespace):Likewise
(is_non_decimal_int_literal_separator):Likewise
(is_identifier_start):Likewise
(is_identifier_continue):Likewise
(Lexer::skip_broken_string_input):Likewise
(Lexer::build_token):Remove handling BOM
(Lexer::parse_in_type_suffix):Modify use of `current_char`
(Lexer::parse_in_decimal):Likewise
(Lexer::parse_escape):Likewise
(Lexer::parse_utf8_escape):Likewise
(Lexer::parse_partial_string_continue):Likewise
(Lexer::parse_partial_hex_escape):Likewise
(Lexer::parse_partial_unicode_escape):Likewise
(Lexer::parse_byte_char):Likewise
(Lexer::parse_byte_string):Likewise
(Lexer::parse_raw_byte_string):Likewise
(Lexer::parse_raw_identifier):Likewise
(Lexer::parse_non_decimal_int_literal):Likewise
(Lexer::parse_decimal_int_or_float):Likewise
(Lexer::peek_input):Change return type to `Codepoint`
(Lexer::get_input_codepoint_length):Change to return 1
(Lexer::peek_codepoint_input):Change to be wrapper of `peek_input`
(Lexer::skip_codepoint_input):Change to be wrapper of `skip_input`
(Lexer::test_get_input_codepoint_n_length):Deleted
(Lexer::split_current_token):Deleted
(Lexer::test_peek_codepoint_input):Deleted
(Lexer::start_line):Move backwards
(assert_source_content):New helper function for selftest
(test_buffer_input_source):New helper function for selftest
(test_file_input_source):Likewise
(rust_input_source_test):New test
* lex/rust-lex.h (rust_input_source_test):New test
* rust-lang.cc (run_rust_tests):Add selftest

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
18 months agogccrs: Insert empty repeated metavars for nested repetitions
Owen Avery [Wed, 7 Jun 2023 16:26:37 +0000 (12:26 -0400)] 
gccrs: Insert empty repeated metavars for nested repetitions

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::match_repetition_skipped_metavars): New.
(MacroExpander::match_repetition): Use new method.
* expand/rust-macro-expand.h
(MacroExpander::match_repetition_skipped_metavars): New.

gcc/testsuite/ChangeLog:

* rust/compile/macro-issue2268.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: [E0023] Incorrect Number of Fields in Pattern Extraction
Muhammad Mahad [Mon, 26 Jun 2023 10:48:08 +0000 (15:48 +0500)] 
gccrs: [E0023] Incorrect Number of Fields in Pattern Extraction

ErrorCode[E0023]: Incorrect Number of Fields in Pattern Extraction.
The pattern has x fields, but the corresponding tuple variant
has y field

gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): called rust_error_at

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: gccrs:[E0026] Non-Existent Field Extraction in Struct Pattern
Muhammad Mahad [Tue, 20 Jun 2023 15:19:13 +0000 (20:19 +0500)] 
gccrs: gccrs:[E0026] Non-Existent Field Extraction in Struct Pattern

Non-Existent Field Extraction in Struct Pattern
variant `Foo::D` does not have a field named `b`

gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): called rust_error_at

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: Match tokens in macros more closely
Owen Avery [Mon, 26 Jun 2023 17:06:10 +0000 (13:06 -0400)] 
gccrs: Match tokens in macros more closely

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::match_token): Match token instead of token id.
* parse/rust-parse-impl.h
(Parser::skip_token): Add token-skipping variant.
(Parser::expect_token): Likewise.
* parse/rust-parse.h
(Parser::skip_token): Likewise.
(Parser::expect_token): Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/macro-issue2264.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agorust: Add -frust-name-resolution-2.0 option
Arthur Cohen [Tue, 20 Jun 2023 00:22:57 +0000 (02:22 +0200)] 
rust: Add -frust-name-resolution-2.0 option

This option enables an experimental name resolution algorithm. Disabled
by default.

gcc/rust/ChangeLog:

* lang.opt: Add -frust-name-resolution-2.0 option

18 months agogccrs: Stop autoderef of raw pointer types
Philip Herron [Sun, 25 Jun 2023 20:36:32 +0000 (21:36 +0100)] 
gccrs: Stop autoderef of raw pointer types

It is unsafe to deref raw pointers during autoderef this adds a check to
stop autoderef early when we are about to try and deref pointers.

Fixes #2015

gcc/rust/ChangeLog:

* typecheck/rust-autoderef.cc (AutoderefCycle::cycle): add check for pointers

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: Add method selection to operator overloading
Philip Herron [Sun, 25 Jun 2023 20:21:36 +0000 (21:21 +0100)] 
gccrs: Add method selection to operator overloading

When we do operator overloading we can get multiple candidates and we need
to use the optional arguments to filter the candidates event further. In
the bug we had:

  <integer> + <usize>

Without the Add impl blocks for the primitive interger types we unify
against the rhs to figure out that the lhs should be a usize but when we
are using the impl blocks we need to use the rhs to ensure that its
possible to coerce the rhs to the expected fntype parameter to filter the
candidates.

Fixes #2304

gcc/rust/ChangeLog:

* typecheck/rust-autoderef.cc: use new selection filter
* typecheck/rust-hir-dot-operator.cc (MethodResolver::Select): new slection Filter
* typecheck/rust-hir-dot-operator.h: New select prototype
* typecheck/rust-hir-type-check-expr.cc: call select
* typecheck/rust-type-util.cc (try_coercion): new helper
* typecheck/rust-type-util.h (try_coercion): helper prototype

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: ErrorCode[E0277] Type Does Not Implement Expected Trait
Muhammad Mahad [Thu, 22 Jun 2023 08:04:12 +0000 (13:04 +0500)] 
gccrs: ErrorCode[E0277] Type Does Not Implement Expected Trait

Type Does Not Implement Expected Trait - the type [{integer}] cannot be indexed by u32

gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): passed "E0277"
* typecheck/rust-tyty.cc (BaseType::bounds_compatible): passed "E0277"

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: Expand expressions/types correctly in more contexts.
Matthew Jasper [Sat, 24 Jun 2023 11:21:44 +0000 (12:21 +0100)] 
gccrs: Expand expressions/types correctly in more contexts.

Expressions are expanded in constant/static items, match expressions
and in parentheses now. Types are expanded in enum variants.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit):
Expand expressions in more contexts.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
Visit enum variants.

gcc/testsuite/ChangeLog:

* rust/compile/macro54.rs: New test.
* rust/compile/macro55.rs: New test.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
18 months agogccrs: expand: Change names and document behavior
Pierre-Emmanuel Patry [Wed, 21 Jun 2023 13:56:26 +0000 (15:56 +0200)] 
gccrs: expand: Change names and document behavior

Change some argument name, as well as some documentation.

gcc/rust/ChangeLog:

* ast/rust-ast.cc (BlockExpr::normalize_tail_expr): Refactor
code and warn about dangling reference.
* expand/rust-expand-visitor.cc (expand_stmt_attribute):
Document function and change argument names to make those
clearer.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Merge arguments into one
Pierre-Emmanuel Patry [Mon, 19 Jun 2023 12:01:13 +0000 (14:01 +0200)] 
gccrs: expand: Merge arguments into one

The second argument could be derived from the first one, the second
argument is hence useless as the first argument alone could be kept to
access it's data.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::expand_inner_stmts):
Remove second argument.
(ExpandVisitor::visit): Remove argument from function call.
* expand/rust-expand-visitor.h: Update function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Convert last statement to tail expr
Pierre-Emmanuel Patry [Mon, 12 Jun 2023 14:36:51 +0000 (16:36 +0200)] 
gccrs: expand: Convert last statement to tail expr

Some inner statements may be expanded to statements as well as a tail
expression, which should then be propagated to the parent tail
expression field.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Change
call to expand_inner_stmts.
(expand_tail_expr): Change argument name and try to expand the
last statement as a tail expression once all statements have
been expanded.
* expand/rust-expand-visitor.h: Change prototype to accept
parent class.
* ast/rust-expr.h: Add try_convert_last_stmt function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add tail expr expansion
Pierre-Emmanuel Patry [Fri, 9 Jun 2023 15:13:41 +0000 (17:13 +0200)] 
gccrs: expand: Add tail expr expansion

Tail expression may contain attribute and thus should be expanded.

gcc/rust/ChangeLog:

* ast/rust-expr.h: Add a function to take tail expr as well
as a function to set a tail expression.
* expand/rust-expand-visitor.cc (expand_tail_expr): Add tail
expression expansion function.
(ExpandVisitor::visit): Add call to tail expr expansion in for
BlockExpr.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Derive statements
Arthur Cohen [Thu, 25 May 2023 13:46:47 +0000 (15:46 +0200)] 
gccrs: expand: Derive statements

Expand procedural macros on statements properly.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc : Change type from pointer to
reference and expand statements from statements.
* ast/rust-macro.h: Return macro kind.
* ast/rust-ast.h: Add Statement kind.
* ast/rust-item.h: Change module.
* ast/rust-stmt.h: Return kind.

Co-authored-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: Make MatchedFragmentContainer a tree-like structure
Owen Avery [Thu, 1 Jun 2023 17:37:35 +0000 (13:37 -0400)] 
gccrs: Make MatchedFragmentContainer a tree-like structure

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::expand_decl_macro):
Use pointers/smart pointers to refer to MatchedFragmentContainer instances.
(MacroExpander::transcribe_rule): Likewise.
(MatchedFragmentContainer::get_single_fragment): New implementation.
(MatchedFragmentContainer::get_fragments): Likewise.
(MatchedFragmentContainer::add_fragment): Likewise.
(MatchedFragmentContainer::zero): Likewise.
(MatchedFragmentContainer::metavar): Likewise.
* expand/rust-macro-expand.h
(class MatchedFragmentContainer): Make abstract.
(class MatchedFragmentContainerMetaVar): New.
(class MatchedFragmentContainerRepetition): New.
(class SubstitutionScope):
Use pointers/smart pointers to refer to MatchedFragmentContainer instances.
(MacroExpander::transcribe_rule): Likewise.
* expand/rust-macro-substitute-ctx.cc
(SubstituteCtx::substitute_metavar): Likewise.
(SubstituteCtx::check_repetition_amount): Likewise.
(SubstituteCtx::substitute_repetition): Likewise.
* expand/rust-macro-substitute-ctx.h
(class SubstituteCtx): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: ErrorCode[E0433] Use of Undeclared Crate, Module, or Type
Muhammad Mahad [Mon, 19 Jun 2023 19:47:19 +0000 (00:47 +0500)] 
gccrs: ErrorCode[E0433] Use of Undeclared Crate, Module, or Type

gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): called rust_error_at
* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): called rust_error_at

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
18 months agogccrs: Apply generic arguments to the respective trait bounds
Philip Herron [Tue, 20 Jun 2023 18:41:42 +0000 (19:41 +0100)] 
gccrs: Apply generic arguments to the respective trait bounds

When we have an impl block for a generic type such as T which is a generic
type which does not 'bind' generic arguments, which means its not a type
such as an ADT or Fn which holds generic parameter mappings we need to
ensure inference variables are applied to the segment type apropriately so
that inference variables unified correctly and the higher ranked trait
bounds are as well.

Fixes: #1893
gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::ResolveImplBlockSelfWithInference):
arguments mappings as an out parameter and apply them to the bounds
* typecheck/rust-hir-type-check-item.h: update the prototype
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
apply the arguments to the segment type

gcc/testsuite/ChangeLog:

* rust/compile/issue-1893.rs: fully compile the test case

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: Fix generic argument tracking
Philip Herron [Tue, 20 Jun 2023 15:39:25 +0000 (16:39 +0100)] 
gccrs: Fix generic argument tracking

When we do generic argument substitution we creating mappings of the
HIR::GenericArgs argument to the TyTy::SubstitutionParam as a pointer. So
for example when we have Generic Parameters <Self, T> and arguments <T, U>

  T0: Arguments: <Self=T, T=U>
  T1: Self -> replaced-with T
  T2: Arguments: <T=T, T=U>
  T3: T maps back to the replace Self->T
  T4: Arguments <T=T, T=T>

Which is wrong but because we do a string comparison to find the argument
mapping we cant reply on the pointer to the origin parameter mapping as
the parameter will be updated resulting in bad mappings.

This patch changes the Argument mappings to track the _original_ parameter
type so that lookup's for the mappings use this symbol instead not the
updated ones during substitution.

Addresses #1893

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
simplify lookup of the respective predicate
* typecheck/rust-tyty-subst.cc (SubstitutionArg::SubstitutionArg): track original parameter
(SubstitutionArg::operator=): update copy ctor
(SubstitutionArg::get_param_ty): use original param
(SubstitutionArg::as_string): update as_string
* typecheck/rust-tyty-subst.h: add new private field

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: add new method to infer impl block type
Philip Herron [Tue, 20 Jun 2023 11:21:02 +0000 (12:21 +0100)] 
gccrs: add new method to infer impl block type

When we resolve a path segment the associated impl Self may not be a type
which 'binds' generic types. Which would be any type which does not inherit
SubstitionRef base class. There is a case where an impl block for generic
type T has bound generics on the trait bound of the trait impl block this
allows us to infer the T for this case.

Addresses #1893

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::ResolveImplBlockSelfWithInference):
New helper utility to infer non concrete types and handle the case where its not a
binding generic type (which inherits SubstitutionRef)
* typecheck/rust-hir-type-check-item.h: new helper prototype
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): use this helper

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: fix unsafe lookup at zero index
Philip Herron [Tue, 20 Jun 2023 09:39:54 +0000 (10:39 +0100)] 
gccrs: fix unsafe lookup at zero index

Using lookup_predciate by DefId is the safe way to get the relevant
predicate.

Addresses #1893

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc: use lookup_predicate (DefID)
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): fix formatting

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: Parse semicolons in more cases for statement macros
Matthew Jasper [Thu, 8 Jun 2023 19:14:47 +0000 (20:14 +0100)] 
gccrs: Parse semicolons in more cases for statement macros

gccrs: Parse statement macros as statements.

gcc/rust/ChangeLog:

* ast/rust-ast.h (MacroInvocation::add_semicolon): New method.
(Expr::to_stmt): Remove method.
* ast/rust-macro.h (MacroInvocation::add_semicolon): Add override.
(MacroInvocation::to_stmt): Remove override.
* ast/rust-stmt.h: Remove use of Expr::to_stmt.
(ExprStmt::add_semicolon): Add override.
* expand/rust-macro-expand.h (struct MacroExpander):
Add EXPR/STMT and remove BLOCK from ContextType.
* expand/rust-expand-visitor.cc (ExpandVisitor::maybe_expand_expr): Use EXPR context.
(ExpandVisitor::expand_inner_stmts): Use STMT context.
(ExpandVisitor::visitor): Remove use of BLOCK context.
* expand/rust-macro-expand.cc (parse_many): Pass enum by value.
(transcribe_on_delimiter): Remove function.
(transcribe_context): Use EXPR/STMT contexts.
(MacroExpander::parse_proc_macro_output): Use EXPR/STMT contexts.
(transcribe_many_stmts): Parse statements with semicolons.
* parse/rust-parse-impl.h (Parser::parse_stmt):
Delegate macro parsing to parse_expr_stmt, check for ! after macro_rules.
(Parser::parse_let_stmt): Work around lack of NT tokens.
(Parser::parse_expr_stmt): Handle statements at end of macro expansions.
(Parser::parse_expr_stmt): Parse macro statements/expression statements
starting with a macro.
(Parser::parse_match_expr): Don't modify flag unnecessarily.
(Parser::parse_stmt_or_expr):
Parse macro statements/expression statements starting with a macro.
(Parser::parse_path_based_stmt_or_expr): Remove method.
(Parser::parse_macro_invocation_maybe_semi): Remove method.
(Parser::parse_expr): Move code into left_denotations.
(Parser::left_denotations): New method.
(Parser::null_denotation): Split out methods for cases with and without paths.
(Parser::null_denotation_path): New method.
(Parser::null_denotation_not_path): New method.
(Parser::parse_macro_invocation_partial): Don't check for semicolon here.
* parse/rust-parse.h: Update declarations.
(struct ParseRestrictions): Additional flag.

gcc/testsuite/ChangeLog:

* rust/compile/braced_macro_arm.rs: New test.
* rust/compile/braced_macro_statements1.rs: New test.
* rust/compile/braced_macro_statements2.rs: New test.
* rust/compile/braced_macro_statements3.rs: New test.
* rust/compile/issue-2225.rs: Update test.
* rust/compile/macro53.rs: New test.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
18 months agogccrs: Handle tail expression normalization right before lowering to HIR.
Matthew Jasper [Thu, 8 Jun 2023 18:29:44 +0000 (19:29 +0100)] 
gccrs: Handle tail expression normalization right before lowering to HIR.

This allows braced macros at the end of blocks to correctly expand to
zero or more statements followed by a tail expression. Parsing still
creates a tail expression for now.

gcc/rust/ChangeLog:

* ast/rust-ast.cc (BlockExpr::strip_tail_expr):
Don't normalize tail expression in this method.
(BlockExpr::normalize_tail_expr): New method that only does the normalization.
* ast/rust-expr.h: Declare new method.
* hir/rust-ast-lower-block.h: Normalize tail expressions on blocks before lowering.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
18 months agogccrs: Print macro invocations in AST dump
Matthew Jasper [Sat, 3 Jun 2023 12:37:51 +0000 (13:37 +0100)] 
gccrs: Print macro invocations in AST dump

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::visit): Print macro invocations.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
18 months agogccrs: tokenize Unicode identifiers
Raiki Tamura [Mon, 19 Jun 2023 09:06:11 +0000 (18:06 +0900)] 
gccrs: tokenize Unicode identifiers

gcc/rust/ChangeLog:

* lex/rust-lex.cc (is_whitespace):add all lacked codepoints valid as whitespaces
(is_identifier_start):new function to check XID_Start and underscore
(is_identifier_continue):new function to check XID_Continue
(Lexer::build_token):tokenize Unicode identifiers
(Lexer::parse_partial_string_continue):add comments
(Lexer::parse_partial_unicode_escape):add comments
(Lexer::parse_raw_identifier):change to use `is_identifier_scontinue`
(Lexer::parse_identifier_or_keyword):change to use `is_identifier_continue`
(Lexer::parse_char_or_lifetime):change to use `is_identifier_start/continue`
(Lexer::skip_codepoint_input):do not attempt to skip input when bumping EOF
* lex/rust-lex.h:add `current_char32` field

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
18 months agogccrs: Prevent invalid iterator dereference
Owen Avery [Sat, 17 Jun 2023 04:32:38 +0000 (00:32 -0400)] 
gccrs: Prevent invalid iterator dereference

gcc/rust/ChangeLog:

* lex/rust-lex.cc
(Lexer::classify_keyword): Check if iterator is valid before dereferencing.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: Remove direct error on macro rule count-related match failure
Owen Avery [Mon, 12 Jun 2023 20:17:09 +0000 (16:17 -0400)] 
gccrs: Remove direct error on macro rule count-related match failure

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::match_repetition): Remove redundant rust_error_at call.

gcc/testsuite/ChangeLog:

* rust/compile/macro6.rs: Remove check for direct error.
* rust/compile/macro7.rs: Likewise.
* rust/compile/macro-issue2229.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: we can't check the bounds involving empty placeholder types
Philip Herron [Wed, 14 Jun 2023 11:11:38 +0000 (12:11 +0100)] 
gccrs: we can't check the bounds involving empty placeholder types

We use placeholders for assoicated types on traits but if we are unifying
types against a placeholder its not possible to check the bounds as the
placeholder does not have enough information yet at this point to determine
if bounds will or won't be satisfied. That check will occur when associated
types and generics are setup.

Fixes #2036

gcc/rust/ChangeLog:

* typecheck/rust-unify.cc (UnifyRules::go): dont check bounds on placeholders

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: fixup: Remove old typecheck dump
tieway59 [Wed, 14 Jun 2023 06:22:34 +0000 (14:22 +0800)] 
gccrs: fixup: Remove old typecheck dump

fixes https://github.com/Rust-GCC/gccrs/issues/2020

gcc/rust/ChangeLog:

* typecheck/rust-tycheck-dump.h: Delete this file
* rust-session-manager.cc: Remove `Session::dump_type_resolution`
and `TYPE_RESOLUTION_DUMP`
* rust-session-manager.h: Likewise

Signed-off-by: Taiwei Wu tieway59@foxmail.com
18 months agogccrs: fortify resolve_method_address to match the types
Philip Herron [Tue, 13 Jun 2023 18:14:27 +0000 (19:14 +0100)] 
gccrs: fortify resolve_method_address to match the types

Fixes #2019

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address):
match the fntype to the candidate

gcc/testsuite/ChangeLog:

* rust/compile/issue-2019-2.rs: New test.
* rust/compile/issue-2019-3.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: derive: Factor common fields inside the base visitor
Arthur Cohen [Fri, 26 May 2023 13:36:23 +0000 (15:36 +0200)] 
gccrs: derive: Factor common fields inside the base visitor

gcc/rust/ChangeLog:

* expand/rust-derive.h: Store AstBuilder and location.
* expand/rust-derive.cc (DeriveVisitor::DeriveVisitor): Update constructor.
* expand/rust-derive-clone.h: Remove members now stored in `DeriveVisitor`.
* expand/rust-derive-copy.h: Likewise.
* expand/rust-derive-clone.cc (DeriveClone::DeriveClone): Update constructor.
* expand/rust-derive-copy.cc (DeriveCopy::DeriveCopy): Likewise.

18 months agogccrs: ensure assoicated types are setup for concrete trait impl blocks
Philip Herron [Mon, 12 Jun 2023 15:07:13 +0000 (16:07 +0100)] 
gccrs: ensure assoicated types are setup for concrete trait impl blocks

We have been concentrating on generic impl blocks for so long that we have
not handled the simple concrete impl block case where associated types need
to be setup when compling a function/method on a trait impl block which
uses associated types.

Addresses #2019

gcc/rust/ChangeLog:

* backend/rust-compile-item.cc (CompileItem::visit): ensure assoicated types are setup
* typecheck/rust-hir-trait-reference.h: new interface for concrete impl block
* typecheck/rust-hir-trait-resolve.cc (AssociatedImplTrait::setup_raw_associated_types):
ensure we setup assoicated types for this impl block

gcc/testsuite/ChangeLog:

* rust/compile/issue-2019-1.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: Fix ICE for reference patterns in match statements
Dave Evans [Fri, 9 Jun 2023 00:18:42 +0000 (19:18 -0500)] 
gccrs: Fix ICE for reference patterns in match statements

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (check_match_scrutinee): Add REF type to rust_assert.
(CompileExpr::visit): Add REF type for scrutinee_kind in MatchExpr visitor.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Dave Evans <dave@dmetwo.org>
18 months agogccrs: Remove obsolete error diagnostic
TieWay59 [Sat, 13 May 2023 07:49:42 +0000 (15:49 +0800)] 
gccrs: Remove obsolete error diagnostic

fixes https://github.com/Rust-GCC/gccrs/issues/2064

Remove every `{ dg-error "failed to type resolve expression" }`
annotation that causes a failure when running the testsuite.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc: Remove obsolete error diagnostic

gcc/testsuite/ChangeLog:

* rust/compile/break1.rs: remove dg-error "failed to type resolve expression"
* rust/compile/break2.rs: Likewise
* rust/compile/cfg2.rs: Likewise
* rust/compile/continue1.rs: Likewise
* rust/compile/generics4.rs: Likewise
* rust/compile/generics6.rs: Likewise
* rust/compile/generics7.rs: Likewise
* rust/compile/issue-2029.rs: Likewise
* rust/compile/issue-2139.rs: Likewise
* rust/compile/issue-2190-1.rs: Likewise
* rust/compile/issue-925.rs: Likewise
* rust/compile/method1.rs: Likewise
* rust/compile/shadow1.rs: Likewise
* rust/compile/type-bindings1.rs: Likewise
* rust/compile/unary_negation.rs: Likewise
* rust/compile/unary_not.rs: Likewise
* rust/compile/unconstrained_type_param.rs: Likewise
* rust/compile/usize1.rs: Likewise

Signed-off-by: Taiwei Wu <tieway59@foxmail.com>
18 months agogccrs: introduce new types_compatable
Philip Herron [Sat, 10 Jun 2023 20:07:27 +0000 (21:07 +0100)] 
gccrs: introduce new types_compatable

This is an initiative to begin getting rid of the can_eq interface.

Addresses #2019

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit):
use new interface
* typecheck/rust-type-util.cc (types_compatable): implementation of new interface
* typecheck/rust-type-util.h (types_compatable): prototype
* typecheck/rust-unify.cc (UnifyRules::expect_placeholder):
It is allow for unification against placeholders

gcc/testsuite/ChangeLog:

* rust/compile/traits2.rs: update error message
* rust/compile/traits3.rs: update error message

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: expand: Make to_derive argument const
Pierre-Emmanuel Patry [Wed, 7 Jun 2023 10:43:07 +0000 (12:43 +0200)] 
gccrs: expand: Make to_derive argument const

This string will never change and can be constified.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (derive_item): Add const
attribute to the trait name argument.
* expand/rust-macro-expand.h (struct MacroExpander): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Allow empty derive attribute
Pierre-Emmanuel Patry [Mon, 5 Jun 2023 11:17:22 +0000 (13:17 +0200)] 
gccrs: expand: Allow empty derive attribute

Changes derive macro expansion to accept empty derive attributes. This
commit also add a test to prevent future regressions.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (get_traits_to_derive): Change
assertion to allow empty derive directives.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Expand item level attribute proc macros
Pierre-Emmanuel Patry [Mon, 5 Jun 2023 09:24:35 +0000 (11:24 +0200)] 
gccrs: expand: Expand item level attribute proc macros

Expand custom attributes from procedural macros at item level.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (expand_attribute): Add function
to expand a given attribute on a given item.
* expand/rust-macro-expand.h (struct MacroExpander): Change
return type to ast fragment.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add in order trait expansion
Pierre-Emmanuel Patry [Fri, 2 Jun 2023 16:26:58 +0000 (18:26 +0200)] 
gccrs: expand: Add in order trait expansion

Add in order trait expansion using derive macros. Remove expansion of
outer attributes.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (get_traits_to_derive): Change
the function to get traits in a given attribute instead of an
attribute vector.
(ExpandVisitor::expand_struct_fields): Remove call in visitor.
(ExpandVisitor::expand_function_params): Likewise.
(ExpandVisitor::expand_closure_params): Likewise.
(ExpandVisitor::visit): Likewise.
(ExpandVisitor::expand_outer_attribute): Remove function.
(ExpandVisitor::visit_outer_attrs): Remove function.

* expand/rust-expand-visitor.h: Remove function prototypes.
(ExpandVisitor::visit_outer_attrs): Remove function.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Remove proc macro fragment from expander
Pierre-Emmanuel Patry [Mon, 5 Jun 2023 13:21:15 +0000 (15:21 +0200)] 
gccrs: expand: Remove proc macro fragment from expander

Procedural macros are no longer expanded on post order but rather
expanded from their parent. Hence they are no longer stored in the
expander which means this attribute can be removed. This commit also
remove the old expansion mechanism.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.h: Remove fragment collection fro
procedural macros.
* expand/rust-macro-expand.h (struct MacroExpander): Remove
procedural macro fragment from expander.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Move derive system to new one
Pierre-Emmanuel Patry [Fri, 2 Jun 2023 09:41:53 +0000 (11:41 +0200)] 
gccrs: expand: Move derive system to new one

Builtin derive already had their own code scheme, incompatible with the
proc macro pattern. This commit unifies derive macros with it.

gcc/rust/ChangeLog:

* ast/rust-ast-fragment.cc (Fragment::Fragment): Remove
overwrite member in constructor.
(Fragment::operator=): Removal of overwrite member in copy.
(Fragment::should_overwrite): Remove overwrite getter.
* ast/rust-ast-fragment.h: Remove overwrite boolean member.
* expand/rust-expand-visitor.cc (derive_item): Add a function to
derive items using the expander for now.
(builtin_derive_item): Rename from derive_item to
builtin_derive_item.
(ExpandVisitor::visit): Remove visit to derive attributes.
(ExpandVisitor::expand_derive): Remove derive attribute visitor.
(ExpandVisitor::visit_attrs_with_derive): Likewise.
* expand/rust-expand-visitor.h: Update insertion of other kind
of proc macros. We no longer have an overwrite attribute in the
fragment.
* expand/rust-macro-expand.cc (MacroExpander::parse_procmacro_output):
Return the fragment instead of inserting it.
* expand/rust-macro-expand.h (struct MacroExpander): Return
fragment.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add derive proc macro draft
Pierre-Emmanuel Patry [Sun, 21 May 2023 16:13:34 +0000 (18:13 +0200)] 
gccrs: expand: Add derive proc macro draft

Add a first draft for derive proc macros based on the attribute expander
code. Convert the result back to a parsable entity and parse it.
The procedural macro expander was beginning to require almost all
functionalities already provided by the macro expander, hence the merge.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.h: Update enum name to match
definition.
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_derive):
Add call to expander.
(ExpandVisitor::expand_outer_attribute):
Change call to macro expander.
(ExpandVisitor::expand_inner_attribute): Likewise.
* expand/rust-expand-visitor.h: Remove const attribute to match
visitor declaration. Attach result to the AST. Add condition for
item erasure.
* expand/rust-proc-macro.h: Add token collector and expansion
call. Add lexers and parsers for each proc macro type and uses
them to parse macro output.
* expand/rust-macro-expand.h (struct MacroExpander): Add
functions.
* expand/rust-proc-macro.cc (ProcMacroExpander::import_proc_macros):
Moved from here...
* expand/rust-macro-expand.cc (MacroExpander::import_proc_macros):
... to here. Unify procedural macro parsing under one function.
Add a flag to determine whether it originate from a derive
macro.
(MacroExpander::parse_procmacro_output):
Parse macro output to statements. Store an error on parsing
failure.
* Make-lang.in: Add const_TokenPtr specific lexer.
* expand/rust-proc-macro-invoc-lexer.cc: New file.
* expand/rust-proc-macro-invoc-lexer.h: New file.
* rust-session-manager.cc (Session::expansion): Remove
ProcMacroExpander declaration.
* ast/rust-ast-fragment.cc (Fragment::Fragment): Add overwrite
flag.
(Fragment::should_overwrite): Add a getter to determine whether
the fragment shall overwrite it's parent or be appended after.
* ast/rust-ast-fragment.h: Add flag to declaration.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add prc macro expander and registration
Pierre-Emmanuel Patry [Thu, 18 May 2023 12:05:25 +0000 (14:05 +0200)] 
gccrs: expand: Add prc macro expander and registration

Add containers and functions to the hir-map in order to register and
lookup all three kind of procedural macros.
Add a first draft for attribute procedural macro expansion. This
expander still lack proper error handling as well as polishing.

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::insert_bang_proc_macro):
Add a function to insert a new bang proc macro.
(Mappings::lookup_bang_proc_macro): Add a function to lookup a
bang procedural macro.
(Mappings::insert_derive_proc_macro): Add a function to insert a
derive procedural macro.
(Mappings::lookup_derive_proc_macro): Add a function to lookup a
derive procedural macro.
(Mappings::insert_attribute_proc_macro): Add a function to
insert an attribute procedural macro.
(Mappings::lookup_attribute_proc_macro): Add a function to
lookup an attribute procedural macro.
* util/rust-hir-map.h: Add function prototypes.
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_outer_attribute):
Implement expansion of outer attributes.
(ExpandVisitor::expand_inner_attribute):
Add call for inner attribute expansion.
* expand/rust-expand-visitor.h:
Add new procedural macro expander attribute.
* expand/rust-proc-macro.cc (load_macros_array): Add a function
to load the proc macro array from a given shared object.
(load_macros): Add a function to retrieve procedural macro
vector from a given shared object.
(ProcMacroExpander::import_proc_macros): Add a function to load
procedural macros from a given extern crate name.
* expand/rust-proc-macro.h (RUST_PROC_MACRO_H): Add new
proc-macro file.
(class ProcMacroExpander): Add new ProcMacroExpander class.
* rust-session-manager.cc (Session::expansion): Create new macro
expander and feed it to the expand visitor.
* util/rust-attributes.cc: Add macro_export builtin attribute.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: ast: Fix attribute collection
Pierre-Emmanuel Patry [Fri, 26 May 2023 11:16:11 +0000 (13:16 +0200)] 
gccrs: ast: Fix attribute collection

Items attribute were not correctly collected and thus could not be
expanded from a previous macro invocation.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Fix
attribute collection.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: libproc_macro: Fix Tokenstream growth
Pierre-Emmanuel Patry [Tue, 23 May 2023 14:45:08 +0000 (16:45 +0200)] 
gccrs: libproc_macro: Fix Tokenstream growth

TokenStream did not copy back enough old data to the new location. This
commit also add more explicit memcpy usages in order to facilitate
change to utf-8 later.

libgrust/ChangeLog:

* libproc_macro/ffistring.cc (FFIString::make_ffistring):
Add explicit sizeof and utf-8 warning.
(FFIString::clone): Add explicit sizeof and utf-8 warning.
* libproc_macro/ident.cc (Ident::clone): Likewise.
(Ident::make_ident): Likewise.
* libproc_macro/tokenstream.cc (TokenStream::grow):
Fix vector growth.
(TokenStream__clone): Add explicit sizeof.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: libproc_macro: Fix capacity update in tokenstream
Pierre-Emmanuel Patry [Thu, 18 May 2023 14:18:58 +0000 (16:18 +0200)] 
gccrs: libproc_macro: Fix capacity update in tokenstream

The capacity was not updated on tokenstream grow. This commit also add a
new mechanism to prevent a tokenstream to grow with a zero delta capacity.

libgrust/ChangeLog:

* libproc_macro/tokenstream.cc (TokenStream::grow): Add
minimum growing capacity.
(TokenStream::push): Change condition.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: collector: Make visitors public
Pierre-Emmanuel Patry [Thu, 18 May 2023 11:58:28 +0000 (13:58 +0200)] 
gccrs: collector: Make visitors public

Make all trivial visitor functions public so we could easily call the
collection of an ast's subtree from any node.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.h: Make trivial visitors public.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: libproc_macro: Rename Procmacro subtypes
Pierre-Emmanuel Patry [Tue, 16 May 2023 12:54:11 +0000 (14:54 +0200)] 
gccrs: libproc_macro: Rename Procmacro subtypes

Rename procedural macro specializations without the "payload" suffix.
Those types will be reused in several places where the old suffix might
be counterintuitive.

libgrust/ChangeLog:

* libproc_macro/proc_macro.h (struct CustomDerivePayload):
Rename from CustomDerivePayload to...
(struct CustomDerive): ... CustomDerive
(struct AttrPayload): Rename from AttrPayload to ...
(struct Attribute): ... Attribute
(struct BangPayload): Rename from BangPayload to ...
(struct Bang): ... Bang
(union ProcmacroPayload): Update union with new names.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: expand: Add procmacro loading function
Pierre-Emmanuel Patry [Mon, 15 May 2023 13:23:50 +0000 (15:23 +0200)] 
gccrs: expand: Add procmacro loading function

Add a function to load a shared object procmacro and retrieve the list
of available procedural macros.

gcc/rust/ChangeLog:

* Make-lang.in: Add rust-proc-macro object to list.
* expand/rust-proc-macro.cc: New file.
* expand/rust-proc-macro.h: New file.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: libproc_macro: Add procmacro array definition
Pierre-Emmanuel Patry [Mon, 15 May 2023 13:13:38 +0000 (15:13 +0200)] 
gccrs: libproc_macro: Add procmacro array definition

Standard types do not guarantee anything about their ABI and are
therefore unconvenient to use as a primarily exposed container.
With such asumption this commit introduces a container.

libgrust/ChangeLog:

* libproc_macro/proc_macro.h (struct ProcmacroArray):
Add ProcmacroArray definition.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: attributes: Add allow_internal_unstable attribute
Pierre-Emmanuel Patry [Tue, 6 Jun 2023 16:45:36 +0000 (18:45 +0200)] 
gccrs: attributes: Add allow_internal_unstable attribute

Add allow_internal_unstable to the attribute table in order for it to be
flagged as builtin.

gcc/rust/ChangeLog:

* util/rust-attributes.cc: Add attribute to the table.

18 months agogccrs: collector: Move implementation to header
Pierre-Emmanuel Patry [Tue, 6 Jun 2023 12:57:14 +0000 (14:57 +0200)] 
gccrs: collector: Move implementation to header

Move implementation from cc file to header, in order to allow call from
other headers and prevent linker errors.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Move
implementation from here...
* ast/rust-ast-collector.h: ... to here.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: Fix non-mod-rs files' external module loading paths
Owen Avery [Sun, 28 May 2023 13:44:39 +0000 (09:44 -0400)] 
gccrs: Fix non-mod-rs files' external module loading paths

gcc/rust/ChangeLog:

* Make-lang.in: Add "rust-dir-owner.o".
* ast/rust-ast.cc: Include "rust-dir-owner.h".
(Module::process_file_path):
Handle non-mod-rs external file modules properly.
* parse/rust-parse-impl.h: Include "rust-dir-owner.h".
(Parser::parse_module):
Handle non-mod-rs external file modules properly.
* util/rust-dir-owner.cc: New file.
* util/rust-dir-owner.h: New file.

gcc/testsuite/ChangeLog:

* rust/compile/test_mod.rs: Moved to...
* rust/compile/issue-1089/test_mod.rs: ...here.
* rust/compile/mod_missing_middle.rs: Fix paths.
* rust/compile/missing_middle/both_path.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/both_path.rs: ...here.
* rust/compile/missing_middle/explicit.not.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/explicit.not.rs: ...here.
* rust/compile/missing_middle/other.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/explicit.not/other.rs: ...here.
* rust/compile/missing_middle/inner_path.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/inner_path.rs: ...here.
* rust/compile/missing_middle/outer_path.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/outer_path.rs: ...here.
* rust/compile/missing_middle/sub/mod.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/sub/mod.rs: ...here.
* rust/compile/torture/modules/mod.rs: Moved to...
* rust/compile/torture/extern_mod1/modules/mod.rs: ...here.
* rust/execute/torture/modules/mod.rs: Moved to...
* rust/execute/torture/extern_mod4/modules/mod.rs: ...here.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: Add error message when derive macro is invoked
Owen Avery [Mon, 5 Jun 2023 14:41:43 +0000 (10:41 -0400)] 
gccrs: Add error message when derive macro is invoked

gcc/rust/ChangeLog:

* expand/rust-macro-builtins.cc: Fix comments.
(MacroBuiltin::proc_macro_builtin): Add error message.
* expand/rust-macro-builtins.h: Fix comments.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: Respect the concrete type when resolving qualifed path types
Philip Herron [Wed, 31 May 2023 15:06:58 +0000 (16:06 +0100)] 
gccrs: Respect the concrete type when resolving qualifed path types

Concrete types can resolve to assoicated impl blocks which will allow us to
resolve the path to the projection type instead of the placeholder trait
associated type which can change. The projection will be fixed and is
safer to use.

Fixes #2165 #2166

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc: when the bound is concrete keep the mapping
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): add missing call
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): make this the same as paths

gcc/testsuite/ChangeLog:

* rust/compile/issue-2165.rs: New test.
* rust/compile/issue-2166.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
18 months agogccrs: Prevent initialization related warnings/errors for Optional
Owen Avery [Mon, 5 Jun 2023 19:54:08 +0000 (15:54 -0400)] 
gccrs: Prevent initialization related warnings/errors for Optional

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635#c53

gcc/rust/ChangeLog:

* util/rust-optional.h
(class Optional): Add volatile field to union.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: Handle crate metavariable
Owen Avery [Sat, 3 Jun 2023 23:57:33 +0000 (19:57 -0400)] 
gccrs: Handle crate metavariable

gcc/rust/ChangeLog:

* expand/rust-macro-substitute-ctx.cc
(SubstituteCtx::substitute_metavar): Substitute crate metavar.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: Allow parsing a borrow from struct expression
Owen Avery [Mon, 5 Jun 2023 20:48:55 +0000 (16:48 -0400)] 
gccrs: Allow parsing a borrow from struct expression

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h
(Parser::null_denotation): Allow struct expression referencing.

gcc/testsuite/ChangeLog:

* rust/compile/struct-expr-parse.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
18 months agogccrs: ast: dump literals correctly
Charalampos Mitrodimas [Sat, 3 Jun 2023 18:09:26 +0000 (18:09 +0000)] 
gccrs: ast: dump literals correctly

This commit fixes printing of literals based on their
type.

Previous implementation printed literals the same, regardless
their type. Now we are printing:
  * int, float, bool don't require special printing
  * char        -> '<char>'
  * string      -> "<string>"
  * byte        -> b'<byte>'
  * byte_string -> b"<byte_string>"

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::visit):
print literals based on their type.

Signed-off-by: Charalampos Mitrodimas <charmitro@gmail.com>
18 months agogccrs: derive: Add dummy builtin transcriber for builtin derive macros.
Arthur Cohen [Thu, 1 Jun 2023 14:23:27 +0000 (16:23 +0200)] 
gccrs: derive: Add dummy builtin transcriber for builtin derive macros.

Fixes #2257.

Derive builtins do not need a real transcriber, but still need one. It
will however never be called since builtin derive macros get expanded
differently, and benefit from knowing on what kind of items they are
applied (struct, enums, unions) rather than receiving a list of tokens
like regular builtin macros.

gcc/rust/ChangeLog:

* expand/rust-macro-builtins.cc (MacroBuiltin::dummy): New function.
* expand/rust-macro-builtins.h: Declare it.

gcc/testsuite/ChangeLog:

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

18 months agogccrs: derive: Add proper derive(Clone) for unions
Arthur Cohen [Fri, 12 May 2023 13:14:49 +0000 (15:14 +0200)] 
gccrs: derive: Add proper derive(Clone) for unions

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc (AstBuilder::struct_expr_struct): New function.
(AstBuilder::let): Likewise.
(AstBuilder::struct_expr): Likewise.
(AstBuilder::struct_expr_field): Likewise.
(AstBuilder::field_access): Likewise.
(AstBuilder::wildcard): Likewise.
* ast/rust-ast-builder.h: Likewise.
* expand/rust-derive-clone.cc (DeriveClone::visit_union): Implement
properly.

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro4.rs: New test.
* rust/compile/derive_macro6.rs: New test.

18 months agogccrs: derive: Add #[derive(Copy)] builtin
Arthur Cohen [Fri, 26 May 2023 11:37:13 +0000 (13:37 +0200)] 
gccrs: derive: Add #[derive(Copy)] builtin

gcc/rust/ChangeLog:

* Make-lang.in: Add new files.
* expand/rust-derive.cc (DeriveVisitor::derive): Call into DeriveCopy.
* expand/rust-derive-copy.cc: New file.
* expand/rust-derive-copy.h: New file.

18 months agogccrs: derive: Add #[derive(Clone)] for regular structs
Arthur Cohen [Wed, 17 May 2023 10:17:58 +0000 (12:17 +0200)] 
gccrs: derive: Add #[derive(Clone)] for regular structs

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::visit_struct): Implement proper
cloning for structs with fields.
* ast/rust-ast-builder.cc (AstBuilder::struct_expr): New function.
(AstBuilder::struct_expr_field): Likewise.
(AstBuilder::field_access): Likewise.
(AstBuilder::let): Likewise.
* ast/rust-ast-builder.h: Declare new functions.

gcc/testsuite/ChangeLog:

* rust/execute/torture/derive_macro4.rs: New test.

18 months agogccrs: converter: Refactor locations
Pierre-Emmanuel Patry [Wed, 31 May 2023 11:08:10 +0000 (13:08 +0200)] 
gccrs: converter: Refactor locations

Refactor location to make code smaller and cleaner.

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (convert): Use a variable instead
of multiple nested calls.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: converter: Convert back Locations from spans
Pierre-Emmanuel Patry [Wed, 31 May 2023 10:52:16 +0000 (12:52 +0200)] 
gccrs: converter: Convert back Locations from spans

Use spans to get locations of expanded tokens instead of using unknown
locations.

gcc/rust/ChangeLog:

* lex/rust-lex.h: Make build_token method public.
* lex/rust-token.h: Add a setter for a token location.
* util/rust-token-converter.cc (convert): Add the function to
convert from a Span to a Location.
(from_ident): Convert Ident span to a Location.
(from_literal): Convert Literal span to a Location.
(from_punct): Convert Punct span to a Location.
(from_group): Convert Group span to a Location.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
18 months agogccrs: converter: Add conversion from locations to spans
Pierre-Emmanuel Patry [Wed, 31 May 2023 09:17:56 +0000 (11:17 +0200)] 
gccrs: converter: Add conversion from locations to spans

Add function implementation to convert either a Location or a
RicheLocation to a libproc_macro span.

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (convert): Add implementation.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>