| 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-hir-type-check-base.h" |
| 20 |
#include "rust-hir-type-check-expr.h" |
| 21 |
#include "rust-hir-type-check-type.h" |
| 22 |
#include "rust-hir-trait-resolve.h" |
| 23 |
#include "rust-type-util.h" |
| 24 |
#include "rust-attribute-values.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace Resolver { |
| 28 |
|
| 29 |
TypeCheckBase::TypeCheckBase () |
| 30 |
: mappings (Analysis::Mappings::get ()), resolver (Resolver::get ()), |
| 31 |
context (TypeCheckContext::get ()) |
| 32 |
{} |
| 33 |
|
| 34 |
void |
| 35 |
TypeCheckBase::ResolveGenericParams ( |
| 36 |
const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params, |
| 37 |
std::vector<TyTy::SubstitutionParamMapping> &substitutions, bool is_foreign, |
| 38 |
ABI abi) |
| 39 |
{ |
| 40 |
TypeCheckBase ctx; |
| 41 |
ctx.resolve_generic_params (generic_params, substitutions, is_foreign, abi); |
| 42 |
} |
| 43 |
|
| 44 |
static void |
| 45 |
walk_types_to_constrain (std::set<HirId> &constrained_symbols, |
| 46 |
const TyTy::SubstitutionArgumentMappings &constraints) |
| 47 |
{ |
| 48 |
for (const auto &c : constraints.get_mappings ()) |
| 49 |
{ |
| 50 |
const TyTy::BaseType *arg = c.get_tyty (); |
| 51 |
if (arg != nullptr) |
| 52 |
{ |
| 53 |
const TyTy::BaseType *p = arg->get_root (); |
| 54 |
constrained_symbols.insert (p->get_ty_ref ()); |
| 55 |
if (p->has_substitutions_defined ()) |
| 56 |
{ |
| 57 |
walk_types_to_constrain (constrained_symbols, |
| 58 |
p->get_subst_argument_mappings ()); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
bool |
| 65 |
TypeCheckBase::check_for_unconstrained ( |
| 66 |
const std::vector<TyTy::SubstitutionParamMapping> ¶ms_to_constrain, |
| 67 |
const TyTy::SubstitutionArgumentMappings &constraint_a, |
| 68 |
const TyTy::SubstitutionArgumentMappings &constraint_b, |
| 69 |
const TyTy::BaseType *reference) |
| 70 |
{ |
| 71 |
bool check_result = false; |
| 72 |
bool check_completed |
| 73 |
= context->have_checked_for_unconstrained (reference->get_ref (), |
| 74 |
&check_result); |
| 75 |
if (check_completed) |
| 76 |
return check_result; |
| 77 |
|
| 78 |
std::set<HirId> symbols_to_constrain; |
| 79 |
std::map<HirId, location_t> symbol_to_location; |
| 80 |
for (const auto &p : params_to_constrain) |
| 81 |
{ |
| 82 |
HirId ref = p.get_param_ty ()->get_ref (); |
| 83 |
symbols_to_constrain.insert (ref); |
| 84 |
symbol_to_location.insert ({ref, p.get_param_locus ()}); |
| 85 |
|
| 86 |
rust_debug_loc (p.get_param_locus (), "XX constrain THIS"); |
| 87 |
} |
| 88 |
|
| 89 |
// set up the set of constrained symbols |
| 90 |
std::set<HirId> constrained_symbols; |
| 91 |
walk_types_to_constrain (constrained_symbols, constraint_a); |
| 92 |
walk_types_to_constrain (constrained_symbols, constraint_b); |
| 93 |
|
| 94 |
const auto root = reference->get_root (); |
| 95 |
if (root->get_kind () == TyTy::TypeKind::PARAM) |
| 96 |
{ |
| 97 |
const TyTy::ParamType *p = static_cast<const TyTy::ParamType *> (root); |
| 98 |
constrained_symbols.insert (p->get_ty_ref ()); |
| 99 |
} |
| 100 |
|
| 101 |
// check for unconstrained |
| 102 |
bool unconstrained = false; |
| 103 |
for (auto &sym : symbols_to_constrain) |
| 104 |
{ |
| 105 |
bool used = constrained_symbols.find (sym) != constrained_symbols.end (); |
| 106 |
if (!used) |
| 107 |
{ |
| 108 |
location_t locus = symbol_to_location.at (sym); |
| 109 |
rust_error_at (locus, "unconstrained type parameter"); |
| 110 |
unconstrained = true; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
context->insert_unconstrained_check_marker (reference->get_ref (), |
| 115 |
unconstrained); |
| 116 |
|
| 117 |
return unconstrained; |
| 118 |
} |
| 119 |
|
| 120 |
TyTy::BaseType * |
| 121 |
TypeCheckBase::resolve_literal (const Analysis::NodeMapping &expr_mappings, |
| 122 |
HIR::Literal &literal, location_t locus) |
| 123 |
{ |
| 124 |
TyTy::BaseType *infered = nullptr; |
| 125 |
switch (literal.get_lit_type ()) |
| 126 |
{ |
| 127 |
case HIR::Literal::LitType::INT: { |
| 128 |
bool ok = false; |
| 129 |
|
| 130 |
switch (literal.get_type_hint ()) |
| 131 |
{ |
| 132 |
case CORETYPE_I8: |
| 133 |
ok = context->lookup_builtin ("i8", &infered); |
| 134 |
break; |
| 135 |
case CORETYPE_I16: |
| 136 |
ok = context->lookup_builtin ("i16", &infered); |
| 137 |
break; |
| 138 |
case CORETYPE_I32: |
| 139 |
ok = context->lookup_builtin ("i32", &infered); |
| 140 |
break; |
| 141 |
case CORETYPE_I64: |
| 142 |
ok = context->lookup_builtin ("i64", &infered); |
| 143 |
break; |
| 144 |
case CORETYPE_I128: |
| 145 |
ok = context->lookup_builtin ("i128", &infered); |
| 146 |
break; |
| 147 |
|
| 148 |
case CORETYPE_U8: |
| 149 |
ok = context->lookup_builtin ("u8", &infered); |
| 150 |
break; |
| 151 |
case CORETYPE_U16: |
| 152 |
ok = context->lookup_builtin ("u16", &infered); |
| 153 |
break; |
| 154 |
case CORETYPE_U32: |
| 155 |
ok = context->lookup_builtin ("u32", &infered); |
| 156 |
break; |
| 157 |
case CORETYPE_U64: |
| 158 |
ok = context->lookup_builtin ("u64", &infered); |
| 159 |
break; |
| 160 |
case CORETYPE_U128: |
| 161 |
ok = context->lookup_builtin ("u128", &infered); |
| 162 |
break; |
| 163 |
|
| 164 |
case CORETYPE_F32: |
| 165 |
literal.set_lit_type (HIR::Literal::LitType::FLOAT); |
| 166 |
ok = context->lookup_builtin ("f32", &infered); |
| 167 |
break; |
| 168 |
case CORETYPE_F64: |
| 169 |
literal.set_lit_type (HIR::Literal::LitType::FLOAT); |
| 170 |
ok = context->lookup_builtin ("f64", &infered); |
| 171 |
break; |
| 172 |
|
| 173 |
case CORETYPE_ISIZE: |
| 174 |
ok = context->lookup_builtin ("isize", &infered); |
| 175 |
break; |
| 176 |
|
| 177 |
case CORETYPE_USIZE: |
| 178 |
ok = context->lookup_builtin ("usize", &infered); |
| 179 |
break; |
| 180 |
|
| 181 |
default: |
| 182 |
ok = true; |
| 183 |
infered |
| 184 |
= new TyTy::InferType (expr_mappings.get_hirid (), |
| 185 |
TyTy::InferType::InferTypeKind::INTEGRAL, |
| 186 |
TyTy::InferType::TypeHint::Default (), |
| 187 |
locus); |
| 188 |
break; |
| 189 |
} |
| 190 |
rust_assert (ok); |
| 191 |
} |
| 192 |
break; |
| 193 |
|
| 194 |
case HIR::Literal::LitType::FLOAT: { |
| 195 |
bool ok = false; |
| 196 |
|
| 197 |
switch (literal.get_type_hint ()) |
| 198 |
{ |
| 199 |
case CORETYPE_F32: |
| 200 |
ok = context->lookup_builtin ("f32", &infered); |
| 201 |
break; |
| 202 |
case CORETYPE_F64: |
| 203 |
ok = context->lookup_builtin ("f64", &infered); |
| 204 |
break; |
| 205 |
|
| 206 |
default: |
| 207 |
ok = true; |
| 208 |
infered |
| 209 |
= new TyTy::InferType (expr_mappings.get_hirid (), |
| 210 |
TyTy::InferType::InferTypeKind::FLOAT, |
| 211 |
TyTy::InferType::TypeHint::Default (), |
| 212 |
locus); |
| 213 |
break; |
| 214 |
} |
| 215 |
rust_assert (ok); |
| 216 |
} |
| 217 |
break; |
| 218 |
|
| 219 |
case HIR::Literal::LitType::BOOL: { |
| 220 |
auto ok = context->lookup_builtin ("bool", &infered); |
| 221 |
rust_assert (ok); |
| 222 |
} |
| 223 |
break; |
| 224 |
|
| 225 |
case HIR::Literal::LitType::CHAR: { |
| 226 |
auto ok = context->lookup_builtin ("char", &infered); |
| 227 |
rust_assert (ok); |
| 228 |
} |
| 229 |
break; |
| 230 |
|
| 231 |
case HIR::Literal::LitType::BYTE: { |
| 232 |
auto ok = context->lookup_builtin ("u8", &infered); |
| 233 |
rust_assert (ok); |
| 234 |
} |
| 235 |
break; |
| 236 |
|
| 237 |
case HIR::Literal::LitType::STRING: { |
| 238 |
TyTy::BaseType *base = nullptr; |
| 239 |
auto ok = context->lookup_builtin ("str", &base); |
| 240 |
rust_assert (ok); |
| 241 |
|
| 242 |
infered = new TyTy::ReferenceType (expr_mappings.get_hirid (), |
| 243 |
TyTy::TyVar (base->get_ref ()), |
| 244 |
Mutability::Imm, |
| 245 |
TyTy::Region::make_static ()); |
| 246 |
} |
| 247 |
break; |
| 248 |
|
| 249 |
case HIR::Literal::LitType::BYTE_STRING: { |
| 250 |
/* This is an arraytype of u8 reference (&[u8;size]). It isn't in |
| 251 |
UTF-8, but really just a byte array. Code to construct the array |
| 252 |
reference copied from ArrayElemsValues and ArrayType. */ |
| 253 |
TyTy::BaseType *u8; |
| 254 |
auto ok = context->lookup_builtin ("u8", &u8); |
| 255 |
rust_assert (ok); |
| 256 |
|
| 257 |
auto crate_num = mappings.get_current_crate (); |
| 258 |
Analysis::NodeMapping capacity_mapping (crate_num, UNKNOWN_NODEID, |
| 259 |
mappings.get_next_hir_id ( |
| 260 |
crate_num), |
| 261 |
UNKNOWN_LOCAL_DEFID); |
| 262 |
|
| 263 |
/* Capacity is the size of the string (number of chars). |
| 264 |
It is a constant, but for fold it to get a tree. */ |
| 265 |
std::string capacity_str |
| 266 |
= std::to_string (literal.as_string ().size ()); |
| 267 |
HIR::LiteralExpr *literal_capacity |
| 268 |
= new HIR::LiteralExpr (capacity_mapping, capacity_str, |
| 269 |
HIR::Literal::LitType::INT, |
| 270 |
PrimitiveCoreType::CORETYPE_USIZE, locus, {}); |
| 271 |
|
| 272 |
// mark the type for this implicit node |
| 273 |
TyTy::BaseType *expected_ty = nullptr; |
| 274 |
ok = context->lookup_builtin ("usize", &expected_ty); |
| 275 |
rust_assert (ok); |
| 276 |
context->insert_type (capacity_mapping, expected_ty); |
| 277 |
|
| 278 |
Analysis::NodeMapping array_mapping (crate_num, UNKNOWN_NODEID, |
| 279 |
mappings.get_next_hir_id ( |
| 280 |
crate_num), |
| 281 |
UNKNOWN_LOCAL_DEFID); |
| 282 |
|
| 283 |
TyTy::ArrayType *array |
| 284 |
= new TyTy::ArrayType (array_mapping.get_hirid (), locus, |
| 285 |
*literal_capacity, |
| 286 |
TyTy::TyVar (u8->get_ref ())); |
| 287 |
context->insert_type (array_mapping, array); |
| 288 |
|
| 289 |
infered = new TyTy::ReferenceType (expr_mappings.get_hirid (), |
| 290 |
TyTy::TyVar (array->get_ref ()), |
| 291 |
Mutability::Imm, |
| 292 |
TyTy::Region::make_static ()); |
| 293 |
} |
| 294 |
break; |
| 295 |
|
| 296 |
default: |
| 297 |
rust_unreachable (); |
| 298 |
break; |
| 299 |
} |
| 300 |
|
| 301 |
return infered; |
| 302 |
} |
| 303 |
|
| 304 |
TyTy::ADTType::ReprOptions |
| 305 |
TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus) |
| 306 |
{ |
| 307 |
TyTy::ADTType::ReprOptions repr; |
| 308 |
repr.pack = 0; |
| 309 |
repr.align = 0; |
| 310 |
|
| 311 |
// FIXME handle repr types.... |
| 312 |
bool ok = context->lookup_builtin ("isize", &repr.repr); |
| 313 |
rust_assert (ok); |
| 314 |
|
| 315 |
for (const auto &attr : attrs) |
| 316 |
{ |
| 317 |
bool is_repr = attr.get_path ().as_string () == Values::Attributes::REPR; |
| 318 |
if (is_repr && !attr.has_attr_input ()) |
| 319 |
{ |
| 320 |
rust_error_at (attr.get_locus (), "malformed %qs attribute", "repr"); |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
if (is_repr) |
| 325 |
{ |
| 326 |
const AST::AttrInput &input = attr.get_attr_input (); |
| 327 |
bool is_token_tree = input.get_attr_input_type () |
| 328 |
== AST::AttrInput::AttrInputType::TOKEN_TREE; |
| 329 |
rust_assert (is_token_tree); |
| 330 |
const auto &option = static_cast<const AST::DelimTokenTree &> (input); |
| 331 |
AST::AttrInputMetaItemContainer *meta_items |
| 332 |
= option.parse_to_meta_item (); |
| 333 |
|
| 334 |
if (meta_items == nullptr) |
| 335 |
{ |
| 336 |
rust_error_at (attr.get_locus (), "malformed %qs attribute", |
| 337 |
"repr"); |
| 338 |
continue; |
| 339 |
} |
| 340 |
|
| 341 |
auto &items = meta_items->get_items (); |
| 342 |
if (items.size () == 0) |
| 343 |
{ |
| 344 |
// nothing to do with this its empty |
| 345 |
delete meta_items; |
| 346 |
continue; |
| 347 |
} |
| 348 |
|
| 349 |
const std::string inline_option = items.at (0)->as_string (); |
| 350 |
|
| 351 |
// TODO: it would probably be better to make the MetaItems more aware |
| 352 |
// of constructs with nesting like #[repr(packed(2))] rather than |
| 353 |
// manually parsing the string "packed(2)" here. |
| 354 |
|
| 355 |
size_t oparen = inline_option.find ('(', 0); |
| 356 |
bool is_pack = false, is_align = false; |
| 357 |
unsigned char value = 1; |
| 358 |
|
| 359 |
if (oparen == std::string::npos) |
| 360 |
{ |
| 361 |
is_pack = inline_option.compare ("packed") == 0; |
| 362 |
is_align = inline_option.compare ("align") == 0; |
| 363 |
} |
| 364 |
|
| 365 |
else |
| 366 |
{ |
| 367 |
std::string rep = inline_option.substr (0, oparen); |
| 368 |
is_pack = rep.compare ("packed") == 0; |
| 369 |
is_align = rep.compare ("align") == 0; |
| 370 |
|
| 371 |
size_t cparen = inline_option.find (')', oparen); |
| 372 |
if (cparen == std::string::npos) |
| 373 |
{ |
| 374 |
rust_error_at (locus, "malformed attribute"); |
| 375 |
} |
| 376 |
|
| 377 |
std::string value_str = inline_option.substr (oparen, cparen); |
| 378 |
value = strtoul (value_str.c_str () + 1, NULL, 10); |
| 379 |
} |
| 380 |
|
| 381 |
if (is_pack) |
| 382 |
repr.pack = value; |
| 383 |
else if (is_align) |
| 384 |
repr.align = value; |
| 385 |
|
| 386 |
delete meta_items; |
| 387 |
|
| 388 |
// Multiple repr options must be specified with e.g. #[repr(C, |
| 389 |
// packed(2))]. |
| 390 |
break; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
return repr; |
| 395 |
} |
| 396 |
|
| 397 |
void |
| 398 |
TypeCheckBase::resolve_generic_params ( |
| 399 |
const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params, |
| 400 |
std::vector<TyTy::SubstitutionParamMapping> &substitutions, bool is_foreign, |
| 401 |
ABI abi) |
| 402 |
{ |
| 403 |
for (auto &generic_param : generic_params) |
| 404 |
{ |
| 405 |
switch (generic_param->get_kind ()) |
| 406 |
{ |
| 407 |
case HIR::GenericParam::GenericKind::LIFETIME: { |
| 408 |
auto lifetime_param |
| 409 |
= static_cast<HIR::LifetimeParam &> (*generic_param); |
| 410 |
auto lifetime = lifetime_param.get_lifetime (); |
| 411 |
context->get_lifetime_resolver ().insert_mapping ( |
| 412 |
context->intern_lifetime (lifetime)); |
| 413 |
} |
| 414 |
break; |
| 415 |
|
| 416 |
case HIR::GenericParam::GenericKind::CONST: { |
| 417 |
if (is_foreign && abi != Rust::ABI::INTRINSIC) |
| 418 |
{ |
| 419 |
rust_error_at (generic_param->get_locus (), ErrorCode::E0044, |
| 420 |
"foreign items may not have const parameters"); |
| 421 |
} |
| 422 |
|
| 423 |
auto ¶m |
| 424 |
= static_cast<HIR::ConstGenericParam &> (*generic_param); |
| 425 |
auto specified_type = TypeCheckType::Resolve (param.get_type ()); |
| 426 |
|
| 427 |
if (param.has_default_expression ()) |
| 428 |
{ |
| 429 |
auto expr_type |
| 430 |
= TypeCheckExpr::Resolve (param.get_default_expression ()); |
| 431 |
|
| 432 |
coercion_site (param.get_mappings ().get_hirid (), |
| 433 |
TyTy::TyWithLocation (specified_type), |
| 434 |
TyTy::TyWithLocation ( |
| 435 |
expr_type, |
| 436 |
param.get_default_expression ().get_locus ()), |
| 437 |
param.get_locus ()); |
| 438 |
} |
| 439 |
|
| 440 |
context->insert_type (generic_param->get_mappings (), |
| 441 |
specified_type); |
| 442 |
} |
| 443 |
break; |
| 444 |
|
| 445 |
case HIR::GenericParam::GenericKind::TYPE: { |
| 446 |
if (is_foreign && abi != Rust::ABI::INTRINSIC) |
| 447 |
{ |
| 448 |
rust_error_at (generic_param->get_locus (), ErrorCode::E0044, |
| 449 |
"foreign items may not have type parameters"); |
| 450 |
} |
| 451 |
|
| 452 |
auto param_type = TypeResolveGenericParam::Resolve ( |
| 453 |
*generic_param, false /*resolve_trait_bounds*/); |
| 454 |
context->insert_type (generic_param->get_mappings (), param_type); |
| 455 |
|
| 456 |
auto ¶m = static_cast<HIR::TypeParam &> (*generic_param); |
| 457 |
TyTy::SubstitutionParamMapping p (param, param_type); |
| 458 |
substitutions.push_back (p); |
| 459 |
} |
| 460 |
break; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
// now walk them to setup any specified type param bounds |
| 465 |
for (auto &subst : substitutions) |
| 466 |
{ |
| 467 |
auto pty = subst.get_param_ty (); |
| 468 |
TypeResolveGenericParam::ApplyAnyTraitBounds (subst.get_generic_param (), |
| 469 |
pty); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
TyTy::TypeBoundPredicate |
| 474 |
TypeCheckBase::get_marker_predicate (LangItem::Kind item_type, location_t locus) |
| 475 |
{ |
| 476 |
DefId item_id = mappings.get_lang_item (item_type, locus); |
| 477 |
HIR::Item *item = mappings.lookup_defid (item_id).value (); |
| 478 |
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait); |
| 479 |
|
| 480 |
HIR::Trait &trait = *static_cast<HIR::Trait *> (item); |
| 481 |
TraitReference *ref = TraitResolver::Resolve (trait); |
| 482 |
rust_assert (ref != nullptr); |
| 483 |
|
| 484 |
return TyTy::TypeBoundPredicate (*ref, BoundPolarity::RegularBound, locus); |
| 485 |
} |
| 486 |
|
| 487 |
} // namespace Resolver |
| 488 |
} // namespace Rust |