diff --git a/gcc/rust/ast/rust-collect-lang-items.cc b/gcc/rust/ast/rust-collect-lang-items.cc index 308720ae69ab..50d134a429f5 100644 --- a/gcc/rust/ast/rust-collect-lang-items.cc +++ b/gcc/rust/ast/rust-collect-lang-items.cc @@ -82,5 +82,13 @@ CollectLangItems::visit (AST::TraitItemType &item) DefaultASTVisitor::visit (item); } +void +CollectLangItems::visit (AST::Function &item) +{ + maybe_add_lang_item (item); + + DefaultASTVisitor::visit (item); +} + } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-collect-lang-items.h b/gcc/rust/ast/rust-collect-lang-items.h index 552648f04eda..1d021b1d9c50 100644 --- a/gcc/rust/ast/rust-collect-lang-items.h +++ b/gcc/rust/ast/rust-collect-lang-items.h @@ -47,6 +47,7 @@ class CollectLangItems : public DefaultASTVisitor void visit (AST::Trait &item) override; void visit (AST::TraitItemType &item) override; + void visit (AST::Function &item) override; private: template void maybe_add_lang_item (const T &item); diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 71cdcf0471ea..e5db7a71c50b 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -459,6 +459,8 @@ class MacroRulesDefinition : public VisItem DeclMacro, }; + MacroRulesDefinition (const MacroRulesDefinition &other) = default; + private: std::vector outer_attrs; Identifier rule_name; @@ -602,6 +604,18 @@ class MacroInvocation : public TypeNoBounds, public ExprWithoutBlock { public: + MacroInvocation (const MacroInvocation &other) + : TraitItem (other.locus), outer_attrs (other.outer_attrs), + locus (other.locus), node_id (other.node_id), + invoc_data (other.invoc_data), is_semi_coloned (other.is_semi_coloned), + kind (other.kind), builtin_kind (other.builtin_kind) + { + if (other.kind == InvocKind::Builtin) + for (auto &pending : other.pending_eager_invocs) + pending_eager_invocs.emplace_back ( + pending->clone_macro_invocation_impl ()); + } + enum class InvocKind { Regular, @@ -726,19 +740,6 @@ class MacroInvocation : public TypeNoBounds, pending_eager_invocs (std::move (pending_eager_invocs)) {} - MacroInvocation (const MacroInvocation &other) - : TraitItem (other.locus), ExternalItem (other.node_id), - outer_attrs (other.outer_attrs), locus (other.locus), - node_id (other.node_id), invoc_data (other.invoc_data), - is_semi_coloned (other.is_semi_coloned), kind (other.kind), - builtin_kind (other.builtin_kind) - { - if (other.kind == InvocKind::Builtin) - for (auto &pending : other.pending_eager_invocs) - pending_eager_invocs.emplace_back ( - pending->clone_macro_invocation_impl ()); - } - std::vector outer_attrs; location_t locus; NodeId node_id; diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 7323413bfce2..3070b3803b86 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -1013,7 +1013,15 @@ check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx) || scrutinee_kind == TyTy::TypeKind::TUPLE || scrutinee_kind == TyTy::TypeKind::REF); - if (scrutinee_kind == TyTy::TypeKind::FLOAT) + if (scrutinee_kind == TyTy::TypeKind::ADT) + { + // this will need to change but for now the first pass implementation, + // lets assert this is the case + TyTy::ADTType *adt = static_cast (scrutinee_expr_tyty); + if (adt->is_enum ()) + rust_assert (adt->number_of_variants () > 0); + } + else if (scrutinee_kind == TyTy::TypeKind::FLOAT) { // FIXME: CASE_LABEL_EXPR does not support floating point types. // Find another way to compile these. diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index eac2cba5c752..62ee758e5b96 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -638,6 +638,7 @@ ASTLoweringBase::lower_generic_args (AST::GenericArgs &args) for (auto &arg : args.get_generic_args ()) { + rust_debug ("[ARTHUR ] %s", arg.as_string ().c_str ()); switch (arg.get_kind ()) { case AST::GenericArg::Kind::Type: { diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index c92c9432a68e..91a9c4c02d21 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -17,6 +17,7 @@ // . #include "rust-ast-lower.h" +#include "optional.h" #include "rust-ast-lower-item.h" #include "rust-ast-lower-stmt.h" #include "rust-ast-lower-expr.h" @@ -24,8 +25,16 @@ #include "rust-ast-lower-type.h" #include "rust-ast-lower-pattern.h" #include "rust-ast-lower-struct-field-expr.h" +#include "rust-common.h" #include "rust-expr.h" +#include "rust-diagnostics.h" #include "rust-hir-expr.h" +#include "rust-hir-full-decls.h" +#include "rust-hir-map.h" +#include "rust-hir-path.h" +#include "rust-hir-stmt.h" +#include "rust-hir.h" +#include "rust-mapping-common.h" namespace Rust { namespace HIR { @@ -411,27 +420,308 @@ ASTLoweringExprWithBlock::visit (AST::WhileLoopExpr &expr) expr.get_outer_attrs ()); } +struct ForLoopDesugarCtx +{ + NodeId id; + location_t loc; + Analysis::Mappings &mappings; + + Analysis::NodeMapping next_mapping () + { + auto crate_num = mappings.get_current_crate (); + return Analysis::NodeMapping (crate_num, id, + mappings.get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + } + + PathExprSegment path_segment (std::string &&segment_name) + { + return PathExprSegment (next_mapping (), std::move (segment_name), loc, + GenericArgs::create_empty ()); + } + + std::unique_ptr lang_item_path (LangItem::Kind lang_item) + { + return Rust::make_unique (next_mapping (), lang_item); + } + + std::unique_ptr + path_in_expr (std::vector &&segment_names) + { + auto segments = std::vector (); + + for (const auto &seg : segment_names) + segments.emplace_back (PathExprSegment (next_mapping (), seg, loc, + GenericArgs::create_empty ())); + + return std::unique_ptr ( + new PathInExpression (next_mapping (), std::move (segments))); + } + + std::unique_ptr make_break_expr () + { + // FIXME(Arthur): This shouldn't call Lifetime::error, it does not make + // sense + return std::unique_ptr ( + new BreakExpr (next_mapping (), loc, Lifetime::error ())); + } + + MatchArm make_match_arm (std::unique_ptr pattern) + { + auto patterns = std::vector> (); + patterns.emplace_back (std::move (pattern)); + + return MatchArm (std::move (patterns), loc); + } + + MatchCase make_break_arm () + { + auto path = lang_item_path (LangItem::Kind::OPTION_NONE); + auto arm = make_match_arm (std::move (path)); + + auto break_expr = make_break_expr (); + + return MatchCase (next_mapping (), arm, std::move (break_expr)); + } + + std::unique_ptr make_assign_expr () + { + auto next = path_in_expr ({"__next"}); + auto val = path_in_expr ({"val"}); + + return std::unique_ptr (new AssignmentExpr (next_mapping (), + std::move (next), + std::move (val), loc)); + } + + MatchCase make_continue_arm () + { + // FIXME(Arthur): This is missing the `val` binding. Should be a + // TupleStructPattern? + auto path = lang_item_path (LangItem::Kind::OPTION_SOME); + auto arm = make_match_arm (std::move (path)); + + auto assign_expr = make_assign_expr (); + + return MatchCase (next_mapping (), arm, std::move (assign_expr)); + } + + std::unique_ptr + make_let (std::unique_ptr &&pattern, + tl::optional> &&init_expr = tl::nullopt) + { + return std::unique_ptr ( + new LetStmt (next_mapping (), std::move (pattern), std::move (init_expr), + tl::nullopt, {}, loc)); + } + + std::unique_ptr + make_function_call (std::unique_ptr &&function, + std::unique_ptr &&arg) + { + auto args = std::vector> (); + args.emplace_back (std::move (arg)); + + return std::unique_ptr (new CallExpr (next_mapping (), + std::move (function), + std::move (args), {}, loc)); + } + + std::unique_ptr + make_method_call (std::unique_ptr &&receiver, + PathExprSegment &&method_path, + tl::optional> &&arg) + { + auto args = std::vector> (); + if (arg) + args.emplace_back (std::move (*arg)); + + return std::unique_ptr ( + new MethodCallExpr (next_mapping (), std::move (receiver), + std::move (method_path), std::move (args), {}, loc)); + } + + std::unique_ptr make_identifier_pattern (std::string &&name, + Mutability mutability, + bool is_ref = false) + { + return std::unique_ptr ( + new IdentifierPattern (next_mapping (), Identifier (name), loc, is_ref, + mutability)); + } + + MatchArm make_identifier_pattern_arm (std::string &&name, + Mutability mutability, + bool is_ref = false) + { + auto pat = make_identifier_pattern (std::move (name), mutability, is_ref); + + return make_match_arm (std::move (pat)); + } + + std::unique_ptr make_mutable_borrow (std::unique_ptr &&to_borrow) + { + return std::unique_ptr (new BorrowExpr (next_mapping (), + std::move (to_borrow), + Mutability::Mut, {}, loc)); + } + LoopLabel no_label () + { + return LoopLabel (next_mapping (), Lifetime::error (), loc); + } + + std::unique_ptr make_loop (std::vector> &&stmts) + { + auto block = std::unique_ptr ( + new BlockExpr (next_mapping (), std::move (stmts), nullptr, false, {}, {}, + no_label (), loc, loc)); + + return std::unique_ptr ( + new LoopExpr (next_mapping (), std::move (block), loc, no_label ())); + } + + std::unique_ptr make_match (std::unique_ptr scrutinee, + std::vector &&cases) + { + return std::unique_ptr ( + new MatchExpr (next_mapping (), std::move (scrutinee), std::move (cases), + {}, {}, loc)); + } + + // Transform an expression into an expression-statement + std::unique_ptr statementify (std::unique_ptr &&expr) + { + return std::unique_ptr ( + new ExprStmt (next_mapping (), std::move (expr), loc)); + } + + std::unique_ptr make_block (std::unique_ptr stmt, + std::unique_ptr tail_expr) + { + auto stmts = std::vector> (); + stmts.emplace_back (std::move (stmt)); + + return std::unique_ptr ( + new BlockExpr (next_mapping (), std::move (stmts), std::move (tail_expr), + true, {}, {}, no_label (), loc, loc)); + } +}; + +// TODO(Arthur): Here, rustc 1.49 uses a match arm for the original call to +// IntoIterator::into_iter(). Can we avoid that? Why is that done? Lifetime +// reasons? + +// for in +// +// becomes: +// +// { +// let result = match ::std::iter::IntoIterator::into_iter() { +// mut iter => { +// loop { +// let mut __next; +// match ::std::iter::Iterator::next(&mut iter) { +// ::std::option::Option::Some(val) => __next = val, +// ::std::option::Option::None => break +// }; +// let = __next; +// +// ; +// } +// } +// }; +// result +// } void ASTLoweringExprWithBlock::visit (AST::ForLoopExpr &expr) { - // TODO FIXME - - // HIR::BlockExpr *loop_block - // = ASTLoweringBlock::translate (expr.get_loop_block ().get (), - // &terminated); - // HIR::LoopLabel loop_label = lower_loop_label (expr.get_loop_label ()); - // HIR::Expr *iterator_expr - // = ASTLoweringExpr::translate (expr.get_iterator_expr ().get (), - // &terminated); - // HIR::Pattern *loop_pattern - // = ASTLoweringPattern::translate (expr.get_pattern ().get ()); - - // auto crate_num = mappings->get_current_crate (); - // Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), - // mappings->get_next_hir_id (crate_num), - // UNKNOWN_LOCAL_DEFID); - - gcc_unreachable (); + auto ctx = ForLoopDesugarCtx{expr.get_node_id (), expr.get_locus (), + Analysis::Mappings::get ()}; + + auto head = std::unique_ptr ( + ASTLoweringExpr::translate (expr.get_iterator_expr ())); + auto pat = std::unique_ptr ( + ASTLoweringPattern::translate (expr.get_pattern ())); + auto body = std::unique_ptr ( + ASTLoweringExpr::translate (expr.get_loop_block ())); + + rust_assert (head); + rust_assert (pat); + rust_assert (body); + + // core::iter::IntoIterator::into_iter() + auto into_iter_call = ctx.make_function_call ( + ctx.lang_item_path (LangItem::Kind::INTOITER_INTOITER), std::move (head)); + + auto iter = ctx.path_in_expr ({"iter"}); + + // core::iter::Iterator::next(&mut iter) + auto next_call + = ctx.make_function_call (ctx.lang_item_path ( + LangItem::Kind::ITERATOR_NEXT), + ctx.make_mutable_borrow (iter->clone_expr ())); + + // None => break, + auto break_arm = ctx.make_break_arm (); + // Some(val) => __next = val, + auto continue_arm = ctx.make_continue_arm (); + + // match { + // + // + // } + auto match_next + = ctx.make_match (std::move (next_call), {continue_arm, break_arm}); + + // let mut __next; + auto let_next = ctx.make_let (ctx.path_in_expr ({"__next"})); + // let = __next; + auto let_pat = ctx.make_let (std::move (pat), ctx.path_in_expr ({"__next"})); + + auto loop_stmts = std::vector> (); + loop_stmts.emplace_back (std::move (let_next)); + loop_stmts.emplace_back (ctx.statementify (std::move (match_next))); + loop_stmts.emplace_back (std::move (let_pat)); + loop_stmts.emplace_back (ctx.statementify (std::move (body))); + + // loop { + // ; + // ; + // ; + // + // ; + // } + auto loop = ctx.make_loop (std::move (loop_stmts)); + + // mut iter => ... + auto mut_iter_pat = ctx.make_identifier_pattern_arm ("iter", Mutability::Mut); + + // TODO(Arthur): Put in ctx as a method? + // => + auto match_iter_case = MatchCase (ctx.next_mapping (), + std::move (mut_iter_pat), std::move (loop)); + + // match { } + auto outer_match + = ctx.make_match (std::move (into_iter_call), {match_iter_case}); + + // let result = ; + auto let_result + = ctx.make_let (ctx.make_identifier_pattern ("result", Mutability::Imm), + std::move (outer_match)); + + // result /* as a return value */ + auto result = ctx.path_in_expr ({"result"}); + + // { + // ; + // + // } + auto block = ctx.make_block (std::move (let_result), std::move (result)); + + rust_debug ("[ARTHUR] \n%s", block->as_string ().c_str ()); + + translated = block.release (); } void diff --git a/gcc/rust/hir/tree/rust-hir-path.cc b/gcc/rust/hir/tree/rust-hir-path.cc index c8d3079a85e3..097941fff0c6 100644 --- a/gcc/rust/hir/tree/rust-hir-path.cc +++ b/gcc/rust/hir/tree/rust-hir-path.cc @@ -132,6 +132,9 @@ PathExprSegment::operator= (PathExprSegment const &other) void PathPattern::iterate_path_segments (std::function cb) { + // FIXME: Is this necessary? + rust_assert (kind == Kind::Segmented); + for (auto it = segments.begin (); it != segments.end (); it++) { if (!cb (*it)) @@ -149,6 +152,15 @@ PathInExpression::PathInExpression (Analysis::NodeMapping mappings, has_opening_scope_resolution (has_opening_scope_resolution), locus (locus) {} +PathInExpression::PathInExpression (Analysis::NodeMapping mappings, + LangItem::Kind lang_item, location_t locus, + bool has_opening_scope_resolution, + std::vector outer_attrs) + : PathPattern (lang_item), + PathExpr (std::move (mappings), std::move (outer_attrs)), + has_opening_scope_resolution (has_opening_scope_resolution), locus (locus) +{} + bool PathInExpression::is_self () const @@ -341,6 +353,15 @@ QualifiedPathInExpression::QualifiedPathInExpression ( path_type (std::move (qual_path_type)), locus (locus) {} +QualifiedPathInExpression::QualifiedPathInExpression ( + Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, + LangItem::Kind lang_item, location_t locus, + std::vector outer_attrs) + : PathPattern (lang_item), + PathExpr (std::move (mappings), std::move (outer_attrs)), + path_type (std::move (qual_path_type)), locus (locus) +{} + QualifiedPathInType::QualifiedPathInType ( Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, std::unique_ptr associated_segment, diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index df5fd0c4e469..3ce54cce30c6 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -19,10 +19,12 @@ #ifndef RUST_HIR_PATH_H #define RUST_HIR_PATH_H +#include "rust-hir-map.h" #include "rust-hir-simple-path.h" #include "rust-hir-type-no-bounds.h" #include "rust-hir-pattern-abstract.h" #include "rust-hir-expr-abstract.h" +#include "rust-system.h" namespace Rust { namespace HIR { @@ -230,15 +232,34 @@ class PathExprSegment // HIR node representing a pattern that involves a "path" - abstract base class class PathPattern : public Pattern { +public: + enum class Kind + { + Segmented, + LangItem + }; + +private: std::vector segments; + tl::optional lang_item; + Kind kind; protected: PathPattern (std::vector segments) - : segments (std::move (segments)) + : segments (std::move (segments)), lang_item (tl::nullopt), + kind (Kind::Segmented) + {} + + PathPattern (LangItem::Kind lang_item) + : segments ({}), lang_item (lang_item), kind (Kind::LangItem) {} // Returns whether path has segments. - bool has_segments () const { return !segments.empty (); } + bool has_segments () const + { + rust_assert (kind == Kind::Segmented); + return !segments.empty (); + } /* Converts path segments to their equivalent SimplePath segments if possible, * and creates a SimplePath from them. */ @@ -248,26 +269,65 @@ class PathPattern : public Pattern public: /* Returns whether the path is a single segment (excluding qualified path * initial as segment). */ - bool is_single_segment () const { return segments.size () == 1; } + bool is_single_segment () const + { + rust_assert (kind == Kind::Segmented); + return segments.size () == 1; + } std::string as_string () const override; void iterate_path_segments (std::function cb); - size_t get_num_segments () const { return segments.size (); } + size_t get_num_segments () const + { + if (kind == Kind::LangItem) + { + // rust_fatal_error (UNKNOWN_LOCATION, "[ARTHUR] %s", + // LangItem::ToString (*lang_item).c_str ()); + rust_unreachable (); + } + + rust_assert (kind == Kind::Segmented); + return segments.size (); + } - std::vector &get_segments () { return segments; } + std::vector &get_segments () + { + rust_assert (kind == Kind::Segmented); + return segments; + } - const std::vector &get_segments () const { return segments; } + const std::vector &get_segments () const + { + rust_assert (kind == Kind::Segmented); + return segments; + } - PathExprSegment &get_root_seg () { return segments.at (0); } + PathExprSegment &get_root_seg () + { + rust_assert (kind == Kind::Segmented); + return segments.at (0); + } - const PathExprSegment &get_final_segment () const { return segments.back (); } + const PathExprSegment &get_final_segment () const + { + rust_assert (kind == Kind::Segmented); + return segments.back (); + } PatternType get_pattern_type () const override final { return PatternType::PATH; } + + LangItem::Kind get_lang_item_kind () const + { + rust_assert (kind == Kind::LangItem); + return *lang_item; + } + + Kind get_path_kind () const { return kind; } }; /* HIR node representing a path-in-expression pattern (path that allows generic @@ -288,6 +348,13 @@ class PathInExpression : public PathPattern, public PathExpr std::vector outer_attrs = std::vector ()); + // lang-item Constructor + PathInExpression (Analysis::NodeMapping mappings, LangItem::Kind kind, + location_t locus = UNDEF_LOCATION, + bool has_opening_scope_resolution = false, + std::vector outer_attrs + = std::vector ()); + // Creates an error state path in expression. static PathInExpression create_error () { @@ -671,6 +738,14 @@ class QualifiedPathInExpression : public PathPattern, public PathExpr std::vector outer_attrs = std::vector ()); + // lang-item constructor + QualifiedPathInExpression (Analysis::NodeMapping mappings, + QualifiedPathType qual_path_type, + LangItem::Kind lang_item, + location_t locus = UNDEF_LOCATION, + std::vector outer_attrs + = std::vector ()); + location_t get_locus () const override final { return locus; } void accept_vis (HIRFullVisitor &vis) override; diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index 509a897ce39c..9b3bd145ba18 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -19,6 +19,7 @@ #include "rust-ast-full.h" #include "rust-hir-expr.h" #include "rust-hir-full.h" +#include "rust-hir-path.h" #include "rust-hir-visitor.h" #include "rust-diagnostics.h" @@ -1210,6 +1211,9 @@ ClosureExpr::as_string () const std::string PathPattern::as_string () const { + if (kind == PathPattern::Kind::LangItem) + return LangItem::ToString (*lang_item); + std::string str; for (const auto &segment : segments) @@ -2187,6 +2191,8 @@ TypeParam::as_string () const AST::SimplePath PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const { + rust_assert (kind == Kind::Segmented); + if (!has_segments ()) { return AST::SimplePath::create_empty (); diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc index 15ae8db9389e..f30dc00cba39 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.cc +++ b/gcc/rust/resolve/rust-ast-resolve-type.cc @@ -17,6 +17,7 @@ // . #include "rust-ast-resolve-type.h" +#include "rust-ast-dump.h" #include "rust-ast-resolve-expr.h" namespace Rust { @@ -495,10 +496,12 @@ ResolveTypeToCanonicalPath::visit (AST::TraitObjectTypeOneBound &type) } void -ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &) +ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &ty) { // FIXME is this actually allowed? dyn A+B - rust_unreachable (); + // FIXME: This is also reached when we have something like a lifetime bound on + // a generic, e.g. Iter<'a, T: 'a> What do we do in that case? + // rust_unreachable (); } void diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index c1700fafdfc9..655f1382a00b 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc @@ -38,6 +38,9 @@ TypeCheckBase::check_for_unconstrained ( const TyTy::SubstitutionArgumentMappings &constraint_b, const TyTy::BaseType *reference) { + rust_debug ("[ARTHUR] %s ", constraint_a.as_string ().c_str ()); + rust_debug ("[ARTHUR] %s ", constraint_b.as_string ().c_str ()); + bool check_result = false; bool check_completed = context->have_checked_for_unconstrained (reference->get_ref (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 7daa27195db6..b681cf94aa51 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -1102,8 +1102,10 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr) richloc.add_fixit_replace ("method not found"); rust_error_at ( richloc, ErrorCode::E0599, - "no method named %qs found in the current scope", - expr.get_method_name ().get_segment ().as_string ().c_str ()); + "no method named %qs found in the current scope for type %qs (%qs)", + expr.get_method_name ().get_segment ().as_string ().c_str (), + receiver_tyty->as_string ().c_str (), + receiver_tyty->debug_str ().c_str ()); return; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index 17c331e9a21a..7e57b3b7bcd4 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -740,8 +740,8 @@ TypeCheckItem::resolve_impl_block_substitutions (HIR::ImplBlock &impl_block, const TyTy::SubstitutionArgumentMappings impl_constraints = GetUsedSubstArgs::From (self); - failure_flag = check_for_unconstrained (substitutions, trait_constraints, - impl_constraints, self); + // failure_flag = check_for_unconstrained (substitutions, trait_constraints, + // impl_constraints, self); return {substitutions, region_constraints}; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc index d3f341264482..9d9493ad0fb9 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-path.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc @@ -16,6 +16,9 @@ // along with GCC; see the file COPYING3. If not see // . +#include "rust-diagnostics.h" +#include "rust-hir-map.h" +#include "rust-hir-path.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-item.h" @@ -24,6 +27,7 @@ #include "rust-hir-path-probe.h" #include "rust-type-util.h" #include "rust-hir-type-bounds.h" +#include "rust-hir-item.h" #include "rust-session-manager.h" #include "rust-immutable-name-resolution-context.h" @@ -179,20 +183,42 @@ void TypeCheckExpr::visit (HIR::PathInExpression &expr) { NodeId resolved_node_id = UNKNOWN_NODEID; - size_t offset = -1; - TyTy::BaseType *tyseg = resolve_root_path (expr, &offset, &resolved_node_id); - if (tyseg->get_kind () == TyTy::TypeKind::ERROR) - return; - - bool fully_resolved = offset == expr.get_segments ().size (); - if (fully_resolved) + if (expr.get_path_kind () == HIR::PathPattern::Kind::LangItem) { - infered = tyseg; - return; + if (auto lookup = Analysis::Mappings::get ().lookup_lang_item_node ( + expr.get_lang_item_kind ())) + { + auto hir_id = mappings.lookup_node_to_hir (*lookup); + + TyTy::BaseType *resolved = nullptr; + context->lookup_type (*hir_id, &resolved); + + infered = resolved; + return; + } + else + rust_fatal_error ( + expr.get_locus (), "lang item missing: %s", + LangItem::ToString (expr.get_lang_item_kind ()).c_str ()); } + else + { + size_t offset = -1; + TyTy::BaseType *tyseg + = resolve_root_path (expr, &offset, &resolved_node_id); + if (tyseg->get_kind () == TyTy::TypeKind::ERROR) + return; + + bool fully_resolved = offset == expr.get_segments ().size (); + if (fully_resolved) + { + infered = tyseg; + return; + } - resolve_segments (resolved_node_id, expr.get_segments (), offset, tyseg, - expr.get_mappings (), expr.get_locus ()); + resolve_segments (resolved_node_id, expr.get_segments (), offset, tyseg, + expr.get_mappings (), expr.get_locus ()); + } } TyTy::BaseType * @@ -234,7 +260,8 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset, } rust_error_at (seg.get_locus (), - "failed to type resolve root segment"); + "failed to type resolve root segment: %s", + expr.as_string ().c_str ()); return new TyTy::ErrorType (expr.get_mappings ().get_hirid ()); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 2962674da62f..60da1b351e59 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -377,6 +377,8 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset, NodeId *root_resolved_node_id, bool *wasBigSelf) { + rust_debug ("[ARTHURl ] %s", path.as_string ().c_str ()); + TyTy::BaseType *root_tyty = nullptr; *offset = 0; for (size_t i = 0; i < path.get_num_segments (); i++) diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc index 0d8a98077d1e..8b1b1e346474 100644 --- a/gcc/rust/util/rust-lang-item.cc +++ b/gcc/rust/util/rust-lang-item.cc @@ -91,6 +91,12 @@ const BiMap Rust::LangItem::lang_items = {{ {"str", Kind::STR}, {"f32_runtime", Kind::F32_RUNTIME}, {"f64_runtime", Kind::F64_RUNTIME}, + + {"Some", Kind::OPTION_SOME}, + {"None", Kind::OPTION_NONE}, + + {"into_iter", Kind::INTOITER_INTOITER}, + {"next", Kind::ITERATOR_NEXT}, }}; tl::optional diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h index bcf41df559e5..4b0217616783 100644 --- a/gcc/rust/util/rust-lang-item.h +++ b/gcc/rust/util/rust-lang-item.h @@ -26,6 +26,9 @@ namespace Rust { class LangItem { public: + // FIXME: We should clean up that enum to make it more inline with the list of + // lang-items in Rust 1.49 + // https://github.com/rust-lang/rust/blob/1.49.0/compiler/rustc_hir/src/lang_items.rs enum class Kind { // https://github.com/rust-lang/rust/blob/master/library/core/src/ops/arith.rs @@ -116,6 +119,12 @@ class LangItem STR, F32_RUNTIME, F64_RUNTIME, + + OPTION_SOME, + OPTION_NONE, + + INTOITER_INTOITER, + ITERATOR_NEXT, }; static const BiMap lang_items;