| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include "rust-tyty-subst.h" |
| 20 |
|
| 21 |
#include "rust-system.h" |
| 22 |
#include "rust-tyty.h" |
| 23 |
#include "rust-hir-type-check.h" |
| 24 |
#include "rust-substitution-mapper.h" |
| 25 |
#include "rust-hir-type-check-type.h" |
| 26 |
#include "rust-type-util.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace TyTy { |
| 30 |
|
| 31 |
SubstitutionParamMapping::SubstitutionParamMapping (HIR::TypeParam &generic, |
| 32 |
ParamType *param) |
| 33 |
: generic (generic), param (param) |
| 34 |
{} |
| 35 |
|
| 36 |
SubstitutionParamMapping::SubstitutionParamMapping ( |
| 37 |
const SubstitutionParamMapping &other) |
| 38 |
: generic (other.generic), param (other.param) |
| 39 |
{} |
| 40 |
|
| 41 |
std::string |
| 42 |
SubstitutionParamMapping::as_string () const |
| 43 |
{ |
| 44 |
if (param == nullptr) |
| 45 |
return "nullptr"; |
| 46 |
|
| 47 |
return param->get_name (); |
| 48 |
} |
| 49 |
|
| 50 |
SubstitutionParamMapping |
| 51 |
SubstitutionParamMapping::clone () const |
| 52 |
{ |
| 53 |
return SubstitutionParamMapping (generic, |
| 54 |
static_cast<ParamType *> (param->clone ())); |
| 55 |
} |
| 56 |
|
| 57 |
ParamType * |
| 58 |
SubstitutionParamMapping::get_param_ty () |
| 59 |
{ |
| 60 |
return param; |
| 61 |
} |
| 62 |
|
| 63 |
const ParamType * |
| 64 |
SubstitutionParamMapping::get_param_ty () const |
| 65 |
{ |
| 66 |
return param; |
| 67 |
} |
| 68 |
|
| 69 |
HIR::TypeParam & |
| 70 |
SubstitutionParamMapping::get_generic_param () |
| 71 |
{ |
| 72 |
return generic; |
| 73 |
} |
| 74 |
|
| 75 |
bool |
| 76 |
SubstitutionParamMapping::needs_substitution () const |
| 77 |
{ |
| 78 |
return !(get_param_ty ()->is_concrete ()); |
| 79 |
} |
| 80 |
|
| 81 |
location_t |
| 82 |
SubstitutionParamMapping::get_param_locus () const |
| 83 |
{ |
| 84 |
return generic.get_locus (); |
| 85 |
} |
| 86 |
|
| 87 |
bool |
| 88 |
SubstitutionParamMapping::param_has_default_ty () const |
| 89 |
{ |
| 90 |
return generic.has_type (); |
| 91 |
} |
| 92 |
|
| 93 |
BaseType * |
| 94 |
SubstitutionParamMapping::get_default_ty () const |
| 95 |
{ |
| 96 |
TyVar var (generic.get_type_mappings ().get_hirid ()); |
| 97 |
return var.get_tyty (); |
| 98 |
} |
| 99 |
|
| 100 |
bool |
| 101 |
SubstitutionParamMapping::need_substitution () const |
| 102 |
{ |
| 103 |
if (!param->can_resolve ()) |
| 104 |
return true; |
| 105 |
|
| 106 |
auto resolved = param->resolve (); |
| 107 |
return !resolved->is_concrete (); |
| 108 |
} |
| 109 |
|
| 110 |
bool |
| 111 |
SubstitutionParamMapping::fill_param_ty ( |
| 112 |
SubstitutionArgumentMappings &subst_mappings, location_t locus) |
| 113 |
{ |
| 114 |
SubstitutionArg arg = SubstitutionArg::error (); |
| 115 |
bool ok = subst_mappings.get_argument_for_symbol (get_param_ty (), &arg); |
| 116 |
if (!ok) |
| 117 |
return true; |
| 118 |
|
| 119 |
TyTy::BaseType &type = *arg.get_tyty (); |
| 120 |
if (type.get_kind () == TyTy::TypeKind::INFER) |
| 121 |
{ |
| 122 |
type.inherit_bounds (*param); |
| 123 |
} |
| 124 |
|
| 125 |
if (type.get_kind () == TypeKind::PARAM) |
| 126 |
{ |
| 127 |
// delete param; |
| 128 |
param = static_cast<ParamType *> (type.clone ()); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
// check the substitution is compatible with bounds |
| 133 |
rust_debug_loc (locus, |
| 134 |
"fill_param_ty bounds_compatible: param %s type %s", |
| 135 |
param->get_name ().c_str (), type.get_name ().c_str ()); |
| 136 |
|
| 137 |
if (!param->is_implicit_self_trait ()) |
| 138 |
{ |
| 139 |
if (!param->bounds_compatible (type, locus, true)) |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
// recursively pass this down to all HRTB's |
| 144 |
for (auto &bound : param->get_specified_bounds ()) |
| 145 |
bound.handle_substitions (subst_mappings); |
| 146 |
|
| 147 |
param->set_ty_ref (type.get_ref ()); |
| 148 |
subst_mappings.on_param_subst (*param, arg); |
| 149 |
} |
| 150 |
|
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
SubstitutionParamMapping::override_context () |
| 156 |
{ |
| 157 |
if (!param->can_resolve ()) |
| 158 |
return; |
| 159 |
|
| 160 |
auto &mappings = Analysis::Mappings::get (); |
| 161 |
auto context = Resolver::TypeCheckContext::get (); |
| 162 |
|
| 163 |
context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (), |
| 164 |
UNKNOWN_NODEID, |
| 165 |
param->get_ref (), |
| 166 |
UNKNOWN_LOCAL_DEFID), |
| 167 |
param->resolve ()); |
| 168 |
} |
| 169 |
|
| 170 |
SubstitutionArg::SubstitutionArg (const SubstitutionParamMapping *param, |
| 171 |
BaseType *argument) |
| 172 |
: param (param), argument (argument) |
| 173 |
{ |
| 174 |
if (param != nullptr) |
| 175 |
original_param = param->get_param_ty (); |
| 176 |
} |
| 177 |
|
| 178 |
SubstitutionArg::SubstitutionArg (const SubstitutionArg &other) |
| 179 |
: param (other.param), original_param (other.original_param), |
| 180 |
argument (other.argument) |
| 181 |
{} |
| 182 |
|
| 183 |
SubstitutionArg & |
| 184 |
SubstitutionArg::operator= (const SubstitutionArg &other) |
| 185 |
{ |
| 186 |
param = other.param; |
| 187 |
argument = other.argument; |
| 188 |
original_param = other.original_param; |
| 189 |
|
| 190 |
return *this; |
| 191 |
} |
| 192 |
|
| 193 |
BaseType * |
| 194 |
SubstitutionArg::get_tyty () |
| 195 |
{ |
| 196 |
return argument; |
| 197 |
} |
| 198 |
|
| 199 |
const BaseType * |
| 200 |
SubstitutionArg::get_tyty () const |
| 201 |
{ |
| 202 |
return argument; |
| 203 |
} |
| 204 |
|
| 205 |
const SubstitutionParamMapping * |
| 206 |
SubstitutionArg::get_param_mapping () const |
| 207 |
{ |
| 208 |
return param; |
| 209 |
} |
| 210 |
|
| 211 |
const ParamType * |
| 212 |
SubstitutionArg::get_param_ty () const |
| 213 |
{ |
| 214 |
return original_param; |
| 215 |
} |
| 216 |
|
| 217 |
SubstitutionArg |
| 218 |
SubstitutionArg::error () |
| 219 |
{ |
| 220 |
return SubstitutionArg (nullptr, nullptr); |
| 221 |
} |
| 222 |
|
| 223 |
bool |
| 224 |
SubstitutionArg::is_error () const |
| 225 |
{ |
| 226 |
return param == nullptr || argument == nullptr; |
| 227 |
} |
| 228 |
|
| 229 |
bool |
| 230 |
SubstitutionArg::is_conrete () const |
| 231 |
{ |
| 232 |
if (argument == nullptr) |
| 233 |
return false; |
| 234 |
|
| 235 |
if (argument->get_kind () == TyTy::TypeKind::PARAM) |
| 236 |
return false; |
| 237 |
|
| 238 |
return argument->is_concrete (); |
| 239 |
} |
| 240 |
|
| 241 |
std::string |
| 242 |
SubstitutionArg::as_string () const |
| 243 |
{ |
| 244 |
return original_param->as_string () |
| 245 |
+ (argument != nullptr ? ":" + argument->as_string () : ""); |
| 246 |
} |
| 247 |
|
| 248 |
const RegionParamList & |
| 249 |
SubstitutionArgumentMappings::get_regions () const |
| 250 |
{ |
| 251 |
return regions; |
| 252 |
} |
| 253 |
|
| 254 |
RegionParamList & |
| 255 |
SubstitutionArgumentMappings::get_mut_regions () |
| 256 |
{ |
| 257 |
return regions; |
| 258 |
} |
| 259 |
|
| 260 |
// SubstitutionArgumentMappings |
| 261 |
|
| 262 |
SubstitutionArgumentMappings::SubstitutionArgumentMappings ( |
| 263 |
std::vector<SubstitutionArg> mappings, |
| 264 |
std::map<std::string, BaseType *> binding_args, RegionParamList regions, |
| 265 |
location_t locus, ParamSubstCb param_subst_cb, bool trait_item_flag, |
| 266 |
bool error_flag) |
| 267 |
: mappings (std::move (mappings)), binding_args (binding_args), |
| 268 |
regions (regions), locus (locus), param_subst_cb (param_subst_cb), |
| 269 |
trait_item_flag (trait_item_flag), error_flag (error_flag) |
| 270 |
{} |
| 271 |
|
| 272 |
SubstitutionArgumentMappings::SubstitutionArgumentMappings ( |
| 273 |
const SubstitutionArgumentMappings &other) |
| 274 |
: mappings (other.mappings), binding_args (other.binding_args), |
| 275 |
regions (other.regions), locus (other.locus), param_subst_cb (nullptr), |
| 276 |
trait_item_flag (other.trait_item_flag), error_flag (other.error_flag) |
| 277 |
{} |
| 278 |
|
| 279 |
SubstitutionArgumentMappings & |
| 280 |
SubstitutionArgumentMappings::operator= ( |
| 281 |
const SubstitutionArgumentMappings &other) |
| 282 |
{ |
| 283 |
mappings = other.mappings; |
| 284 |
binding_args = other.binding_args; |
| 285 |
regions = other.regions; |
| 286 |
locus = other.locus; |
| 287 |
param_subst_cb = nullptr; |
| 288 |
trait_item_flag = other.trait_item_flag; |
| 289 |
error_flag = other.error_flag; |
| 290 |
|
| 291 |
return *this; |
| 292 |
} |
| 293 |
|
| 294 |
SubstitutionArgumentMappings |
| 295 |
SubstitutionArgumentMappings::error () |
| 296 |
{ |
| 297 |
return SubstitutionArgumentMappings ({}, {}, 0, UNDEF_LOCATION, nullptr, |
| 298 |
false, true); |
| 299 |
} |
| 300 |
|
| 301 |
SubstitutionArgumentMappings |
| 302 |
SubstitutionArgumentMappings::empty (size_t num_regions) |
| 303 |
{ |
| 304 |
return SubstitutionArgumentMappings ({}, {}, num_regions, UNDEF_LOCATION, |
| 305 |
nullptr, false, false); |
| 306 |
} |
| 307 |
|
| 308 |
bool |
| 309 |
SubstitutionArgumentMappings::is_error () const |
| 310 |
{ |
| 311 |
return error_flag; |
| 312 |
} |
| 313 |
|
| 314 |
bool |
| 315 |
SubstitutionArgumentMappings::get_argument_for_symbol ( |
| 316 |
const ParamType *param_to_find, SubstitutionArg *argument) const |
| 317 |
{ |
| 318 |
for (const auto &mapping : mappings) |
| 319 |
{ |
| 320 |
const ParamType *p = mapping.get_param_ty (); |
| 321 |
if (p->get_symbol () == param_to_find->get_symbol ()) |
| 322 |
{ |
| 323 |
*argument = mapping; |
| 324 |
return true; |
| 325 |
} |
| 326 |
} |
| 327 |
return false; |
| 328 |
} |
| 329 |
tl::optional<size_t> |
| 330 |
SubstitutionArgumentMappings::find_symbol (const ParamType ¶m_to_find) const |
| 331 |
{ |
| 332 |
auto it = std::find_if (mappings.begin (), mappings.end (), |
| 333 |
[param_to_find] (const SubstitutionArg &arg) { |
| 334 |
return arg.get_param_ty ()->get_symbol () |
| 335 |
== param_to_find.get_symbol (); |
| 336 |
}); |
| 337 |
if (it == mappings.end ()) |
| 338 |
return tl::nullopt; |
| 339 |
return std::distance (mappings.begin (), it); |
| 340 |
} |
| 341 |
|
| 342 |
bool |
| 343 |
SubstitutionArgumentMappings::get_argument_at (size_t index, |
| 344 |
SubstitutionArg *argument) |
| 345 |
{ |
| 346 |
if (index > mappings.size ()) |
| 347 |
return false; |
| 348 |
|
| 349 |
*argument = mappings.at (index); |
| 350 |
return true; |
| 351 |
} |
| 352 |
|
| 353 |
bool |
| 354 |
SubstitutionArgumentMappings::is_concrete () const |
| 355 |
{ |
| 356 |
for (auto &mapping : mappings) |
| 357 |
{ |
| 358 |
if (!mapping.is_conrete ()) |
| 359 |
return false; |
| 360 |
} |
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
location_t |
| 365 |
SubstitutionArgumentMappings::get_locus () const |
| 366 |
{ |
| 367 |
return locus; |
| 368 |
} |
| 369 |
|
| 370 |
size_t |
| 371 |
SubstitutionArgumentMappings::size () const |
| 372 |
{ |
| 373 |
return mappings.size (); |
| 374 |
} |
| 375 |
|
| 376 |
bool |
| 377 |
SubstitutionArgumentMappings::is_empty () const |
| 378 |
{ |
| 379 |
return size () == 0; |
| 380 |
} |
| 381 |
|
| 382 |
std::vector<SubstitutionArg> & |
| 383 |
SubstitutionArgumentMappings::get_mappings () |
| 384 |
{ |
| 385 |
return mappings; |
| 386 |
} |
| 387 |
|
| 388 |
const std::vector<SubstitutionArg> & |
| 389 |
SubstitutionArgumentMappings::get_mappings () const |
| 390 |
{ |
| 391 |
return mappings; |
| 392 |
} |
| 393 |
|
| 394 |
std::map<std::string, BaseType *> & |
| 395 |
SubstitutionArgumentMappings::get_binding_args () |
| 396 |
{ |
| 397 |
return binding_args; |
| 398 |
} |
| 399 |
|
| 400 |
const std::map<std::string, BaseType *> & |
| 401 |
SubstitutionArgumentMappings::get_binding_args () const |
| 402 |
{ |
| 403 |
return binding_args; |
| 404 |
} |
| 405 |
|
| 406 |
std::string |
| 407 |
SubstitutionArgumentMappings::as_string () const |
| 408 |
{ |
| 409 |
std::string buffer; |
| 410 |
for (auto &mapping : mappings) |
| 411 |
{ |
| 412 |
buffer += mapping.as_string () + ", "; |
| 413 |
} |
| 414 |
return "<" + buffer + ">"; |
| 415 |
} |
| 416 |
|
| 417 |
void |
| 418 |
SubstitutionArgumentMappings::on_param_subst (const ParamType &p, |
| 419 |
const SubstitutionArg &a) const |
| 420 |
{ |
| 421 |
if (param_subst_cb == nullptr) |
| 422 |
return; |
| 423 |
|
| 424 |
param_subst_cb (p, a); |
| 425 |
} |
| 426 |
|
| 427 |
ParamSubstCb |
| 428 |
SubstitutionArgumentMappings::get_subst_cb () const |
| 429 |
{ |
| 430 |
return param_subst_cb; |
| 431 |
} |
| 432 |
|
| 433 |
bool |
| 434 |
SubstitutionArgumentMappings::trait_item_mode () const |
| 435 |
{ |
| 436 |
return trait_item_flag; |
| 437 |
} |
| 438 |
|
| 439 |
// SubstitutionRef |
| 440 |
|
| 441 |
SubstitutionRef::SubstitutionRef ( |
| 442 |
std::vector<SubstitutionParamMapping> substitutions, |
| 443 |
SubstitutionArgumentMappings arguments, RegionConstraints region_constraints) |
| 444 |
: substitutions (substitutions), used_arguments (arguments), |
| 445 |
region_constraints (region_constraints) |
| 446 |
{} |
| 447 |
|
| 448 |
bool |
| 449 |
SubstitutionRef::has_substitutions () const |
| 450 |
{ |
| 451 |
return substitutions.size () > 0; |
| 452 |
} |
| 453 |
|
| 454 |
std::string |
| 455 |
SubstitutionRef::subst_as_string () const |
| 456 |
{ |
| 457 |
std::string buffer; |
| 458 |
for (size_t i = 0; i < substitutions.size (); i++) |
| 459 |
{ |
| 460 |
const SubstitutionParamMapping &sub = substitutions.at (i); |
| 461 |
buffer += sub.as_string (); |
| 462 |
|
| 463 |
if ((i + 1) < substitutions.size ()) |
| 464 |
buffer += ", "; |
| 465 |
} |
| 466 |
|
| 467 |
return buffer.empty () ? "" : "<" + buffer + ">"; |
| 468 |
} |
| 469 |
|
| 470 |
bool |
| 471 |
SubstitutionRef::supports_associated_bindings () const |
| 472 |
{ |
| 473 |
return get_num_associated_bindings () > 0; |
| 474 |
} |
| 475 |
|
| 476 |
size_t |
| 477 |
SubstitutionRef::get_num_associated_bindings () const |
| 478 |
{ |
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
TypeBoundPredicateItem |
| 483 |
SubstitutionRef::lookup_associated_type (const std::string &search) |
| 484 |
{ |
| 485 |
return TypeBoundPredicateItem::error (); |
| 486 |
} |
| 487 |
|
| 488 |
size_t |
| 489 |
SubstitutionRef::get_num_substitutions () const |
| 490 |
{ |
| 491 |
return substitutions.size (); |
| 492 |
} |
| 493 |
size_t |
| 494 |
SubstitutionRef::get_num_lifetime_params () const |
| 495 |
{ |
| 496 |
return used_arguments.get_regions ().size (); |
| 497 |
} |
| 498 |
size_t |
| 499 |
SubstitutionRef::get_num_type_params () const |
| 500 |
{ |
| 501 |
return get_num_substitutions (); |
| 502 |
} |
| 503 |
|
| 504 |
std::vector<SubstitutionParamMapping> & |
| 505 |
SubstitutionRef::get_substs () |
| 506 |
{ |
| 507 |
return substitutions; |
| 508 |
} |
| 509 |
|
| 510 |
const std::vector<SubstitutionParamMapping> & |
| 511 |
SubstitutionRef::get_substs () const |
| 512 |
{ |
| 513 |
return substitutions; |
| 514 |
} |
| 515 |
|
| 516 |
std::vector<SubstitutionParamMapping> |
| 517 |
SubstitutionRef::clone_substs () const |
| 518 |
{ |
| 519 |
std::vector<SubstitutionParamMapping> clone; |
| 520 |
|
| 521 |
for (auto &sub : substitutions) |
| 522 |
clone.push_back (sub.clone ()); |
| 523 |
|
| 524 |
return clone; |
| 525 |
} |
| 526 |
|
| 527 |
void |
| 528 |
SubstitutionRef::override_context () |
| 529 |
{ |
| 530 |
for (auto &sub : substitutions) |
| 531 |
{ |
| 532 |
sub.override_context (); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
bool |
| 537 |
SubstitutionRef::needs_substitution () const |
| 538 |
{ |
| 539 |
return std::any_of (substitutions.begin (), substitutions.end (), |
| 540 |
std::mem_fn ( |
| 541 |
&SubstitutionParamMapping::needs_substitution)); |
| 542 |
} |
| 543 |
|
| 544 |
bool |
| 545 |
SubstitutionRef::was_substituted () const |
| 546 |
{ |
| 547 |
return !needs_substitution (); |
| 548 |
} |
| 549 |
|
| 550 |
SubstitutionArgumentMappings & |
| 551 |
SubstitutionRef::get_substitution_arguments () |
| 552 |
{ |
| 553 |
return used_arguments; |
| 554 |
} |
| 555 |
|
| 556 |
const SubstitutionArgumentMappings & |
| 557 |
SubstitutionRef::get_substitution_arguments () const |
| 558 |
{ |
| 559 |
return used_arguments; |
| 560 |
} |
| 561 |
|
| 562 |
size_t |
| 563 |
SubstitutionRef::num_required_substitutions () const |
| 564 |
{ |
| 565 |
size_t n = 0; |
| 566 |
for (auto &p : substitutions) |
| 567 |
{ |
| 568 |
if (p.needs_substitution ()) |
| 569 |
n++; |
| 570 |
} |
| 571 |
return n; |
| 572 |
} |
| 573 |
|
| 574 |
size_t |
| 575 |
SubstitutionRef::min_required_substitutions () const |
| 576 |
{ |
| 577 |
size_t n = 0; |
| 578 |
for (auto &p : substitutions) |
| 579 |
{ |
| 580 |
if (p.needs_substitution () && !p.param_has_default_ty ()) |
| 581 |
n++; |
| 582 |
} |
| 583 |
return n; |
| 584 |
} |
| 585 |
|
| 586 |
const SubstitutionArgumentMappings & |
| 587 |
SubstitutionRef::get_used_arguments () const |
| 588 |
{ |
| 589 |
return used_arguments; |
| 590 |
} |
| 591 |
|
| 592 |
tl::optional<SubstitutionArg> |
| 593 |
SubstitutionRef::get_arg_at (size_t i) const |
| 594 |
{ |
| 595 |
auto param_ty = get_substs ().at (i).get_param_ty (); |
| 596 |
SubstitutionArg arg = SubstitutionArg::error (); |
| 597 |
get_used_arguments ().get_argument_for_symbol (param_ty, &arg); |
| 598 |
if (arg.is_error ()) |
| 599 |
return tl::nullopt; |
| 600 |
return arg; |
| 601 |
} |
| 602 |
|
| 603 |
const RegionConstraints & |
| 604 |
SubstitutionRef::get_region_constraints () const |
| 605 |
{ |
| 606 |
return region_constraints; |
| 607 |
} |
| 608 |
|
| 609 |
SubstitutionArgumentMappings |
| 610 |
SubstitutionRef::get_mappings_from_generic_args ( |
| 611 |
HIR::GenericArgs &args, const std::vector<Region> ®ions) |
| 612 |
{ |
| 613 |
std::map<std::string, BaseType *> binding_arguments; |
| 614 |
if (args.get_binding_args ().size () > 0) |
| 615 |
{ |
| 616 |
if (supports_associated_bindings ()) |
| 617 |
{ |
| 618 |
if (args.get_binding_args ().size () > get_num_associated_bindings ()) |
| 619 |
{ |
| 620 |
rich_location r (line_table, args.get_locus ()); |
| 621 |
|
| 622 |
rust_error_at (r, |
| 623 |
"generic item takes at most %lu type binding " |
| 624 |
"arguments but %lu were supplied", |
| 625 |
(unsigned long) get_num_associated_bindings (), |
| 626 |
(unsigned long) args.get_binding_args ().size ()); |
| 627 |
return SubstitutionArgumentMappings::error (); |
| 628 |
} |
| 629 |
|
| 630 |
for (auto &binding : args.get_binding_args ()) |
| 631 |
{ |
| 632 |
BaseType *resolved |
| 633 |
= Resolver::TypeCheckType::Resolve (binding.get_type ()); |
| 634 |
if (resolved == nullptr |
| 635 |
|| resolved->get_kind () == TyTy::TypeKind::ERROR) |
| 636 |
{ |
| 637 |
return SubstitutionArgumentMappings::error (); |
| 638 |
} |
| 639 |
|
| 640 |
// resolve to relevant binding |
| 641 |
auto binding_item = lookup_associated_type ( |
| 642 |
binding.get_identifier ().as_string ()); |
| 643 |
if (binding_item.is_error ()) |
| 644 |
{ |
| 645 |
rust_error_at ( |
| 646 |
binding.get_locus (), "unknown associated type binding: %s", |
| 647 |
binding.get_identifier ().as_string ().c_str ()); |
| 648 |
return SubstitutionArgumentMappings::error (); |
| 649 |
} |
| 650 |
|
| 651 |
binding_arguments[binding.get_identifier ().as_string ()] |
| 652 |
= resolved; |
| 653 |
} |
| 654 |
} |
| 655 |
else |
| 656 |
{ |
| 657 |
rich_location r (line_table, args.get_locus ()); |
| 658 |
for (auto &binding : args.get_binding_args ()) |
| 659 |
r.add_range (binding.get_locus ()); |
| 660 |
|
| 661 |
rust_error_at (r, ErrorCode::E0229, |
| 662 |
"associated type bindings are not allowed here"); |
| 663 |
return SubstitutionArgumentMappings::error (); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
// for inherited arguments |
| 668 |
size_t offs = used_arguments.size (); |
| 669 |
if (args.get_type_args ().size () + offs > substitutions.size ()) |
| 670 |
{ |
| 671 |
rich_location r (line_table, args.get_locus ()); |
| 672 |
if (!substitutions.empty ()) |
| 673 |
r.add_range (substitutions.front ().get_param_locus ()); |
| 674 |
|
| 675 |
rust_error_at ( |
| 676 |
r, |
| 677 |
"generic item takes at most %lu type arguments but %lu were supplied", |
| 678 |
(unsigned long) substitutions.size (), |
| 679 |
(unsigned long) args.get_type_args ().size ()); |
| 680 |
return SubstitutionArgumentMappings::error (); |
| 681 |
} |
| 682 |
|
| 683 |
if (args.get_type_args ().size () + offs < min_required_substitutions ()) |
| 684 |
{ |
| 685 |
rich_location r (line_table, args.get_locus ()); |
| 686 |
r.add_range (substitutions.front ().get_param_locus ()); |
| 687 |
|
| 688 |
rust_error_at ( |
| 689 |
r, ErrorCode::E0107, |
| 690 |
"generic item takes at least %lu type arguments but %lu were supplied", |
| 691 |
(unsigned long) (min_required_substitutions () - offs), |
| 692 |
(unsigned long) args.get_type_args ().size ()); |
| 693 |
return SubstitutionArgumentMappings::error (); |
| 694 |
} |
| 695 |
|
| 696 |
std::vector<SubstitutionArg> mappings = used_arguments.get_mappings (); |
| 697 |
for (auto &arg : args.get_type_args ()) |
| 698 |
{ |
| 699 |
BaseType *resolved = Resolver::TypeCheckType::Resolve (*arg); |
| 700 |
if (resolved == nullptr || resolved->get_kind () == TyTy::TypeKind::ERROR) |
| 701 |
{ |
| 702 |
return SubstitutionArgumentMappings::error (); |
| 703 |
} |
| 704 |
|
| 705 |
SubstitutionArg subst_arg (&substitutions.at (offs), resolved); |
| 706 |
offs++; |
| 707 |
mappings.push_back (std::move (subst_arg)); |
| 708 |
} |
| 709 |
|
| 710 |
// we must need to fill out defaults |
| 711 |
size_t left_over |
| 712 |
= num_required_substitutions () - min_required_substitutions (); |
| 713 |
if (left_over > 0) |
| 714 |
{ |
| 715 |
for (size_t offs = mappings.size (); offs < substitutions.size (); offs++) |
| 716 |
{ |
| 717 |
SubstitutionParamMapping ¶m = substitutions.at (offs); |
| 718 |
rust_assert (param.param_has_default_ty ()); |
| 719 |
|
| 720 |
BaseType *resolved = param.get_default_ty (); |
| 721 |
if (resolved->get_kind () == TypeKind::ERROR) |
| 722 |
return SubstitutionArgumentMappings::error (); |
| 723 |
|
| 724 |
// this resolved default might already contain default parameters |
| 725 |
if (!resolved->is_concrete ()) |
| 726 |
{ |
| 727 |
SubstitutionArgumentMappings intermediate ( |
| 728 |
mappings, binding_arguments, |
| 729 |
{used_arguments.get_regions ().size ()}, args.get_locus ()); |
| 730 |
resolved = Resolver::SubstMapperInternal::Resolve (resolved, |
| 731 |
intermediate); |
| 732 |
|
| 733 |
if (resolved->get_kind () == TypeKind::ERROR) |
| 734 |
return SubstitutionArgumentMappings::error (); |
| 735 |
} |
| 736 |
|
| 737 |
SubstitutionArg subst_arg (¶m, resolved); |
| 738 |
mappings.push_back (std::move (subst_arg)); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
return {mappings, binding_arguments, |
| 743 |
RegionParamList::from_subst (used_arguments.get_regions ().size (), |
| 744 |
regions), |
| 745 |
args.get_locus ()}; |
| 746 |
} |
| 747 |
|
| 748 |
BaseType * |
| 749 |
SubstitutionRef::infer_substitions (location_t locus) |
| 750 |
{ |
| 751 |
std::vector<SubstitutionArg> args; |
| 752 |
std::map<std::string, BaseType *> argument_mappings; |
| 753 |
for (auto &p : get_substs ()) |
| 754 |
{ |
| 755 |
if (p.needs_substitution ()) |
| 756 |
{ |
| 757 |
const std::string &symbol = p.get_param_ty ()->get_symbol (); |
| 758 |
auto it = argument_mappings.find (symbol); |
| 759 |
bool have_mapping = it != argument_mappings.end (); |
| 760 |
|
| 761 |
if (have_mapping) |
| 762 |
{ |
| 763 |
args.push_back (SubstitutionArg (&p, it->second)); |
| 764 |
} |
| 765 |
else |
| 766 |
{ |
| 767 |
TyVar infer_var = TyVar::get_implicit_infer_var (locus); |
| 768 |
args.push_back (SubstitutionArg (&p, infer_var.get_tyty ())); |
| 769 |
argument_mappings[symbol] = infer_var.get_tyty (); |
| 770 |
} |
| 771 |
} |
| 772 |
else |
| 773 |
{ |
| 774 |
args.push_back (SubstitutionArg (&p, p.get_param_ty ()->resolve ())); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
// FIXME do we need to add inference variables to all the possible bindings? |
| 779 |
// it might just lead to inference variable hell not 100% sure if rustc does |
| 780 |
// this i think the language might needs this to be explicitly set |
| 781 |
|
| 782 |
SubstitutionArgumentMappings infer_arguments (std::move (args), |
| 783 |
{} /* binding_arguments */, |
| 784 |
used_arguments.get_regions (), |
| 785 |
locus); |
| 786 |
return handle_substitions (infer_arguments); |
| 787 |
} |
| 788 |
|
| 789 |
SubstitutionArgumentMappings |
| 790 |
SubstitutionRef::adjust_mappings_for_this ( |
| 791 |
SubstitutionArgumentMappings &mappings, bool trait_mode) |
| 792 |
{ |
| 793 |
std::vector<SubstitutionArg> resolved_mappings; |
| 794 |
for (size_t i = 0; i < substitutions.size (); i++) |
| 795 |
{ |
| 796 |
auto &subst = substitutions.at (i); |
| 797 |
|
| 798 |
SubstitutionArg arg = SubstitutionArg::error (); |
| 799 |
if (mappings.size () == substitutions.size ()) |
| 800 |
{ |
| 801 |
mappings.get_argument_at (i, &arg); |
| 802 |
} |
| 803 |
else |
| 804 |
{ |
| 805 |
if (subst.needs_substitution ()) |
| 806 |
{ |
| 807 |
// get from passed in mappings |
| 808 |
mappings.get_argument_for_symbol (subst.get_param_ty (), &arg); |
| 809 |
} |
| 810 |
else |
| 811 |
{ |
| 812 |
// we should already have this somewhere |
| 813 |
used_arguments.get_argument_for_symbol (subst.get_param_ty (), |
| 814 |
&arg); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
bool ok = !arg.is_error (); |
| 819 |
if (ok || (trait_mode && i == 0)) |
| 820 |
{ |
| 821 |
SubstitutionArg adjusted (&subst, arg.get_tyty ()); |
| 822 |
resolved_mappings.push_back (std::move (adjusted)); |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
if (resolved_mappings.empty ()) |
| 827 |
return SubstitutionArgumentMappings::error (); |
| 828 |
|
| 829 |
return SubstitutionArgumentMappings (resolved_mappings, |
| 830 |
mappings.get_binding_args (), |
| 831 |
mappings.get_regions (), |
| 832 |
mappings.get_locus (), |
| 833 |
mappings.get_subst_cb (), |
| 834 |
mappings.trait_item_mode ()); |
| 835 |
} |
| 836 |
|
| 837 |
bool |
| 838 |
SubstitutionRef::are_mappings_bound (SubstitutionArgumentMappings &mappings) |
| 839 |
{ |
| 840 |
std::vector<SubstitutionArg> resolved_mappings; |
| 841 |
for (size_t i = 0; i < substitutions.size (); i++) |
| 842 |
{ |
| 843 |
auto &subst = substitutions.at (i); |
| 844 |
|
| 845 |
SubstitutionArg arg = SubstitutionArg::error (); |
| 846 |
if (mappings.size () == substitutions.size ()) |
| 847 |
{ |
| 848 |
mappings.get_argument_at (i, &arg); |
| 849 |
} |
| 850 |
else |
| 851 |
{ |
| 852 |
if (subst.needs_substitution ()) |
| 853 |
{ |
| 854 |
// get from passed in mappings |
| 855 |
mappings.get_argument_for_symbol (subst.get_param_ty (), &arg); |
| 856 |
} |
| 857 |
else |
| 858 |
{ |
| 859 |
// we should already have this somewhere |
| 860 |
used_arguments.get_argument_for_symbol (subst.get_param_ty (), |
| 861 |
&arg); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
bool ok = !arg.is_error (); |
| 866 |
if (ok) |
| 867 |
{ |
| 868 |
SubstitutionArg adjusted (&subst, arg.get_tyty ()); |
| 869 |
resolved_mappings.push_back (std::move (adjusted)); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
return !resolved_mappings.empty (); |
| 874 |
} |
| 875 |
|
| 876 |
// this function assumes that the mappings being passed are for the same type as |
| 877 |
// this new substitution reference so ordering matters here |
| 878 |
SubstitutionArgumentMappings |
| 879 |
SubstitutionRef::solve_mappings_from_receiver_for_self ( |
| 880 |
SubstitutionArgumentMappings &mappings) const |
| 881 |
{ |
| 882 |
std::vector<SubstitutionArg> resolved_mappings; |
| 883 |
|
| 884 |
rust_assert (mappings.size () == get_num_substitutions ()); |
| 885 |
for (size_t i = 0; i < get_num_substitutions (); i++) |
| 886 |
{ |
| 887 |
const SubstitutionParamMapping ¶m_mapping = substitutions.at (i); |
| 888 |
SubstitutionArg &arg = mappings.get_mappings ().at (i); |
| 889 |
|
| 890 |
if (param_mapping.needs_substitution ()) |
| 891 |
{ |
| 892 |
SubstitutionArg adjusted (¶m_mapping, arg.get_tyty ()); |
| 893 |
resolved_mappings.push_back (std::move (adjusted)); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
return SubstitutionArgumentMappings (resolved_mappings, |
| 898 |
mappings.get_binding_args (), |
| 899 |
mappings.get_regions (), |
| 900 |
mappings.get_locus ()); |
| 901 |
} |
| 902 |
|
| 903 |
void |
| 904 |
SubstitutionRef::prepare_higher_ranked_bounds () |
| 905 |
{ |
| 906 |
for (const auto &subst : get_substs ()) |
| 907 |
{ |
| 908 |
const TyTy::ParamType *pty = subst.get_param_ty (); |
| 909 |
for (const auto &bound : pty->get_specified_bounds ()) |
| 910 |
{ |
| 911 |
const auto ref = bound.get (); |
| 912 |
ref->clear_associated_type_projections (); |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
bool |
| 918 |
SubstitutionRef::monomorphize () |
| 919 |
{ |
| 920 |
for (const auto &subst : get_substs ()) |
| 921 |
{ |
| 922 |
const TyTy::ParamType *pty = subst.get_param_ty (); |
| 923 |
|
| 924 |
if (!pty->can_resolve ()) |
| 925 |
continue; |
| 926 |
|
| 927 |
const TyTy::BaseType *binding = pty->resolve (); |
| 928 |
if (binding->get_kind () == TyTy::TypeKind::PARAM) |
| 929 |
continue; |
| 930 |
|
| 931 |
for (const auto &bound : pty->get_specified_bounds ()) |
| 932 |
{ |
| 933 |
bool ambigious = false; |
| 934 |
auto associated |
| 935 |
= Resolver::lookup_associated_impl_block (bound, binding, |
| 936 |
&ambigious); |
| 937 |
if (associated != nullptr) |
| 938 |
associated->setup_associated_types (binding, bound); |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
return true; |
| 943 |
} |
| 944 |
|
| 945 |
} // namespace TyTy |
| 946 |
} // namespace Rust |