static bool method_chain(pass_opt_t* opt, ast_t* ast) { if(!method_application(opt, ast, false)) return false; // We check the receiver cap now instead of in method_application because // we need to know whether the receiver was recovered. ast_t* lhs = ast_childidx(ast, 2); ast_t* r_type = method_receiver_type(lhs); if(ast_id(lhs) == TK_FUNCHAIN) { bool recovered; if(!check_receiver_cap(opt, ast, &recovered)) return false; if(!check_nonsendable_recover(opt, ast)) return false; ast_t* f_type = ast_type(lhs); token_id f_cap = ast_id(ast_child(f_type)); ast_t* c_type = chain_type(r_type, f_cap, recovered); ast_settype(ast, c_type); } else { ast_settype(ast, r_type); } return true; }
static bool method_chain(pass_opt_t* opt, ast_t* ast) { if(!method_application(opt, ast, false)) return false; AST_GET_CHILDREN(ast, positional, namedargs, question, lhs); ast_t* type = ast_type(lhs); if(ast_id(ast_child(type)) == TK_AT) { ast_error(opt->check.errors, ast, "a bare method cannot be chained"); return false; } // We check the receiver cap now instead of in method_application because // we need to know whether the receiver was recovered. ast_t* r_type = method_receiver_type(lhs); if(ast_id(lhs) == TK_FUNCHAIN) { bool recovered; if(!check_receiver_cap(opt, ast, &recovered)) return false; if(!check_nonsendable_recover(opt, ast)) return false; ast_t* f_type = ast_type(lhs); token_id f_cap = ast_id(ast_child(f_type)); ast_t* c_type = chain_type(r_type, f_cap, recovered); ast_settype(ast, c_type); } else { ast_settype(ast, r_type); } return true; }
static bool check_receiver_cap(pass_opt_t* opt, ast_t* ast, bool* recovered) { AST_GET_CHILDREN(ast, positional, namedargs, question, lhs); ast_t* type = ast_type(lhs); if(is_typecheck_error(type)) return false; AST_GET_CHILDREN(type, cap, typeparams, params, result); // Receiver type, alias of receiver type, and target type. ast_t* r_type = method_receiver_type(lhs); if(is_typecheck_error(r_type)) return false; ast_t* t_type = set_cap_and_ephemeral(r_type, ast_id(cap), TK_NONE); ast_t* a_type; // If we can recover the receiver, we don't alias it here. bool can_recover = auto_recover_call(lhs, r_type, positional, result); bool cap_recover = false; switch(ast_id(cap)) { case TK_ISO: case TK_TRN: case TK_VAL: case TK_TAG: break; case TK_REF: case TK_BOX: cap_recover = true; break; default: pony_assert(0); } if(can_recover && cap_recover) { a_type = r_type; if(recovered != NULL) *recovered = true; } else { a_type = alias(r_type); if(recovered != NULL) *recovered = false; } errorframe_t info = NULL; bool ok = is_subtype(a_type, t_type, &info, opt); if(!ok) { errorframe_t frame = NULL; ast_error_frame(&frame, ast, "receiver type is not a subtype of target type"); ast_error_frame(&frame, ast_child(lhs), "receiver type: %s", ast_print_type(a_type)); ast_error_frame(&frame, cap, "target type: %s", ast_print_type(t_type)); errorframe_append(&frame, &info); if(ast_checkflag(ast_type(method_receiver(lhs)), AST_FLAG_INCOMPLETE)) ast_error_frame(&frame, method_receiver(lhs), "this might be possible if all fields were already defined"); if(!can_recover && cap_recover && is_subtype(r_type, t_type, NULL, opt)) { ast_error_frame(&frame, ast, "this would be possible if the arguments and return value " "were all sendable"); } errorframe_report(&frame, opt->check.errors); } if(a_type != r_type) ast_free_unattached(a_type); ast_free_unattached(r_type); ast_free_unattached(t_type); return ok; }