| 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-type-util.h" |
| 20 |
#include "rust-diagnostics.h" |
| 21 |
#include "rust-hir-map.h" |
| 22 |
#include "rust-hir-type-check-implitem.h" |
| 23 |
#include "rust-hir-type-check-item.h" |
| 24 |
#include "rust-hir-type-check.h" |
| 25 |
#include "rust-hir-type-check-type.h" |
| 26 |
#include "rust-casts.h" |
| 27 |
#include "rust-unify.h" |
| 28 |
#include "rust-coercion.h" |
| 29 |
#include "rust-hir-type-bounds.h" |
| 30 |
#include "rust-immutable-name-resolution-context.h" |
| 31 |
#include "options.h" |
| 32 |
|
| 33 |
namespace Rust { |
| 34 |
namespace Resolver { |
| 35 |
|
| 36 |
bool |
| 37 |
query_type (HirId reference, TyTy::BaseType **result) |
| 38 |
{ |
| 39 |
auto &mappings = Analysis::Mappings::get (); |
| 40 |
auto &resolver = *Resolver::get (); |
| 41 |
TypeCheckContext *context = TypeCheckContext::get (); |
| 42 |
|
| 43 |
if (context->query_in_progress (reference)) |
| 44 |
return false; |
| 45 |
|
| 46 |
if (context->lookup_type (reference, result)) |
| 47 |
return true; |
| 48 |
|
| 49 |
context->insert_query (reference); |
| 50 |
|
| 51 |
std::pair<HIR::Enum *, HIR::EnumItem *> enum_candidiate |
| 52 |
= mappings.lookup_hir_enumitem (reference); |
| 53 |
bool enum_candidiate_ok |
| 54 |
= enum_candidiate.first != nullptr && enum_candidiate.second != nullptr; |
| 55 |
if (enum_candidiate_ok) |
| 56 |
{ |
| 57 |
HIR::Enum *parent = enum_candidiate.first; |
| 58 |
HIR::EnumItem *enum_item = enum_candidiate.second; |
| 59 |
rust_debug_loc (enum_item->get_locus (), "resolved item {%u} to", |
| 60 |
reference); |
| 61 |
|
| 62 |
*result = TypeCheckItem::Resolve (*parent); |
| 63 |
|
| 64 |
context->query_completed (reference); |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
if (auto item = mappings.lookup_hir_item (reference)) |
| 69 |
{ |
| 70 |
rust_debug_loc (item.value ()->get_locus (), "resolved item {%u} to", |
| 71 |
reference); |
| 72 |
*result = TypeCheckItem::Resolve (*item.value ()); |
| 73 |
context->query_completed (reference); |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
if (auto impl_item = mappings.lookup_hir_implitem (reference)) |
| 78 |
{ |
| 79 |
auto impl_block |
| 80 |
= mappings.lookup_hir_impl_block (impl_item->second).value (); |
| 81 |
|
| 82 |
// found an impl item |
| 83 |
rust_debug_loc (impl_item->first->get_locus (), |
| 84 |
"resolved impl-item {%u} to", reference); |
| 85 |
|
| 86 |
*result = TypeCheckItem::ResolveImplItem (*impl_block, *impl_item->first); |
| 87 |
context->query_completed (reference); |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
// is it an impl_type? |
| 92 |
if (auto impl_block_by_type = mappings.lookup_impl_block_type (reference)) |
| 93 |
{ |
| 94 |
// found an impl item |
| 95 |
HIR::ImplBlock *impl = impl_block_by_type.value (); |
| 96 |
rust_debug_loc (impl->get_locus (), "resolved impl block type {%u} to", |
| 97 |
reference); |
| 98 |
|
| 99 |
// this could be recursive to the root type |
| 100 |
if (impl->has_type ()) |
| 101 |
{ |
| 102 |
HIR::Type &ty = impl->get_type (); |
| 103 |
NodeId ref_node_id = UNKNOWN_NODEID; |
| 104 |
NodeId ast_node_id = ty.get_mappings ().get_nodeid (); |
| 105 |
|
| 106 |
if (flag_name_resolution_2_0) |
| 107 |
{ |
| 108 |
auto &nr_ctx = Resolver2_0::ImmutableNameResolutionContext::get () |
| 109 |
.resolver (); |
| 110 |
|
| 111 |
// assign the ref_node_id if we've found something |
| 112 |
nr_ctx.lookup (ast_node_id) |
| 113 |
.map ( |
| 114 |
[&ref_node_id] (NodeId resolved) { ref_node_id = resolved; }); |
| 115 |
} |
| 116 |
else if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) |
| 117 |
resolver.lookup_resolved_type (ast_node_id, &ref_node_id); |
| 118 |
|
| 119 |
if (ref_node_id != UNKNOWN_NODEID) |
| 120 |
{ |
| 121 |
tl::optional<HirId> hid |
| 122 |
= mappings.lookup_node_to_hir (ref_node_id); |
| 123 |
if (hid.has_value () && context->query_in_progress (hid.value ())) |
| 124 |
{ |
| 125 |
context->query_completed (reference); |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
*result = TypeCheckItem::ResolveImplBlockSelf (*impl); |
| 132 |
context->query_completed (reference); |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
// is it an extern item? |
| 137 |
if (auto extern_item = mappings.lookup_hir_extern_item (reference)) |
| 138 |
{ |
| 139 |
auto block = mappings.lookup_hir_extern_block (extern_item->second); |
| 140 |
rust_assert (block.has_value ()); |
| 141 |
|
| 142 |
*result |
| 143 |
= TypeCheckTopLevelExternItem::Resolve (*extern_item.value ().first, |
| 144 |
*block.value ()); |
| 145 |
context->query_completed (reference); |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
// more? |
| 150 |
location_t possible_locus = mappings.lookup_location (reference); |
| 151 |
rust_debug_loc (possible_locus, "query system failed to resolve: [%u]", |
| 152 |
reference); |
| 153 |
context->query_completed (reference); |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
bool |
| 159 |
types_compatable (TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, |
| 160 |
location_t unify_locus, bool emit_errors) |
| 161 |
{ |
| 162 |
TyTy::BaseType *result |
| 163 |
= unify_site_and (UNKNOWN_HIRID, lhs, rhs, unify_locus, emit_errors, |
| 164 |
false /*commit*/, true /*infer*/, true /*cleanup*/); |
| 165 |
return result->get_kind () != TyTy::TypeKind::ERROR; |
| 166 |
} |
| 167 |
|
| 168 |
TyTy::BaseType * |
| 169 |
unify_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, |
| 170 |
location_t unify_locus) |
| 171 |
{ |
| 172 |
TyTy::BaseType *expected = lhs.get_ty (); |
| 173 |
TyTy::BaseType *expr = rhs.get_ty (); |
| 174 |
|
| 175 |
rust_debug ("unify_site id={%u} expected={%s} expr={%s}", id, |
| 176 |
expected->debug_str ().c_str (), expr->debug_str ().c_str ()); |
| 177 |
|
| 178 |
std::vector<UnifyRules::CommitSite> commits; |
| 179 |
std::vector<UnifyRules::InferenceSite> infers; |
| 180 |
return UnifyRules::Resolve (lhs, rhs, unify_locus, true /*commit*/, |
| 181 |
true /*emit_error*/, false /*infer*/, commits, |
| 182 |
infers); |
| 183 |
} |
| 184 |
|
| 185 |
TyTy::BaseType * |
| 186 |
unify_site_and (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, |
| 187 |
location_t unify_locus, bool emit_errors, bool commit_if_ok, |
| 188 |
bool implicit_infer_vars, bool cleanup) |
| 189 |
{ |
| 190 |
TypeCheckContext &context = *TypeCheckContext::get (); |
| 191 |
|
| 192 |
TyTy::BaseType *expected = lhs.get_ty (); |
| 193 |
TyTy::BaseType *expr = rhs.get_ty (); |
| 194 |
|
| 195 |
rust_debug ( |
| 196 |
"unify_site_and commit %s infer %s id={%u} expected={%s} expr={%s}", |
| 197 |
commit_if_ok ? "true" : "false", implicit_infer_vars ? "true" : "false", id, |
| 198 |
expected->debug_str ().c_str (), expr->debug_str ().c_str ()); |
| 199 |
|
| 200 |
std::vector<UnifyRules::CommitSite> commits; |
| 201 |
std::vector<UnifyRules::InferenceSite> infers; |
| 202 |
TyTy::BaseType *result |
| 203 |
= UnifyRules::Resolve (lhs, rhs, unify_locus, false /*commit inline*/, |
| 204 |
emit_errors, implicit_infer_vars, commits, infers); |
| 205 |
bool ok = result->get_kind () != TyTy::TypeKind::ERROR; |
| 206 |
if (ok && commit_if_ok) |
| 207 |
{ |
| 208 |
for (auto &c : commits) |
| 209 |
{ |
| 210 |
UnifyRules::commit (c.lhs, c.rhs, c.resolved); |
| 211 |
} |
| 212 |
} |
| 213 |
else if (cleanup) |
| 214 |
{ |
| 215 |
// FIXME |
| 216 |
// reset the get_next_hir_id |
| 217 |
|
| 218 |
for (auto &i : infers) |
| 219 |
{ |
| 220 |
i.param->set_ref (i.pref); |
| 221 |
i.param->set_ty_ref (i.ptyref); |
| 222 |
|
| 223 |
// remove the inference variable |
| 224 |
context.clear_type (i.infer); |
| 225 |
delete i.infer; |
| 226 |
} |
| 227 |
} |
| 228 |
return result; |
| 229 |
} |
| 230 |
|
| 231 |
TyTy::BaseType * |
| 232 |
coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, |
| 233 |
location_t locus) |
| 234 |
{ |
| 235 |
TyTy::BaseType *expected = lhs.get_ty (); |
| 236 |
TyTy::BaseType *expr = rhs.get_ty (); |
| 237 |
|
| 238 |
rust_debug ("coercion_site id={%u} expected={%s} expr={%s}", id, |
| 239 |
expected->debug_str ().c_str (), expr->debug_str ().c_str ()); |
| 240 |
|
| 241 |
auto context = TypeCheckContext::get (); |
| 242 |
if (expected->get_kind () == TyTy::TypeKind::ERROR |
| 243 |
|| expr->get_kind () == TyTy::TypeKind::ERROR) |
| 244 |
return expr; |
| 245 |
|
| 246 |
// can we autoderef it? |
| 247 |
auto result = TypeCoercionRules::Coerce (expr, expected, locus, |
| 248 |
true /*allow-autodref*/); |
| 249 |
|
| 250 |
// the result needs to be unified |
| 251 |
TyTy::BaseType *receiver = expr; |
| 252 |
if (!result.is_error ()) |
| 253 |
{ |
| 254 |
receiver = result.tyty; |
| 255 |
} |
| 256 |
|
| 257 |
rust_debug ("coerce_default_unify(a={%s}, b={%s})", |
| 258 |
receiver->debug_str ().c_str (), expected->debug_str ().c_str ()); |
| 259 |
TyTy::BaseType *coerced |
| 260 |
= unify_site_and (id, lhs, |
| 261 |
TyTy::TyWithLocation (receiver, rhs.get_locus ()), locus, |
| 262 |
true /*emit_error*/, true /*commit*/, true /*infer*/, |
| 263 |
true /*cleanup*/); |
| 264 |
context->insert_autoderef_mappings (id, std::move (result.adjustments)); |
| 265 |
return coerced; |
| 266 |
} |
| 267 |
|
| 268 |
TyTy::BaseType * |
| 269 |
try_coercion (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, |
| 270 |
location_t locus) |
| 271 |
{ |
| 272 |
TyTy::BaseType *expected = lhs.get_ty (); |
| 273 |
TyTy::BaseType *expr = rhs.get_ty (); |
| 274 |
|
| 275 |
rust_debug ("try_coercion_site id={%u} expected={%s} expr={%s}", id, |
| 276 |
expected->debug_str ().c_str (), expr->debug_str ().c_str ()); |
| 277 |
|
| 278 |
auto result = TypeCoercionRules::TryCoerce (expr, expected, locus, |
| 279 |
true /*allow-autodref*/); |
| 280 |
if (result.is_error ()) |
| 281 |
return new TyTy::ErrorType (id); |
| 282 |
|
| 283 |
return result.tyty; |
| 284 |
} |
| 285 |
|
| 286 |
TyTy::BaseType * |
| 287 |
cast_site (HirId id, TyTy::TyWithLocation from, TyTy::TyWithLocation to, |
| 288 |
location_t cast_locus) |
| 289 |
{ |
| 290 |
rust_debug ("cast_site id={%u} from={%s} to={%s}", id, |
| 291 |
from.get_ty ()->debug_str ().c_str (), |
| 292 |
to.get_ty ()->debug_str ().c_str ()); |
| 293 |
|
| 294 |
auto context = TypeCheckContext::get (); |
| 295 |
if (from.get_ty ()->get_kind () == TyTy::TypeKind::ERROR |
| 296 |
|| to.get_ty ()->get_kind () == TyTy::TypeKind::ERROR) |
| 297 |
return to.get_ty (); |
| 298 |
|
| 299 |
// do the cast |
| 300 |
auto result = TypeCastRules::resolve (cast_locus, from, to); |
| 301 |
|
| 302 |
// we assume error has already been emitted |
| 303 |
if (result.is_error ()) |
| 304 |
return to.get_ty (); |
| 305 |
|
| 306 |
// the result needs to be unified |
| 307 |
TyTy::BaseType *casted_result = result.tyty; |
| 308 |
rust_debug ("cast_default_unify(a={%s}, b={%s})", |
| 309 |
casted_result->debug_str ().c_str (), |
| 310 |
to.get_ty ()->debug_str ().c_str ()); |
| 311 |
|
| 312 |
TyTy::BaseType *casted |
| 313 |
= unify_site (id, to, |
| 314 |
TyTy::TyWithLocation (casted_result, from.get_locus ()), |
| 315 |
cast_locus); |
| 316 |
context->insert_cast_autoderef_mappings (id, std::move (result.adjustments)); |
| 317 |
return casted; |
| 318 |
} |
| 319 |
|
| 320 |
AssociatedImplTrait * |
| 321 |
lookup_associated_impl_block (const TyTy::TypeBoundPredicate &bound, |
| 322 |
const TyTy::BaseType *binding, bool *ambigious) |
| 323 |
{ |
| 324 |
auto context = TypeCheckContext::get (); |
| 325 |
|
| 326 |
// setup any associated type mappings for the specified bonds and this |
| 327 |
// type |
| 328 |
auto candidates = TypeBoundsProbe::Probe (binding); |
| 329 |
std::vector<AssociatedImplTrait *> associated_impl_traits; |
| 330 |
for (auto &probed_bound : candidates) |
| 331 |
{ |
| 332 |
HIR::ImplBlock *associated_impl = probed_bound.second; |
| 333 |
|
| 334 |
HirId impl_block_id = associated_impl->get_mappings ().get_hirid (); |
| 335 |
AssociatedImplTrait *associated = nullptr; |
| 336 |
bool found_impl_trait |
| 337 |
= context->lookup_associated_trait_impl (impl_block_id, &associated); |
| 338 |
if (found_impl_trait) |
| 339 |
{ |
| 340 |
// compare the bounds from here i think is what we can do: |
| 341 |
if (bound.is_equal (associated->get_predicate ())) |
| 342 |
{ |
| 343 |
associated_impl_traits.push_back (associated); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if (associated_impl_traits.empty ()) |
| 349 |
return nullptr; |
| 350 |
|
| 351 |
// This code is important when you look at slices for example when |
| 352 |
// you have a slice such as: |
| 353 |
// |
| 354 |
// let slice = &array[1..3] |
| 355 |
// |
| 356 |
// the higher ranked bounds will end up having an Index trait |
| 357 |
// implementation for Range<usize> so we need this code to resolve |
| 358 |
// that we have an integer inference variable that needs to become |
| 359 |
// a usize |
| 360 |
// |
| 361 |
// The other complicated issue is that we might have an intrinsic |
| 362 |
// which requires the :Clone or Copy bound but the libcore adds |
| 363 |
// implementations for all the integral types so when there are |
| 364 |
// multiple candidates we need to resolve to the default |
| 365 |
// implementation for that type otherwise its an error for |
| 366 |
// ambiguous type bounds |
| 367 |
|
| 368 |
// if we have a non-general inference variable we need to be |
| 369 |
// careful about the selection here |
| 370 |
bool is_infer_var = binding->get_kind () == TyTy::TypeKind::INFER; |
| 371 |
bool is_integer_infervar |
| 372 |
= is_infer_var |
| 373 |
&& static_cast<const TyTy::InferType *> (binding)->get_infer_kind () |
| 374 |
== TyTy::InferType::InferTypeKind::INTEGRAL; |
| 375 |
bool is_float_infervar |
| 376 |
= is_infer_var |
| 377 |
&& static_cast<const TyTy::InferType *> (binding)->get_infer_kind () |
| 378 |
== TyTy::InferType::InferTypeKind::FLOAT; |
| 379 |
|
| 380 |
AssociatedImplTrait *associate_impl_trait = nullptr; |
| 381 |
if (associated_impl_traits.size () == 1) |
| 382 |
{ |
| 383 |
// just go for it |
| 384 |
associate_impl_trait = associated_impl_traits.at (0); |
| 385 |
} |
| 386 |
else if (is_integer_infervar) |
| 387 |
{ |
| 388 |
TyTy::BaseType *type = nullptr; |
| 389 |
bool ok = context->lookup_builtin ("i32", &type); |
| 390 |
rust_assert (ok); |
| 391 |
|
| 392 |
for (auto &impl : associated_impl_traits) |
| 393 |
{ |
| 394 |
bool found = impl->get_self ()->is_equal (*type); |
| 395 |
if (found) |
| 396 |
{ |
| 397 |
associate_impl_trait = impl; |
| 398 |
break; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
else if (is_float_infervar) |
| 403 |
{ |
| 404 |
TyTy::BaseType *type = nullptr; |
| 405 |
bool ok = context->lookup_builtin ("f64", &type); |
| 406 |
rust_assert (ok); |
| 407 |
|
| 408 |
for (auto &impl : associated_impl_traits) |
| 409 |
{ |
| 410 |
bool found = impl->get_self ()->is_equal (*type); |
| 411 |
if (found) |
| 412 |
{ |
| 413 |
associate_impl_trait = impl; |
| 414 |
break; |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
if (associate_impl_trait == nullptr && ambigious != nullptr) |
| 420 |
{ |
| 421 |
*ambigious = true; |
| 422 |
} |
| 423 |
|
| 424 |
return associate_impl_trait; |
| 425 |
} |
| 426 |
|
| 427 |
} // namespace Resolver |
| 428 |
} // namespace Rust |