| 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 "optional.h" |
| 20 |
#include "rust-hir-expr.h" |
| 21 |
#include "rust-system.h" |
| 22 |
#include "rust-tyty-call.h" |
| 23 |
#include "rust-hir-type-check-struct-field.h" |
| 24 |
#include "rust-hir-path-probe.h" |
| 25 |
#include "rust-substitution-mapper.h" |
| 26 |
#include "rust-hir-trait-resolve.h" |
| 27 |
#include "rust-hir-dot-operator.h" |
| 28 |
#include "rust-hir-type-check-pattern.h" |
| 29 |
#include "rust-hir-type-check-expr.h" |
| 30 |
#include "rust-hir-type-check-stmt.h" |
| 31 |
#include "rust-hir-type-check-item.h" |
| 32 |
#include "rust-type-util.h" |
| 33 |
#include "rust-immutable-name-resolution-context.h" |
| 34 |
|
| 35 |
// for flag_name_resolution_2_0 |
| 36 |
#include "options.h" |
| 37 |
|
| 38 |
namespace Rust { |
| 39 |
namespace Resolver { |
| 40 |
|
| 41 |
TypeCheckExpr::TypeCheckExpr () : TypeCheckBase (), infered (nullptr) {} |
| 42 |
|
| 43 |
// Perform type checking on expr. Also runs type unification algorithm. |
| 44 |
// Returns the unified type of expr |
| 45 |
TyTy::BaseType * |
| 46 |
TypeCheckExpr::Resolve (HIR::Expr &expr) |
| 47 |
{ |
| 48 |
TypeCheckExpr resolver; |
| 49 |
expr.accept_vis (resolver); |
| 50 |
|
| 51 |
if (resolver.infered == nullptr) |
| 52 |
return new TyTy::ErrorType (expr.get_mappings ().get_hirid ()); |
| 53 |
|
| 54 |
auto ref = expr.get_mappings ().get_hirid (); |
| 55 |
resolver.infered->set_ref (ref); |
| 56 |
resolver.context->insert_type (expr.get_mappings (), resolver.infered); |
| 57 |
|
| 58 |
return resolver.infered; |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
TypeCheckExpr::visit (HIR::TupleIndexExpr &expr) |
| 63 |
{ |
| 64 |
auto resolved = TypeCheckExpr::Resolve (expr.get_tuple_expr ()); |
| 65 |
if (resolved->get_kind () == TyTy::TypeKind::ERROR) |
| 66 |
{ |
| 67 |
rust_error_at (expr.get_tuple_expr ().get_locus (), |
| 68 |
"failed to resolve TupleIndexExpr receiver"); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// FIXME does this require autoderef here? |
| 73 |
if (resolved->get_kind () == TyTy::TypeKind::REF) |
| 74 |
{ |
| 75 |
TyTy::ReferenceType *r = static_cast<TyTy::ReferenceType *> (resolved); |
| 76 |
resolved = r->get_base (); |
| 77 |
} |
| 78 |
|
| 79 |
bool is_valid_type = resolved->get_kind () == TyTy::TypeKind::ADT |
| 80 |
|| resolved->get_kind () == TyTy::TypeKind::TUPLE; |
| 81 |
if (!is_valid_type) |
| 82 |
{ |
| 83 |
rust_error_at (expr.get_tuple_expr ().get_locus (), |
| 84 |
"Expected Tuple or ADT got: %s", |
| 85 |
resolved->as_string ().c_str ()); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if (resolved->get_kind () == TyTy::TypeKind::TUPLE) |
| 90 |
{ |
| 91 |
TyTy::TupleType *tuple = static_cast<TyTy::TupleType *> (resolved); |
| 92 |
TupleIndex index = expr.get_tuple_index (); |
| 93 |
if ((size_t) index >= tuple->num_fields ()) |
| 94 |
{ |
| 95 |
rust_error_at (expr.get_locus (), ErrorCode::E0609, |
| 96 |
"no field %qi on type %qs", index, |
| 97 |
resolved->get_name ().c_str ()); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
auto field_tyty = tuple->get_field ((size_t) index); |
| 102 |
if (field_tyty == nullptr) |
| 103 |
{ |
| 104 |
rust_error_at (expr.get_locus (), |
| 105 |
"failed to lookup field type at index %i", index); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
infered = field_tyty; |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (resolved); |
| 114 |
rust_assert (!adt->is_enum ()); |
| 115 |
rust_assert (adt->number_of_variants () == 1); |
| 116 |
|
| 117 |
TyTy::VariantDef *variant = adt->get_variants ().at (0); |
| 118 |
TupleIndex index = expr.get_tuple_index (); |
| 119 |
if ((size_t) index >= variant->num_fields ()) |
| 120 |
{ |
| 121 |
rust_error_at (expr.get_locus (), "unknown field at index %i", index); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
auto field_tyty = variant->get_field_at_index ((size_t) index); |
| 126 |
if (field_tyty == nullptr) |
| 127 |
{ |
| 128 |
rust_error_at (expr.get_locus (), |
| 129 |
"failed to lookup field type at index %i", index); |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
infered = field_tyty->get_field_type (); |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
TypeCheckExpr::visit (HIR::TupleExpr &expr) |
| 138 |
{ |
| 139 |
if (expr.is_unit ()) |
| 140 |
{ |
| 141 |
infered = TyTy::TupleType::get_unit_type (); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
std::vector<TyTy::TyVar> fields; |
| 146 |
for (auto &elem : expr.get_tuple_elems ()) |
| 147 |
{ |
| 148 |
auto field_ty = TypeCheckExpr::Resolve (*elem); |
| 149 |
fields.push_back (TyTy::TyVar (field_ty->get_ref ())); |
| 150 |
} |
| 151 |
infered = new TyTy::TupleType (expr.get_mappings ().get_hirid (), |
| 152 |
expr.get_locus (), fields); |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
TypeCheckExpr::visit (HIR::ReturnExpr &expr) |
| 157 |
{ |
| 158 |
if (!context->have_function_context ()) |
| 159 |
{ |
| 160 |
rust_error_at (expr.get_locus (), ErrorCode::E0572, |
| 161 |
"return statement outside of function body"); |
| 162 |
infered = new TyTy::ErrorType (expr.get_mappings ().get_hirid ()); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
auto fn_return_tyty = context->peek_return_type (); |
| 167 |
location_t expr_locus = expr.has_return_expr () |
| 168 |
? expr.get_expr ().get_locus () |
| 169 |
: expr.get_locus (); |
| 170 |
|
| 171 |
TyTy::BaseType *expr_ty = expr.has_return_expr () |
| 172 |
? TypeCheckExpr::Resolve (expr.get_expr ()) |
| 173 |
: TyTy::TupleType::get_unit_type (); |
| 174 |
|
| 175 |
coercion_site (expr.get_mappings ().get_hirid (), |
| 176 |
TyTy::TyWithLocation (fn_return_tyty), |
| 177 |
TyTy::TyWithLocation (expr_ty, expr_locus), expr.get_locus ()); |
| 178 |
|
| 179 |
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ()); |
| 180 |
} |
| 181 |
|
| 182 |
void |
| 183 |
TypeCheckExpr::visit (HIR::CallExpr &expr) |
| 184 |
{ |
| 185 |
TyTy::BaseType *function_tyty = TypeCheckExpr::Resolve (expr.get_fnexpr ()); |
| 186 |
|
| 187 |
rust_debug_loc (expr.get_locus (), "resolved_call_expr to: {%s}", |
| 188 |
function_tyty->get_name ().c_str ()); |
| 189 |
|
| 190 |
TyTy::VariantDef &variant = TyTy::VariantDef::get_error_node (); |
| 191 |
if (function_tyty->get_kind () == TyTy::TypeKind::ADT) |
| 192 |
{ |
| 193 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (function_tyty); |
| 194 |
if (adt->is_enum ()) |
| 195 |
{ |
| 196 |
// lookup variant id |
| 197 |
HirId variant_id; |
| 198 |
bool ok = context->lookup_variant_definition ( |
| 199 |
expr.get_fnexpr ().get_mappings ().get_hirid (), &variant_id); |
| 200 |
|
| 201 |
if (!ok) |
| 202 |
{ |
| 203 |
rust_error_at (expr.get_locus (), ErrorCode::E0423, |
| 204 |
"expected function, tuple struct or tuple " |
| 205 |
"variant, found enum"); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
TyTy::VariantDef *lookup_variant = nullptr; |
| 210 |
ok = adt->lookup_variant_by_id (variant_id, &lookup_variant); |
| 211 |
rust_assert (ok); |
| 212 |
|
| 213 |
variant = std::move (*lookup_variant->clone ()); |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
rust_assert (adt->number_of_variants () == 1); |
| 218 |
variant = std::move (*adt->get_variants ().at (0)->clone ()); |
| 219 |
} |
| 220 |
infered |
| 221 |
= TyTy::TypeCheckCallExpr::go (function_tyty, expr, variant, context); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
bool resolved_fn_trait_call |
| 226 |
= resolve_fn_trait_call (expr, function_tyty, &infered); |
| 227 |
if (resolved_fn_trait_call) |
| 228 |
return; |
| 229 |
|
| 230 |
bool valid_tyty |
| 231 |
= function_tyty->is<TyTy::FnType> () || function_tyty->is<TyTy::FnPtr> (); |
| 232 |
if (!valid_tyty) |
| 233 |
{ |
| 234 |
bool emit_error = !function_tyty->is<TyTy::ErrorType> (); |
| 235 |
if (emit_error) |
| 236 |
{ |
| 237 |
rich_location r (line_table, expr.get_locus ()); |
| 238 |
rust_error_at (r, ErrorCode::E0618, "expected function, found %<%s%>", |
| 239 |
function_tyty->get_name ().c_str ()); |
| 240 |
} |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
infered = TyTy::TypeCheckCallExpr::go (function_tyty, expr, variant, context); |
| 245 |
|
| 246 |
auto discriminant_type_lookup |
| 247 |
= mappings.lookup_lang_item (LangItem::Kind::DISCRIMINANT_TYPE); |
| 248 |
if (infered->is<TyTy::PlaceholderType> () && discriminant_type_lookup) |
| 249 |
{ |
| 250 |
const auto &p = *static_cast<const TyTy::PlaceholderType *> (infered); |
| 251 |
if (p.get_def_id () == discriminant_type_lookup.value ()) |
| 252 |
{ |
| 253 |
// this is a special case where this will actually return the repr of |
| 254 |
// the enum. We dont currently support repr on enum yet to change the |
| 255 |
// discriminant type but the default is always isize. We need to |
| 256 |
// assert this is a generic function with one param |
| 257 |
// |
| 258 |
// fn<BookFormat> (v & T=BookFormat{Paperback) -> <placeholder:> |
| 259 |
// |
| 260 |
// note the default is isize |
| 261 |
|
| 262 |
bool ok = context->lookup_builtin ("isize", &infered); |
| 263 |
rust_assert (ok); |
| 264 |
|
| 265 |
rust_assert (function_tyty->is<TyTy::FnType> ()); |
| 266 |
auto &fn = *static_cast<TyTy::FnType *> (function_tyty); |
| 267 |
rust_assert (fn.has_substitutions ()); |
| 268 |
rust_assert (fn.get_num_type_params () == 1); |
| 269 |
auto &mapping = fn.get_substs ().at (0); |
| 270 |
auto param_ty = mapping.get_param_ty (); |
| 271 |
|
| 272 |
if (!param_ty->can_resolve ()) |
| 273 |
{ |
| 274 |
// this could be a valid error need to test more weird cases and |
| 275 |
// look at rustc |
| 276 |
rust_internal_error_at (expr.get_locus (), |
| 277 |
"something wrong computing return type"); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
auto resolved = param_ty->resolve (); |
| 282 |
bool is_adt = resolved->is<TyTy::ADTType> (); |
| 283 |
if (is_adt) |
| 284 |
{ |
| 285 |
const auto &adt = *static_cast<TyTy::ADTType *> (resolved); |
| 286 |
infered = adt.get_repr_options ().repr; |
| 287 |
rust_assert (infered != nullptr); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
void |
| 294 |
TypeCheckExpr::visit (HIR::AssignmentExpr &expr) |
| 295 |
{ |
| 296 |
infered = TyTy::TupleType::get_unit_type (); |
| 297 |
|
| 298 |
auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); |
| 299 |
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); |
| 300 |
|
| 301 |
coercion_site (expr.get_mappings ().get_hirid (), |
| 302 |
TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), |
| 303 |
TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), |
| 304 |
expr.get_locus ()); |
| 305 |
} |
| 306 |
|
| 307 |
void |
| 308 |
TypeCheckExpr::visit (HIR::CompoundAssignmentExpr &expr) |
| 309 |
{ |
| 310 |
infered = TyTy::TupleType::get_unit_type (); |
| 311 |
|
| 312 |
auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); |
| 313 |
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); |
| 314 |
|
| 315 |
// we dont care about the result of the unify from a compound assignment |
| 316 |
// since this is a unit-type expr |
| 317 |
coercion_site (expr.get_mappings ().get_hirid (), |
| 318 |
TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), |
| 319 |
TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), |
| 320 |
expr.get_locus ()); |
| 321 |
|
| 322 |
auto lang_item_type |
| 323 |
= LangItem::CompoundAssignmentOperatorToLangItem (expr.get_expr_type ()); |
| 324 |
bool operator_overloaded |
| 325 |
= resolve_operator_overload (lang_item_type, expr, lhs, rhs); |
| 326 |
if (operator_overloaded) |
| 327 |
return; |
| 328 |
|
| 329 |
bool valid_lhs = validate_arithmetic_type (lhs, expr.get_expr_type ()); |
| 330 |
bool valid_rhs = validate_arithmetic_type (rhs, expr.get_expr_type ()); |
| 331 |
bool valid = valid_lhs && valid_rhs; |
| 332 |
if (!valid) |
| 333 |
{ |
| 334 |
rust_error_at (expr.get_locus (), |
| 335 |
"cannot apply operator %qs to types %s and %s", |
| 336 |
expr.get_operator_str ().c_str (), |
| 337 |
lhs->as_string ().c_str (), rhs->as_string ().c_str ()); |
| 338 |
return; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void |
| 343 |
TypeCheckExpr::visit (HIR::LiteralExpr &expr) |
| 344 |
{ |
| 345 |
infered = resolve_literal (expr.get_mappings (), expr.get_literal (), |
| 346 |
expr.get_locus ()); |
| 347 |
} |
| 348 |
|
| 349 |
void |
| 350 |
TypeCheckExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) |
| 351 |
{ |
| 352 |
auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); |
| 353 |
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); |
| 354 |
|
| 355 |
auto lang_item_type = LangItem::OperatorToLangItem (expr.get_expr_type ()); |
| 356 |
bool operator_overloaded |
| 357 |
= resolve_operator_overload (lang_item_type, expr, lhs, rhs); |
| 358 |
if (operator_overloaded) |
| 359 |
return; |
| 360 |
|
| 361 |
bool valid_lhs = validate_arithmetic_type (lhs, expr.get_expr_type ()); |
| 362 |
bool valid_rhs = validate_arithmetic_type (rhs, expr.get_expr_type ()); |
| 363 |
bool valid = valid_lhs && valid_rhs; |
| 364 |
if (!valid) |
| 365 |
{ |
| 366 |
rust_error_at (expr.get_locus (), |
| 367 |
"cannot apply operator %qs to types %s and %s", |
| 368 |
expr.get_operator_str ().c_str (), |
| 369 |
lhs->as_string ().c_str (), rhs->as_string ().c_str ()); |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
switch (expr.get_expr_type ()) |
| 374 |
{ |
| 375 |
case ArithmeticOrLogicalOperator::LEFT_SHIFT: |
| 376 |
case ArithmeticOrLogicalOperator::RIGHT_SHIFT: { |
| 377 |
TyTy::TyWithLocation from (rhs, expr.get_rhs ().get_locus ()); |
| 378 |
TyTy::TyWithLocation to (lhs, expr.get_lhs ().get_locus ()); |
| 379 |
infered = cast_site (expr.get_mappings ().get_hirid (), from, to, |
| 380 |
expr.get_locus ()); |
| 381 |
} |
| 382 |
break; |
| 383 |
|
| 384 |
default: { |
| 385 |
infered = unify_site ( |
| 386 |
expr.get_mappings ().get_hirid (), |
| 387 |
TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), |
| 388 |
TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), |
| 389 |
expr.get_locus ()); |
| 390 |
} |
| 391 |
break; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
void |
| 396 |
TypeCheckExpr::visit (HIR::ComparisonExpr &expr) |
| 397 |
{ |
| 398 |
auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); |
| 399 |
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); |
| 400 |
|
| 401 |
auto borrowed_rhs |
| 402 |
= new TyTy::ReferenceType (mappings.get_next_hir_id (), |
| 403 |
TyTy::TyVar (rhs->get_ref ()), Mutability::Imm); |
| 404 |
context->insert_implicit_type (borrowed_rhs->get_ref (), borrowed_rhs); |
| 405 |
|
| 406 |
auto seg_name = LangItem::ComparisonToSegment (expr.get_expr_type ()); |
| 407 |
auto segment = HIR::PathIdentSegment (seg_name); |
| 408 |
auto lang_item_type = LangItem::ComparisonToLangItem (expr.get_expr_type ()); |
| 409 |
|
| 410 |
bool operator_overloaded |
| 411 |
= resolve_operator_overload (lang_item_type, expr, lhs, borrowed_rhs, |
| 412 |
segment); |
| 413 |
if (operator_overloaded) |
| 414 |
return; |
| 415 |
|
| 416 |
unify_site (expr.get_mappings ().get_hirid (), |
| 417 |
TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), |
| 418 |
TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), |
| 419 |
expr.get_locus ()); |
| 420 |
|
| 421 |
bool ok = context->lookup_builtin ("bool", &infered); |
| 422 |
rust_assert (ok); |
| 423 |
} |
| 424 |
|
| 425 |
void |
| 426 |
TypeCheckExpr::visit (HIR::LazyBooleanExpr &expr) |
| 427 |
{ |
| 428 |
auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); |
| 429 |
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); |
| 430 |
|
| 431 |
// we expect the lhs and rhs must be bools at this point |
| 432 |
TyTy::BaseType *boolean_node = nullptr; |
| 433 |
bool ok = context->lookup_builtin ("bool", &boolean_node); |
| 434 |
rust_assert (ok); |
| 435 |
|
| 436 |
// verify the lhs and rhs before unifying together |
| 437 |
lhs = unify_site (expr.get_mappings ().get_hirid (), |
| 438 |
TyTy::TyWithLocation (boolean_node, |
| 439 |
expr.get_lhs ().get_locus ()), |
| 440 |
TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), |
| 441 |
expr.get_locus ()); |
| 442 |
|
| 443 |
rhs = unify_site (expr.get_mappings ().get_hirid (), |
| 444 |
TyTy::TyWithLocation (boolean_node, |
| 445 |
expr.get_rhs ().get_locus ()), |
| 446 |
TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), |
| 447 |
expr.get_locus ()); |
| 448 |
|
| 449 |
infered |
| 450 |
= unify_site (expr.get_mappings ().get_hirid (), |
| 451 |
TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), |
| 452 |
TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), |
| 453 |
expr.get_locus ()); |
| 454 |
} |
| 455 |
|
| 456 |
void |
| 457 |
TypeCheckExpr::visit (HIR::NegationExpr &expr) |
| 458 |
{ |
| 459 |
auto negated_expr_ty = TypeCheckExpr::Resolve (expr.get_expr ()); |
| 460 |
|
| 461 |
// check for operator overload |
| 462 |
auto lang_item_type |
| 463 |
= LangItem::NegationOperatorToLangItem (expr.get_expr_type ()); |
| 464 |
bool operator_overloaded |
| 465 |
= resolve_operator_overload (lang_item_type, expr, negated_expr_ty, |
| 466 |
nullptr); |
| 467 |
if (operator_overloaded) |
| 468 |
return; |
| 469 |
|
| 470 |
// https://doc.rust-lang.org/reference/expressions/operator-expr.html#negation-operators |
| 471 |
switch (expr.get_expr_type ()) |
| 472 |
{ |
| 473 |
case NegationOperator::NEGATE: { |
| 474 |
bool valid |
| 475 |
= (negated_expr_ty->get_kind () == TyTy::TypeKind::INT) |
| 476 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::UINT) |
| 477 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::FLOAT) |
| 478 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::ISIZE) |
| 479 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::USIZE) |
| 480 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::INFER |
| 481 |
&& (((TyTy::InferType *) negated_expr_ty)->get_infer_kind () |
| 482 |
== TyTy::InferType::INTEGRAL)) |
| 483 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::INFER |
| 484 |
&& (((TyTy::InferType *) negated_expr_ty)->get_infer_kind () |
| 485 |
== TyTy::InferType::FLOAT)); |
| 486 |
if (!valid) |
| 487 |
{ |
| 488 |
rust_error_at (expr.get_locus (), "cannot apply unary - to %s", |
| 489 |
negated_expr_ty->as_string ().c_str ()); |
| 490 |
return; |
| 491 |
} |
| 492 |
} |
| 493 |
break; |
| 494 |
|
| 495 |
case NegationOperator::NOT: { |
| 496 |
bool valid |
| 497 |
= (negated_expr_ty->get_kind () == TyTy::TypeKind::BOOL) |
| 498 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::INT) |
| 499 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::UINT) |
| 500 |
|| (negated_expr_ty->get_kind () == TyTy::TypeKind::INFER |
| 501 |
&& (((TyTy::InferType *) negated_expr_ty)->get_infer_kind () |
| 502 |
== TyTy::InferType::INTEGRAL)); |
| 503 |
if (!valid) |
| 504 |
{ |
| 505 |
rust_error_at (expr.get_locus (), "cannot apply unary %<!%> to %s", |
| 506 |
negated_expr_ty->as_string ().c_str ()); |
| 507 |
return; |
| 508 |
} |
| 509 |
} |
| 510 |
break; |
| 511 |
} |
| 512 |
|
| 513 |
infered = negated_expr_ty->clone (); |
| 514 |
infered->append_reference (negated_expr_ty->get_ref ()); |
| 515 |
} |
| 516 |
|
| 517 |
void |
| 518 |
TypeCheckExpr::visit (HIR::IfExpr &expr) |
| 519 |
{ |
| 520 |
TyTy::BaseType *bool_ty = nullptr; |
| 521 |
bool ok = context->lookup_builtin ("bool", &bool_ty); |
| 522 |
rust_assert (ok); |
| 523 |
|
| 524 |
TyTy::BaseType *cond_type = TypeCheckExpr::Resolve (expr.get_if_condition ()); |
| 525 |
|
| 526 |
unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (bool_ty), |
| 527 |
TyTy::TyWithLocation (cond_type, |
| 528 |
expr.get_if_condition ().get_locus ()), |
| 529 |
expr.get_locus ()); |
| 530 |
|
| 531 |
TyTy::BaseType *block_type = TypeCheckExpr::Resolve (expr.get_if_block ()); |
| 532 |
|
| 533 |
TyTy::BaseType *unit_ty = nullptr; |
| 534 |
ok = context->lookup_builtin ("()", &unit_ty); |
| 535 |
rust_assert (ok); |
| 536 |
|
| 537 |
infered |
| 538 |
= coercion_site (expr.get_mappings ().get_hirid (), |
| 539 |
TyTy::TyWithLocation (unit_ty), |
| 540 |
TyTy::TyWithLocation (block_type, |
| 541 |
expr.get_if_block ().get_locus ()), |
| 542 |
expr.get_locus ()); |
| 543 |
} |
| 544 |
|
| 545 |
void |
| 546 |
TypeCheckExpr::visit (HIR::IfExprConseqElse &expr) |
| 547 |
{ |
| 548 |
TyTy::BaseType *bool_ty = nullptr; |
| 549 |
bool ok = context->lookup_builtin ("bool", &bool_ty); |
| 550 |
rust_assert (ok); |
| 551 |
|
| 552 |
TyTy::BaseType *cond_type = TypeCheckExpr::Resolve (expr.get_if_condition ()); |
| 553 |
|
| 554 |
unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (bool_ty), |
| 555 |
TyTy::TyWithLocation (cond_type, |
| 556 |
expr.get_if_condition ().get_locus ()), |
| 557 |
expr.get_locus ()); |
| 558 |
|
| 559 |
auto if_blk_resolved = TypeCheckExpr::Resolve (expr.get_if_block ()); |
| 560 |
auto else_blk_resolved = TypeCheckExpr::Resolve (expr.get_else_block ()); |
| 561 |
|
| 562 |
if (if_blk_resolved->get_kind () == TyTy::NEVER) |
| 563 |
infered = else_blk_resolved; |
| 564 |
else if (else_blk_resolved->get_kind () == TyTy::NEVER) |
| 565 |
infered = if_blk_resolved; |
| 566 |
else |
| 567 |
{ |
| 568 |
infered |
| 569 |
= unify_site (expr.get_mappings ().get_hirid (), |
| 570 |
TyTy::TyWithLocation (if_blk_resolved, |
| 571 |
expr.get_if_block ().get_locus ()), |
| 572 |
TyTy::TyWithLocation ( |
| 573 |
else_blk_resolved, expr.get_else_block ().get_locus ()), |
| 574 |
expr.get_locus ()); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
void |
| 579 |
TypeCheckExpr::visit (HIR::UnsafeBlockExpr &expr) |
| 580 |
{ |
| 581 |
infered = TypeCheckExpr::Resolve (expr.get_block_expr ()); |
| 582 |
} |
| 583 |
|
| 584 |
void |
| 585 |
TypeCheckExpr::visit (HIR::BlockExpr &expr) |
| 586 |
{ |
| 587 |
if (expr.has_label ()) |
| 588 |
context->push_new_loop_context (expr.get_mappings ().get_hirid (), |
| 589 |
expr.get_locus ()); |
| 590 |
|
| 591 |
for (auto &s : expr.get_statements ()) |
| 592 |
{ |
| 593 |
if (!s->is_item ()) |
| 594 |
continue; |
| 595 |
|
| 596 |
TypeCheckStmt::Resolve (*s); |
| 597 |
} |
| 598 |
|
| 599 |
for (auto &s : expr.get_statements ()) |
| 600 |
{ |
| 601 |
if (s->is_item ()) |
| 602 |
continue; |
| 603 |
|
| 604 |
auto resolved = TypeCheckStmt::Resolve (*s); |
| 605 |
if (resolved == nullptr) |
| 606 |
{ |
| 607 |
rust_error_at (s->get_locus (), "failure to resolve type"); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
if (s->is_unit_check_needed () && !resolved->is_unit ()) |
| 612 |
{ |
| 613 |
auto unit = TyTy::TupleType::get_unit_type (); |
| 614 |
resolved |
| 615 |
= unify_site (s->get_mappings ().get_hirid (), |
| 616 |
TyTy::TyWithLocation (unit), |
| 617 |
TyTy::TyWithLocation (resolved), s->get_locus ()); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
if (expr.has_expr ()) |
| 622 |
infered = TypeCheckExpr::Resolve (expr.get_final_expr ())->clone (); |
| 623 |
else if (expr.is_tail_reachable ()) |
| 624 |
infered = TyTy::TupleType::get_unit_type (); |
| 625 |
else if (expr.has_label ()) |
| 626 |
{ |
| 627 |
TyTy::BaseType *loop_context_type = context->pop_loop_context (); |
| 628 |
|
| 629 |
bool loop_context_type_infered |
| 630 |
= (loop_context_type->get_kind () != TyTy::TypeKind::INFER) |
| 631 |
|| ((loop_context_type->get_kind () == TyTy::TypeKind::INFER) |
| 632 |
&& (((TyTy::InferType *) loop_context_type)->get_infer_kind () |
| 633 |
!= TyTy::InferType::GENERAL)); |
| 634 |
|
| 635 |
infered = loop_context_type_infered ? loop_context_type |
| 636 |
: TyTy::TupleType::get_unit_type (); |
| 637 |
} |
| 638 |
else |
| 639 |
{ |
| 640 |
// FIXME this seems wrong |
| 641 |
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ()); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
void |
| 646 |
TypeCheckExpr::visit (HIR::RangeFromToExpr &expr) |
| 647 |
{ |
| 648 |
auto lang_item_type = LangItem::Kind::RANGE; |
| 649 |
|
| 650 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); |
| 651 |
// we need to have it maybe |
| 652 |
if (!lang_item_defined) |
| 653 |
{ |
| 654 |
rust_internal_error_at (expr.get_locus (), |
| 655 |
"unable to find relevant lang item: %s", |
| 656 |
LangItem::ToString (lang_item_type).c_str ()); |
| 657 |
return; |
| 658 |
} |
| 659 |
DefId respective_lang_item_id = lang_item_defined.value (); |
| 660 |
|
| 661 |
// look it up and it _must_ be a struct definition |
| 662 |
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); |
| 663 |
|
| 664 |
TyTy::BaseType *item_type = nullptr; |
| 665 |
bool ok |
| 666 |
= context->lookup_type (item->get_mappings ().get_hirid (), &item_type); |
| 667 |
rust_assert (ok); |
| 668 |
rust_assert (item_type->get_kind () == TyTy::TypeKind::ADT); |
| 669 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (item_type); |
| 670 |
|
| 671 |
// this is a single generic item lets assert that |
| 672 |
rust_assert (adt->get_num_substitutions () == 1); |
| 673 |
|
| 674 |
// resolve the range expressions and these types must unify then we use that |
| 675 |
// type to substitute into the ADT |
| 676 |
TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_from_expr ()); |
| 677 |
TyTy::BaseType *to_ty = TypeCheckExpr::Resolve (expr.get_to_expr ()); |
| 678 |
|
| 679 |
TyTy::BaseType *unified = unify_site ( |
| 680 |
expr.get_mappings ().get_hirid (), |
| 681 |
TyTy::TyWithLocation (from_ty, expr.get_from_expr ().get_locus ()), |
| 682 |
TyTy::TyWithLocation (to_ty, expr.get_to_expr ().get_locus ()), |
| 683 |
expr.get_locus ()); |
| 684 |
|
| 685 |
// substitute it in |
| 686 |
std::vector<TyTy::SubstitutionArg> subst_mappings; |
| 687 |
const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); |
| 688 |
subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, unified)); |
| 689 |
|
| 690 |
TyTy::SubstitutionArgumentMappings subst ( |
| 691 |
subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), |
| 692 |
expr.get_locus ()); |
| 693 |
infered = SubstMapperInternal::Resolve (adt, subst); |
| 694 |
} |
| 695 |
|
| 696 |
void |
| 697 |
TypeCheckExpr::visit (HIR::RangeFromExpr &expr) |
| 698 |
{ |
| 699 |
auto lang_item_type = LangItem::Kind::RANGE_FROM; |
| 700 |
|
| 701 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); |
| 702 |
// we need to have it maybe |
| 703 |
if (!lang_item_defined) |
| 704 |
{ |
| 705 |
rust_internal_error_at (expr.get_locus (), |
| 706 |
"unable to find relevant lang item: %s", |
| 707 |
LangItem::ToString (lang_item_type).c_str ()); |
| 708 |
return; |
| 709 |
} |
| 710 |
DefId &respective_lang_item_id = lang_item_defined.value (); |
| 711 |
|
| 712 |
// look it up and it _must_ be a struct definition |
| 713 |
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); |
| 714 |
|
| 715 |
TyTy::BaseType *item_type = nullptr; |
| 716 |
bool ok |
| 717 |
= context->lookup_type (item->get_mappings ().get_hirid (), &item_type); |
| 718 |
rust_assert (ok); |
| 719 |
rust_assert (item_type->get_kind () == TyTy::TypeKind::ADT); |
| 720 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (item_type); |
| 721 |
|
| 722 |
// this is a single generic item lets assert that |
| 723 |
rust_assert (adt->get_num_substitutions () == 1); |
| 724 |
|
| 725 |
// resolve the range expressions and these types must unify then we use that |
| 726 |
// type to substitute into the ADT |
| 727 |
TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_from_expr ()); |
| 728 |
|
| 729 |
// substitute it in |
| 730 |
std::vector<TyTy::SubstitutionArg> subst_mappings; |
| 731 |
const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); |
| 732 |
subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, from_ty)); |
| 733 |
|
| 734 |
TyTy::SubstitutionArgumentMappings subst ( |
| 735 |
subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), |
| 736 |
expr.get_locus ()); |
| 737 |
infered = SubstMapperInternal::Resolve (adt, subst); |
| 738 |
} |
| 739 |
|
| 740 |
void |
| 741 |
TypeCheckExpr::visit (HIR::RangeToExpr &expr) |
| 742 |
{ |
| 743 |
auto lang_item_type = LangItem::Kind::RANGE_TO; |
| 744 |
|
| 745 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); |
| 746 |
// we need to have it maybe |
| 747 |
if (!lang_item_defined) |
| 748 |
{ |
| 749 |
rust_internal_error_at (expr.get_locus (), |
| 750 |
"unable to find relevant lang item: %s", |
| 751 |
LangItem::ToString (lang_item_type).c_str ()); |
| 752 |
return; |
| 753 |
} |
| 754 |
|
| 755 |
DefId &respective_lang_item_id = lang_item_defined.value (); |
| 756 |
// look it up and it _must_ be a struct definition |
| 757 |
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); |
| 758 |
|
| 759 |
TyTy::BaseType *item_type = nullptr; |
| 760 |
bool ok |
| 761 |
= context->lookup_type (item->get_mappings ().get_hirid (), &item_type); |
| 762 |
rust_assert (ok); |
| 763 |
rust_assert (item_type->get_kind () == TyTy::TypeKind::ADT); |
| 764 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (item_type); |
| 765 |
|
| 766 |
// this is a single generic item lets assert that |
| 767 |
rust_assert (adt->get_num_substitutions () == 1); |
| 768 |
|
| 769 |
// resolve the range expressions and these types must unify then we use that |
| 770 |
// type to substitute into the ADT |
| 771 |
TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_to_expr ()); |
| 772 |
|
| 773 |
// substitute it in |
| 774 |
std::vector<TyTy::SubstitutionArg> subst_mappings; |
| 775 |
const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); |
| 776 |
subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, from_ty)); |
| 777 |
|
| 778 |
TyTy::SubstitutionArgumentMappings subst ( |
| 779 |
subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), |
| 780 |
expr.get_locus ()); |
| 781 |
infered = SubstMapperInternal::Resolve (adt, subst); |
| 782 |
} |
| 783 |
|
| 784 |
void |
| 785 |
typecheck_inline_asm_operand (HIR::InlineAsm &expr) |
| 786 |
{ |
| 787 |
const auto &operands = expr.get_operands (); |
| 788 |
using RegisterType = AST::InlineAsmOperand::RegisterType; |
| 789 |
for (auto &operand : operands) |
| 790 |
{ |
| 791 |
switch (operand.get_register_type ()) |
| 792 |
{ |
| 793 |
case RegisterType::In: { |
| 794 |
auto in = operand.get_in (); |
| 795 |
TypeCheckExpr::Resolve (*in.expr); |
| 796 |
break; |
| 797 |
} |
| 798 |
case RegisterType::Out: { |
| 799 |
auto out = operand.get_out (); |
| 800 |
TypeCheckExpr::Resolve (*out.expr); |
| 801 |
break; |
| 802 |
} |
| 803 |
case RegisterType::InOut: { |
| 804 |
auto in_out = operand.get_in_out (); |
| 805 |
TypeCheckExpr::Resolve (*in_out.expr); |
| 806 |
break; |
| 807 |
} |
| 808 |
case RegisterType::SplitInOut: { |
| 809 |
auto split_in_out = operand.get_split_in_out (); |
| 810 |
TypeCheckExpr::Resolve (*split_in_out.in_expr); |
| 811 |
TypeCheckExpr::Resolve (*split_in_out.out_expr); |
| 812 |
break; |
| 813 |
} |
| 814 |
case RegisterType::Const: { |
| 815 |
auto anon_const = operand.get_const ().anon_const; |
| 816 |
TypeCheckExpr::Resolve (*anon_const.expr); |
| 817 |
break; |
| 818 |
} |
| 819 |
case RegisterType::Sym: { |
| 820 |
auto sym = operand.get_sym (); |
| 821 |
TypeCheckExpr::Resolve (*sym.expr); |
| 822 |
break; |
| 823 |
} |
| 824 |
case RegisterType::Label: { |
| 825 |
auto label = operand.get_label (); |
| 826 |
TypeCheckExpr::Resolve (*label.expr); |
| 827 |
break; |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
void |
| 833 |
TypeCheckExpr::visit (HIR::InlineAsm &expr) |
| 834 |
{ |
| 835 |
typecheck_inline_asm_operand (expr); |
| 836 |
|
| 837 |
// NOTE: Hoise out if we have noreturn as an option |
| 838 |
// to return a never type |
| 839 |
// TODO : new keyword for memory seems sooooo shaky |
| 840 |
if (expr.options.count (AST::InlineAsmOption::NORETURN) == 1) |
| 841 |
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ()); |
| 842 |
else |
| 843 |
infered = TyTy::TupleType::get_unit_type (); |
| 844 |
} |
| 845 |
|
| 846 |
void |
| 847 |
TypeCheckExpr::visit (HIR::RangeFullExpr &expr) |
| 848 |
{ |
| 849 |
auto lang_item_type = LangItem::Kind::RANGE_FULL; |
| 850 |
|
| 851 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); |
| 852 |
// we need to have it maybe |
| 853 |
if (!lang_item_defined) |
| 854 |
{ |
| 855 |
rust_internal_error_at (expr.get_locus (), |
| 856 |
"unable to find relevant lang item: %s", |
| 857 |
LangItem::ToString (lang_item_type).c_str ()); |
| 858 |
return; |
| 859 |
} |
| 860 |
DefId &respective_lang_item_id = lang_item_defined.value (); |
| 861 |
|
| 862 |
// look it up and it _must_ be a struct definition |
| 863 |
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); |
| 864 |
|
| 865 |
TyTy::BaseType *item_type = nullptr; |
| 866 |
bool ok |
| 867 |
= context->lookup_type (item->get_mappings ().get_hirid (), &item_type); |
| 868 |
rust_assert (ok); |
| 869 |
rust_assert (item_type->is_unit ()); |
| 870 |
|
| 871 |
infered = item_type; |
| 872 |
} |
| 873 |
|
| 874 |
void |
| 875 |
TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr) |
| 876 |
{ |
| 877 |
auto lang_item_type = LangItem::Kind::RANGE_INCLUSIVE; |
| 878 |
|
| 879 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); |
| 880 |
// we need to have it maybe |
| 881 |
if (!lang_item_defined) |
| 882 |
{ |
| 883 |
rust_internal_error_at (expr.get_locus (), |
| 884 |
"unable to find relevant lang item: %s", |
| 885 |
LangItem::ToString (lang_item_type).c_str ()); |
| 886 |
return; |
| 887 |
} |
| 888 |
DefId respective_lang_item_id = lang_item_defined.value (); |
| 889 |
|
| 890 |
// look it up and it _must_ be a struct definition |
| 891 |
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); |
| 892 |
|
| 893 |
TyTy::BaseType *item_type = nullptr; |
| 894 |
bool ok |
| 895 |
= context->lookup_type (item->get_mappings ().get_hirid (), &item_type); |
| 896 |
rust_assert (ok); |
| 897 |
rust_assert (item_type->get_kind () == TyTy::TypeKind::ADT); |
| 898 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (item_type); |
| 899 |
|
| 900 |
// this is a single generic item lets assert that |
| 901 |
rust_assert (adt->get_num_substitutions () == 1); |
| 902 |
|
| 903 |
// resolve the range expressions and these types must unify then we use that |
| 904 |
// type to substitute into the ADT |
| 905 |
TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_from_expr ()); |
| 906 |
TyTy::BaseType *to_ty = TypeCheckExpr::Resolve (expr.get_to_expr ()); |
| 907 |
TyTy::BaseType *unified = unify_site ( |
| 908 |
expr.get_mappings ().get_hirid (), |
| 909 |
TyTy::TyWithLocation (from_ty, expr.get_from_expr ().get_locus ()), |
| 910 |
TyTy::TyWithLocation (to_ty, expr.get_to_expr ().get_locus ()), |
| 911 |
expr.get_locus ()); |
| 912 |
|
| 913 |
// substitute it in |
| 914 |
std::vector<TyTy::SubstitutionArg> subst_mappings; |
| 915 |
const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); |
| 916 |
subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, unified)); |
| 917 |
|
| 918 |
TyTy::SubstitutionArgumentMappings subst ( |
| 919 |
subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), |
| 920 |
expr.get_locus ()); |
| 921 |
infered = SubstMapperInternal::Resolve (adt, subst); |
| 922 |
} |
| 923 |
|
| 924 |
void |
| 925 |
TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr) |
| 926 |
{ |
| 927 |
auto array_expr_ty = TypeCheckExpr::Resolve (expr.get_array_expr ()); |
| 928 |
if (array_expr_ty->get_kind () == TyTy::TypeKind::ERROR) |
| 929 |
return; |
| 930 |
|
| 931 |
auto index_expr_ty = TypeCheckExpr::Resolve (expr.get_index_expr ()); |
| 932 |
if (index_expr_ty->get_kind () == TyTy::TypeKind::ERROR) |
| 933 |
return; |
| 934 |
|
| 935 |
// first attempt to use direct array index logic |
| 936 |
auto direct_array_expr_ty = array_expr_ty; |
| 937 |
if (direct_array_expr_ty->get_kind () == TyTy::TypeKind::REF) |
| 938 |
{ |
| 939 |
// lets try and deref it since rust allows this |
| 940 |
auto ref = static_cast<TyTy::ReferenceType *> (direct_array_expr_ty); |
| 941 |
auto base = ref->get_base (); |
| 942 |
if (base->get_kind () == TyTy::TypeKind::ARRAY) |
| 943 |
direct_array_expr_ty = base; |
| 944 |
} |
| 945 |
|
| 946 |
TyTy::BaseType *size_ty; |
| 947 |
bool ok = context->lookup_builtin ("usize", &size_ty); |
| 948 |
rust_assert (ok); |
| 949 |
|
| 950 |
bool maybe_simple_array_access = index_expr_ty->can_eq (size_ty, false); |
| 951 |
if (maybe_simple_array_access |
| 952 |
&& direct_array_expr_ty->get_kind () == TyTy::TypeKind::ARRAY) |
| 953 |
{ |
| 954 |
unify_site (expr.get_index_expr ().get_mappings ().get_hirid (), |
| 955 |
TyTy::TyWithLocation (size_ty), |
| 956 |
TyTy::TyWithLocation (index_expr_ty, |
| 957 |
expr.get_index_expr ().get_locus ()), |
| 958 |
expr.get_locus ()); |
| 959 |
|
| 960 |
TyTy::ArrayType *array_type |
| 961 |
= static_cast<TyTy::ArrayType *> (direct_array_expr_ty); |
| 962 |
infered = array_type->get_element_type ()->clone (); |
| 963 |
return; |
| 964 |
} |
| 965 |
|
| 966 |
// is this a case of core::ops::index? |
| 967 |
auto lang_item_type = LangItem::Kind::INDEX; |
| 968 |
bool operator_overloaded |
| 969 |
= resolve_operator_overload (lang_item_type, expr, array_expr_ty, |
| 970 |
index_expr_ty); |
| 971 |
if (operator_overloaded) |
| 972 |
{ |
| 973 |
// index and index mut always return a reference to the element |
| 974 |
TyTy::BaseType *resolved = infered; |
| 975 |
rust_assert (resolved->get_kind () == TyTy::TypeKind::REF); |
| 976 |
TyTy::ReferenceType *ref = static_cast<TyTy::ReferenceType *> (resolved); |
| 977 |
|
| 978 |
infered = ref->get_base ()->clone (); |
| 979 |
return; |
| 980 |
} |
| 981 |
|
| 982 |
// error[E0277]: the type `[{integer}]` cannot be indexed by `u32` |
| 983 |
rich_location r (line_table, expr.get_locus ()); |
| 984 |
r.add_range (expr.get_array_expr ().get_locus ()); |
| 985 |
r.add_range (expr.get_index_expr ().get_locus ()); |
| 986 |
rust_error_at (r, ErrorCode::E0277, |
| 987 |
"the type %qs cannot be indexed by %qs", |
| 988 |
array_expr_ty->get_name ().c_str (), |
| 989 |
index_expr_ty->get_name ().c_str ()); |
| 990 |
} |
| 991 |
|
| 992 |
void |
| 993 |
TypeCheckExpr::visit (HIR::ArrayExpr &expr) |
| 994 |
{ |
| 995 |
auto &elements = expr.get_internal_elements (); |
| 996 |
|
| 997 |
HIR::Expr *capacity_expr = nullptr; |
| 998 |
TyTy::BaseType *element_type = nullptr; |
| 999 |
switch (elements.get_array_expr_type ()) |
| 1000 |
{ |
| 1001 |
case HIR::ArrayElems::ArrayExprType::COPIED: { |
| 1002 |
HIR::ArrayElemsCopied &elems |
| 1003 |
= static_cast<HIR::ArrayElemsCopied &> (elements); |
| 1004 |
element_type = TypeCheckExpr::Resolve (elems.get_elem_to_copy ()); |
| 1005 |
|
| 1006 |
auto capacity_type |
| 1007 |
= TypeCheckExpr::Resolve (elems.get_num_copies_expr ()); |
| 1008 |
|
| 1009 |
TyTy::BaseType *expected_ty = nullptr; |
| 1010 |
bool ok = context->lookup_builtin ("usize", &expected_ty); |
| 1011 |
rust_assert (ok); |
| 1012 |
context->insert_type (elems.get_num_copies_expr ().get_mappings (), |
| 1013 |
expected_ty); |
| 1014 |
|
| 1015 |
unify_site (expr.get_mappings ().get_hirid (), |
| 1016 |
TyTy::TyWithLocation (expected_ty), |
| 1017 |
TyTy::TyWithLocation ( |
| 1018 |
capacity_type, elems.get_num_copies_expr ().get_locus ()), |
| 1019 |
expr.get_locus ()); |
| 1020 |
|
| 1021 |
capacity_expr = &elems.get_num_copies_expr (); |
| 1022 |
} |
| 1023 |
break; |
| 1024 |
|
| 1025 |
case HIR::ArrayElems::ArrayExprType::VALUES: { |
| 1026 |
HIR::ArrayElemsValues &elems |
| 1027 |
= static_cast<HIR::ArrayElemsValues &> (elements); |
| 1028 |
|
| 1029 |
std::vector<TyTy::BaseType *> types; |
| 1030 |
for (auto &elem : elems.get_values ()) |
| 1031 |
{ |
| 1032 |
types.push_back (TypeCheckExpr::Resolve (*elem)); |
| 1033 |
} |
| 1034 |
|
| 1035 |
// this is a LUB |
| 1036 |
element_type |
| 1037 |
= TyTy::TyVar::get_implicit_infer_var (expr.get_locus ()).get_tyty (); |
| 1038 |
for (auto &type : types) |
| 1039 |
{ |
| 1040 |
element_type |
| 1041 |
= unify_site (expr.get_mappings ().get_hirid (), |
| 1042 |
TyTy::TyWithLocation (element_type), |
| 1043 |
TyTy::TyWithLocation (type, type->get_locus ()), |
| 1044 |
expr.get_locus ()); |
| 1045 |
} |
| 1046 |
|
| 1047 |
auto crate_num = mappings.get_current_crate (); |
| 1048 |
Analysis::NodeMapping mapping (crate_num, UNKNOWN_NODEID, |
| 1049 |
mappings.get_next_hir_id (crate_num), |
| 1050 |
UNKNOWN_LOCAL_DEFID); |
| 1051 |
std::string capacity_str = std::to_string (elems.get_num_elements ()); |
| 1052 |
capacity_expr = new HIR::LiteralExpr (mapping, capacity_str, |
| 1053 |
HIR::Literal::LitType::INT, |
| 1054 |
PrimitiveCoreType::CORETYPE_USIZE, |
| 1055 |
UNDEF_LOCATION, {}); |
| 1056 |
|
| 1057 |
// mark the type for this implicit node |
| 1058 |
TyTy::BaseType *expected_ty = nullptr; |
| 1059 |
bool ok = context->lookup_builtin ("usize", &expected_ty); |
| 1060 |
rust_assert (ok); |
| 1061 |
context->insert_type (mapping, expected_ty); |
| 1062 |
} |
| 1063 |
break; |
| 1064 |
} |
| 1065 |
|
| 1066 |
infered = new TyTy::ArrayType (expr.get_mappings ().get_hirid (), |
| 1067 |
expr.get_locus (), *capacity_expr, |
| 1068 |
TyTy::TyVar (element_type->get_ref ())); |
| 1069 |
} |
| 1070 |
|
| 1071 |
// empty struct |
| 1072 |
void |
| 1073 |
TypeCheckExpr::visit (HIR::StructExprStruct &struct_expr) |
| 1074 |
{ |
| 1075 |
TyTy::BaseType *struct_path_ty |
| 1076 |
= TypeCheckExpr::Resolve (struct_expr.get_struct_name ()); |
| 1077 |
if (struct_path_ty->get_kind () != TyTy::TypeKind::ADT) |
| 1078 |
{ |
| 1079 |
rust_error_at (struct_expr.get_struct_name ().get_locus (), |
| 1080 |
"expected an ADT type for constructor"); |
| 1081 |
return; |
| 1082 |
} |
| 1083 |
|
| 1084 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (struct_path_ty); |
| 1085 |
for (auto variant : adt->get_variants ()) |
| 1086 |
{ |
| 1087 |
if (!variant->get_fields ().empty ()) |
| 1088 |
{ |
| 1089 |
std::vector<std::string> field_names; |
| 1090 |
for (auto &field : variant->get_fields ()) |
| 1091 |
field_names.push_back (field->get_name ()); |
| 1092 |
Error missing_fields_error |
| 1093 |
= TypeCheckStructExpr::make_missing_field_error ( |
| 1094 |
struct_expr.get_locus (), field_names, |
| 1095 |
struct_path_ty->get_name ()); |
| 1096 |
// We might want to return or handle these in the future emit for now. |
| 1097 |
missing_fields_error.emit (); |
| 1098 |
return; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
infered = struct_path_ty; |
| 1103 |
} |
| 1104 |
|
| 1105 |
void |
| 1106 |
TypeCheckExpr::visit (HIR::StructExprStructFields &struct_expr) |
| 1107 |
{ |
| 1108 |
infered = TypeCheckStructExpr::Resolve (struct_expr); |
| 1109 |
} |
| 1110 |
|
| 1111 |
void |
| 1112 |
TypeCheckExpr::visit (HIR::GroupedExpr &expr) |
| 1113 |
{ |
| 1114 |
infered = TypeCheckExpr::Resolve (expr.get_expr_in_parens ()); |
| 1115 |
} |
| 1116 |
|
| 1117 |
void |
| 1118 |
TypeCheckExpr::visit (HIR::FieldAccessExpr &expr) |
| 1119 |
{ |
| 1120 |
auto struct_base = TypeCheckExpr::Resolve (expr.get_receiver_expr ()); |
| 1121 |
|
| 1122 |
// FIXME does this require autoderef here? |
| 1123 |
if (struct_base->get_kind () == TyTy::TypeKind::REF) |
| 1124 |
{ |
| 1125 |
TyTy::ReferenceType *r = static_cast<TyTy::ReferenceType *> (struct_base); |
| 1126 |
struct_base = r->get_base (); |
| 1127 |
} |
| 1128 |
|
| 1129 |
bool is_valid_type = struct_base->get_kind () == TyTy::TypeKind::ADT; |
| 1130 |
if (!is_valid_type) |
| 1131 |
{ |
| 1132 |
rust_error_at (expr.get_locus (), |
| 1133 |
"expected algebraic data type got: [%s]", |
| 1134 |
struct_base->as_string ().c_str ()); |
| 1135 |
return; |
| 1136 |
} |
| 1137 |
|
| 1138 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (struct_base); |
| 1139 |
rust_assert (!adt->is_enum ()); |
| 1140 |
rust_assert (adt->number_of_variants () == 1); |
| 1141 |
|
| 1142 |
TyTy::VariantDef *vaiant = adt->get_variants ().at (0); |
| 1143 |
|
| 1144 |
TyTy::StructFieldType *lookup = nullptr; |
| 1145 |
bool found = vaiant->lookup_field (expr.get_field_name ().as_string (), |
| 1146 |
&lookup, nullptr); |
| 1147 |
if (!found) |
| 1148 |
{ |
| 1149 |
rust_error_at (expr.get_locus (), ErrorCode::E0609, |
| 1150 |
"no field %qs on type %qs", |
| 1151 |
expr.get_field_name ().as_string ().c_str (), |
| 1152 |
adt->as_string ().c_str ()); |
| 1153 |
return; |
| 1154 |
} |
| 1155 |
|
| 1156 |
infered = lookup->get_field_type (); |
| 1157 |
} |
| 1158 |
|
| 1159 |
bool |
| 1160 |
is_default_fn (const MethodCandidate &candidate) |
| 1161 |
{ |
| 1162 |
if (candidate.candidate.is_impl_candidate ()) |
| 1163 |
{ |
| 1164 |
auto *item = candidate.candidate.item.impl.impl_item; |
| 1165 |
|
| 1166 |
if (item->get_impl_item_type () == HIR::ImplItem::FUNCTION) |
| 1167 |
{ |
| 1168 |
auto &fn = static_cast<HIR::Function &> (*item); |
| 1169 |
|
| 1170 |
return fn.is_default (); |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
return false; |
| 1175 |
} |
| 1176 |
|
| 1177 |
void |
| 1178 |
emit_ambiguous_resolution_error (HIR::MethodCallExpr &expr, |
| 1179 |
std::set<MethodCandidate> &candidates) |
| 1180 |
{ |
| 1181 |
rich_location r (line_table, expr.get_method_name ().get_locus ()); |
| 1182 |
std::string rich_msg = "multiple " |
| 1183 |
+ expr.get_method_name ().get_segment ().as_string () |
| 1184 |
+ " found"; |
| 1185 |
|
| 1186 |
// We have to filter out default candidates |
| 1187 |
for (auto &c : candidates) |
| 1188 |
if (!is_default_fn (c)) |
| 1189 |
r.add_range (c.candidate.locus); |
| 1190 |
|
| 1191 |
r.add_fixit_replace (rich_msg.c_str ()); |
| 1192 |
|
| 1193 |
rust_error_at (r, ErrorCode::E0592, "duplicate definitions with name %qs", |
| 1194 |
expr.get_method_name ().get_segment ().as_string ().c_str ()); |
| 1195 |
} |
| 1196 |
|
| 1197 |
// We are allowed to have multiple candidates if they are all specializable |
| 1198 |
// functions or if all of them except one are specializable functions. |
| 1199 |
// In the later case, we just return a valid candidate without erroring out |
| 1200 |
// about ambiguity. If there are two or more specialized functions, then we |
| 1201 |
// error out. |
| 1202 |
// |
| 1203 |
// FIXME: The first case is not handled at the moment, so we error out |
| 1204 |
tl::optional<const MethodCandidate &> |
| 1205 |
handle_multiple_candidates (HIR::MethodCallExpr &expr, |
| 1206 |
std::set<MethodCandidate> &candidates) |
| 1207 |
{ |
| 1208 |
auto all_default = true; |
| 1209 |
tl::optional<const MethodCandidate &> found = tl::nullopt; |
| 1210 |
|
| 1211 |
for (auto &c : candidates) |
| 1212 |
{ |
| 1213 |
if (!is_default_fn (c)) |
| 1214 |
{ |
| 1215 |
all_default = false; |
| 1216 |
|
| 1217 |
// We haven't found a final candidate yet, so we can select |
| 1218 |
// this one. However, if we already have a candidate, then |
| 1219 |
// that means there are multiple non-default candidates - we |
| 1220 |
// must error out |
| 1221 |
if (!found) |
| 1222 |
{ |
| 1223 |
found = c; |
| 1224 |
} |
| 1225 |
else |
| 1226 |
{ |
| 1227 |
emit_ambiguous_resolution_error (expr, candidates); |
| 1228 |
return tl::nullopt; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
// None of the candidates were a non-default (specialized) function, so we |
| 1234 |
// error out |
| 1235 |
if (all_default) |
| 1236 |
{ |
| 1237 |
rust_sorry_at (expr.get_locus (), |
| 1238 |
"cannot resolve method calls to non-specialized methods " |
| 1239 |
"(all function candidates are %qs)", |
| 1240 |
"default"); |
| 1241 |
return tl::nullopt; |
| 1242 |
} |
| 1243 |
|
| 1244 |
return found; |
| 1245 |
} |
| 1246 |
|
| 1247 |
void |
| 1248 |
TypeCheckExpr::visit (HIR::MethodCallExpr &expr) |
| 1249 |
{ |
| 1250 |
auto receiver_tyty = TypeCheckExpr::Resolve (expr.get_receiver ()); |
| 1251 |
if (receiver_tyty->get_kind () == TyTy::TypeKind::ERROR) |
| 1252 |
{ |
| 1253 |
rust_error_at (expr.get_receiver ().get_locus (), |
| 1254 |
"failed to resolve receiver in MethodCallExpr"); |
| 1255 |
return; |
| 1256 |
} |
| 1257 |
|
| 1258 |
rust_debug_loc (expr.get_locus (), "attempting to resolve method for %s", |
| 1259 |
receiver_tyty->debug_str ().c_str ()); |
| 1260 |
auto candidates |
| 1261 |
= MethodResolver::Probe (receiver_tyty, |
| 1262 |
expr.get_method_name ().get_segment ()); |
| 1263 |
if (candidates.empty ()) |
| 1264 |
{ |
| 1265 |
rich_location richloc (line_table, expr.get_method_name ().get_locus ()); |
| 1266 |
richloc.add_fixit_replace ("method not found"); |
| 1267 |
rust_error_at ( |
| 1268 |
richloc, ErrorCode::E0599, |
| 1269 |
"no method named %qs found in the current scope", |
| 1270 |
expr.get_method_name ().get_segment ().as_string ().c_str ()); |
| 1271 |
return; |
| 1272 |
} |
| 1273 |
|
| 1274 |
tl::optional<const MethodCandidate &> candidate = *candidates.begin (); |
| 1275 |
|
| 1276 |
if (candidates.size () > 1) |
| 1277 |
candidate = handle_multiple_candidates (expr, candidates); |
| 1278 |
|
| 1279 |
if (!candidate) |
| 1280 |
return; |
| 1281 |
|
| 1282 |
auto found_candidate = *candidate; |
| 1283 |
|
| 1284 |
rust_debug_loc (expr.get_method_name ().get_locus (), |
| 1285 |
"resolved method to: {%u} {%s} with [%lu] adjustments", |
| 1286 |
found_candidate.candidate.ty->get_ref (), |
| 1287 |
found_candidate.candidate.ty->debug_str ().c_str (), |
| 1288 |
(unsigned long) found_candidate.adjustments.size ()); |
| 1289 |
|
| 1290 |
// Get the adjusted self |
| 1291 |
Adjuster adj (receiver_tyty); |
| 1292 |
TyTy::BaseType *adjusted_self = adj.adjust_type (found_candidate.adjustments); |
| 1293 |
rust_debug ("receiver: %s adjusted self %s", |
| 1294 |
receiver_tyty->debug_str ().c_str (), |
| 1295 |
adjusted_self->debug_str ().c_str ()); |
| 1296 |
|
| 1297 |
// store the adjustments for code-generation to know what to do which must be |
| 1298 |
// stored onto the receiver to so as we don't trigger duplicate deref mappings |
| 1299 |
// ICE when an argument is a method call |
| 1300 |
HirId autoderef_mappings_id |
| 1301 |
= expr.get_receiver ().get_mappings ().get_hirid (); |
| 1302 |
context->insert_autoderef_mappings (autoderef_mappings_id, |
| 1303 |
std::move (found_candidate.adjustments)); |
| 1304 |
|
| 1305 |
PathProbeCandidate &resolved_candidate = found_candidate.candidate; |
| 1306 |
TyTy::BaseType *lookup_tyty = found_candidate.candidate.ty; |
| 1307 |
NodeId resolved_node_id |
| 1308 |
= resolved_candidate.is_impl_candidate () |
| 1309 |
? resolved_candidate.item.impl.impl_item->get_impl_mappings () |
| 1310 |
.get_nodeid () |
| 1311 |
: resolved_candidate.item.trait.item_ref->get_mappings ().get_nodeid (); |
| 1312 |
|
| 1313 |
if (lookup_tyty->get_kind () != TyTy::TypeKind::FNDEF) |
| 1314 |
{ |
| 1315 |
rich_location r (line_table, expr.get_method_name ().get_locus ()); |
| 1316 |
r.add_range (resolved_candidate.locus); |
| 1317 |
rust_error_at (r, "associated impl item is not a method"); |
| 1318 |
return; |
| 1319 |
} |
| 1320 |
|
| 1321 |
TyTy::BaseType *lookup = lookup_tyty; |
| 1322 |
TyTy::FnType *fn = static_cast<TyTy::FnType *> (lookup); |
| 1323 |
if (!fn->is_method ()) |
| 1324 |
{ |
| 1325 |
rich_location r (line_table, expr.get_method_name ().get_locus ()); |
| 1326 |
r.add_range (resolved_candidate.locus); |
| 1327 |
rust_error_at (r, "associated function is not a method"); |
| 1328 |
return; |
| 1329 |
} |
| 1330 |
|
| 1331 |
fn->prepare_higher_ranked_bounds (); |
| 1332 |
rust_debug_loc (expr.get_locus (), "resolved method call to: {%u} {%s}", |
| 1333 |
found_candidate.candidate.ty->get_ref (), |
| 1334 |
found_candidate.candidate.ty->debug_str ().c_str ()); |
| 1335 |
|
| 1336 |
if (resolved_candidate.is_impl_candidate ()) |
| 1337 |
{ |
| 1338 |
auto infer_arguments = TyTy::SubstitutionArgumentMappings::empty (); |
| 1339 |
infer_arguments.get_mut_regions () |
| 1340 |
= fn->get_used_arguments ().get_regions (); |
| 1341 |
HIR::ImplBlock &impl = *resolved_candidate.item.impl.parent; |
| 1342 |
TyTy::BaseType *impl_self_infer |
| 1343 |
= TypeCheckItem::ResolveImplBlockSelfWithInference (impl, |
| 1344 |
expr.get_locus (), |
| 1345 |
&infer_arguments); |
| 1346 |
if (impl_self_infer->get_kind () == TyTy::TypeKind::ERROR) |
| 1347 |
{ |
| 1348 |
rich_location r (line_table, expr.get_locus ()); |
| 1349 |
r.add_range (impl.get_type ().get_locus ()); |
| 1350 |
rust_error_at ( |
| 1351 |
r, "failed to resolve impl type for method call resolution"); |
| 1352 |
return; |
| 1353 |
} |
| 1354 |
|
| 1355 |
if (!infer_arguments.is_empty ()) |
| 1356 |
{ |
| 1357 |
lookup = SubstMapperInternal::Resolve (lookup, infer_arguments); |
| 1358 |
lookup->debug (); |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
// apply any remaining generic arguments |
| 1363 |
if (expr.get_method_name ().has_generic_args ()) |
| 1364 |
{ |
| 1365 |
HIR::GenericArgs &args = expr.get_method_name ().get_generic_args (); |
| 1366 |
rust_debug_loc (args.get_locus (), |
| 1367 |
"applying generic arguments to method_call: {%s}", |
| 1368 |
lookup->debug_str ().c_str ()); |
| 1369 |
|
| 1370 |
lookup |
| 1371 |
= SubstMapper::Resolve (lookup, expr.get_method_name ().get_locus (), |
| 1372 |
&args); |
| 1373 |
if (lookup->get_kind () == TyTy::TypeKind::ERROR) |
| 1374 |
return; |
| 1375 |
} |
| 1376 |
else if (lookup->needs_generic_substitutions ()) |
| 1377 |
{ |
| 1378 |
rust_debug ("method needs inference: {%s}", |
| 1379 |
lookup->debug_str ().c_str ()); |
| 1380 |
lookup = SubstMapper::InferSubst (lookup, |
| 1381 |
expr.get_method_name ().get_locus ()); |
| 1382 |
} |
| 1383 |
|
| 1384 |
rust_debug ("type-checking method_call: {%s}", lookup->debug_str ().c_str ()); |
| 1385 |
|
| 1386 |
TyTy::BaseType *function_ret_tyty |
| 1387 |
= TyTy::TypeCheckMethodCallExpr::go (static_cast<TyTy::FnType *> (lookup), |
| 1388 |
expr, adjusted_self, context); |
| 1389 |
if (function_ret_tyty == nullptr |
| 1390 |
|| function_ret_tyty->get_kind () == TyTy::TypeKind::ERROR) |
| 1391 |
{ |
| 1392 |
rust_error_at (expr.get_locus (), |
| 1393 |
"failed to lookup type to MethodCallExpr"); |
| 1394 |
return; |
| 1395 |
} |
| 1396 |
|
| 1397 |
// store the expected fntype |
| 1398 |
context->insert_type (expr.get_method_name ().get_mappings (), lookup); |
| 1399 |
|
| 1400 |
if (flag_name_resolution_2_0) |
| 1401 |
{ |
| 1402 |
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> ( |
| 1403 |
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()); |
| 1404 |
|
| 1405 |
nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()), |
| 1406 |
Resolver2_0::Definition (resolved_node_id)); |
| 1407 |
} |
| 1408 |
// set up the resolved name on the path |
| 1409 |
else if (resolver->get_name_scope ().decl_was_declared_here ( |
| 1410 |
resolved_node_id)) |
| 1411 |
{ |
| 1412 |
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (), |
| 1413 |
resolved_node_id); |
| 1414 |
} |
| 1415 |
else |
| 1416 |
{ |
| 1417 |
resolver->insert_resolved_misc (expr.get_mappings ().get_nodeid (), |
| 1418 |
resolved_node_id); |
| 1419 |
} |
| 1420 |
|
| 1421 |
// return the result of the function back |
| 1422 |
infered = function_ret_tyty; |
| 1423 |
} |
| 1424 |
|
| 1425 |
void |
| 1426 |
TypeCheckExpr::visit (HIR::LoopExpr &expr) |
| 1427 |
{ |
| 1428 |
context->push_new_loop_context (expr.get_mappings ().get_hirid (), |
| 1429 |
expr.get_locus ()); |
| 1430 |
TyTy::BaseType *block_expr = TypeCheckExpr::Resolve (expr.get_loop_block ()); |
| 1431 |
if (!block_expr->is_unit ()) |
| 1432 |
{ |
| 1433 |
rust_error_at (expr.get_loop_block ().get_locus (), |
| 1434 |
"expected %<()%> got %s", |
| 1435 |
block_expr->as_string ().c_str ()); |
| 1436 |
return; |
| 1437 |
} |
| 1438 |
|
| 1439 |
TyTy::BaseType *loop_context_type = context->pop_loop_context (); |
| 1440 |
|
| 1441 |
bool loop_context_type_infered |
| 1442 |
= (loop_context_type->get_kind () != TyTy::TypeKind::INFER) |
| 1443 |
|| ((loop_context_type->get_kind () == TyTy::TypeKind::INFER) |
| 1444 |
&& (((TyTy::InferType *) loop_context_type)->get_infer_kind () |
| 1445 |
!= TyTy::InferType::GENERAL)); |
| 1446 |
|
| 1447 |
infered = loop_context_type_infered ? loop_context_type |
| 1448 |
: TyTy::TupleType::get_unit_type (); |
| 1449 |
} |
| 1450 |
|
| 1451 |
void |
| 1452 |
TypeCheckExpr::visit (HIR::WhileLoopExpr &expr) |
| 1453 |
{ |
| 1454 |
context->push_new_while_loop_context (expr.get_mappings ().get_hirid ()); |
| 1455 |
|
| 1456 |
TypeCheckExpr::Resolve (expr.get_predicate_expr ()); |
| 1457 |
TyTy::BaseType *block_expr = TypeCheckExpr::Resolve (expr.get_loop_block ()); |
| 1458 |
|
| 1459 |
if (!block_expr->is_unit ()) |
| 1460 |
{ |
| 1461 |
rust_error_at (expr.get_loop_block ().get_locus (), |
| 1462 |
"expected %<()%> got %s", |
| 1463 |
block_expr->as_string ().c_str ()); |
| 1464 |
return; |
| 1465 |
} |
| 1466 |
|
| 1467 |
context->pop_loop_context (); |
| 1468 |
infered = TyTy::TupleType::get_unit_type (); |
| 1469 |
} |
| 1470 |
|
| 1471 |
void |
| 1472 |
TypeCheckExpr::visit (HIR::BreakExpr &expr) |
| 1473 |
{ |
| 1474 |
if (!context->have_loop_context ()) |
| 1475 |
{ |
| 1476 |
rust_error_at (expr.get_locus (), ErrorCode::E0268, |
| 1477 |
"%<break%> outside of a loop or labeled block"); |
| 1478 |
return; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (expr.has_break_expr ()) |
| 1482 |
{ |
| 1483 |
TyTy::BaseType *break_expr_tyty |
| 1484 |
= TypeCheckExpr::Resolve (expr.get_expr ()); |
| 1485 |
|
| 1486 |
TyTy::BaseType *loop_context = context->peek_loop_context (); |
| 1487 |
if (loop_context->get_kind () == TyTy::TypeKind::ERROR) |
| 1488 |
{ |
| 1489 |
rust_error_at ( |
| 1490 |
expr.get_locus (), ErrorCode::E0571, |
| 1491 |
"can only %<break%> with a value inside a %<loop%> block"); |
| 1492 |
return; |
| 1493 |
} |
| 1494 |
|
| 1495 |
TyTy::BaseType *unified_ty |
| 1496 |
= unify_site (expr.get_mappings ().get_hirid (), |
| 1497 |
TyTy::TyWithLocation (loop_context), |
| 1498 |
TyTy::TyWithLocation (break_expr_tyty, |
| 1499 |
expr.get_expr ().get_locus ()), |
| 1500 |
expr.get_locus ()); |
| 1501 |
context->swap_head_loop_context (unified_ty); |
| 1502 |
} |
| 1503 |
|
| 1504 |
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ()); |
| 1505 |
} |
| 1506 |
|
| 1507 |
void |
| 1508 |
TypeCheckExpr::visit (HIR::ContinueExpr &expr) |
| 1509 |
{ |
| 1510 |
if (!context->have_loop_context ()) |
| 1511 |
{ |
| 1512 |
rust_error_at (expr.get_locus (), ErrorCode::E0268, |
| 1513 |
"%<continue%> outside of a loop"); |
| 1514 |
return; |
| 1515 |
} |
| 1516 |
|
| 1517 |
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ()); |
| 1518 |
} |
| 1519 |
|
| 1520 |
void |
| 1521 |
TypeCheckExpr::visit (HIR::BorrowExpr &expr) |
| 1522 |
{ |
| 1523 |
TyTy::BaseType *resolved_base = TypeCheckExpr::Resolve (expr.get_expr ()); |
| 1524 |
if (resolved_base->is<TyTy::ErrorType> ()) |
| 1525 |
return; |
| 1526 |
|
| 1527 |
// In Rust this is valid because of DST's |
| 1528 |
// |
| 1529 |
// fn test() { |
| 1530 |
// let a:&str = "TEST 1"; |
| 1531 |
// let b:&str = &"TEST 2"; |
| 1532 |
// } |
| 1533 |
if (resolved_base->get_kind () == TyTy::TypeKind::REF) |
| 1534 |
{ |
| 1535 |
const TyTy::ReferenceType *ref |
| 1536 |
= static_cast<const TyTy::ReferenceType *> (resolved_base); |
| 1537 |
|
| 1538 |
// this might end up being a more generic is_dyn object check but lets |
| 1539 |
// double check dyn traits type-layout first |
| 1540 |
if (ref->is_dyn_str_type ()) |
| 1541 |
{ |
| 1542 |
infered = resolved_base; |
| 1543 |
return; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
|
| 1547 |
if (expr.is_raw_borrow ()) |
| 1548 |
{ |
| 1549 |
infered = new TyTy::PointerType (expr.get_mappings ().get_hirid (), |
| 1550 |
TyTy::TyVar (resolved_base->get_ref ()), |
| 1551 |
expr.get_mut ()); |
| 1552 |
|
| 1553 |
return; |
| 1554 |
} |
| 1555 |
|
| 1556 |
infered = new TyTy::ReferenceType (expr.get_mappings ().get_hirid (), |
| 1557 |
TyTy::TyVar (resolved_base->get_ref ()), |
| 1558 |
expr.get_mut ()); |
| 1559 |
} |
| 1560 |
|
| 1561 |
void |
| 1562 |
TypeCheckExpr::visit (HIR::DereferenceExpr &expr) |
| 1563 |
{ |
| 1564 |
TyTy::BaseType *resolved_base = TypeCheckExpr::Resolve (expr.get_expr ()); |
| 1565 |
|
| 1566 |
rust_debug_loc (expr.get_locus (), "attempting deref operator overload"); |
| 1567 |
auto lang_item_type = LangItem::Kind::DEREF; |
| 1568 |
bool operator_overloaded |
| 1569 |
= resolve_operator_overload (lang_item_type, expr, resolved_base, nullptr); |
| 1570 |
if (operator_overloaded) |
| 1571 |
{ |
| 1572 |
// operator overloaded deref always refurns a reference type lets assert |
| 1573 |
// this |
| 1574 |
rust_assert (infered->get_kind () == TyTy::TypeKind::REF); |
| 1575 |
resolved_base = infered; |
| 1576 |
} |
| 1577 |
|
| 1578 |
bool is_valid_type = resolved_base->get_kind () == TyTy::TypeKind::REF |
| 1579 |
|| resolved_base->get_kind () == TyTy::TypeKind::POINTER; |
| 1580 |
if (!is_valid_type) |
| 1581 |
{ |
| 1582 |
rust_error_at (expr.get_locus (), "expected reference type got %s", |
| 1583 |
resolved_base->as_string ().c_str ()); |
| 1584 |
return; |
| 1585 |
} |
| 1586 |
|
| 1587 |
if (resolved_base->get_kind () == TyTy::TypeKind::REF) |
| 1588 |
{ |
| 1589 |
TyTy::ReferenceType *ref_base |
| 1590 |
= static_cast<TyTy::ReferenceType *> (resolved_base); |
| 1591 |
infered = ref_base->get_base ()->clone (); |
| 1592 |
} |
| 1593 |
else |
| 1594 |
{ |
| 1595 |
TyTy::PointerType *ref_base |
| 1596 |
= static_cast<TyTy::PointerType *> (resolved_base); |
| 1597 |
infered = ref_base->get_base ()->clone (); |
| 1598 |
} |
| 1599 |
} |
| 1600 |
|
| 1601 |
void |
| 1602 |
TypeCheckExpr::visit (HIR::TypeCastExpr &expr) |
| 1603 |
{ |
| 1604 |
TyTy::BaseType *expr_to_convert |
| 1605 |
= TypeCheckExpr::Resolve (expr.get_casted_expr ()); |
| 1606 |
TyTy::BaseType *tyty_to_convert_to |
| 1607 |
= TypeCheckType::Resolve (expr.get_type_to_convert_to ()); |
| 1608 |
|
| 1609 |
TyTy::TyWithLocation from (expr_to_convert, |
| 1610 |
expr.get_casted_expr ().get_locus ()); |
| 1611 |
TyTy::TyWithLocation to (tyty_to_convert_to, |
| 1612 |
expr.get_type_to_convert_to ().get_locus ()); |
| 1613 |
infered = cast_site (expr.get_mappings ().get_hirid (), from, to, |
| 1614 |
expr.get_locus ()); |
| 1615 |
} |
| 1616 |
|
| 1617 |
void |
| 1618 |
TypeCheckExpr::visit (HIR::MatchExpr &expr) |
| 1619 |
{ |
| 1620 |
// this needs to perform a least upper bound coercion on the blocks and then |
| 1621 |
// unify the scruintee and arms |
| 1622 |
TyTy::BaseType *scrutinee_tyty |
| 1623 |
= TypeCheckExpr::Resolve (expr.get_scrutinee_expr ()); |
| 1624 |
|
| 1625 |
// https://github.com/Rust-GCC/gccrs/issues/3231#issuecomment-2462660048 |
| 1626 |
// https://github.com/rust-lang/rust/blob/3d1dba830a564d1118361345d7ada47a05241f45/compiler/rustc_hir_typeck/src/_match.rs#L32-L36 |
| 1627 |
if (!expr.has_match_arms ()) |
| 1628 |
{ |
| 1629 |
// this is a special case where rustc returns ! |
| 1630 |
TyTy::BaseType *lookup = nullptr; |
| 1631 |
bool ok = context->lookup_builtin ("!", &lookup); |
| 1632 |
rust_assert (ok); |
| 1633 |
infered = lookup->clone (); |
| 1634 |
return; |
| 1635 |
} |
| 1636 |
|
| 1637 |
bool saw_error = false; |
| 1638 |
std::vector<TyTy::BaseType *> kase_block_tys; |
| 1639 |
for (auto &kase : expr.get_match_cases ()) |
| 1640 |
{ |
| 1641 |
// lets check the arms |
| 1642 |
HIR::MatchArm &kase_arm = kase.get_arm (); |
| 1643 |
for (auto &pattern : kase_arm.get_patterns ()) |
| 1644 |
{ |
| 1645 |
TyTy::BaseType *kase_arm_ty |
| 1646 |
= TypeCheckPattern::Resolve (*pattern, scrutinee_tyty); |
| 1647 |
if (kase_arm_ty->get_kind () == TyTy ::TypeKind::ERROR) |
| 1648 |
{ |
| 1649 |
saw_error = true; |
| 1650 |
continue; |
| 1651 |
} |
| 1652 |
|
| 1653 |
TyTy::BaseType *checked_kase = unify_site ( |
| 1654 |
expr.get_mappings ().get_hirid (), |
| 1655 |
TyTy::TyWithLocation (scrutinee_tyty, |
| 1656 |
expr.get_scrutinee_expr ().get_locus ()), |
| 1657 |
TyTy::TyWithLocation (kase_arm_ty, pattern->get_locus ()), |
| 1658 |
expr.get_locus ()); |
| 1659 |
if (checked_kase->get_kind () == TyTy::TypeKind::ERROR) |
| 1660 |
{ |
| 1661 |
saw_error = true; |
| 1662 |
continue; |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
// check the kase type |
| 1667 |
TyTy::BaseType *kase_block_ty = TypeCheckExpr::Resolve (kase.get_expr ()); |
| 1668 |
kase_block_tys.push_back (kase_block_ty); |
| 1669 |
} |
| 1670 |
if (saw_error) |
| 1671 |
return; |
| 1672 |
|
| 1673 |
if (kase_block_tys.size () == 0) |
| 1674 |
{ |
| 1675 |
infered = TyTy::TupleType::get_unit_type (); |
| 1676 |
return; |
| 1677 |
} |
| 1678 |
|
| 1679 |
// this is a LUB |
| 1680 |
infered = kase_block_tys.at (0); |
| 1681 |
for (size_t i = 1; i < kase_block_tys.size (); i++) |
| 1682 |
{ |
| 1683 |
TyTy::BaseType *kase_ty = kase_block_tys.at (i); |
| 1684 |
infered |
| 1685 |
= coercion_site (expr.get_mappings ().get_hirid (), |
| 1686 |
TyTy::TyWithLocation (infered), |
| 1687 |
TyTy::TyWithLocation (kase_ty), expr.get_locus ()); |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
void |
| 1692 |
TypeCheckExpr::visit (HIR::ClosureExpr &expr) |
| 1693 |
{ |
| 1694 |
TypeCheckContextItem current_context = context->peek_context (); |
| 1695 |
TyTy::FnType *current_context_fndecl = current_context.get_context_type (); |
| 1696 |
|
| 1697 |
HirId ref = expr.get_mappings ().get_hirid (); |
| 1698 |
DefId id = expr.get_mappings ().get_defid (); |
| 1699 |
RustIdent ident{current_context_fndecl->get_ident ().path, expr.get_locus ()}; |
| 1700 |
|
| 1701 |
// get from parent context |
| 1702 |
std::vector<TyTy::SubstitutionParamMapping> subst_refs |
| 1703 |
= current_context_fndecl->clone_substs (); |
| 1704 |
|
| 1705 |
std::vector<TyTy::TyVar> parameter_types; |
| 1706 |
for (auto &p : expr.get_params ()) |
| 1707 |
{ |
| 1708 |
TyTy::BaseType *param_tyty = nullptr; |
| 1709 |
if (p.has_type_given ()) |
| 1710 |
{ |
| 1711 |
param_tyty = TypeCheckType::Resolve (p.get_type ()); |
| 1712 |
} |
| 1713 |
else |
| 1714 |
{ |
| 1715 |
param_tyty = ClosureParamInfer::Resolve (p.get_pattern ()); |
| 1716 |
} |
| 1717 |
|
| 1718 |
TyTy::TyVar param_ty (param_tyty->get_ref ()); |
| 1719 |
parameter_types.push_back (param_ty); |
| 1720 |
|
| 1721 |
TypeCheckPattern::Resolve (p.get_pattern (), param_ty.get_tyty ()); |
| 1722 |
} |
| 1723 |
|
| 1724 |
// we generate an implicit hirid for the closure args |
| 1725 |
HirId implicit_args_id = mappings.get_next_hir_id (); |
| 1726 |
TyTy::TupleType *closure_args |
| 1727 |
= new TyTy::TupleType (implicit_args_id, expr.get_locus (), |
| 1728 |
parameter_types); |
| 1729 |
context->insert_implicit_type (closure_args->get_ref (), closure_args); |
| 1730 |
|
| 1731 |
location_t result_type_locus = expr.has_return_type () |
| 1732 |
? expr.get_return_type ().get_locus () |
| 1733 |
: expr.get_locus (); |
| 1734 |
TyTy::TyVar result_type |
| 1735 |
= expr.has_return_type () |
| 1736 |
? TyTy::TyVar ( |
| 1737 |
TypeCheckType::Resolve (expr.get_return_type ())->get_ref ()) |
| 1738 |
: TyTy::TyVar::get_implicit_infer_var (expr.get_locus ()); |
| 1739 |
|
| 1740 |
// resolve the block |
| 1741 |
location_t closure_expr_locus = expr.get_expr ().get_locus (); |
| 1742 |
TyTy::BaseType *closure_expr_ty = TypeCheckExpr::Resolve (expr.get_expr ()); |
| 1743 |
coercion_site (expr.get_mappings ().get_hirid (), |
| 1744 |
TyTy::TyWithLocation (result_type.get_tyty (), |
| 1745 |
result_type_locus), |
| 1746 |
TyTy::TyWithLocation (closure_expr_ty, closure_expr_locus), |
| 1747 |
expr.get_locus ()); |
| 1748 |
|
| 1749 |
// generate the closure type |
| 1750 |
NodeId closure_node_id = expr.get_mappings ().get_nodeid (); |
| 1751 |
|
| 1752 |
// Resolve closure captures |
| 1753 |
|
| 1754 |
std::set<NodeId> captures; |
| 1755 |
if (flag_name_resolution_2_0) |
| 1756 |
{ |
| 1757 |
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> ( |
| 1758 |
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()); |
| 1759 |
|
| 1760 |
if (auto opt_cap = nr_ctx.mappings.lookup_captures (closure_node_id)) |
| 1761 |
for (auto cap : opt_cap.value ()) |
| 1762 |
captures.insert (cap); |
| 1763 |
} |
| 1764 |
else |
| 1765 |
{ |
| 1766 |
captures = resolver->get_captures (closure_node_id); |
| 1767 |
} |
| 1768 |
|
| 1769 |
infered = new TyTy::ClosureType (ref, id, ident, closure_args, result_type, |
| 1770 |
subst_refs, captures); |
| 1771 |
|
| 1772 |
// FIXME |
| 1773 |
// all closures automatically inherit the appropriate fn trait. Lets just |
| 1774 |
// assume FnOnce for now. I think this is based on the return type of the |
| 1775 |
// closure |
| 1776 |
|
| 1777 |
LangItem::Kind lang_item_type = LangItem::Kind::FN_ONCE; |
| 1778 |
|
| 1779 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); |
| 1780 |
if (!lang_item_defined) |
| 1781 |
{ |
| 1782 |
// FIXME |
| 1783 |
// we need to have a unified way or error'ing when we are missing lang |
| 1784 |
// items that is useful |
| 1785 |
rust_fatal_error (expr.get_locus (), "unable to find lang item: %qs", |
| 1786 |
LangItem::ToString (lang_item_type).c_str ()); |
| 1787 |
} |
| 1788 |
DefId &respective_lang_item_id = lang_item_defined.value (); |
| 1789 |
|
| 1790 |
// these lang items are always traits |
| 1791 |
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); |
| 1792 |
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait); |
| 1793 |
HIR::Trait *trait_item = static_cast<HIR::Trait *> (item); |
| 1794 |
|
| 1795 |
TraitReference *trait = TraitResolver::Resolve (*trait_item); |
| 1796 |
rust_assert (!trait->is_error ()); |
| 1797 |
|
| 1798 |
TyTy::TypeBoundPredicate predicate (*trait, BoundPolarity::RegularBound, |
| 1799 |
expr.get_locus ()); |
| 1800 |
|
| 1801 |
// resolve the trait bound where the <(Args)> are the parameter tuple type |
| 1802 |
HIR::GenericArgs args = HIR::GenericArgs::create_empty (expr.get_locus ()); |
| 1803 |
|
| 1804 |
// lets generate an implicit Type so that it resolves to the implict tuple |
| 1805 |
// type we have created |
| 1806 |
auto crate_num = mappings.get_current_crate (); |
| 1807 |
Analysis::NodeMapping mapping (crate_num, expr.get_mappings ().get_nodeid (), |
| 1808 |
implicit_args_id, UNKNOWN_LOCAL_DEFID); |
| 1809 |
HIR::TupleType *implicit_tuple |
| 1810 |
= new HIR::TupleType (mapping, |
| 1811 |
{} // we dont need to fill this out because it will |
| 1812 |
// auto resolve because the hir id's match |
| 1813 |
, |
| 1814 |
expr.get_locus ()); |
| 1815 |
args.get_type_args ().push_back (std::unique_ptr<HIR::Type> (implicit_tuple)); |
| 1816 |
|
| 1817 |
// apply the arguments |
| 1818 |
predicate.apply_generic_arguments (&args, false); |
| 1819 |
|
| 1820 |
// finally inherit the trait bound |
| 1821 |
infered->inherit_bounds ({predicate}); |
| 1822 |
} |
| 1823 |
|
| 1824 |
bool |
| 1825 |
TypeCheckExpr::resolve_operator_overload ( |
| 1826 |
LangItem::Kind lang_item_type, HIR::OperatorExprMeta expr, |
| 1827 |
TyTy::BaseType *lhs, TyTy::BaseType *rhs, |
| 1828 |
HIR::PathIdentSegment specified_segment) |
| 1829 |
{ |
| 1830 |
// look up lang item for arithmetic type |
| 1831 |
std::string associated_item_name = LangItem::ToString (lang_item_type); |
| 1832 |
|
| 1833 |
auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); |
| 1834 |
// probe for the lang-item |
| 1835 |
if (!lang_item_defined) |
| 1836 |
return false; |
| 1837 |
DefId &respective_lang_item_id = lang_item_defined.value (); |
| 1838 |
|
| 1839 |
// we might be in a static or const context and unknown is fine |
| 1840 |
TypeCheckContextItem current_context = TypeCheckContextItem::get_error (); |
| 1841 |
if (context->have_function_context ()) |
| 1842 |
{ |
| 1843 |
current_context = context->peek_context (); |
| 1844 |
} |
| 1845 |
|
| 1846 |
auto segment = specified_segment.is_error () |
| 1847 |
? HIR::PathIdentSegment (associated_item_name) |
| 1848 |
: specified_segment; |
| 1849 |
auto candidates = MethodResolver::Probe (lhs, segment); |
| 1850 |
|
| 1851 |
// remove any recursive candidates |
| 1852 |
std::set<MethodCandidate> resolved_candidates; |
| 1853 |
for (auto &c : candidates) |
| 1854 |
{ |
| 1855 |
const TyTy::BaseType *candidate_type = c.candidate.ty; |
| 1856 |
rust_assert (candidate_type->get_kind () == TyTy::TypeKind::FNDEF); |
| 1857 |
|
| 1858 |
const TyTy::FnType &fn |
| 1859 |
= *static_cast<const TyTy::FnType *> (candidate_type); |
| 1860 |
|
| 1861 |
DefId current_fn_defid = current_context.get_defid (); |
| 1862 |
bool recursive_candidated = fn.get_id () == current_fn_defid; |
| 1863 |
if (!recursive_candidated) |
| 1864 |
{ |
| 1865 |
resolved_candidates.insert (c); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
std::vector<TyTy::BaseType *> select_args = {}; |
| 1870 |
if (rhs != nullptr) |
| 1871 |
select_args = {rhs}; |
| 1872 |
auto selected_candidates |
| 1873 |
= MethodResolver::Select (resolved_candidates, lhs, select_args); |
| 1874 |
|
| 1875 |
bool have_implementation_for_lang_item = selected_candidates.size () > 0; |
| 1876 |
if (!have_implementation_for_lang_item) |
| 1877 |
return false; |
| 1878 |
|
| 1879 |
if (selected_candidates.size () > 1) |
| 1880 |
{ |
| 1881 |
// mutliple candidates |
| 1882 |
rich_location r (line_table, expr.get_locus ()); |
| 1883 |
for (auto &c : resolved_candidates) |
| 1884 |
r.add_range (c.candidate.locus); |
| 1885 |
|
| 1886 |
rust_error_at ( |
| 1887 |
r, "multiple candidates found for possible operator overload"); |
| 1888 |
|
| 1889 |
return false; |
| 1890 |
} |
| 1891 |
|
| 1892 |
// Get the adjusted self |
| 1893 |
MethodCandidate candidate = *selected_candidates.begin (); |
| 1894 |
Adjuster adj (lhs); |
| 1895 |
TyTy::BaseType *adjusted_self = adj.adjust_type (candidate.adjustments); |
| 1896 |
|
| 1897 |
// store the adjustments for code-generation to know what to do |
| 1898 |
context->insert_autoderef_mappings (expr.get_lvalue_mappings ().get_hirid (), |
| 1899 |
std::move (candidate.adjustments)); |
| 1900 |
|
| 1901 |
PathProbeCandidate &resolved_candidate = candidate.candidate; |
| 1902 |
TyTy::BaseType *lookup_tyty = candidate.candidate.ty; |
| 1903 |
NodeId resolved_node_id |
| 1904 |
= resolved_candidate.is_impl_candidate () |
| 1905 |
? resolved_candidate.item.impl.impl_item->get_impl_mappings () |
| 1906 |
.get_nodeid () |
| 1907 |
: resolved_candidate.item.trait.item_ref->get_mappings ().get_nodeid (); |
| 1908 |
|
| 1909 |
rust_assert (lookup_tyty->get_kind () == TyTy::TypeKind::FNDEF); |
| 1910 |
TyTy::BaseType *lookup = lookup_tyty; |
| 1911 |
TyTy::FnType *fn = static_cast<TyTy::FnType *> (lookup); |
| 1912 |
rust_assert (fn->is_method ()); |
| 1913 |
|
| 1914 |
rust_debug ("is_impl_item_candidate: %s", |
| 1915 |
resolved_candidate.is_impl_candidate () ? "true" : "false"); |
| 1916 |
|
| 1917 |
if (resolved_candidate.is_impl_candidate ()) |
| 1918 |
{ |
| 1919 |
auto infer_arguments = TyTy::SubstitutionArgumentMappings::error (); |
| 1920 |
HIR::ImplBlock &impl = *resolved_candidate.item.impl.parent; |
| 1921 |
TyTy::BaseType *impl_self_infer |
| 1922 |
= TypeCheckItem::ResolveImplBlockSelfWithInference (impl, |
| 1923 |
expr.get_locus (), |
| 1924 |
&infer_arguments); |
| 1925 |
if (impl_self_infer->get_kind () == TyTy::TypeKind::ERROR) |
| 1926 |
{ |
| 1927 |
return false; |
| 1928 |
} |
| 1929 |
if (!infer_arguments.is_empty ()) |
| 1930 |
{ |
| 1931 |
lookup = SubstMapperInternal::Resolve (lookup, infer_arguments); |
| 1932 |
} |
| 1933 |
} |
| 1934 |
|
| 1935 |
// in the case where we resolve to a trait bound we have to be careful we are |
| 1936 |
// able to do so there is a case where we are currently resolving the deref |
| 1937 |
// operator overload function which is generic and this might resolve to the |
| 1938 |
// trait item of deref which is not valid as its just another recursive case |
| 1939 |
if (current_context.get_type () == TypeCheckContextItem::ItemType::IMPL_ITEM) |
| 1940 |
{ |
| 1941 |
auto &impl_item = current_context.get_impl_item (); |
| 1942 |
HIR::ImplBlock *parent = impl_item.first; |
| 1943 |
HIR::Function *fn = impl_item.second; |
| 1944 |
|
| 1945 |
bool is_deref = lang_item_type == LangItem::Kind::DEREF |
| 1946 |
|| lang_item_type == LangItem::Kind::DEREF_MUT; |
| 1947 |
bool is_deref_match = fn->get_function_name ().as_string ().compare ( |
| 1948 |
LangItem::ToString (LangItem::Kind::DEREF)) |
| 1949 |
== 0 |
| 1950 |
|| fn->get_function_name ().as_string ().compare ( |
| 1951 |
LangItem::ToString (LangItem::Kind::DEREF_MUT)) |
| 1952 |
== 0; |
| 1953 |
|
| 1954 |
bool is_recursive_op |
| 1955 |
= fn->get_function_name ().as_string ().compare (associated_item_name) |
| 1956 |
== 0 |
| 1957 |
|| (is_deref && is_deref_match); |
| 1958 |
if (parent->has_trait_ref () && is_recursive_op) |
| 1959 |
{ |
| 1960 |
TraitReference *trait_reference |
| 1961 |
= TraitResolver::Lookup (parent->get_trait_ref ()); |
| 1962 |
if (!trait_reference->is_error ()) |
| 1963 |
{ |
| 1964 |
TyTy::BaseType *lookup = nullptr; |
| 1965 |
bool ok = context->lookup_type (fn->get_mappings ().get_hirid (), |
| 1966 |
&lookup); |
| 1967 |
rust_assert (ok); |
| 1968 |
rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF); |
| 1969 |
|
| 1970 |
TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup); |
| 1971 |
rust_assert (fntype->is_method ()); |
| 1972 |
|
| 1973 |
bool is_lang_item_impl |
| 1974 |
= trait_reference->get_mappings ().get_defid () |
| 1975 |
== respective_lang_item_id |
| 1976 |
|| (is_deref && is_deref_match); |
| 1977 |
bool self_is_lang_item_self |
| 1978 |
= fntype->get_self_type ()->is_equal (*adjusted_self); |
| 1979 |
bool recursive_operator_overload |
| 1980 |
= is_lang_item_impl && self_is_lang_item_self; |
| 1981 |
|
| 1982 |
if (recursive_operator_overload) |
| 1983 |
return false; |
| 1984 |
} |
| 1985 |
} |
| 1986 |
} |
| 1987 |
|
| 1988 |
// we found a valid operator overload |
| 1989 |
fn->prepare_higher_ranked_bounds (); |
| 1990 |
rust_debug_loc (expr.get_locus (), "resolved operator overload to: {%u} {%s}", |
| 1991 |
candidate.candidate.ty->get_ref (), |
| 1992 |
candidate.candidate.ty->debug_str ().c_str ()); |
| 1993 |
|
| 1994 |
// handle generics |
| 1995 |
if (lookup->needs_generic_substitutions ()) |
| 1996 |
lookup = SubstMapper::InferSubst (lookup, expr.get_locus ()); |
| 1997 |
|
| 1998 |
// type check the arguments if required |
| 1999 |
TyTy::FnType *type = static_cast<TyTy::FnType *> (lookup); |
| 2000 |
rust_assert (type->num_params () > 0); |
| 2001 |
auto &fnparam = type->param_at (0); |
| 2002 |
|
| 2003 |
// typecheck the self |
| 2004 |
unify_site (expr.get_mappings ().get_hirid (), |
| 2005 |
TyTy::TyWithLocation (fnparam.get_type ()), |
| 2006 |
TyTy::TyWithLocation (adjusted_self), expr.get_locus ()); |
| 2007 |
if (rhs == nullptr) |
| 2008 |
{ |
| 2009 |
rust_assert (type->num_params () == 1); |
| 2010 |
} |
| 2011 |
else |
| 2012 |
{ |
| 2013 |
rust_assert (type->num_params () == 2); |
| 2014 |
auto &fnparam = type->param_at (1); |
| 2015 |
unify_site (expr.get_mappings ().get_hirid (), |
| 2016 |
TyTy::TyWithLocation (fnparam.get_type ()), |
| 2017 |
TyTy::TyWithLocation (rhs), expr.get_locus ()); |
| 2018 |
} |
| 2019 |
|
| 2020 |
rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF); |
| 2021 |
fn = static_cast<TyTy::FnType *> (lookup); |
| 2022 |
fn->monomorphize (); |
| 2023 |
|
| 2024 |
// get the return type |
| 2025 |
TyTy::BaseType *function_ret_tyty |
| 2026 |
= type->get_return_type ()->monomorphized_clone (); |
| 2027 |
|
| 2028 |
// store the expected fntype |
| 2029 |
context->insert_operator_overload (expr.get_mappings ().get_hirid (), type); |
| 2030 |
|
| 2031 |
// set up the resolved name on the path |
| 2032 |
if (flag_name_resolution_2_0) |
| 2033 |
{ |
| 2034 |
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> ( |
| 2035 |
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()); |
| 2036 |
|
| 2037 |
nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()), |
| 2038 |
Resolver2_0::Definition (resolved_node_id)); |
| 2039 |
} |
| 2040 |
else |
| 2041 |
{ |
| 2042 |
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (), |
| 2043 |
resolved_node_id); |
| 2044 |
} |
| 2045 |
|
| 2046 |
// return the result of the function back |
| 2047 |
infered = function_ret_tyty; |
| 2048 |
|
| 2049 |
return true; |
| 2050 |
} |
| 2051 |
|
| 2052 |
HIR::PathIdentSegment |
| 2053 |
TypeCheckExpr::resolve_possible_fn_trait_call_method_name ( |
| 2054 |
TyTy::BaseType &receiver, TyTy::TypeBoundPredicate *associated_predicate) |
| 2055 |
{ |
| 2056 |
// Question |
| 2057 |
// do we need to probe possible bounds here? I think not, i think when we |
| 2058 |
// support Fn traits they are explicitly specified |
| 2059 |
|
| 2060 |
// FIXME |
| 2061 |
// the logic to map the FnTrait to their respective call trait-item is |
| 2062 |
// duplicated over in the backend/rust-compile-expr.cc |
| 2063 |
for (auto &bound : receiver.get_specified_bounds ()) |
| 2064 |
{ |
| 2065 |
bool found_fn = bound.get_name ().compare ("Fn") == 0; |
| 2066 |
bool found_fn_mut = bound.get_name ().compare ("FnMut") == 0; |
| 2067 |
bool found_fn_once = bound.get_name ().compare ("FnOnce") == 0; |
| 2068 |
|
| 2069 |
if (found_fn) |
| 2070 |
{ |
| 2071 |
*associated_predicate = bound; |
| 2072 |
return HIR::PathIdentSegment ("call"); |
| 2073 |
} |
| 2074 |
else if (found_fn_mut) |
| 2075 |
{ |
| 2076 |
*associated_predicate = bound; |
| 2077 |
return HIR::PathIdentSegment ("call_mut"); |
| 2078 |
} |
| 2079 |
else if (found_fn_once) |
| 2080 |
{ |
| 2081 |
*associated_predicate = bound; |
| 2082 |
return HIR::PathIdentSegment ("call_once"); |
| 2083 |
} |
| 2084 |
} |
| 2085 |
|
| 2086 |
// nothing |
| 2087 |
*associated_predicate = TyTy::TypeBoundPredicate::error (); |
| 2088 |
return HIR::PathIdentSegment (""); |
| 2089 |
} |
| 2090 |
|
| 2091 |
bool |
| 2092 |
TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr, |
| 2093 |
TyTy::BaseType *receiver_tyty, |
| 2094 |
TyTy::BaseType **result) |
| 2095 |
{ |
| 2096 |
// we turn this into a method call expr |
| 2097 |
// TODO: add implicit self argument (?) |
| 2098 |
auto associated_predicate = TyTy::TypeBoundPredicate::error (); |
| 2099 |
HIR::PathIdentSegment method_name |
| 2100 |
= resolve_possible_fn_trait_call_method_name (*receiver_tyty, |
| 2101 |
&associated_predicate); |
| 2102 |
if (method_name.is_error () || associated_predicate.is_error ()) |
| 2103 |
return false; |
| 2104 |
|
| 2105 |
auto candidates = MethodResolver::Probe (receiver_tyty, method_name); |
| 2106 |
if (candidates.empty ()) |
| 2107 |
return false; |
| 2108 |
|
| 2109 |
if (candidates.size () > 1) |
| 2110 |
{ |
| 2111 |
rich_location r (line_table, expr.get_locus ()); |
| 2112 |
for (auto &c : candidates) |
| 2113 |
r.add_range (c.candidate.locus); |
| 2114 |
|
| 2115 |
rust_error_at ( |
| 2116 |
r, "multiple candidates found for function trait method call %qs", |
| 2117 |
method_name.as_string ().c_str ()); |
| 2118 |
return false; |
| 2119 |
} |
| 2120 |
|
| 2121 |
if (receiver_tyty->get_kind () == TyTy::TypeKind::CLOSURE) |
| 2122 |
{ |
| 2123 |
const TyTy::ClosureType &closure |
| 2124 |
= static_cast<TyTy::ClosureType &> (*receiver_tyty); |
| 2125 |
closure.setup_fn_once_output (); |
| 2126 |
} |
| 2127 |
|
| 2128 |
auto candidate = *candidates.begin (); |
| 2129 |
rust_debug_loc (expr.get_locus (), |
| 2130 |
"resolved call-expr to fn trait: {%u} {%s}", |
| 2131 |
candidate.candidate.ty->get_ref (), |
| 2132 |
candidate.candidate.ty->debug_str ().c_str ()); |
| 2133 |
|
| 2134 |
// Get the adjusted self |
| 2135 |
Adjuster adj (receiver_tyty); |
| 2136 |
TyTy::BaseType *adjusted_self = adj.adjust_type (candidate.adjustments); |
| 2137 |
|
| 2138 |
// store the adjustments for code-generation to know what to do which must be |
| 2139 |
// stored onto the receiver to so as we don't trigger duplicate deref mappings |
| 2140 |
// ICE when an argument is a method call |
| 2141 |
HIR::Expr &fnexpr = expr.get_fnexpr (); |
| 2142 |
HirId autoderef_mappings_id = fnexpr.get_mappings ().get_hirid (); |
| 2143 |
context->insert_autoderef_mappings (autoderef_mappings_id, |
| 2144 |
std::move (candidate.adjustments)); |
| 2145 |
|
| 2146 |
PathProbeCandidate &resolved_candidate = candidate.candidate; |
| 2147 |
TyTy::BaseType *lookup_tyty = candidate.candidate.ty; |
| 2148 |
NodeId resolved_node_id |
| 2149 |
= resolved_candidate.is_impl_candidate () |
| 2150 |
? resolved_candidate.item.impl.impl_item->get_impl_mappings () |
| 2151 |
.get_nodeid () |
| 2152 |
: resolved_candidate.item.trait.item_ref->get_mappings ().get_nodeid (); |
| 2153 |
|
| 2154 |
if (lookup_tyty->get_kind () != TyTy::TypeKind::FNDEF) |
| 2155 |
{ |
| 2156 |
rich_location r (line_table, expr.get_locus ()); |
| 2157 |
r.add_range (resolved_candidate.locus); |
| 2158 |
rust_error_at (r, "associated impl item is not a method"); |
| 2159 |
return false; |
| 2160 |
} |
| 2161 |
|
| 2162 |
TyTy::BaseType *lookup = lookup_tyty; |
| 2163 |
TyTy::FnType *fn = static_cast<TyTy::FnType *> (lookup); |
| 2164 |
if (!fn->is_method ()) |
| 2165 |
{ |
| 2166 |
rich_location r (line_table, expr.get_locus ()); |
| 2167 |
r.add_range (resolved_candidate.locus); |
| 2168 |
rust_error_at (r, "associated function is not a method"); |
| 2169 |
return false; |
| 2170 |
} |
| 2171 |
|
| 2172 |
// fn traits only support tuple argument passing so we need to implicitly set |
| 2173 |
// this up to get the same type checking we get in the rest of the pipeline |
| 2174 |
|
| 2175 |
std::vector<TyTy::TyVar> call_args; |
| 2176 |
for (auto &arg : expr.get_arguments ()) |
| 2177 |
{ |
| 2178 |
TyTy::BaseType *a = TypeCheckExpr::Resolve (*arg); |
| 2179 |
call_args.push_back (TyTy::TyVar (a->get_ref ())); |
| 2180 |
} |
| 2181 |
|
| 2182 |
// crate implicit tuple |
| 2183 |
HirId implicit_arg_id = mappings.get_next_hir_id (); |
| 2184 |
Analysis::NodeMapping mapping (mappings.get_current_crate (), UNKNOWN_NODEID, |
| 2185 |
implicit_arg_id, UNKNOWN_LOCAL_DEFID); |
| 2186 |
|
| 2187 |
TyTy::TupleType *tuple |
| 2188 |
= new TyTy::TupleType (implicit_arg_id, expr.get_locus (), call_args); |
| 2189 |
context->insert_implicit_type (implicit_arg_id, tuple); |
| 2190 |
|
| 2191 |
std::vector<TyTy::Argument> args; |
| 2192 |
TyTy::Argument a (mapping, tuple, |
| 2193 |
expr.get_locus () /*FIXME is there a better location*/); |
| 2194 |
args.push_back (std::move (a)); |
| 2195 |
|
| 2196 |
TyTy::BaseType *function_ret_tyty |
| 2197 |
= TyTy::TypeCheckMethodCallExpr::go (fn, expr.get_mappings (), args, |
| 2198 |
expr.get_locus (), expr.get_locus (), |
| 2199 |
adjusted_self, context); |
| 2200 |
if (function_ret_tyty == nullptr |
| 2201 |
|| function_ret_tyty->get_kind () == TyTy::TypeKind::ERROR) |
| 2202 |
{ |
| 2203 |
rust_error_at (expr.get_locus (), |
| 2204 |
"failed check fn trait call-expr MethodCallExpr"); |
| 2205 |
return false; |
| 2206 |
} |
| 2207 |
|
| 2208 |
// store the expected fntype |
| 2209 |
context->insert_operator_overload (expr.get_mappings ().get_hirid (), fn); |
| 2210 |
|
| 2211 |
// set up the resolved name on the path |
| 2212 |
if (flag_name_resolution_2_0) |
| 2213 |
{ |
| 2214 |
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> ( |
| 2215 |
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()); |
| 2216 |
|
| 2217 |
auto existing = nr_ctx.lookup (expr.get_mappings ().get_nodeid ()); |
| 2218 |
if (existing) |
| 2219 |
rust_assert (*existing == resolved_node_id); |
| 2220 |
else |
| 2221 |
nr_ctx.map_usage (Resolver2_0::Usage ( |
| 2222 |
expr.get_mappings ().get_nodeid ()), |
| 2223 |
Resolver2_0::Definition (resolved_node_id)); |
| 2224 |
} |
| 2225 |
else |
| 2226 |
{ |
| 2227 |
NodeId existing = UNKNOWN_NODEID; |
| 2228 |
bool ok |
| 2229 |
= resolver->lookup_resolved_name (expr.get_mappings ().get_nodeid (), |
| 2230 |
&existing); |
| 2231 |
|
| 2232 |
if (ok) |
| 2233 |
rust_assert (existing == resolved_node_id); |
| 2234 |
else |
| 2235 |
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (), |
| 2236 |
resolved_node_id); |
| 2237 |
} |
| 2238 |
|
| 2239 |
// return the result of the function back |
| 2240 |
*result = function_ret_tyty; |
| 2241 |
|
| 2242 |
return true; |
| 2243 |
} |
| 2244 |
|
| 2245 |
bool |
| 2246 |
TypeCheckExpr::validate_arithmetic_type ( |
| 2247 |
const TyTy::BaseType *tyty, HIR::ArithmeticOrLogicalExpr::ExprType expr_type) |
| 2248 |
{ |
| 2249 |
const TyTy::BaseType *type = tyty->destructure (); |
| 2250 |
|
| 2251 |
// https://doc.rust-lang.org/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators |
| 2252 |
// this will change later when traits are added |
| 2253 |
switch (expr_type) |
| 2254 |
{ |
| 2255 |
case ArithmeticOrLogicalOperator::ADD: |
| 2256 |
case ArithmeticOrLogicalOperator::SUBTRACT: |
| 2257 |
case ArithmeticOrLogicalOperator::MULTIPLY: |
| 2258 |
case ArithmeticOrLogicalOperator::DIVIDE: |
| 2259 |
case ArithmeticOrLogicalOperator::MODULUS: |
| 2260 |
return (type->get_kind () == TyTy::TypeKind::INT) |
| 2261 |
|| (type->get_kind () == TyTy::TypeKind::UINT) |
| 2262 |
|| (type->get_kind () == TyTy::TypeKind::FLOAT) |
| 2263 |
|| (type->get_kind () == TyTy::TypeKind::USIZE) |
| 2264 |
|| (type->get_kind () == TyTy::TypeKind::ISIZE) |
| 2265 |
|| (type->get_kind () == TyTy::TypeKind::INFER |
| 2266 |
&& (((const TyTy::InferType *) type)->get_infer_kind () |
| 2267 |
== TyTy::InferType::INTEGRAL)) |
| 2268 |
|| (type->get_kind () == TyTy::TypeKind::INFER |
| 2269 |
&& (((const TyTy::InferType *) type)->get_infer_kind () |
| 2270 |
== TyTy::InferType::FLOAT)); |
| 2271 |
|
| 2272 |
// integers or bools |
| 2273 |
case ArithmeticOrLogicalOperator::BITWISE_AND: |
| 2274 |
case ArithmeticOrLogicalOperator::BITWISE_OR: |
| 2275 |
case ArithmeticOrLogicalOperator::BITWISE_XOR: |
| 2276 |
return (type->get_kind () == TyTy::TypeKind::INT) |
| 2277 |
|| (type->get_kind () == TyTy::TypeKind::UINT) |
| 2278 |
|| (type->get_kind () == TyTy::TypeKind::USIZE) |
| 2279 |
|| (type->get_kind () == TyTy::TypeKind::ISIZE) |
| 2280 |
|| (type->get_kind () == TyTy::TypeKind::BOOL) |
| 2281 |
|| (type->get_kind () == TyTy::TypeKind::INFER |
| 2282 |
&& (((const TyTy::InferType *) type)->get_infer_kind () |
| 2283 |
== TyTy::InferType::INTEGRAL)); |
| 2284 |
|
| 2285 |
// integers only |
| 2286 |
case ArithmeticOrLogicalOperator::LEFT_SHIFT: |
| 2287 |
case ArithmeticOrLogicalOperator::RIGHT_SHIFT: |
| 2288 |
return (type->get_kind () == TyTy::TypeKind::INT) |
| 2289 |
|| (type->get_kind () == TyTy::TypeKind::UINT) |
| 2290 |
|| (type->get_kind () == TyTy::TypeKind::USIZE) |
| 2291 |
|| (type->get_kind () == TyTy::TypeKind::ISIZE) |
| 2292 |
|| (type->get_kind () == TyTy::TypeKind::INFER |
| 2293 |
&& (((const TyTy::InferType *) type)->get_infer_kind () |
| 2294 |
== TyTy::InferType::INTEGRAL)); |
| 2295 |
} |
| 2296 |
|
| 2297 |
rust_unreachable (); |
| 2298 |
return false; |
| 2299 |
} |
| 2300 |
|
| 2301 |
} // namespace Resolver |
| 2302 |
} // namespace Rust |