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