| 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-full-decls.h" |
| 20 |
#include "rust-hir-type-bounds.h" |
| 21 |
#include "rust-hir-trait-resolve.h" |
| 22 |
#include "rust-substitution-mapper.h" |
| 23 |
#include "rust-hir-trait-resolve.h" |
| 24 |
#include "rust-type-util.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace Resolver { |
| 28 |
|
| 29 |
TypeBoundsProbe::TypeBoundsProbe (const TyTy::BaseType *receiver) |
| 30 |
: TypeCheckBase (), receiver (receiver) |
| 31 |
{} |
| 32 |
|
| 33 |
std::vector<std::pair<TraitReference *, HIR::ImplBlock *>> |
| 34 |
TypeBoundsProbe::Probe (const TyTy::BaseType *receiver) |
| 35 |
{ |
| 36 |
TypeBoundsProbe probe (receiver); |
| 37 |
probe.scan (); |
| 38 |
return probe.trait_references; |
| 39 |
} |
| 40 |
|
| 41 |
bool |
| 42 |
TypeBoundsProbe::is_bound_satisfied_for_type (TyTy::BaseType *receiver, |
| 43 |
TraitReference *ref) |
| 44 |
{ |
| 45 |
for (auto &bound : receiver->get_specified_bounds ()) |
| 46 |
{ |
| 47 |
const TraitReference *b = bound.get (); |
| 48 |
if (b->is_equal (*ref)) |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
std::vector<std::pair<TraitReference *, HIR::ImplBlock *>> bounds |
| 53 |
= Probe (receiver); |
| 54 |
for (auto &bound : bounds) |
| 55 |
{ |
| 56 |
const TraitReference *b = bound.first; |
| 57 |
if (b->is_equal (*ref)) |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
TypeBoundsProbe::scan () |
| 66 |
{ |
| 67 |
std::vector<std::pair<HIR::TypePath *, HIR::ImplBlock *>> |
| 68 |
possible_trait_paths; |
| 69 |
mappings.iterate_impl_blocks ( |
| 70 |
[&] (HirId id, HIR::ImplBlock *impl) mutable -> bool { |
| 71 |
// we are filtering for trait-impl-blocks |
| 72 |
if (!impl->has_trait_ref ()) |
| 73 |
return true; |
| 74 |
|
| 75 |
// can be recursive trait resolution |
| 76 |
HIR::Trait *t = TraitResolver::ResolveHirItem (impl->get_trait_ref ()); |
| 77 |
if (t == nullptr) |
| 78 |
return true; |
| 79 |
DefId trait_id = t->get_mappings ().get_defid (); |
| 80 |
if (context->trait_query_in_progress (trait_id)) |
| 81 |
return true; |
| 82 |
|
| 83 |
HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid (); |
| 84 |
TyTy::BaseType *impl_type = nullptr; |
| 85 |
if (!query_type (impl_ty_id, &impl_type)) |
| 86 |
return true; |
| 87 |
|
| 88 |
if (!receiver->can_eq (impl_type, false)) |
| 89 |
{ |
| 90 |
if (!impl_type->can_eq (receiver, false)) |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
possible_trait_paths.push_back ({&impl->get_trait_ref (), impl}); |
| 95 |
return true; |
| 96 |
}); |
| 97 |
|
| 98 |
for (auto &path : possible_trait_paths) |
| 99 |
{ |
| 100 |
HIR::TypePath *trait_path = path.first; |
| 101 |
TraitReference *trait_ref = TraitResolver::Resolve (*trait_path); |
| 102 |
|
| 103 |
if (!trait_ref->is_error ()) |
| 104 |
trait_references.push_back ({trait_ref, path.second}); |
| 105 |
} |
| 106 |
|
| 107 |
// marker traits... |
| 108 |
assemble_sized_builtin (); |
| 109 |
|
| 110 |
// add auto trait bounds |
| 111 |
for (auto *auto_trait : mappings.get_auto_traits ()) |
| 112 |
add_trait_bound (auto_trait); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
TypeBoundsProbe::assemble_sized_builtin () |
| 117 |
{ |
| 118 |
const TyTy::BaseType *raw = receiver->destructure (); |
| 119 |
|
| 120 |
// https://runrust.miraheze.org/wiki/Dynamically_Sized_Type |
| 121 |
// everything is sized except for: |
| 122 |
// |
| 123 |
// 1. dyn traits |
| 124 |
// 2. slices |
| 125 |
// 3. str |
| 126 |
// 4. ADT's which contain any of the above |
| 127 |
// t. tuples which contain any of the above |
| 128 |
switch (raw->get_kind ()) |
| 129 |
{ |
| 130 |
case TyTy::ARRAY: |
| 131 |
case TyTy::REF: |
| 132 |
case TyTy::POINTER: |
| 133 |
case TyTy::PARAM: |
| 134 |
case TyTy::FNDEF: |
| 135 |
case TyTy::FNPTR: |
| 136 |
case TyTy::BOOL: |
| 137 |
case TyTy::CHAR: |
| 138 |
case TyTy::INT: |
| 139 |
case TyTy::UINT: |
| 140 |
case TyTy::FLOAT: |
| 141 |
case TyTy::USIZE: |
| 142 |
case TyTy::ISIZE: |
| 143 |
case TyTy::CLOSURE: |
| 144 |
case TyTy::INFER: |
| 145 |
case TyTy::NEVER: |
| 146 |
case TyTy::PLACEHOLDER: |
| 147 |
case TyTy::PROJECTION: |
| 148 |
case TyTy::OPAQUE: |
| 149 |
assemble_builtin_candidate (LangItem::Kind::SIZED); |
| 150 |
break; |
| 151 |
|
| 152 |
// FIXME str and slice need to be moved and test cases updated |
| 153 |
case TyTy::SLICE: |
| 154 |
case TyTy::STR: |
| 155 |
case TyTy::ADT: |
| 156 |
case TyTy::TUPLE: |
| 157 |
// FIXME add extra checks |
| 158 |
assemble_builtin_candidate (LangItem::Kind::SIZED); |
| 159 |
break; |
| 160 |
|
| 161 |
case TyTy::DYNAMIC: |
| 162 |
case TyTy::ERROR: |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
void |
| 168 |
TypeBoundsProbe::add_trait_bound (HIR::Trait *trait) |
| 169 |
{ |
| 170 |
auto trait_ref = TraitResolver::Resolve (*trait); |
| 171 |
|
| 172 |
trait_references.push_back ({trait_ref, mappings.lookup_builtin_marker ()}); |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
TypeBoundsProbe::assemble_builtin_candidate (LangItem::Kind lang_item) |
| 177 |
{ |
| 178 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item); |
| 179 |
if (!lang_item_defined) |
| 180 |
return; |
| 181 |
DefId &id = lang_item_defined.value (); |
| 182 |
|
| 183 |
auto defid = mappings.lookup_defid (id); |
| 184 |
if (!defid) |
| 185 |
return; |
| 186 |
auto item = defid.value (); |
| 187 |
|
| 188 |
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait); |
| 189 |
HIR::Trait *trait = static_cast<HIR::Trait *> (item); |
| 190 |
const TyTy::BaseType *raw = receiver->destructure (); |
| 191 |
|
| 192 |
add_trait_bound (trait); |
| 193 |
|
| 194 |
rust_debug ("Added builtin lang_item: %s for %s", |
| 195 |
LangItem::ToString (lang_item).c_str (), |
| 196 |
raw->get_name ().c_str ()); |
| 197 |
} |
| 198 |
|
| 199 |
TraitReference * |
| 200 |
TypeCheckBase::resolve_trait_path (HIR::TypePath &path) |
| 201 |
{ |
| 202 |
return TraitResolver::Resolve (path); |
| 203 |
} |
| 204 |
|
| 205 |
TyTy::TypeBoundPredicate |
| 206 |
TypeCheckBase::get_predicate_from_bound ( |
| 207 |
HIR::TypePath &type_path, |
| 208 |
tl::optional<std::reference_wrapper<HIR::Type>> associated_self, |
| 209 |
BoundPolarity polarity, bool is_qualified_type_path) |
| 210 |
{ |
| 211 |
TyTy::TypeBoundPredicate lookup = TyTy::TypeBoundPredicate::error (); |
| 212 |
bool already_resolved |
| 213 |
= context->lookup_predicate (type_path.get_mappings ().get_hirid (), |
| 214 |
&lookup); |
| 215 |
if (already_resolved) |
| 216 |
return lookup; |
| 217 |
|
| 218 |
TraitReference *trait = resolve_trait_path (type_path); |
| 219 |
if (trait->is_error ()) |
| 220 |
return TyTy::TypeBoundPredicate::error (); |
| 221 |
|
| 222 |
TyTy::TypeBoundPredicate predicate (*trait, polarity, type_path.get_locus ()); |
| 223 |
HIR::GenericArgs args |
| 224 |
= HIR::GenericArgs::create_empty (type_path.get_locus ()); |
| 225 |
|
| 226 |
auto &final_seg = type_path.get_final_segment (); |
| 227 |
switch (final_seg.get_type ()) |
| 228 |
{ |
| 229 |
case HIR::TypePathSegment::SegmentType::GENERIC: { |
| 230 |
auto &final_generic_seg |
| 231 |
= static_cast<HIR::TypePathSegmentGeneric &> (final_seg); |
| 232 |
if (final_generic_seg.has_generic_args ()) |
| 233 |
{ |
| 234 |
args = final_generic_seg.get_generic_args (); |
| 235 |
if (args.get_binding_args ().size () > 0 |
| 236 |
&& associated_self.has_value () && is_qualified_type_path) |
| 237 |
{ |
| 238 |
auto &binding_args = args.get_binding_args (); |
| 239 |
|
| 240 |
rich_location r (line_table, args.get_locus ()); |
| 241 |
for (auto it = binding_args.begin (); it != binding_args.end (); |
| 242 |
it++) |
| 243 |
{ |
| 244 |
auto &arg = *it; |
| 245 |
r.add_fixit_remove (arg.get_locus ()); |
| 246 |
} |
| 247 |
rust_error_at (r, ErrorCode::E0229, |
| 248 |
"associated type bindings are not allowed here"); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
break; |
| 253 |
|
| 254 |
case HIR::TypePathSegment::SegmentType::FUNCTION: { |
| 255 |
auto &final_function_seg |
| 256 |
= static_cast<HIR::TypePathSegmentFunction &> (final_seg); |
| 257 |
auto &fn = final_function_seg.get_function_path (); |
| 258 |
|
| 259 |
// we need to make implicit generic args which must be an implicit |
| 260 |
// Tuple |
| 261 |
auto crate_num = mappings.get_current_crate (); |
| 262 |
HirId implicit_args_id = mappings.get_next_hir_id (); |
| 263 |
Analysis::NodeMapping mapping (crate_num, |
| 264 |
final_seg.get_mappings ().get_nodeid (), |
| 265 |
implicit_args_id, UNKNOWN_LOCAL_DEFID); |
| 266 |
|
| 267 |
std::vector<std::unique_ptr<HIR::Type>> params_copy; |
| 268 |
for (auto &p : fn.get_params ()) |
| 269 |
{ |
| 270 |
params_copy.push_back (p->clone_type ()); |
| 271 |
} |
| 272 |
|
| 273 |
std::vector<std::unique_ptr<HIR::Type>> inputs; |
| 274 |
inputs.push_back ( |
| 275 |
std::make_unique<HIR::TupleType> (mapping, std::move (params_copy), |
| 276 |
final_seg.get_locus ())); |
| 277 |
|
| 278 |
// resolve the fn_once_output type which assumes there must be an output |
| 279 |
// set |
| 280 |
rust_assert (fn.has_return_type ()); |
| 281 |
TypeCheckType::Resolve (fn.get_return_type ()); |
| 282 |
|
| 283 |
HIR::TraitItem *trait_item |
| 284 |
= mappings |
| 285 |
.lookup_trait_item_lang_item (LangItem::Kind::FN_ONCE_OUTPUT, |
| 286 |
final_seg.get_locus ()) |
| 287 |
.value (); |
| 288 |
|
| 289 |
std::vector<HIR::GenericArgsBinding> bindings; |
| 290 |
location_t output_locus = fn.get_return_type ().get_locus (); |
| 291 |
HIR::GenericArgsBinding binding (Identifier ( |
| 292 |
trait_item->trait_identifier ()), |
| 293 |
fn.get_return_type ().clone_type (), |
| 294 |
output_locus); |
| 295 |
bindings.push_back (std::move (binding)); |
| 296 |
|
| 297 |
args = HIR::GenericArgs ({} /* lifetimes */, |
| 298 |
std::move (inputs) /* type_args*/, |
| 299 |
std::move (bindings) /* binding_args*/, |
| 300 |
{} /* const_args */, final_seg.get_locus ()); |
| 301 |
} |
| 302 |
break; |
| 303 |
|
| 304 |
default: |
| 305 |
/* nothing to do */ |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
if (associated_self.has_value ()) |
| 310 |
{ |
| 311 |
std::vector<std::unique_ptr<HIR::Type>> type_args; |
| 312 |
type_args.push_back (std::unique_ptr<HIR::Type> ( |
| 313 |
associated_self.value ().get ().clone_type ())); |
| 314 |
for (auto &arg : args.get_type_args ()) |
| 315 |
{ |
| 316 |
type_args.push_back (std::unique_ptr<HIR::Type> (arg->clone_type ())); |
| 317 |
} |
| 318 |
|
| 319 |
args = HIR::GenericArgs (args.get_lifetime_args (), std::move (type_args), |
| 320 |
args.get_binding_args (), args.get_const_args (), |
| 321 |
args.get_locus ()); |
| 322 |
} |
| 323 |
|
| 324 |
// we try to apply generic arguments when they are non empty and or when the |
| 325 |
// predicate requires them so that we get the relevant Foo expects x number |
| 326 |
// arguments but got zero see test case rust/compile/traits12.rs |
| 327 |
if (!args.is_empty () || predicate.requires_generic_args ()) |
| 328 |
{ |
| 329 |
// this is applying generic arguments to a trait reference |
| 330 |
predicate.apply_generic_arguments (&args, associated_self.has_value ()); |
| 331 |
} |
| 332 |
|
| 333 |
context->insert_resolved_predicate (type_path.get_mappings ().get_hirid (), |
| 334 |
predicate); |
| 335 |
|
| 336 |
return predicate; |
| 337 |
} |
| 338 |
|
| 339 |
} // namespace Resolver |
| 340 |
|
| 341 |
namespace TyTy { |
| 342 |
|
| 343 |
TypeBoundPredicate::TypeBoundPredicate ( |
| 344 |
const Resolver::TraitReference &trait_reference, BoundPolarity polarity, |
| 345 |
location_t locus) |
| 346 |
: SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}), |
| 347 |
reference (trait_reference.get_mappings ().get_defid ()), locus (locus), |
| 348 |
error_flag (false), polarity (polarity), |
| 349 |
super_traits (trait_reference.get_super_traits ()) |
| 350 |
{ |
| 351 |
rust_assert (!trait_reference.get_trait_substs ().empty ()); |
| 352 |
|
| 353 |
substitutions.clear (); |
| 354 |
for (const auto &p : trait_reference.get_trait_substs ()) |
| 355 |
substitutions.push_back (p.clone ()); |
| 356 |
|
| 357 |
// we setup a dummy implict self argument |
| 358 |
SubstitutionArg placeholder_self (&get_substs ().front (), nullptr); |
| 359 |
used_arguments.get_mappings ().push_back (placeholder_self); |
| 360 |
} |
| 361 |
|
| 362 |
TypeBoundPredicate::TypeBoundPredicate ( |
| 363 |
DefId reference, std::vector<SubstitutionParamMapping> subst, |
| 364 |
BoundPolarity polarity, location_t locus) |
| 365 |
: SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}), |
| 366 |
reference (reference), locus (locus), error_flag (false), |
| 367 |
polarity (polarity) |
| 368 |
{ |
| 369 |
rust_assert (!subst.empty ()); |
| 370 |
|
| 371 |
substitutions.clear (); |
| 372 |
for (const auto &p : subst) |
| 373 |
substitutions.push_back (p.clone ()); |
| 374 |
|
| 375 |
// we setup a dummy implict self argument |
| 376 |
SubstitutionArg placeholder_self (&get_substs ().front (), nullptr); |
| 377 |
used_arguments.get_mappings ().push_back (placeholder_self); |
| 378 |
} |
| 379 |
|
| 380 |
TypeBoundPredicate::TypeBoundPredicate (mark_is_error) |
| 381 |
: SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}), |
| 382 |
reference (UNKNOWN_DEFID), locus (UNDEF_LOCATION), error_flag (true), |
| 383 |
polarity (BoundPolarity::RegularBound) |
| 384 |
{} |
| 385 |
|
| 386 |
TypeBoundPredicate::TypeBoundPredicate (const TypeBoundPredicate &other) |
| 387 |
: SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}), |
| 388 |
reference (other.reference), locus (other.locus), |
| 389 |
error_flag (other.error_flag), polarity (other.polarity), |
| 390 |
super_traits (other.super_traits) |
| 391 |
{ |
| 392 |
substitutions.clear (); |
| 393 |
for (const auto &p : other.get_substs ()) |
| 394 |
substitutions.push_back (p.clone ()); |
| 395 |
|
| 396 |
std::vector<SubstitutionArg> mappings; |
| 397 |
for (size_t i = 0; i < other.used_arguments.get_mappings ().size (); i++) |
| 398 |
{ |
| 399 |
const SubstitutionArg &oa = other.used_arguments.get_mappings ().at (i); |
| 400 |
SubstitutionArg arg (oa); |
| 401 |
mappings.push_back (std::move (arg)); |
| 402 |
} |
| 403 |
|
| 404 |
// we need to remap the argument mappings based on this copied constructor |
| 405 |
std::vector<SubstitutionArg> copied_arg_mappings; |
| 406 |
size_t i = 0; |
| 407 |
for (const auto &m : other.used_arguments.get_mappings ()) |
| 408 |
{ |
| 409 |
TyTy::BaseType *argument |
| 410 |
= m.get_tyty () == nullptr ? nullptr : m.get_tyty ()->clone (); |
| 411 |
SubstitutionArg c (&substitutions.at (i++), argument); |
| 412 |
copied_arg_mappings.push_back (std::move (c)); |
| 413 |
} |
| 414 |
|
| 415 |
used_arguments |
| 416 |
= SubstitutionArgumentMappings (copied_arg_mappings, {}, |
| 417 |
other.used_arguments.get_regions (), |
| 418 |
other.used_arguments.get_locus ()); |
| 419 |
} |
| 420 |
|
| 421 |
TypeBoundPredicate & |
| 422 |
TypeBoundPredicate::operator= (const TypeBoundPredicate &other) |
| 423 |
{ |
| 424 |
reference = other.reference; |
| 425 |
locus = other.locus; |
| 426 |
error_flag = other.error_flag; |
| 427 |
polarity = other.polarity; |
| 428 |
used_arguments = SubstitutionArgumentMappings::empty (); |
| 429 |
|
| 430 |
substitutions.clear (); |
| 431 |
for (const auto &p : other.get_substs ()) |
| 432 |
substitutions.push_back (p.clone ()); |
| 433 |
|
| 434 |
if (other.is_error ()) |
| 435 |
return *this; |
| 436 |
|
| 437 |
std::vector<SubstitutionArg> mappings; |
| 438 |
for (size_t i = 0; i < other.used_arguments.get_mappings ().size (); i++) |
| 439 |
{ |
| 440 |
const SubstitutionArg &oa = other.used_arguments.get_mappings ().at (i); |
| 441 |
SubstitutionArg arg (oa); |
| 442 |
mappings.push_back (std::move (arg)); |
| 443 |
} |
| 444 |
|
| 445 |
// we need to remap the argument mappings based on this copied constructor |
| 446 |
std::vector<SubstitutionArg> copied_arg_mappings; |
| 447 |
size_t i = 0; |
| 448 |
for (const auto &m : other.used_arguments.get_mappings ()) |
| 449 |
{ |
| 450 |
TyTy::BaseType *argument |
| 451 |
= m.get_tyty () == nullptr ? nullptr : m.get_tyty ()->clone (); |
| 452 |
SubstitutionArg c (&substitutions.at (i++), argument); |
| 453 |
copied_arg_mappings.push_back (std::move (c)); |
| 454 |
} |
| 455 |
|
| 456 |
used_arguments |
| 457 |
= SubstitutionArgumentMappings (copied_arg_mappings, {}, |
| 458 |
other.used_arguments.get_regions (), |
| 459 |
other.used_arguments.get_locus ()); |
| 460 |
super_traits = other.super_traits; |
| 461 |
|
| 462 |
return *this; |
| 463 |
} |
| 464 |
|
| 465 |
TypeBoundPredicate |
| 466 |
TypeBoundPredicate::error () |
| 467 |
{ |
| 468 |
return TypeBoundPredicate (mark_is_error ()); |
| 469 |
} |
| 470 |
|
| 471 |
std::string |
| 472 |
TypeBoundPredicate::as_string () const |
| 473 |
{ |
| 474 |
return get ()->as_string () + subst_as_string (); |
| 475 |
} |
| 476 |
|
| 477 |
std::string |
| 478 |
TypeBoundPredicate::as_name () const |
| 479 |
{ |
| 480 |
return get ()->get_name () + subst_as_string (); |
| 481 |
} |
| 482 |
|
| 483 |
const Resolver::TraitReference * |
| 484 |
TypeBoundPredicate::get () const |
| 485 |
{ |
| 486 |
auto context = Resolver::TypeCheckContext::get (); |
| 487 |
|
| 488 |
Resolver::TraitReference *ref = nullptr; |
| 489 |
bool ok = context->lookup_trait_reference (reference, &ref); |
| 490 |
rust_assert (ok); |
| 491 |
|
| 492 |
return ref; |
| 493 |
} |
| 494 |
|
| 495 |
std::string |
| 496 |
TypeBoundPredicate::get_name () const |
| 497 |
{ |
| 498 |
return get ()->get_name (); |
| 499 |
} |
| 500 |
|
| 501 |
bool |
| 502 |
TypeBoundPredicate::is_object_safe (bool emit_error, location_t locus) const |
| 503 |
{ |
| 504 |
const Resolver::TraitReference *trait = get (); |
| 505 |
rust_assert (trait != nullptr); |
| 506 |
return trait->is_object_safe (emit_error, locus); |
| 507 |
} |
| 508 |
|
| 509 |
void |
| 510 |
TypeBoundPredicate::apply_generic_arguments (HIR::GenericArgs *generic_args, |
| 511 |
bool has_associated_self) |
| 512 |
{ |
| 513 |
rust_assert (!substitutions.empty ()); |
| 514 |
if (has_associated_self) |
| 515 |
{ |
| 516 |
used_arguments = SubstitutionArgumentMappings::empty (); |
| 517 |
} |
| 518 |
else |
| 519 |
{ |
| 520 |
// we need to get the substitutions argument mappings but also remember |
| 521 |
// that we have an implicit Self argument which we must be careful to |
| 522 |
// respect |
| 523 |
rust_assert (!used_arguments.is_empty ()); |
| 524 |
} |
| 525 |
|
| 526 |
// now actually perform a substitution |
| 527 |
auto args = get_mappings_from_generic_args ( |
| 528 |
*generic_args, |
| 529 |
Resolver::TypeCheckContext::get ()->regions_from_generic_args ( |
| 530 |
*generic_args)); |
| 531 |
|
| 532 |
apply_argument_mappings (args); |
| 533 |
} |
| 534 |
|
| 535 |
void |
| 536 |
TypeBoundPredicate::apply_argument_mappings ( |
| 537 |
SubstitutionArgumentMappings &arguments) |
| 538 |
{ |
| 539 |
used_arguments = arguments; |
| 540 |
error_flag |= used_arguments.is_error (); |
| 541 |
auto &subst_mappings = used_arguments; |
| 542 |
for (auto &sub : get_substs ()) |
| 543 |
{ |
| 544 |
SubstitutionArg arg = SubstitutionArg::error (); |
| 545 |
bool ok |
| 546 |
= subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg); |
| 547 |
if (ok && arg.get_tyty () != nullptr) |
| 548 |
sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ()); |
| 549 |
} |
| 550 |
|
| 551 |
// associated argument mappings |
| 552 |
for (auto &it : subst_mappings.get_binding_args ()) |
| 553 |
{ |
| 554 |
std::string identifier = it.first; |
| 555 |
TyTy::BaseType *type = it.second; |
| 556 |
|
| 557 |
TypeBoundPredicateItem item = lookup_associated_item (identifier); |
| 558 |
rust_assert (!item.is_error ()); |
| 559 |
|
| 560 |
const auto item_ref = item.get_raw_item (); |
| 561 |
item_ref->associated_type_set (type); |
| 562 |
} |
| 563 |
|
| 564 |
for (auto &super_trait : super_traits) |
| 565 |
{ |
| 566 |
auto adjusted |
| 567 |
= super_trait.adjust_mappings_for_this (used_arguments, |
| 568 |
true /*trait mode*/); |
| 569 |
super_trait.apply_argument_mappings (adjusted); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
bool |
| 574 |
TypeBoundPredicate::contains_item (const std::string &search) const |
| 575 |
{ |
| 576 |
auto trait_ref = get (); |
| 577 |
const Resolver::TraitItemReference *trait_item_ref = nullptr; |
| 578 |
return trait_ref->lookup_trait_item (search, &trait_item_ref); |
| 579 |
} |
| 580 |
|
| 581 |
TypeBoundPredicateItem |
| 582 |
TypeBoundPredicate::lookup_associated_item (const std::string &search) const |
| 583 |
{ |
| 584 |
auto trait_ref = get (); |
| 585 |
const Resolver::TraitItemReference *trait_item_ref = nullptr; |
| 586 |
if (trait_ref->lookup_trait_item (search, &trait_item_ref, |
| 587 |
false /*lookup supers*/)) |
| 588 |
return TypeBoundPredicateItem (*this, trait_item_ref); |
| 589 |
|
| 590 |
for (auto &super_trait : super_traits) |
| 591 |
{ |
| 592 |
auto lookup = super_trait.lookup_associated_item (search); |
| 593 |
if (!lookup.is_error ()) |
| 594 |
return lookup; |
| 595 |
} |
| 596 |
|
| 597 |
return TypeBoundPredicateItem::error (); |
| 598 |
} |
| 599 |
|
| 600 |
TypeBoundPredicateItem::TypeBoundPredicateItem ( |
| 601 |
const TypeBoundPredicate parent, |
| 602 |
const Resolver::TraitItemReference *trait_item_ref) |
| 603 |
: parent (parent), trait_item_ref (trait_item_ref) |
| 604 |
{} |
| 605 |
|
| 606 |
TypeBoundPredicateItem::TypeBoundPredicateItem ( |
| 607 |
const TypeBoundPredicateItem &other) |
| 608 |
: parent (other.parent), trait_item_ref (other.trait_item_ref) |
| 609 |
{} |
| 610 |
|
| 611 |
TypeBoundPredicateItem & |
| 612 |
TypeBoundPredicateItem::operator= (const TypeBoundPredicateItem &other) |
| 613 |
{ |
| 614 |
parent = other.parent; |
| 615 |
trait_item_ref = other.trait_item_ref; |
| 616 |
|
| 617 |
return *this; |
| 618 |
} |
| 619 |
|
| 620 |
TypeBoundPredicateItem |
| 621 |
TypeBoundPredicateItem::error () |
| 622 |
{ |
| 623 |
return TypeBoundPredicateItem (TypeBoundPredicate::error (), nullptr); |
| 624 |
} |
| 625 |
|
| 626 |
bool |
| 627 |
TypeBoundPredicateItem::is_error () const |
| 628 |
{ |
| 629 |
return parent.is_error () || trait_item_ref == nullptr; |
| 630 |
} |
| 631 |
|
| 632 |
const TypeBoundPredicate * |
| 633 |
TypeBoundPredicateItem::get_parent () const |
| 634 |
{ |
| 635 |
return &parent; |
| 636 |
} |
| 637 |
|
| 638 |
TypeBoundPredicateItem |
| 639 |
TypeBoundPredicate::lookup_associated_item ( |
| 640 |
const Resolver::TraitItemReference *ref) const |
| 641 |
{ |
| 642 |
return lookup_associated_item (ref->get_identifier ()); |
| 643 |
} |
| 644 |
|
| 645 |
BaseType * |
| 646 |
TypeBoundPredicateItem::get_tyty_for_receiver (const TyTy::BaseType *receiver) |
| 647 |
{ |
| 648 |
TyTy::BaseType *trait_item_tyty = get_raw_item ()->get_tyty (); |
| 649 |
if (parent.get_substitution_arguments ().is_empty ()) |
| 650 |
return trait_item_tyty; |
| 651 |
|
| 652 |
const Resolver::TraitItemReference *tref = get_raw_item (); |
| 653 |
bool is_associated_type = tref->get_trait_item_type (); |
| 654 |
if (is_associated_type) |
| 655 |
return trait_item_tyty; |
| 656 |
|
| 657 |
// set up the self mapping |
| 658 |
SubstitutionArgumentMappings gargs = parent.get_substitution_arguments (); |
| 659 |
rust_assert (!gargs.is_empty ()); |
| 660 |
|
| 661 |
// setup the adjusted mappings |
| 662 |
std::vector<SubstitutionArg> adjusted_mappings; |
| 663 |
for (size_t i = 0; i < gargs.get_mappings ().size (); i++) |
| 664 |
{ |
| 665 |
auto &mapping = gargs.get_mappings ().at (i); |
| 666 |
|
| 667 |
bool is_implicit_self = i == 0; |
| 668 |
TyTy::BaseType *argument |
| 669 |
= is_implicit_self ? receiver->clone () : mapping.get_tyty (); |
| 670 |
|
| 671 |
SubstitutionArg arg (mapping.get_param_mapping (), argument); |
| 672 |
adjusted_mappings.push_back (std::move (arg)); |
| 673 |
} |
| 674 |
|
| 675 |
SubstitutionArgumentMappings adjusted (adjusted_mappings, {}, |
| 676 |
gargs.get_regions (), |
| 677 |
gargs.get_locus (), |
| 678 |
gargs.get_subst_cb (), |
| 679 |
true /* trait-mode-flag */); |
| 680 |
return Resolver::SubstMapperInternal::Resolve (trait_item_tyty, adjusted); |
| 681 |
} |
| 682 |
bool |
| 683 |
TypeBoundPredicate::is_error () const |
| 684 |
{ |
| 685 |
auto context = Resolver::TypeCheckContext::get (); |
| 686 |
|
| 687 |
Resolver::TraitReference *ref = nullptr; |
| 688 |
bool ok = context->lookup_trait_reference (reference, &ref); |
| 689 |
|
| 690 |
return !ok || error_flag; |
| 691 |
} |
| 692 |
|
| 693 |
BaseType * |
| 694 |
TypeBoundPredicate::handle_substitions ( |
| 695 |
SubstitutionArgumentMappings &subst_mappings) |
| 696 |
{ |
| 697 |
for (auto &sub : get_substs ()) |
| 698 |
{ |
| 699 |
if (sub.get_param_ty () == nullptr) |
| 700 |
continue; |
| 701 |
|
| 702 |
ParamType *p = sub.get_param_ty (); |
| 703 |
BaseType *r = p->resolve (); |
| 704 |
BaseType *s = Resolver::SubstMapperInternal::Resolve (r, subst_mappings); |
| 705 |
|
| 706 |
p->set_ty_ref (s->get_ty_ref ()); |
| 707 |
} |
| 708 |
|
| 709 |
// associated argument mappings |
| 710 |
for (auto &it : subst_mappings.get_binding_args ()) |
| 711 |
{ |
| 712 |
std::string identifier = it.first; |
| 713 |
TyTy::BaseType *type = it.second; |
| 714 |
|
| 715 |
TypeBoundPredicateItem item = lookup_associated_item (identifier); |
| 716 |
if (!item.is_error ()) |
| 717 |
{ |
| 718 |
const auto item_ref = item.get_raw_item (); |
| 719 |
item_ref->associated_type_set (type); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
// FIXME more error handling at some point |
| 724 |
// used_arguments = subst_mappings; |
| 725 |
// error_flag |= used_arguments.is_error (); |
| 726 |
|
| 727 |
return nullptr; |
| 728 |
} |
| 729 |
|
| 730 |
bool |
| 731 |
TypeBoundPredicate::requires_generic_args () const |
| 732 |
{ |
| 733 |
if (is_error ()) |
| 734 |
return false; |
| 735 |
|
| 736 |
return substitutions.size () > 1; |
| 737 |
} |
| 738 |
|
| 739 |
bool |
| 740 |
TypeBoundPredicate::contains_associated_types () const |
| 741 |
{ |
| 742 |
return get_num_associated_bindings () > 0; |
| 743 |
} |
| 744 |
|
| 745 |
size_t |
| 746 |
TypeBoundPredicate::get_num_associated_bindings () const |
| 747 |
{ |
| 748 |
size_t count = 0; |
| 749 |
auto trait_ref = get (); |
| 750 |
for (const auto &trait_item : trait_ref->get_trait_items ()) |
| 751 |
{ |
| 752 |
bool is_associated_type |
| 753 |
= trait_item.get_trait_item_type () |
| 754 |
== Resolver::TraitItemReference::TraitItemType::TYPE; |
| 755 |
if (is_associated_type) |
| 756 |
count++; |
| 757 |
} |
| 758 |
return count; |
| 759 |
} |
| 760 |
|
| 761 |
TypeBoundPredicateItem |
| 762 |
TypeBoundPredicate::lookup_associated_type (const std::string &search) |
| 763 |
{ |
| 764 |
TypeBoundPredicateItem item = lookup_associated_item (search); |
| 765 |
|
| 766 |
// only need to check that it is infact an associated type because other |
| 767 |
// wise if it was not found it will just be an error node anyway |
| 768 |
if (!item.is_error ()) |
| 769 |
{ |
| 770 |
const auto raw = item.get_raw_item (); |
| 771 |
if (raw->get_trait_item_type () |
| 772 |
!= Resolver::TraitItemReference::TraitItemType::TYPE) |
| 773 |
return TypeBoundPredicateItem::error (); |
| 774 |
} |
| 775 |
return item; |
| 776 |
} |
| 777 |
|
| 778 |
std::vector<TypeBoundPredicateItem> |
| 779 |
TypeBoundPredicate::get_associated_type_items () |
| 780 |
{ |
| 781 |
std::vector<TypeBoundPredicateItem> items; |
| 782 |
auto trait_ref = get (); |
| 783 |
for (const auto &trait_item : trait_ref->get_trait_items ()) |
| 784 |
{ |
| 785 |
bool is_associated_type |
| 786 |
= trait_item.get_trait_item_type () |
| 787 |
== Resolver::TraitItemReference::TraitItemType::TYPE; |
| 788 |
if (is_associated_type) |
| 789 |
{ |
| 790 |
TypeBoundPredicateItem item (*this, &trait_item); |
| 791 |
items.push_back (std::move (item)); |
| 792 |
} |
| 793 |
} |
| 794 |
return items; |
| 795 |
} |
| 796 |
|
| 797 |
bool |
| 798 |
TypeBoundPredicate::is_equal (const TypeBoundPredicate &other) const |
| 799 |
{ |
| 800 |
// check they match the same trait reference |
| 801 |
if (reference != other.reference) |
| 802 |
return false; |
| 803 |
|
| 804 |
// check that the generics match |
| 805 |
if (get_num_substitutions () != other.get_num_substitutions ()) |
| 806 |
return false; |
| 807 |
|
| 808 |
// then match the generics applied |
| 809 |
for (size_t i = 0; i < get_num_substitutions (); i++) |
| 810 |
{ |
| 811 |
const SubstitutionParamMapping &a = substitutions.at (i); |
| 812 |
const SubstitutionParamMapping &b = other.substitutions.at (i); |
| 813 |
|
| 814 |
const ParamType *ap = a.get_param_ty (); |
| 815 |
const ParamType *bp = b.get_param_ty (); |
| 816 |
|
| 817 |
const BaseType *apd = ap->destructure (); |
| 818 |
const BaseType *bpd = bp->destructure (); |
| 819 |
|
| 820 |
// FIXME use the unify_and infer inteface or try coerce |
| 821 |
if (!apd->can_eq (bpd, false /*emit_errors*/)) |
| 822 |
{ |
| 823 |
if (!bpd->can_eq (apd, false /*emit_errors*/)) |
| 824 |
return false; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
return true; |
| 829 |
} |
| 830 |
|
| 831 |
bool |
| 832 |
TypeBoundPredicate::validate_type_implements_super_traits ( |
| 833 |
TyTy::BaseType &self, HIR::Type &impl_type, HIR::Type &trait) const |
| 834 |
{ |
| 835 |
if (get_polarity () != BoundPolarity::RegularBound) |
| 836 |
return true; |
| 837 |
|
| 838 |
auto &ptref = *get (); |
| 839 |
for (auto &super : super_traits) |
| 840 |
{ |
| 841 |
if (super.get_polarity () != BoundPolarity::RegularBound) |
| 842 |
continue; |
| 843 |
|
| 844 |
if (!super.validate_type_implements_this (self, impl_type, trait)) |
| 845 |
{ |
| 846 |
auto &sptref = *super.get (); |
| 847 |
|
| 848 |
// emit error |
| 849 |
std::string fixit1 |
| 850 |
= "required by this bound in: " + ptref.get_name (); |
| 851 |
std::string fixit2 = "the trait " + sptref.get_name () |
| 852 |
+ " is not implemented for " |
| 853 |
+ impl_type.as_string (); |
| 854 |
|
| 855 |
rich_location r (line_table, trait.get_locus ()); |
| 856 |
r.add_fixit_insert_after (super.get_locus (), fixit1.c_str ()); |
| 857 |
r.add_fixit_insert_after (trait.get_locus (), fixit2.c_str ()); |
| 858 |
rust_error_at (r, ErrorCode::E0277, |
| 859 |
"the trait bound %<%s: %s%> is not satisfied", |
| 860 |
impl_type.as_string ().c_str (), |
| 861 |
sptref.get_name ().c_str ()); |
| 862 |
|
| 863 |
return false; |
| 864 |
} |
| 865 |
|
| 866 |
if (!super.validate_type_implements_super_traits (self, impl_type, trait)) |
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
return true; |
| 871 |
} |
| 872 |
|
| 873 |
bool |
| 874 |
TypeBoundPredicate::validate_type_implements_this (TyTy::BaseType &self, |
| 875 |
HIR::Type &impl_type, |
| 876 |
HIR::Type &trait) const |
| 877 |
{ |
| 878 |
const auto &ptref = *get (); |
| 879 |
auto probed_bounds = Resolver::TypeBoundsProbe::Probe (&self); |
| 880 |
for (auto &elem : probed_bounds) |
| 881 |
{ |
| 882 |
auto &tref = *(elem.first); |
| 883 |
if (ptref.is_equal (tref)) |
| 884 |
return true; |
| 885 |
} |
| 886 |
|
| 887 |
return false; |
| 888 |
} |
| 889 |
|
| 890 |
// trait item reference |
| 891 |
|
| 892 |
const Resolver::TraitItemReference * |
| 893 |
TypeBoundPredicateItem::get_raw_item () const |
| 894 |
{ |
| 895 |
return trait_item_ref; |
| 896 |
} |
| 897 |
|
| 898 |
bool |
| 899 |
TypeBoundPredicateItem::needs_implementation () const |
| 900 |
{ |
| 901 |
return !get_raw_item ()->is_optional (); |
| 902 |
} |
| 903 |
|
| 904 |
location_t |
| 905 |
TypeBoundPredicateItem::get_locus () const |
| 906 |
{ |
| 907 |
return get_raw_item ()->get_locus (); |
| 908 |
} |
| 909 |
|
| 910 |
// TypeBoundsMappings |
| 911 |
|
| 912 |
TypeBoundsMappings::TypeBoundsMappings ( |
| 913 |
std::vector<TypeBoundPredicate> specified_bounds) |
| 914 |
: specified_bounds (specified_bounds) |
| 915 |
{} |
| 916 |
|
| 917 |
std::vector<TypeBoundPredicate> & |
| 918 |
TypeBoundsMappings::get_specified_bounds () |
| 919 |
{ |
| 920 |
return specified_bounds; |
| 921 |
} |
| 922 |
|
| 923 |
const std::vector<TypeBoundPredicate> & |
| 924 |
TypeBoundsMappings::get_specified_bounds () const |
| 925 |
{ |
| 926 |
return specified_bounds; |
| 927 |
} |
| 928 |
|
| 929 |
TypeBoundPredicate |
| 930 |
TypeBoundsMappings::lookup_predicate (DefId id) |
| 931 |
{ |
| 932 |
for (auto &b : specified_bounds) |
| 933 |
{ |
| 934 |
if (b.get_id () == id) |
| 935 |
return b; |
| 936 |
} |
| 937 |
return TypeBoundPredicate::error (); |
| 938 |
} |
| 939 |
|
| 940 |
size_t |
| 941 |
TypeBoundsMappings::num_specified_bounds () const |
| 942 |
{ |
| 943 |
return specified_bounds.size (); |
| 944 |
} |
| 945 |
|
| 946 |
std::string |
| 947 |
TypeBoundsMappings::raw_bounds_as_string () const |
| 948 |
{ |
| 949 |
std::string buf; |
| 950 |
for (size_t i = 0; i < specified_bounds.size (); i++) |
| 951 |
{ |
| 952 |
const TypeBoundPredicate &b = specified_bounds.at (i); |
| 953 |
bool has_next = (i + 1) < specified_bounds.size (); |
| 954 |
buf += b.as_string () + (has_next ? " + " : ""); |
| 955 |
} |
| 956 |
return buf; |
| 957 |
} |
| 958 |
|
| 959 |
std::string |
| 960 |
TypeBoundsMappings::bounds_as_string () const |
| 961 |
{ |
| 962 |
return "bounds:[" + raw_bounds_as_string () + "]"; |
| 963 |
} |
| 964 |
|
| 965 |
std::string |
| 966 |
TypeBoundsMappings::raw_bounds_as_name () const |
| 967 |
{ |
| 968 |
std::string buf; |
| 969 |
for (size_t i = 0; i < specified_bounds.size (); i++) |
| 970 |
{ |
| 971 |
const TypeBoundPredicate &b = specified_bounds.at (i); |
| 972 |
bool has_next = (i + 1) < specified_bounds.size (); |
| 973 |
buf += b.as_name () + (has_next ? " + " : ""); |
| 974 |
} |
| 975 |
|
| 976 |
return buf; |
| 977 |
} |
| 978 |
|
| 979 |
void |
| 980 |
TypeBoundsMappings::add_bound (TypeBoundPredicate predicate) |
| 981 |
{ |
| 982 |
for (auto &bound : specified_bounds) |
| 983 |
{ |
| 984 |
bool same_trait_ref_p = bound.get_id () == predicate.get_id (); |
| 985 |
if (same_trait_ref_p) |
| 986 |
return; |
| 987 |
} |
| 988 |
|
| 989 |
specified_bounds.push_back (predicate); |
| 990 |
} |
| 991 |
|
| 992 |
} // namespace TyTy |
| 993 |
} // namespace Rust |