| // Copyright (C) 2020-2026 Free Software Foundation, Inc. | |
| // This file is part of GCC. | |
| // GCC is free software; you can redistribute it and/or modify it under | |
| // the terms of the GNU General Public License as published by the Free | |
| // Software Foundation; either version 3, or (at your option) any later | |
| // version. | |
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| // for more details. | |
| // You should have received a copy of the GNU General Public License | |
| // along with GCC; see the file COPYING3. If not see | |
| // <http://www.gnu.org/licenses/>. | |
| #include "rust-tyty-call.h" | |
| #include "optional.h" | |
| #include "rust-hir-item.h" | |
| #include "rust-hir-type-check-expr.h" | |
| #include "rust-hir-type-check.h" | |
| #include "rust-type-util.h" | |
| #include "rust-hir-trait-reference.h" | |
| #include "rust-tyty.h" | |
| namespace Rust { | |
| namespace TyTy { | |
| void | |
| emit_unexpected_argument_error (location_t loc, | |
| unsigned long unexpected_arg_count, | |
| unsigned long expected_arg_count) | |
| { | |
| // https://doc.rust-lang.org/error_codes/E0061.html | |
| // rustc treats 1 as singular and others as plural | |
| std::string err_msg = "this function takes %lu "; | |
| if (expected_arg_count == 1) | |
| { | |
| err_msg += "argument"; | |
| } | |
| else | |
| { | |
| err_msg += "arguments"; | |
| } | |
| if (unexpected_arg_count == 1) | |
| { | |
| err_msg += " but %lu argument was supplied"; | |
| } | |
| else | |
| { | |
| err_msg += " but %lu arguments were supplied"; | |
| } | |
| rust_error_at (loc, ErrorCode::E0061, err_msg.c_str (), expected_arg_count, | |
| unexpected_arg_count); | |
| } | |
| static bool | |
| validate_call_argument_associated_impl_bounds (BaseType *param_ty, | |
| BaseType *argument_ty, | |
| location_t locus) | |
| { | |
| auto *context = Resolver::TypeCheckContext::get (); | |
| // impl bodies are checked generically | |
| if (context->have_function_context () | |
| && context->peek_context ().get_type () | |
| == Resolver::TypeCheckContextItem::IMPL_ITEM) | |
| return true; | |
| auto *resolved_argument_ty = argument_ty->destructure (); | |
| if (resolved_argument_ty->get_kind () == TypeKind::PARAM | |
| || resolved_argument_ty->get_kind () == TypeKind::INFER | |
| || resolved_argument_ty->get_kind () == TypeKind::PROJECTION) | |
| return true; | |
| for (const auto &bound : param_ty->get_specified_bounds ()) | |
| { | |
| bool ambigious = false; | |
| auto associated | |
| = Resolver::lookup_associated_impl_block (bound, argument_ty, | |
| &ambigious); | |
| if (associated == nullptr) | |
| continue; | |
| auto mapping = associated->bind_impl_for_bound (argument_ty, bound, locus, | |
| true /*emit_error*/); | |
| if (mapping.is_error ()) | |
| return false; | |
| } | |
| return true; | |
| } | |
| void | |
| TypeCheckCallExpr::visit (ADTType &type) | |
| { | |
| rust_assert (!variant.is_error ()); | |
| if (variant.get_variant_type () != TyTy::VariantDef::VariantType::TUPLE) | |
| { | |
| rust_error_at ( | |
| call.get_locus (), ErrorCode::E0618, | |
| "expected function, tuple struct or tuple variant, found struct %qs", | |
| type.get_name ().c_str ()); | |
| return; | |
| } | |
| if (call.num_params () != variant.num_fields ()) | |
| { | |
| emit_unexpected_argument_error (call.get_locus (), | |
| (unsigned long) call.num_params (), | |
| (unsigned long) variant.num_fields ()); | |
| return; | |
| } | |
| size_t i = 0; | |
| for (auto &argument : call.get_arguments ()) | |
| { | |
| StructFieldType *field = variant.get_field_at_index (i); | |
| BaseType *field_tyty = field->get_field_type (); | |
| location_t arg_locus = argument->get_locus (); | |
| BaseType *arg = Resolver::TypeCheckExpr::Resolve (*argument); | |
| if (arg->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at (argument->get_locus (), | |
| "failed to resolve argument type"); | |
| return; | |
| } | |
| HirId coercion_side_id = argument->get_mappings ().get_hirid (); | |
| auto res = Resolver::coercion_site (coercion_side_id, | |
| TyWithLocation (field_tyty), | |
| TyWithLocation (arg, arg_locus), | |
| argument->get_locus ()); | |
| if (res->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| return; | |
| } | |
| i++; | |
| } | |
| if (i != call.num_params ()) | |
| { | |
| emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, | |
| (unsigned long) call.num_params ()); | |
| return; | |
| } | |
| resolved = type.clone (); | |
| } | |
| void | |
| TypeCheckCallExpr::visit (FnType &type) | |
| { | |
| std::vector<size_t> indexes; | |
| const auto lookup = mappings.lookup_defid (type.get_id ()); | |
| if (lookup.has_value ()) | |
| { | |
| const auto *item = lookup.value (); | |
| rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Function); | |
| const auto &fn = *static_cast<const HIR::Function *> (item); | |
| auto legacy_const_generic_indexes | |
| = fn.get_legacy_const_generic_indexes (); | |
| bool malformed_const_generic_indexes | |
| = !legacy_const_generic_indexes.has_value (); | |
| if (malformed_const_generic_indexes) | |
| return; | |
| indexes = legacy_const_generic_indexes.value (); | |
| } | |
| size_t expected_args = type.num_params () + indexes.size (); | |
| if (call.num_params () < expected_args | |
| || (!type.is_variadic () && call.num_params () != expected_args)) | |
| { | |
| emit_unexpected_argument_error (call.get_locus (), call.num_params (), | |
| expected_args); | |
| return; | |
| } | |
| if (!indexes.empty ()) | |
| { | |
| std::set<size_t> seen; | |
| for (size_t i = 0; i < indexes.size (); i++) | |
| { | |
| size_t index = indexes[i]; | |
| seen.insert (index); | |
| bool duplicate_index = seen.size () != i + 1; | |
| if (index >= call.num_params () || duplicate_index) | |
| { | |
| rust_error_at (call.get_locus (), "invalid const argument index"); | |
| return; | |
| } | |
| } | |
| std::vector<SubstitutionParamMapping *> const_params; | |
| for (auto &sub : type.get_substs ()) | |
| { | |
| if (sub.get_generic_param ().get_kind () | |
| == HIR::GenericParam::GenericKind::CONST) | |
| const_params.push_back (&sub); | |
| } | |
| if (const_params.size () != indexes.size ()) | |
| { | |
| rust_error_at ( | |
| call.get_locus (), | |
| "const argument count does not match const parameters"); | |
| return; | |
| } | |
| auto &subst_mappings = type.get_substitution_arguments (); | |
| for (size_t i = 0; i < indexes.size (); i++) | |
| { | |
| size_t cidx = indexes[i]; | |
| auto &argument = *call.get_arguments ().at (cidx); | |
| auto *param = const_params[i]->get_param_ty (); | |
| auto const_ty = param->as_const_type ()->get_specified_type (); | |
| auto *value | |
| = SubstitutionRef::resolve_const_argument (argument, const_ty); | |
| if (value->is<ErrorType> ()) | |
| return; | |
| SubstitutionArg existing = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (param, &existing); | |
| rust_assert (ok); | |
| Resolver::unify_site (argument.get_mappings ().get_hirid (), | |
| TyWithLocation (existing.get_tyty ()), | |
| TyWithLocation (value), argument.get_locus ()); | |
| } | |
| } | |
| call.set_const_argument_indexes (indexes); | |
| // if the surrounding context has pushed an expected type, try unifying it | |
| // with the fn's return type before checking arguments. This lets the callee | |
| // result constrain inference variables that may appear in parameter | |
| // projections. | |
| auto *ctx = Resolver::TypeCheckContext::get (); | |
| TyTy::BaseType *expected = ctx->peek_expected_type (); | |
| const TyTy::BaseType *return_infer | |
| = type.get_return_type ()->contains_infer (); | |
| if (expected != nullptr && return_infer != nullptr) | |
| { | |
| Resolver::unify_site_and (call.get_mappings ().get_hirid (), | |
| TyWithLocation (expected), | |
| TyWithLocation (type.get_return_type ()), | |
| call.get_locus (), false /*emit_errors*/, | |
| true /*commit_if_ok*/, | |
| true /*implicit_infer_vars*/, true /*cleanup*/); | |
| } | |
| size_t i; | |
| for (i = 0; i < call.get_arguments ().size (); i++) | |
| { | |
| auto &argument = call.get_arguments ().at (i); | |
| if (call.is_const_argument (i)) | |
| continue; | |
| location_t arg_locus = argument->get_locus (); | |
| TyTy::BaseType *param_ty = nullptr; | |
| if (i < type.num_params ()) | |
| param_ty = type.param_at (i).get_type (); | |
| if (param_ty != nullptr) | |
| ctx->push_expected_type (param_ty); | |
| auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument); | |
| if (param_ty != nullptr) | |
| ctx->pop_expected_type (); | |
| if (argument_expr_tyty->is<TyTy::ErrorType> ()) | |
| return; | |
| // it might be a variadic function | |
| if (i < type.num_params ()) | |
| { | |
| auto &fnparam = type.param_at (i); | |
| location_t param_locus | |
| = fnparam.has_pattern () | |
| ? fnparam.get_pattern ().get_locus () | |
| : mappings.lookup_location (param_ty->get_ref ()); | |
| HirId coercion_side_id = argument->get_mappings ().get_hirid (); | |
| auto resolved_argument_type | |
| = Resolver::coercion_site (coercion_side_id, | |
| TyWithLocation (param_ty, param_locus), | |
| TyWithLocation (argument_expr_tyty, | |
| arg_locus), | |
| argument->get_locus ()); | |
| if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| return; | |
| } | |
| if (!validate_call_argument_associated_impl_bounds ( | |
| param_ty, argument_expr_tyty, argument->get_locus ())) | |
| return; | |
| } | |
| else | |
| { | |
| switch (argument_expr_tyty->get_kind ()) | |
| { | |
| case TyTy::TypeKind::ERROR: | |
| return; | |
| case TyTy::TypeKind::INT: | |
| { | |
| auto &int_ty | |
| = static_cast<TyTy::IntType &> (*argument_expr_tyty); | |
| if ((int_ty.get_int_kind () == TyTy::IntType::IntKind::I8) | |
| || (int_ty.get_int_kind () == TyTy::IntType::IntKind::I16)) | |
| { | |
| rich_location richloc (line_table, arg_locus); | |
| richloc.add_fixit_replace ( | |
| "cast the value to c_int: as c_int"); | |
| rust_error_at (richloc, ErrorCode::E0617, | |
| "expected %<c_int%> variadic argument"); | |
| return; | |
| } | |
| break; | |
| } | |
| case TyTy::TypeKind::UINT: | |
| { | |
| auto &uint_ty | |
| = static_cast<TyTy::UintType &> (*argument_expr_tyty); | |
| if ((uint_ty.get_uint_kind () == TyTy::UintType::UintKind::U8) | |
| || (uint_ty.get_uint_kind () | |
| == TyTy::UintType::UintKind::U16)) | |
| { | |
| rich_location richloc (line_table, arg_locus); | |
| richloc.add_fixit_replace ( | |
| "cast the value to c_uint: as c_uint"); | |
| rust_error_at (richloc, ErrorCode::E0617, | |
| "expected %<c_uint%> variadic argument"); | |
| return; | |
| } | |
| break; | |
| } | |
| case TyTy::TypeKind::FLOAT: | |
| { | |
| if (static_cast<TyTy::FloatType &> (*argument_expr_tyty) | |
| .get_float_kind () | |
| == TyTy::FloatType::FloatKind::F32) | |
| { | |
| rich_location richloc (line_table, arg_locus); | |
| richloc.add_fixit_replace ( | |
| "cast the value to c_double: as c_double"); | |
| rust_error_at (richloc, ErrorCode::E0617, | |
| "expected %<c_double%> variadic argument"); | |
| return; | |
| } | |
| break; | |
| } | |
| case TyTy::TypeKind::BOOL: | |
| { | |
| rich_location richloc (line_table, arg_locus); | |
| richloc.add_fixit_replace ("cast the value to c_int: as c_int"); | |
| rust_error_at (arg_locus, ErrorCode::E0617, | |
| "expected %<c_int%> variadic argument"); | |
| return; | |
| } | |
| case TyTy::TypeKind::FNDEF: | |
| { | |
| rust_error_at ( | |
| arg_locus, ErrorCode::E0617, | |
| "unexpected function definition type as variadic " | |
| "argument - cast to function pointer"); | |
| } | |
| return; | |
| default: | |
| break; | |
| } | |
| } | |
| } | |
| if (i + indexes.size () < call.num_params ()) | |
| { | |
| emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, | |
| (unsigned long) call.num_params ()); | |
| return; | |
| } | |
| type.monomorphize (); | |
| Resolver::rebind_projection_self_from_fn (type, type.get_return_type ()); | |
| resolved = type.get_return_type (); | |
| } | |
| void | |
| TypeCheckCallExpr::visit (FnPtr &type) | |
| { | |
| if (call.num_params () != type.num_params ()) | |
| { | |
| emit_unexpected_argument_error (call.get_locus (), | |
| (unsigned long) call.num_params (), | |
| (unsigned long) type.num_params ()); | |
| return; | |
| } | |
| size_t i = 0; | |
| for (auto &argument : call.get_arguments ()) | |
| { | |
| location_t arg_locus = argument->get_locus (); | |
| BaseType *fnparam = type.get_param_type_at (i); | |
| auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument); | |
| if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at ( | |
| argument->get_locus (), | |
| "failed to resolve type for argument expr in CallExpr"); | |
| return; | |
| } | |
| auto resolved_argument_type = Resolver::coercion_site ( | |
| argument->get_mappings ().get_hirid (), TyWithLocation (fnparam), | |
| TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ()); | |
| if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| return; | |
| } | |
| i++; | |
| } | |
| if (i != call.num_params ()) | |
| { | |
| emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, | |
| (unsigned long) call.num_params ()); | |
| return; | |
| } | |
| resolved = type.get_return_type ()->monomorphized_clone (); | |
| } | |
| // method call checker | |
| TypeCheckMethodCallExpr::TypeCheckMethodCallExpr ( | |
| Analysis::NodeMapping call_mappings, std::vector<Argument> &args, | |
| location_t call_locus, location_t receiver_locus, | |
| TyTy::BaseType *adjusted_self, Resolver::TypeCheckContext *context) | |
| : call_mappings (call_mappings), arguments (args), call_locus (call_locus), | |
| receiver_locus (receiver_locus), adjusted_self (adjusted_self), | |
| context (context), mappings (Analysis::Mappings::get ()) | |
| {} | |
| BaseType * | |
| TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call, | |
| TyTy::BaseType *adjusted_self, | |
| Resolver::TypeCheckContext *context) | |
| { | |
| std::vector<Argument> args; | |
| for (auto &arg : call.get_arguments ()) | |
| { | |
| BaseType *argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*arg); | |
| if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at (arg->get_locus (), | |
| "failed to resolve type for argument"); | |
| return new ErrorType (ref->get_ref ()); | |
| } | |
| args.emplace_back (arg->get_mappings (), argument_expr_tyty, | |
| arg->get_locus ()); | |
| } | |
| TypeCheckMethodCallExpr checker (call.get_mappings (), args, | |
| call.get_locus (), | |
| call.get_receiver ().get_locus (), | |
| adjusted_self, context); | |
| return checker.check (*ref); | |
| } | |
| BaseType * | |
| TypeCheckMethodCallExpr::go (FnType *ref, Analysis::NodeMapping call_mappings, | |
| std::vector<Argument> &args, location_t call_locus, | |
| location_t receiver_locus, | |
| TyTy::BaseType *adjusted_self, | |
| Resolver::TypeCheckContext *context) | |
| { | |
| TypeCheckMethodCallExpr checker (call_mappings, args, call_locus, | |
| receiver_locus, adjusted_self, context); | |
| return checker.check (*ref); | |
| } | |
| BaseType * | |
| TypeCheckMethodCallExpr::check (FnType &type) | |
| { | |
| Resolver::unify_site (call_mappings.get_hirid (), | |
| TyWithLocation (type.get_self_type ()), | |
| TyWithLocation (adjusted_self, receiver_locus), | |
| call_locus); | |
| // +1 for the receiver self | |
| size_t num_args_to_call = arguments.size () + 1; | |
| if (num_args_to_call != type.num_params ()) | |
| { | |
| emit_unexpected_argument_error (call_locus, | |
| (unsigned long) num_args_to_call, | |
| (unsigned long) type.num_params ()); | |
| return new ErrorType (type.get_ref ()); | |
| } | |
| size_t i = 1; | |
| for (auto &argument : arguments) | |
| { | |
| location_t arg_locus = argument.get_locus (); | |
| auto &fnparam = type.param_at (i); | |
| BaseType *param_ty = fnparam.get_type (); | |
| location_t param_locus | |
| = fnparam.has_pattern () | |
| ? fnparam.get_pattern ().get_locus () | |
| : mappings.lookup_location (param_ty->get_ref ()); | |
| auto argument_expr_tyty = argument.get_argument_type (); | |
| HirId coercion_side_id = argument.get_mappings ().get_hirid (); | |
| auto resolved_argument_type = Resolver::coercion_site ( | |
| coercion_side_id, TyWithLocation (param_ty, param_locus), | |
| TyWithLocation (argument_expr_tyty, arg_locus), arg_locus); | |
| if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| return new ErrorType (type.get_ref ()); | |
| } | |
| if (!validate_call_argument_associated_impl_bounds ( | |
| param_ty, argument_expr_tyty, argument.get_locus ())) | |
| return new ErrorType (type.get_ref ()); | |
| i++; | |
| } | |
| if (i != num_args_to_call) | |
| { | |
| emit_unexpected_argument_error (call_locus, (unsigned long) i, | |
| (unsigned long) arguments.size ()); | |
| return new ErrorType (type.get_ref ()); | |
| } | |
| type.monomorphize (); | |
| return type.get_return_type ()->monomorphized_clone (); | |
| } | |
| } // namespace TyTy | |
| } // namespace Rust |