| 1 |
// Copyright (C) 2021-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-hir-trait-resolve.h" |
| 20 |
#include "rust-hir-type-check-expr.h" |
| 21 |
#include "rust-substitution-mapper.h" |
| 22 |
#include "rust-type-util.h" |
| 23 |
#include "rust-immutable-name-resolution-context.h" |
| 24 |
|
| 25 |
// used for flag_name_resolution_2_0 |
| 26 |
#include "options.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace Resolver { |
| 30 |
|
| 31 |
TraitItemReference |
| 32 |
ResolveTraitItemToRef::Resolve ( |
| 33 |
HIR::TraitItem &item, TyTy::BaseType *self, |
| 34 |
std::vector<TyTy::SubstitutionParamMapping> substitutions) |
| 35 |
{ |
| 36 |
ResolveTraitItemToRef resolver (self, std::move (substitutions)); |
| 37 |
item.accept_vis (resolver); |
| 38 |
return std::move (resolver.resolved); |
| 39 |
} |
| 40 |
|
| 41 |
void |
| 42 |
ResolveTraitItemToRef::visit (HIR::TraitItemType &type) |
| 43 |
{ |
| 44 |
// create trait-item-ref |
| 45 |
location_t locus = type.get_locus (); |
| 46 |
bool is_optional = false; |
| 47 |
std::string identifier = type.get_name ().as_string (); |
| 48 |
|
| 49 |
resolved = TraitItemReference (identifier, is_optional, |
| 50 |
TraitItemReference::TraitItemType::TYPE, &type, |
| 51 |
self, substitutions, locus); |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
ResolveTraitItemToRef::visit (HIR::TraitItemConst &cst) |
| 56 |
{ |
| 57 |
// create trait-item-ref |
| 58 |
location_t locus = cst.get_locus (); |
| 59 |
bool is_optional = cst.has_expr (); |
| 60 |
std::string identifier = cst.get_name ().as_string (); |
| 61 |
|
| 62 |
resolved = TraitItemReference (identifier, is_optional, |
| 63 |
TraitItemReference::TraitItemType::CONST, &cst, |
| 64 |
self, substitutions, locus); |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
ResolveTraitItemToRef::visit (HIR::TraitItemFunc &fn) |
| 69 |
{ |
| 70 |
// create trait-item-ref |
| 71 |
location_t locus = fn.get_locus (); |
| 72 |
bool is_optional = fn.has_definition (); |
| 73 |
std::string identifier = fn.get_decl ().get_function_name ().as_string (); |
| 74 |
|
| 75 |
resolved = TraitItemReference (identifier, is_optional, |
| 76 |
TraitItemReference::TraitItemType::FN, &fn, |
| 77 |
self, std::move (substitutions), locus); |
| 78 |
} |
| 79 |
|
| 80 |
ResolveTraitItemToRef::ResolveTraitItemToRef ( |
| 81 |
TyTy::BaseType *self, |
| 82 |
std::vector<TyTy::SubstitutionParamMapping> &&substitutions) |
| 83 |
: TypeCheckBase (), resolved (TraitItemReference::error ()), self (self), |
| 84 |
substitutions (std::move (substitutions)) |
| 85 |
{} |
| 86 |
|
| 87 |
// TraitItemReference items |
| 88 |
|
| 89 |
TraitReference * |
| 90 |
TraitResolver::Resolve (HIR::TypePath &path) |
| 91 |
{ |
| 92 |
TraitResolver resolver; |
| 93 |
return resolver.resolve_path (path); |
| 94 |
} |
| 95 |
|
| 96 |
TraitReference * |
| 97 |
TraitResolver::Resolve (HIR::Trait &trait) |
| 98 |
{ |
| 99 |
TraitResolver resolver; |
| 100 |
return resolver.resolve_trait (&trait); |
| 101 |
} |
| 102 |
|
| 103 |
TraitReference * |
| 104 |
TraitResolver::Lookup (HIR::TypePath &path) |
| 105 |
{ |
| 106 |
TraitResolver resolver; |
| 107 |
return resolver.lookup_path (path); |
| 108 |
} |
| 109 |
|
| 110 |
HIR::Trait * |
| 111 |
TraitResolver::ResolveHirItem (const HIR::TypePath &path) |
| 112 |
{ |
| 113 |
TraitResolver resolver; |
| 114 |
|
| 115 |
HIR::Trait *lookup = nullptr; |
| 116 |
bool ok = resolver.resolve_path_to_trait (path, &lookup); |
| 117 |
return ok ? lookup : nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
TraitResolver::TraitResolver () : TypeCheckBase () {} |
| 121 |
|
| 122 |
bool |
| 123 |
TraitResolver::resolve_path_to_trait (const HIR::TypePath &path, |
| 124 |
HIR::Trait **resolved) const |
| 125 |
{ |
| 126 |
NodeId ref; |
| 127 |
bool ok; |
| 128 |
if (flag_name_resolution_2_0) |
| 129 |
{ |
| 130 |
auto &nr_ctx |
| 131 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 132 |
|
| 133 |
auto ref_opt = nr_ctx.lookup (path.get_mappings ().get_nodeid ()); |
| 134 |
|
| 135 |
if ((ok = ref_opt.has_value ())) |
| 136 |
ref = *ref_opt; |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
auto path_nodeid = path.get_mappings ().get_nodeid (); |
| 141 |
ok = resolver->lookup_resolved_type (path_nodeid, &ref) |
| 142 |
|| resolver->lookup_resolved_name (path_nodeid, &ref) |
| 143 |
|| resolver->lookup_resolved_macro (path_nodeid, &ref); |
| 144 |
} |
| 145 |
|
| 146 |
if (!ok) |
| 147 |
{ |
| 148 |
rust_error_at (path.get_locus (), "Failed to resolve path to node-id"); |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
auto hid = mappings.lookup_node_to_hir (ref); |
| 153 |
if (!hid) |
| 154 |
{ |
| 155 |
rust_error_at (path.get_locus (), "Failed to resolve path to hir-id"); |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
auto resolved_item = mappings.lookup_hir_item (hid.value ()); |
| 160 |
if (!resolved_item.has_value ()) |
| 161 |
{ |
| 162 |
rust_error_at (path.get_locus (), |
| 163 |
"Failed to resolve trait by looking up hir node"); |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
if (resolved_item.value ()->get_item_kind () != HIR::Item::ItemKind::Trait) |
| 168 |
{ |
| 169 |
rich_location r (line_table, path.get_locus ()); |
| 170 |
r.add_fixit_replace ("not a trait"); |
| 171 |
rust_error_at (r, ErrorCode::E0404, "Expected a trait found %qs", |
| 172 |
path.as_simple_path ().as_string ().c_str ()); |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
*resolved = static_cast<HIR::Trait *> (*resolved_item); |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
TraitReference * |
| 181 |
TraitResolver::resolve_path (HIR::TypePath &path) |
| 182 |
{ |
| 183 |
HIR::Trait *resolved_trait_reference; |
| 184 |
bool ok = resolve_path_to_trait (path, &resolved_trait_reference); |
| 185 |
if (!ok) |
| 186 |
return &TraitReference::error_node (); |
| 187 |
|
| 188 |
return resolve_trait (resolved_trait_reference); |
| 189 |
} |
| 190 |
|
| 191 |
TraitReference * |
| 192 |
TraitResolver::resolve_trait (HIR::Trait *trait_reference) |
| 193 |
{ |
| 194 |
TraitReference *tref = &TraitReference::error_node (); |
| 195 |
if (context->lookup_trait_reference ( |
| 196 |
trait_reference->get_mappings ().get_defid (), &tref)) |
| 197 |
{ |
| 198 |
return tref; |
| 199 |
} |
| 200 |
|
| 201 |
DefId trait_id = trait_reference->get_mappings ().get_defid (); |
| 202 |
if (context->trait_query_in_progress (trait_id)) |
| 203 |
{ |
| 204 |
rust_error_at ( |
| 205 |
trait_reference->get_locus (), ErrorCode::E0391, |
| 206 |
"cycle detected when computing the super predicates of %qs", |
| 207 |
trait_reference->get_name ().as_string ().c_str ()); |
| 208 |
return &TraitReference::error_node (); |
| 209 |
} |
| 210 |
|
| 211 |
TraitQueryGuard guard (trait_id); |
| 212 |
TyTy::BaseType *self = nullptr; |
| 213 |
std::vector<TyTy::SubstitutionParamMapping> substitutions; |
| 214 |
|
| 215 |
// this needs to be special cased for the sized trait to not auto implemented |
| 216 |
// Sized on Self |
| 217 |
for (auto &generic_param : trait_reference->get_generic_params ()) |
| 218 |
{ |
| 219 |
switch (generic_param.get ()->get_kind ()) |
| 220 |
{ |
| 221 |
case HIR::GenericParam::GenericKind::LIFETIME: |
| 222 |
case HIR::GenericParam::GenericKind::CONST: |
| 223 |
// FIXME: Skipping Lifetime and Const completely until better |
| 224 |
// handling. |
| 225 |
break; |
| 226 |
|
| 227 |
case HIR::GenericParam::GenericKind::TYPE: { |
| 228 |
auto &typaram = static_cast<HIR::TypeParam &> (*generic_param); |
| 229 |
bool is_self |
| 230 |
= typaram.get_type_representation ().as_string ().compare ("Self") |
| 231 |
== 0; |
| 232 |
|
| 233 |
// https://doc.rust-lang.org/std/marker/trait.Sized.html |
| 234 |
// The one exception is the implicit Self type of a trait |
| 235 |
bool apply_sized = !is_self; |
| 236 |
auto param_type |
| 237 |
= TypeResolveGenericParam::Resolve (*generic_param, true, |
| 238 |
apply_sized); |
| 239 |
|
| 240 |
context->insert_type (generic_param->get_mappings (), param_type); |
| 241 |
substitutions.push_back ( |
| 242 |
TyTy::SubstitutionParamMapping (typaram, param_type)); |
| 243 |
|
| 244 |
if (is_self) |
| 245 |
{ |
| 246 |
rust_assert (param_type->get_kind () == TyTy::TypeKind::PARAM); |
| 247 |
TyTy::ParamType *p |
| 248 |
= static_cast<TyTy::ParamType *> (param_type); |
| 249 |
p->set_implicit_self_trait (); |
| 250 |
self = p; |
| 251 |
} |
| 252 |
} |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
rust_assert (self != nullptr); |
| 257 |
|
| 258 |
// Check if there is a super-trait, and apply this bound to the Self |
| 259 |
// TypeParam |
| 260 |
std::vector<TyTy::TypeBoundPredicate> specified_bounds; |
| 261 |
|
| 262 |
// copy the substitition mappings |
| 263 |
std::vector<TyTy::SubstitutionParamMapping> self_subst_copy; |
| 264 |
for (auto &sub : substitutions) |
| 265 |
self_subst_copy.push_back (sub.clone ()); |
| 266 |
|
| 267 |
// They also inherit themselves as a bound this enables a trait item to |
| 268 |
// reference other Self::trait_items |
| 269 |
auto self_hrtb |
| 270 |
= TyTy::TypeBoundPredicate (trait_reference->get_mappings ().get_defid (), |
| 271 |
std::move (self_subst_copy), |
| 272 |
BoundPolarity::RegularBound, |
| 273 |
trait_reference->get_locus ()); |
| 274 |
specified_bounds.push_back (self_hrtb); |
| 275 |
|
| 276 |
// look for any |
| 277 |
std::vector<TyTy::TypeBoundPredicate> super_traits; |
| 278 |
if (trait_reference->has_type_param_bounds ()) |
| 279 |
{ |
| 280 |
for (auto &bound : trait_reference->get_type_param_bounds ()) |
| 281 |
{ |
| 282 |
if (bound->get_bound_type () |
| 283 |
== HIR::TypeParamBound::BoundType::TRAITBOUND) |
| 284 |
{ |
| 285 |
HIR::TraitBound *b |
| 286 |
= static_cast<HIR::TraitBound *> (bound.get ()); |
| 287 |
|
| 288 |
auto predicate = get_predicate_from_bound ( |
| 289 |
b->get_path (), |
| 290 |
tl::nullopt /*this will setup a PLACEHOLDER for self*/); |
| 291 |
if (predicate.is_error ()) |
| 292 |
return &TraitReference::error_node (); |
| 293 |
|
| 294 |
specified_bounds.push_back (predicate); |
| 295 |
super_traits.push_back (predicate); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
self->inherit_bounds (specified_bounds); |
| 300 |
|
| 301 |
context->block_context ().enter (TypeCheckBlockContextItem (trait_reference)); |
| 302 |
std::vector<TraitItemReference> item_refs; |
| 303 |
for (auto &item : trait_reference->get_trait_items ()) |
| 304 |
{ |
| 305 |
// make a copy of the substs |
| 306 |
std::vector<TyTy::SubstitutionParamMapping> item_subst; |
| 307 |
for (auto &sub : substitutions) |
| 308 |
item_subst.push_back (sub.clone ()); |
| 309 |
|
| 310 |
TraitItemReference trait_item_ref |
| 311 |
= ResolveTraitItemToRef::Resolve (*item.get (), self, |
| 312 |
std::move (item_subst)); |
| 313 |
item_refs.push_back (std::move (trait_item_ref)); |
| 314 |
} |
| 315 |
|
| 316 |
TraitReference trait_object (trait_reference, item_refs, super_traits, |
| 317 |
std::move (substitutions)); |
| 318 |
context->insert_trait_reference ( |
| 319 |
trait_reference->get_mappings ().get_defid (), std::move (trait_object)); |
| 320 |
|
| 321 |
tref = &TraitReference::error_node (); |
| 322 |
bool ok = context->lookup_trait_reference ( |
| 323 |
trait_reference->get_mappings ().get_defid (), &tref); |
| 324 |
rust_assert (ok); |
| 325 |
|
| 326 |
// hook to allow the trait to resolve its optional item blocks, we cant |
| 327 |
// resolve the blocks of functions etc because it can end up in a recursive |
| 328 |
// loop of trying to resolve traits as required by the types |
| 329 |
tref->on_resolved (); |
| 330 |
context->block_context ().exit (); |
| 331 |
|
| 332 |
return tref; |
| 333 |
} |
| 334 |
|
| 335 |
TraitReference * |
| 336 |
TraitResolver::lookup_path (HIR::TypePath &path) |
| 337 |
{ |
| 338 |
HIR::Trait *resolved_trait_reference; |
| 339 |
bool ok = resolve_path_to_trait (path, &resolved_trait_reference); |
| 340 |
if (!ok) |
| 341 |
return &TraitReference::error_node (); |
| 342 |
|
| 343 |
TraitReference *tref = &TraitReference::error_node (); |
| 344 |
if (context->lookup_trait_reference ( |
| 345 |
resolved_trait_reference->get_mappings ().get_defid (), &tref)) |
| 346 |
{ |
| 347 |
return tref; |
| 348 |
} |
| 349 |
return &TraitReference::error_node (); |
| 350 |
} |
| 351 |
|
| 352 |
void |
| 353 |
TraitItemReference::on_resolved () |
| 354 |
{ |
| 355 |
switch (type) |
| 356 |
{ |
| 357 |
case CONST: |
| 358 |
resolve_item (static_cast<HIR::TraitItemConst &> (*hir_trait_item)); |
| 359 |
break; |
| 360 |
|
| 361 |
case TYPE: |
| 362 |
resolve_item (static_cast<HIR::TraitItemType &> (*hir_trait_item)); |
| 363 |
break; |
| 364 |
|
| 365 |
case FN: |
| 366 |
resolve_item (static_cast<HIR::TraitItemFunc &> (*hir_trait_item)); |
| 367 |
break; |
| 368 |
|
| 369 |
default: |
| 370 |
break; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
void |
| 375 |
TraitItemReference::resolve_item (HIR::TraitItemType &type) |
| 376 |
{ |
| 377 |
TyTy::BaseType *ty |
| 378 |
= new TyTy::PlaceholderType (type.get_name ().as_string (), |
| 379 |
type.get_mappings ().get_defid (), |
| 380 |
type.get_mappings ().get_hirid ()); |
| 381 |
context->insert_type (type.get_mappings (), ty); |
| 382 |
} |
| 383 |
|
| 384 |
void |
| 385 |
TraitItemReference::resolve_item (HIR::TraitItemConst &constant) |
| 386 |
{ |
| 387 |
// TODO |
| 388 |
} |
| 389 |
|
| 390 |
void |
| 391 |
TraitItemReference::resolve_item (HIR::TraitItemFunc &func) |
| 392 |
{ |
| 393 |
TyTy::BaseType *item_tyty = get_tyty (); |
| 394 |
if (item_tyty->get_kind () == TyTy::TypeKind::ERROR) |
| 395 |
return; |
| 396 |
|
| 397 |
if (!is_optional ()) |
| 398 |
return; |
| 399 |
|
| 400 |
// check the block and return types |
| 401 |
rust_assert (item_tyty->get_kind () == TyTy::TypeKind::FNDEF); |
| 402 |
|
| 403 |
// need to get the return type from this |
| 404 |
TyTy::FnType *resolved_fn_type = static_cast<TyTy::FnType *> (item_tyty); |
| 405 |
auto expected_ret_tyty = resolved_fn_type->get_return_type (); |
| 406 |
context->push_return_type (TypeCheckContextItem (&func), expected_ret_tyty); |
| 407 |
|
| 408 |
auto block_expr_ty = TypeCheckExpr::Resolve (func.get_block_expr ()); |
| 409 |
|
| 410 |
location_t fn_return_locus |
| 411 |
= func.get_decl ().has_return_type () |
| 412 |
? func.get_decl ().get_return_type ().get_locus () |
| 413 |
: func.get_locus (); |
| 414 |
|
| 415 |
coercion_site (func.get_mappings ().get_hirid (), |
| 416 |
TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus), |
| 417 |
TyTy::TyWithLocation (block_expr_ty), func.get_locus ()); |
| 418 |
|
| 419 |
context->pop_return_type (); |
| 420 |
} |
| 421 |
|
| 422 |
void |
| 423 |
TraitItemReference::associated_type_set (TyTy::BaseType *ty) const |
| 424 |
{ |
| 425 |
rust_assert (get_trait_item_type () == TraitItemType::TYPE); |
| 426 |
|
| 427 |
TyTy::BaseType *item_ty = get_tyty (); |
| 428 |
rust_assert (item_ty->get_kind () == TyTy::TypeKind::PLACEHOLDER); |
| 429 |
TyTy::PlaceholderType *placeholder |
| 430 |
= static_cast<TyTy::PlaceholderType *> (item_ty); |
| 431 |
|
| 432 |
placeholder->set_associated_type (ty->get_ty_ref ()); |
| 433 |
} |
| 434 |
|
| 435 |
void |
| 436 |
TraitItemReference::associated_type_reset (bool only_projections) const |
| 437 |
{ |
| 438 |
rust_assert (get_trait_item_type () == TraitItemType::TYPE); |
| 439 |
|
| 440 |
TyTy::BaseType *item_ty = get_tyty (); |
| 441 |
rust_assert (item_ty->get_kind () == TyTy::TypeKind::PLACEHOLDER); |
| 442 |
TyTy::PlaceholderType *placeholder |
| 443 |
= static_cast<TyTy::PlaceholderType *> (item_ty); |
| 444 |
|
| 445 |
if (!only_projections) |
| 446 |
{ |
| 447 |
placeholder->clear_associated_type (); |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
if (!placeholder->can_resolve ()) |
| 452 |
return; |
| 453 |
|
| 454 |
const TyTy::BaseType *r = placeholder->resolve (); |
| 455 |
if (r->get_kind () == TyTy::TypeKind::PROJECTION) |
| 456 |
{ |
| 457 |
placeholder->clear_associated_type (); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
void |
| 463 |
AssociatedImplTrait::setup_raw_associated_types () |
| 464 |
{ |
| 465 |
auto &impl_items = impl->get_impl_items (); |
| 466 |
for (auto &impl_item : impl_items) |
| 467 |
{ |
| 468 |
bool is_type_alias = impl_item->get_impl_item_type () |
| 469 |
== HIR::ImplItem::ImplItemType::TYPE_ALIAS; |
| 470 |
if (!is_type_alias) |
| 471 |
continue; |
| 472 |
|
| 473 |
HIR::TypeAlias &type = *static_cast<HIR::TypeAlias *> (impl_item.get ()); |
| 474 |
|
| 475 |
TraitItemReference *resolved_trait_item = nullptr; |
| 476 |
bool ok |
| 477 |
= trait->lookup_trait_item (type.get_new_type_name ().as_string (), |
| 478 |
&resolved_trait_item); |
| 479 |
if (!ok) |
| 480 |
continue; |
| 481 |
if (resolved_trait_item->get_trait_item_type () |
| 482 |
!= TraitItemReference::TraitItemType::TYPE) |
| 483 |
continue; |
| 484 |
|
| 485 |
TyTy::BaseType *lookup; |
| 486 |
ok = context->lookup_type (type.get_mappings ().get_hirid (), &lookup); |
| 487 |
rust_assert (ok); |
| 488 |
|
| 489 |
resolved_trait_item->associated_type_set (lookup); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
TyTy::BaseType * |
| 494 |
AssociatedImplTrait::setup_associated_types ( |
| 495 |
const TyTy::BaseType *self, const TyTy::TypeBoundPredicate &bound, |
| 496 |
TyTy::SubstitutionArgumentMappings *args, bool infer) |
| 497 |
{ |
| 498 |
// compute the constrained impl block generic arguments based on self and the |
| 499 |
// higher ranked trait bound |
| 500 |
TyTy::BaseType *receiver = self->clone (); |
| 501 |
|
| 502 |
// impl<Y> SliceIndex<[Y]> for Range<usize> |
| 503 |
// vs |
| 504 |
// I: SliceIndex<[<integer>]> and Range<<integer>> |
| 505 |
// |
| 506 |
// we need to figure out what Y is |
| 507 |
|
| 508 |
TyTy::BaseType *associated_self = get_self (); |
| 509 |
|
| 510 |
rust_debug ("setup_associated_types for: %s->%s bound %s", |
| 511 |
associated_self->debug_str ().c_str (), |
| 512 |
self->debug_str ().c_str (), bound.as_string ().c_str ()); |
| 513 |
|
| 514 |
// grab the parameters |
| 515 |
HIR::ImplBlock &impl_block = *get_impl_block (); |
| 516 |
std::vector<TyTy::SubstitutionParamMapping> substitutions; |
| 517 |
for (auto &generic_param : impl_block.get_generic_params ()) |
| 518 |
{ |
| 519 |
switch (generic_param.get ()->get_kind ()) |
| 520 |
{ |
| 521 |
case HIR::GenericParam::GenericKind::LIFETIME: |
| 522 |
case HIR::GenericParam::GenericKind::CONST: |
| 523 |
// FIXME: Skipping Lifetime and Const completely until better |
| 524 |
// handling. |
| 525 |
break; |
| 526 |
|
| 527 |
case HIR::GenericParam::GenericKind::TYPE: { |
| 528 |
TyTy::BaseType *l = nullptr; |
| 529 |
bool ok = context->lookup_type ( |
| 530 |
generic_param->get_mappings ().get_hirid (), &l); |
| 531 |
if (ok && l->get_kind () == TyTy::TypeKind::PARAM) |
| 532 |
{ |
| 533 |
substitutions.push_back (TyTy::SubstitutionParamMapping ( |
| 534 |
static_cast<HIR::TypeParam &> (*generic_param), |
| 535 |
static_cast<TyTy::ParamType *> (l))); |
| 536 |
} |
| 537 |
} |
| 538 |
break; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
// this callback gives us the parameters that get substituted so we can |
| 543 |
// compute the constrained type parameters for this impl block |
| 544 |
std::map<std::string, HirId> param_mappings; |
| 545 |
TyTy::ParamSubstCb param_subst_cb |
| 546 |
= [&] (const TyTy::ParamType &p, const TyTy::SubstitutionArg &a) { |
| 547 |
param_mappings[p.get_symbol ()] = a.get_tyty ()->get_ref (); |
| 548 |
}; |
| 549 |
|
| 550 |
// generate inference variables for these bound arguments so we can compute |
| 551 |
// their values |
| 552 |
location_t locus = UNKNOWN_LOCATION; |
| 553 |
std::vector<TyTy::SubstitutionArg> subst_args; |
| 554 |
for (auto &p : substitutions) |
| 555 |
{ |
| 556 |
if (p.needs_substitution () && infer) |
| 557 |
{ |
| 558 |
TyTy::TyVar infer_var = TyTy::TyVar::get_implicit_infer_var (locus); |
| 559 |
subst_args.push_back ( |
| 560 |
TyTy::SubstitutionArg (&p, infer_var.get_tyty ())); |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
TyTy::ParamType *param = p.get_param_ty (); |
| 565 |
TyTy::BaseType *resolved = param->destructure (); |
| 566 |
subst_args.push_back (TyTy::SubstitutionArg (&p, resolved)); |
| 567 |
param_mappings[param->get_symbol ()] = resolved->get_ref (); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
TyTy::SubstitutionArgumentMappings infer_arguments ( |
| 572 |
std::move (subst_args), {}, |
| 573 |
TyTy::SubstitutionArgumentMappings::regions_from_nullable_args (args), |
| 574 |
locus, param_subst_cb); |
| 575 |
TyTy::BaseType *impl_self_infer |
| 576 |
= (!associated_self->is_concrete ()) |
| 577 |
? SubstMapperInternal::Resolve (associated_self, infer_arguments) |
| 578 |
: associated_self; |
| 579 |
|
| 580 |
const TyTy::TypeBoundPredicate &impl_predicate |
| 581 |
= associated_self->lookup_predicate (bound.get_id ()); |
| 582 |
rust_assert (!impl_predicate.is_error ()); |
| 583 |
|
| 584 |
// infer the arguments on the predicate |
| 585 |
std::vector<TyTy::BaseType *> impl_trait_predicate_args; |
| 586 |
|
| 587 |
for (size_t i = 0; i < impl_predicate.get_substs ().size (); i++) |
| 588 |
{ |
| 589 |
const auto &arg = impl_predicate.get_substs ().at (i); |
| 590 |
if (i == 0) |
| 591 |
continue; |
| 592 |
|
| 593 |
const TyTy::ParamType *p = arg.get_param_ty (); |
| 594 |
TyTy::BaseType *r = p->resolve (); |
| 595 |
if (!r->is_concrete ()) |
| 596 |
{ |
| 597 |
r = SubstMapperInternal::Resolve (r, infer_arguments); |
| 598 |
} |
| 599 |
impl_trait_predicate_args.push_back (r); |
| 600 |
} |
| 601 |
|
| 602 |
// unify the bounds arguments |
| 603 |
std::vector<TyTy::BaseType *> hrtb_bound_arguments; |
| 604 |
for (size_t i = 0; i < bound.get_substs ().size (); i++) |
| 605 |
{ |
| 606 |
const auto &arg = bound.get_substs ().at (i); |
| 607 |
if (i == 0) |
| 608 |
continue; |
| 609 |
|
| 610 |
const TyTy::ParamType *p = arg.get_param_ty (); |
| 611 |
TyTy::BaseType *r = p->resolve (); |
| 612 |
if (!r->is_concrete ()) |
| 613 |
{ |
| 614 |
r = SubstMapperInternal::Resolve (r, infer_arguments); |
| 615 |
} |
| 616 |
hrtb_bound_arguments.push_back (r); |
| 617 |
} |
| 618 |
|
| 619 |
rust_assert (impl_trait_predicate_args.size () |
| 620 |
== hrtb_bound_arguments.size ()); |
| 621 |
for (size_t i = 0; i < impl_trait_predicate_args.size (); i++) |
| 622 |
{ |
| 623 |
TyTy::BaseType *a = impl_trait_predicate_args.at (i); |
| 624 |
TyTy::BaseType *b = hrtb_bound_arguments.at (i); |
| 625 |
|
| 626 |
TyTy::BaseType *result |
| 627 |
= unify_site_and (a->get_ref (), TyTy::TyWithLocation (a), |
| 628 |
TyTy::TyWithLocation (b), impl_predicate.get_locus (), |
| 629 |
true /*emit-errors*/, true /*commit-if-ok*/, |
| 630 |
true /*infer*/, true /*cleanup-on-fail*/); |
| 631 |
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR); |
| 632 |
} |
| 633 |
|
| 634 |
// we need to unify the receiver with the impl-block Self so that we compute |
| 635 |
// the type correctly as our receiver may be generic and we are inferring its |
| 636 |
// generic arguments and this Self might be the concrete version or vice |
| 637 |
// versa. |
| 638 |
auto result = unify_site_and (get_impl_block ()->get_mappings ().get_hirid (), |
| 639 |
TyTy::TyWithLocation (receiver), |
| 640 |
TyTy::TyWithLocation (impl_self_infer), |
| 641 |
impl_predicate.get_locus (), |
| 642 |
true /*emit-errors*/, true /*commit-if-ok*/, |
| 643 |
true /*infer*/, true /*cleanup-on-fail*/); |
| 644 |
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR); |
| 645 |
TyTy::BaseType *self_result = result; |
| 646 |
|
| 647 |
// create the argument list |
| 648 |
std::vector<TyTy::SubstitutionArg> associated_arguments; |
| 649 |
for (auto &p : substitutions) |
| 650 |
{ |
| 651 |
std::string symbol = p.get_param_ty ()->get_symbol (); |
| 652 |
auto it = param_mappings.find (symbol); |
| 653 |
rust_assert (it != param_mappings.end ()); |
| 654 |
|
| 655 |
HirId id = it->second; |
| 656 |
TyTy::BaseType *argument = nullptr; |
| 657 |
bool ok = context->lookup_type (id, &argument); |
| 658 |
rust_assert (ok); |
| 659 |
|
| 660 |
TyTy::SubstitutionArg arg (&p, argument); |
| 661 |
associated_arguments.push_back (arg); |
| 662 |
} |
| 663 |
|
| 664 |
TyTy::SubstitutionArgumentMappings associated_type_args ( |
| 665 |
std::move (associated_arguments), {}, |
| 666 |
TyTy::SubstitutionArgumentMappings::regions_from_nullable_args (args), |
| 667 |
locus); |
| 668 |
|
| 669 |
auto &impl_items = impl->get_impl_items (); |
| 670 |
for (auto &impl_item : impl_items) |
| 671 |
{ |
| 672 |
bool is_type_alias = impl_item->get_impl_item_type () |
| 673 |
== HIR::ImplItem::ImplItemType::TYPE_ALIAS; |
| 674 |
if (!is_type_alias) |
| 675 |
continue; |
| 676 |
|
| 677 |
HIR::TypeAlias &type = *static_cast<HIR::TypeAlias *> (impl_item.get ()); |
| 678 |
|
| 679 |
TraitItemReference *resolved_trait_item = nullptr; |
| 680 |
bool ok |
| 681 |
= trait->lookup_trait_item (type.get_new_type_name ().as_string (), |
| 682 |
&resolved_trait_item); |
| 683 |
if (!ok) |
| 684 |
continue; |
| 685 |
if (resolved_trait_item->get_trait_item_type () |
| 686 |
!= TraitItemReference::TraitItemType::TYPE) |
| 687 |
continue; |
| 688 |
|
| 689 |
TyTy::BaseType *lookup; |
| 690 |
if (!context->lookup_type (type.get_mappings ().get_hirid (), &lookup)) |
| 691 |
continue; |
| 692 |
|
| 693 |
// this might be generic |
| 694 |
TyTy::BaseType *substituted |
| 695 |
= SubstMapperInternal::Resolve (lookup, associated_type_args); |
| 696 |
resolved_trait_item->associated_type_set (substituted); |
| 697 |
} |
| 698 |
|
| 699 |
if (args != nullptr) |
| 700 |
{ |
| 701 |
*args = associated_type_args; |
| 702 |
} |
| 703 |
|
| 704 |
return self_result; |
| 705 |
} |
| 706 |
|
| 707 |
void |
| 708 |
AssociatedImplTrait::reset_associated_types () |
| 709 |
{ |
| 710 |
trait->clear_associated_types (); |
| 711 |
} |
| 712 |
|
| 713 |
location_t |
| 714 |
AssociatedImplTrait::get_locus () const |
| 715 |
{ |
| 716 |
return impl->get_locus (); |
| 717 |
} |
| 718 |
|
| 719 |
Analysis::NodeMapping |
| 720 |
TraitItemReference::get_parent_trait_mappings () const |
| 721 |
{ |
| 722 |
auto &mappings = Analysis::Mappings::get (); |
| 723 |
|
| 724 |
HIR::Trait *trait |
| 725 |
= mappings.lookup_trait_item_mapping (get_mappings ().get_hirid ()); |
| 726 |
rust_assert (trait != nullptr); |
| 727 |
|
| 728 |
return trait->get_mappings (); |
| 729 |
} |
| 730 |
|
| 731 |
bool |
| 732 |
TraitItemReference::is_object_safe () const |
| 733 |
{ |
| 734 |
// https://doc.rust-lang.org/reference/items/traits.html#object-safety |
| 735 |
switch (get_trait_item_type ()) |
| 736 |
{ |
| 737 |
case TraitItemReference::TraitItemType::FN: { |
| 738 |
// lets be boring and just check that this is indeed a method will do |
| 739 |
// for now |
| 740 |
const HIR::TraitItem *item = get_hir_trait_item (); |
| 741 |
const HIR::TraitItemFunc *fn |
| 742 |
= static_cast<const HIR::TraitItemFunc *> (item); |
| 743 |
return fn->get_decl ().is_method (); |
| 744 |
} |
| 745 |
|
| 746 |
// constants are not available via dyn dispatch and so is not object safe |
| 747 |
case TraitItemReference::TraitItemType::CONST: |
| 748 |
return false; |
| 749 |
|
| 750 |
// types are object safe since they are not available via dyn dispatch |
| 751 |
case TraitItemReference::TraitItemType::TYPE: |
| 752 |
return true; |
| 753 |
|
| 754 |
// this is just an error so lets just fail it |
| 755 |
case TraitItemReference::TraitItemType::ERROR: |
| 756 |
return false; |
| 757 |
} |
| 758 |
return false; |
| 759 |
} |
| 760 |
|
| 761 |
} // namespace Resolver |
| 762 |
} // namespace Rust |