From b96299b0b96b6714d5a7b03fa6d4f1ea7d09fc44 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 8 Feb 2021 15:51:54 +0000 Subject: Fix bad type resolution for CallExpr when fn is not simply PathInExpression When we have a function that is for example a FieldAccessExpression the compiler must resolve and compile that field into a reference which can be called. This is not simple direct call to a function in that senario. Fixes #217 --- gcc/rust/backend/rust-compile.cc | 46 +++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 26 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 0b83c72..600b2f9 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -53,33 +53,9 @@ CompileCrate::go () void CompileExpr::visit (HIR::CallExpr &expr) { - // this can be a function call or it can be a constructor for a tuple struct - Bexpression *fn = ResolvePathRef::Compile (expr.get_fnexpr (), ctx); - if (fn != nullptr) + Btype *type = ResolvePathType::Compile (expr.get_fnexpr (), ctx); + if (type != nullptr) { - std::vector args; - expr.iterate_params ([&] (HIR::Expr *p) mutable -> bool { - Bexpression *compiled_expr = CompileExpr::Compile (p, ctx); - rust_assert (compiled_expr != nullptr); - args.push_back (compiled_expr); - return true; - }); - - auto fncontext = ctx->peek_fn (); - translated - = ctx->get_backend ()->call_expression (fncontext.fndecl, fn, args, - nullptr, expr.get_locus ()); - } - else - { - Btype *type = ResolvePathType::Compile (expr.get_fnexpr (), ctx); - if (type == nullptr) - { - rust_fatal_error (expr.get_locus (), - "failed to lookup type associated with call"); - return; - } - // this assumes all fields are in order from type resolution and if a base // struct was specified those fields are filed via accesors std::vector vals; @@ -93,6 +69,24 @@ CompileExpr::visit (HIR::CallExpr &expr) = ctx->get_backend ()->constructor_expression (type, vals, expr.get_locus ()); } + else + { + // must be a call to a function + Bexpression *fn = CompileExpr::Compile (expr.get_fnexpr (), ctx); + + std::vector args; + expr.iterate_params ([&] (HIR::Expr *p) mutable -> bool { + Bexpression *compiled_expr = CompileExpr::Compile (p, ctx); + rust_assert (compiled_expr != nullptr); + args.push_back (compiled_expr); + return true; + }); + + auto fncontext = ctx->peek_fn (); + translated + = ctx->get_backend ()->call_expression (fncontext.fndecl, fn, args, + nullptr, expr.get_locus ()); + } } void -- cgit v1.1