Debian Codegraph @

gcc/rust/typecheck/rust-hir-type-check-item.cc

raw
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-item.h"
20 #include "optional.h"
21 #include "rust-canonical-path.h"
22 #include "rust-diagnostics.h"
23 #include "rust-hir-type-check-enumitem.h"
24 #include "rust-hir-type-check-implitem.h"
25 #include "rust-hir-type-check-type.h"
26 #include "rust-hir-type-check-expr.h"
27 #include "rust-hir-type-check-pattern.h"
28 #include "rust-hir-trait-resolve.h"
29 #include "rust-identifier.h"
30 #include "rust-session-manager.h"
31 #include "rust-immutable-name-resolution-context.h"
32 #include "rust-substitution-mapper.h"
33 #include "rust-type-util.h"
34 #include "rust-tyty-variance-analysis.h"
35
36 // for flag_name_resolution_2_0
37 #include "options.h"
38
39 namespace Rust {
40 namespace Resolver {
41
42 TypeCheckItem::TypeCheckItem () : TypeCheckBase (), infered (nullptr) {}
43
44 TyTy::BaseType *
45 TypeCheckItem::Resolve (HIR::Item &item)
46 {
47 // is it already resolved?
48 auto context = TypeCheckContext::get ();
49 TyTy::BaseType *resolved = nullptr;
50 bool already_resolved
51 = context->lookup_type (item.get_mappings ().get_hirid (), &resolved);
52 if (already_resolved)
53 return resolved;
54
55 rust_assert (item.get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM);
56 HIR::VisItem &vis_item = static_cast<HIR::VisItem &> (item);
57
58 TypeCheckItem resolver;
59 vis_item.accept_vis (resolver);
60 return resolver.infered;
61 }
62
63 TyTy::BaseType *
64 TypeCheckItem::ResolveImplItem (HIR::ImplBlock &impl_block, HIR::ImplItem &item)
65 {
66 TypeCheckItem resolver;
67 return resolver.resolve_impl_item (impl_block, item);
68 }
69
70 TyTy::BaseType *
71 TypeCheckItem::ResolveImplBlockSelf (HIR::ImplBlock &impl_block)
72 {
73 TypeCheckItem resolver;
74
75 bool failed_flag = false;
76 auto result
77 = resolver.resolve_impl_block_substitutions (impl_block, failed_flag);
78 if (failed_flag)
79 {
80 return new TyTy::ErrorType (impl_block.get_mappings ().get_hirid ());
81 }
82 std::vector<TyTy::SubstitutionParamMapping> substitutions
83 = std::move (result.first);
84 TyTy::RegionConstraints region_constraints = std::move (result.second);
85
86 return resolver.resolve_impl_block_self (impl_block);
87 }
88
89 TyTy::BaseType *
90 TypeCheckItem::ResolveImplBlockSelfWithInference (
91 HIR::ImplBlock &impl, location_t locus,
92 TyTy::SubstitutionArgumentMappings *infer_arguments)
93 {
94 TypeCheckItem resolver;
95
96 bool failed_flag = false;
97 auto result = resolver.resolve_impl_block_substitutions (impl, failed_flag);
98 if (failed_flag)
99 {
100 return new TyTy::ErrorType (impl.get_mappings ().get_hirid ());
101 }
102 std::vector<TyTy::SubstitutionParamMapping> substitutions
103 = std::move (result.first);
104 TyTy::RegionConstraints region_constraints = std::move (result.second);
105
106 // now that we have the param mappings we need to query the self type
107 TyTy::BaseType *self = resolver.resolve_impl_block_self (impl);
108
109 // nothing to do
110 if (substitutions.empty () || self->is_concrete ())
111 return self;
112
113 // generate inference variables for the subst-param-mappings
114 std::vector<TyTy::SubstitutionArg> args;
115 for (auto &p : substitutions)
116 {
117 if (p.needs_substitution ())
118 {
119 TyTy::TyVar infer_var = TyTy::TyVar::get_implicit_infer_var (locus);
120 args.push_back (TyTy::SubstitutionArg (&p, infer_var.get_tyty ()));
121 }
122 else
123 {
124 TyTy::ParamType *param = p.get_param_ty ();
125 TyTy::BaseType *resolved = param->destructure ();
126 args.push_back (TyTy::SubstitutionArg (&p, resolved));
127 }
128 }
129
130 // create argument mappings
131 *infer_arguments = TyTy::SubstitutionArgumentMappings (
132 std::move (args), {},
133 TyTy::SubstitutionArgumentMappings::regions_from_nullable_args (
134 infer_arguments),
135 locus);
136
137 TyTy::BaseType *infer = SubstMapperInternal::Resolve (self, *infer_arguments);
138
139 // we only need to apply to the bounds manually on types which dont bind
140 // generics
141 if (!infer->has_substitutions_defined ())
142 {
143 for (auto &bound : infer->get_specified_bounds ())
144 bound.handle_substitions (*infer_arguments);
145 }
146
147 return infer;
148 }
149
150 void
151 TypeCheckItem::visit (HIR::TypeAlias &alias)
152 {
153 TyTy::BaseType *actual_type
154 = TypeCheckType::Resolve (alias.get_type_aliased ());
155
156 context->insert_type (alias.get_mappings (), actual_type);
157
158 TyTy::RegionConstraints region_constraints;
159 for (auto &where_clause_item : alias.get_where_clause ().get_items ())
160 {
161 ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints);
162 }
163 infered = actual_type;
164 }
165
166 void
167 TypeCheckItem::visit (HIR::TupleStruct &struct_decl)
168 {
169 auto lifetime_pin = context->push_clean_lifetime_resolver ();
170
171 std::vector<TyTy::SubstitutionParamMapping> substitutions;
172 if (struct_decl.has_generics ())
173 resolve_generic_params (struct_decl.get_generic_params (), substitutions);
174
175 TyTy::RegionConstraints region_constraints;
176 for (auto &where_clause_item : struct_decl.get_where_clause ().get_items ())
177 {
178 ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints);
179 }
180
181 std::vector<TyTy::StructFieldType *> fields;
182 size_t idx = 0;
183 for (auto &field : struct_decl.get_fields ())
184 {
185 TyTy::BaseType *field_type
186 = TypeCheckType::Resolve (field.get_field_type ());
187 auto *ty_field
188 = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
189 std::to_string (idx), field_type,
190 field.get_locus ());
191 fields.push_back (ty_field);
192 context->insert_type (field.get_mappings (), ty_field->get_field_type ());
193 idx++;
194 }
195
196 // get the path
197
198 auto path = CanonicalPath::create_empty ();
199
200 // FIXME: HACK: ARTHUR: Disgusting
201 if (flag_name_resolution_2_0)
202 {
203 auto &nr_ctx
204 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
205
206 path = nr_ctx.values
207 .to_canonical_path (struct_decl.get_mappings ().get_nodeid ())
208 .value ();
209 }
210 else
211 {
212 path
213 = mappings
214 .lookup_canonical_path (struct_decl.get_mappings ().get_nodeid ())
215 .value ();
216 }
217
218 RustIdent ident{path, struct_decl.get_locus ()};
219
220 // its a single variant ADT
221 std::vector<TyTy::VariantDef *> variants;
222 variants.push_back (
223 new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (),
224 struct_decl.get_mappings ().get_defid (),
225 struct_decl.get_identifier ().as_string (), ident,
226 TyTy::VariantDef::VariantType::TUPLE, tl::nullopt,
227 std::move (fields)));
228
229 // Process #[repr(X)] attribute, if any
230 const AST::AttrVec &attrs = struct_decl.get_outer_attrs ();
231 TyTy::ADTType::ReprOptions repr
232 = parse_repr_options (attrs, struct_decl.get_locus ());
233
234 auto *type = new TyTy::ADTType (
235 struct_decl.get_mappings ().get_defid (),
236 struct_decl.get_mappings ().get_hirid (),
237 struct_decl.get_mappings ().get_hirid (),
238 struct_decl.get_identifier ().as_string (), ident,
239 TyTy::ADTType::ADTKind::TUPLE_STRUCT, std::move (variants),
240 std::move (substitutions), repr,
241 TyTy::SubstitutionArgumentMappings::empty (
242 context->get_lifetime_resolver ().get_num_bound_regions ()),
243 region_constraints);
244
245 context->insert_type (struct_decl.get_mappings (), type);
246 infered = type;
247
248 context->get_variance_analysis_ctx ().add_type_constraints (*type);
249 }
250
251 void
252 TypeCheckItem::visit (HIR::StructStruct &struct_decl)
253 {
254 auto lifetime_pin = context->push_clean_lifetime_resolver ();
255
256 std::vector<TyTy::SubstitutionParamMapping> substitutions;
257 if (struct_decl.has_generics ())
258 resolve_generic_params (struct_decl.get_generic_params (), substitutions);
259
260 TyTy::RegionConstraints region_constraints;
261 for (auto &where_clause_item : struct_decl.get_where_clause ().get_items ())
262 {
263 ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints);
264 }
265
266 std::vector<TyTy::StructFieldType *> fields;
267 for (auto &field : struct_decl.get_fields ())
268 {
269 TyTy::BaseType *field_type
270 = TypeCheckType::Resolve (field.get_field_type ());
271 auto *ty_field
272 = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
273 field.get_field_name ().as_string (),
274 field_type, field.get_locus ());
275 fields.push_back (ty_field);
276 context->insert_type (field.get_mappings (), ty_field->get_field_type ());
277 }
278
279 auto path = CanonicalPath::create_empty ();
280
281 // FIXME: HACK: ARTHUR: Disgusting
282 if (flag_name_resolution_2_0)
283 {
284 auto &nr_ctx
285 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
286 auto canonical_path = nr_ctx.types.to_canonical_path (
287 struct_decl.get_mappings ().get_nodeid ());
288
289 if (!canonical_path.has_value ())
290 rust_unreachable ();
291 path = canonical_path.value ();
292 }
293 else
294 {
295 path
296 = mappings
297 .lookup_canonical_path (struct_decl.get_mappings ().get_nodeid ())
298 .value ();
299 }
300
301 RustIdent ident{path, struct_decl.get_locus ()};
302
303 // its a single variant ADT
304 std::vector<TyTy::VariantDef *> variants;
305 variants.push_back (
306 new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (),
307 struct_decl.get_mappings ().get_defid (),
308 struct_decl.get_identifier ().as_string (), ident,
309 TyTy::VariantDef::VariantType::STRUCT, tl::nullopt,
310 std::move (fields)));
311
312 // Process #[repr(X)] attribute, if any
313 const AST::AttrVec &attrs = struct_decl.get_outer_attrs ();
314 TyTy::ADTType::ReprOptions repr
315 = parse_repr_options (attrs, struct_decl.get_locus ());
316
317 auto *type = new TyTy::ADTType (
318 struct_decl.get_mappings ().get_defid (),
319 struct_decl.get_mappings ().get_hirid (),
320 struct_decl.get_mappings ().get_hirid (),
321 struct_decl.get_identifier ().as_string (), ident,
322 TyTy::ADTType::ADTKind::STRUCT_STRUCT, std::move (variants),
323 std::move (substitutions), repr,
324 TyTy::SubstitutionArgumentMappings::empty (
325 context->get_lifetime_resolver ().get_num_bound_regions ()),
326 region_constraints);
327
328 context->insert_type (struct_decl.get_mappings (), type);
329 infered = type;
330
331 context->get_variance_analysis_ctx ().add_type_constraints (*type);
332 }
333
334 void
335 TypeCheckItem::visit (HIR::Enum &enum_decl)
336 {
337 auto lifetime_pin = context->push_clean_lifetime_resolver ();
338 std::vector<TyTy::SubstitutionParamMapping> substitutions;
339 if (enum_decl.has_generics ())
340 resolve_generic_params (enum_decl.get_generic_params (), substitutions);
341
342 // Process #[repr(X)] attribute, if any
343 const AST::AttrVec &attrs = enum_decl.get_outer_attrs ();
344 TyTy::ADTType::ReprOptions repr
345 = parse_repr_options (attrs, enum_decl.get_locus ());
346
347 std::vector<TyTy::VariantDef *> variants;
348 int64_t discriminant_value = 0;
349 for (auto &variant : enum_decl.get_variants ())
350 {
351 TyTy::VariantDef *field_type
352 = TypeCheckEnumItem::Resolve (*variant, discriminant_value);
353
354 discriminant_value++;
355 variants.push_back (field_type);
356 }
357
358 // get the path
359 tl::optional<CanonicalPath> canonical_path;
360
361 if (flag_name_resolution_2_0)
362 {
363 auto &nr_ctx
364 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
365
366 canonical_path = nr_ctx.types.to_canonical_path (
367 enum_decl.get_mappings ().get_nodeid ());
368 }
369 else
370 {
371 canonical_path = mappings.lookup_canonical_path (
372 enum_decl.get_mappings ().get_nodeid ());
373 }
374
375 rust_assert (canonical_path.has_value ());
376
377 RustIdent ident{*canonical_path, enum_decl.get_locus ()};
378
379 // multi variant ADT
380 auto *type
381 = new TyTy::ADTType (enum_decl.get_mappings ().get_defid (),
382 enum_decl.get_mappings ().get_hirid (),
383 enum_decl.get_mappings ().get_hirid (),
384 enum_decl.get_identifier ().as_string (), ident,
385 TyTy::ADTType::ADTKind::ENUM, std::move (variants),
386 std::move (substitutions), repr);
387
388 context->insert_type (enum_decl.get_mappings (), type);
389 infered = type;
390
391 context->get_variance_analysis_ctx ().add_type_constraints (*type);
392 }
393
394 void
395 TypeCheckItem::visit (HIR::Union &union_decl)
396 {
397 auto lifetime_pin = context->push_clean_lifetime_resolver ();
398 std::vector<TyTy::SubstitutionParamMapping> substitutions;
399 if (union_decl.has_generics ())
400 resolve_generic_params (union_decl.get_generic_params (), substitutions);
401
402 TyTy::RegionConstraints region_constraints;
403 for (auto &where_clause_item : union_decl.get_where_clause ().get_items ())
404 {
405 ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints);
406 }
407
408 std::vector<TyTy::StructFieldType *> fields;
409 for (auto &variant : union_decl.get_variants ())
410 {
411 TyTy::BaseType *variant_type
412 = TypeCheckType::Resolve (variant.get_field_type ());
413 auto *ty_variant
414 = new TyTy::StructFieldType (variant.get_mappings ().get_hirid (),
415 variant.get_field_name ().as_string (),
416 variant_type, variant.get_locus ());
417 fields.push_back (ty_variant);
418 context->insert_type (variant.get_mappings (),
419 ty_variant->get_field_type ());
420 }
421
422 // get the path
423 tl::optional<CanonicalPath> canonical_path;
424
425 if (flag_name_resolution_2_0)
426 {
427 auto &nr_ctx
428 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
429
430 canonical_path = nr_ctx.types.to_canonical_path (
431 union_decl.get_mappings ().get_nodeid ());
432 }
433 else
434 {
435 canonical_path = mappings.lookup_canonical_path (
436 union_decl.get_mappings ().get_nodeid ());
437 }
438
439 rust_assert (canonical_path.has_value ());
440
441 RustIdent ident{*canonical_path, union_decl.get_locus ()};
442
443 // there is only a single variant
444 std::vector<TyTy::VariantDef *> variants;
445 variants.push_back (
446 new TyTy::VariantDef (union_decl.get_mappings ().get_hirid (),
447 union_decl.get_mappings ().get_defid (),
448 union_decl.get_identifier ().as_string (), ident,
449 TyTy::VariantDef::VariantType::STRUCT, tl::nullopt,
450 std::move (fields)));
451
452 auto *type
453 = new TyTy::ADTType (union_decl.get_mappings ().get_defid (),
454 union_decl.get_mappings ().get_hirid (),
455 union_decl.get_mappings ().get_hirid (),
456 union_decl.get_identifier ().as_string (), ident,
457 TyTy::ADTType::ADTKind::UNION, std::move (variants),
458 std::move (substitutions));
459
460 context->insert_type (union_decl.get_mappings (), type);
461 infered = type;
462
463 context->get_variance_analysis_ctx ().add_type_constraints (*type);
464 }
465
466 void
467 TypeCheckItem::visit (HIR::StaticItem &var)
468 {
469 TyTy::BaseType *type = TypeCheckType::Resolve (var.get_type ());
470 TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (var.get_expr ());
471
472 TyTy::BaseType *unified
473 = coercion_site (var.get_mappings ().get_hirid (),
474 TyTy::TyWithLocation (type, var.get_type ().get_locus ()),
475 TyTy::TyWithLocation (expr_type,
476 var.get_expr ().get_locus ()),
477 var.get_locus ());
478 context->insert_type (var.get_mappings (), unified);
479 infered = unified;
480 }
481
482 void
483 TypeCheckItem::visit (HIR::ConstantItem &constant)
484 {
485 TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ());
486 TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ());
487
488 TyTy::BaseType *unified = unify_site (
489 constant.get_mappings ().get_hirid (),
490 TyTy::TyWithLocation (type, constant.get_type ().get_locus ()),
491 TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()),
492 constant.get_locus ());
493 context->insert_type (constant.get_mappings (), unified);
494 infered = unified;
495 }
496
497 void
498 TypeCheckItem::visit (HIR::ImplBlock &impl_block)
499 {
500 auto binder_pin = context->push_clean_lifetime_resolver (true);
501
502 TraitReference *trait_reference = &TraitReference::error_node ();
503 if (impl_block.has_trait_ref ())
504 {
505 HIR::TypePath &ref = impl_block.get_trait_ref ();
506 trait_reference = TraitResolver::Resolve (ref);
507 if (trait_reference->is_error ())
508 return;
509 }
510
511 bool failed_flag = false;
512 auto result = resolve_impl_block_substitutions (impl_block, failed_flag);
513 if (failed_flag)
514 {
515 infered = new TyTy::ErrorType (impl_block.get_mappings ().get_hirid ());
516 return;
517 }
518 std::vector<TyTy::SubstitutionParamMapping> substitutions
519 = std::move (result.first);
520 TyTy::RegionConstraints region_constraints = std::move (result.second);
521
522 TyTy::BaseType *self = resolve_impl_block_self (impl_block);
523
524 // resolve each impl_item
525 for (auto &impl_item : impl_block.get_impl_items ())
526 {
527 TypeCheckImplItem::Resolve (impl_block, *impl_item, self, substitutions);
528 }
529
530 // validate the impl items
531 validate_trait_impl_block (trait_reference, impl_block, self, substitutions);
532 }
533
534 TyTy::BaseType *
535 TypeCheckItem::resolve_impl_item (HIR::ImplBlock &impl_block,
536 HIR::ImplItem &item)
537 {
538 bool failed_flag = false;
539 auto result = resolve_impl_block_substitutions (impl_block, failed_flag);
540 if (failed_flag)
541 {
542 return new TyTy::ErrorType (impl_block.get_mappings ().get_hirid ());
543 }
544
545 std::vector<TyTy::SubstitutionParamMapping> substitutions
546 = std::move (result.first);
547 TyTy::RegionConstraints region_constraints = std::move (result.second);
548
549 TyTy::BaseType *self = resolve_impl_block_self (impl_block);
550
551 return TypeCheckImplItem::Resolve (impl_block, item, self, substitutions);
552 }
553
554 void
555 TypeCheckItem::visit (HIR::Function &function)
556 {
557 auto lifetime_pin = context->push_clean_lifetime_resolver ();
558 std::vector<TyTy::SubstitutionParamMapping> substitutions;
559 if (function.has_generics ())
560 resolve_generic_params (function.get_generic_params (),
561 substitutions); // TODO resolve constraints
562
563 TyTy::RegionConstraints region_constraints;
564 for (auto &where_clause_item : function.get_where_clause ().get_items ())
565 {
566 ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints);
567 }
568
569 TyTy::BaseType *ret_type = nullptr;
570 if (!function.has_function_return_type ())
571 ret_type = TyTy::TupleType::get_unit_type ();
572 else
573 {
574 auto resolved = TypeCheckType::Resolve (function.get_return_type ());
575 if (resolved->get_kind () == TyTy::TypeKind::ERROR)
576 {
577 rust_error_at (function.get_locus (),
578 "failed to resolve return type");
579 return;
580 }
581
582 ret_type = resolved->clone ();
583 ret_type->set_ref (
584 function.get_return_type ().get_mappings ().get_hirid ());
585 }
586
587 std::vector<TyTy::FnParam> params;
588 for (auto &param : function.get_function_params ())
589 {
590 // get the name as well required for later on
591 auto param_tyty = TypeCheckType::Resolve (param.get_type ());
592 context->insert_type (param.get_mappings (), param_tyty);
593 TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
594 params.push_back (
595 TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
596 }
597
598 auto path = CanonicalPath::create_empty ();
599
600 // FIXME: HACK: ARTHUR: Disgusting
601 if (flag_name_resolution_2_0)
602 {
603 auto &nr_ctx
604 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
605 auto canonical_path = nr_ctx.values.to_canonical_path (
606 function.get_mappings ().get_nodeid ());
607
608 path = canonical_path.value ();
609 }
610 else
611 {
612 path = mappings
613 .lookup_canonical_path (function.get_mappings ().get_nodeid ())
614 .value ();
615 }
616
617 RustIdent ident{path, function.get_locus ()};
618
619 auto fn_type = new TyTy::FnType (
620 function.get_mappings ().get_hirid (),
621 function.get_mappings ().get_defid (),
622 function.get_function_name ().as_string (), ident,
623 TyTy::FnType::FNTYPE_DEFAULT_FLAGS, ABI::RUST, std::move (params), ret_type,
624 std::move (substitutions),
625 TyTy::SubstitutionArgumentMappings::empty (
626 context->get_lifetime_resolver ().get_num_bound_regions ()),
627 region_constraints);
628
629 context->insert_type (function.get_mappings (), fn_type);
630
631 // need to get the return type from this
632 TyTy::FnType *resolved_fn_type = fn_type;
633 auto expected_ret_tyty = resolved_fn_type->get_return_type ();
634 context->push_return_type (TypeCheckContextItem (&function),
635 expected_ret_tyty);
636
637 context->switch_to_fn_body ();
638 auto block_expr_ty = TypeCheckExpr::Resolve (function.get_definition ());
639
640 location_t fn_return_locus = function.has_function_return_type ()
641 ? function.get_return_type ().get_locus ()
642 : function.get_locus ();
643 coercion_site (function.get_definition ().get_mappings ().get_hirid (),
644 TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus),
645 TyTy::TyWithLocation (block_expr_ty),
646 function.get_definition ().get_locus ());
647
648 context->pop_return_type ();
649
650 infered = fn_type;
651 }
652
653 void
654 TypeCheckItem::visit (HIR::Module &module)
655 {
656 for (auto &item : module.get_items ())
657 TypeCheckItem::Resolve (*item);
658 }
659
660 void
661 TypeCheckItem::visit (HIR::Trait &trait)
662 {
663 if (trait.has_type_param_bounds ())
664 {
665 for (auto &tp_bound : trait.get_type_param_bounds ())
666 {
667 if (tp_bound.get ()->get_bound_type ()
668 == HIR::TypeParamBound::BoundType::TRAITBOUND)
669 {
670 HIR::TraitBound &tb
671 = static_cast<HIR::TraitBound &> (*tp_bound.get ());
672 if (tb.get_polarity () == BoundPolarity::AntiBound)
673 {
674 rust_error_at (tb.get_locus (),
675 "%<?Trait%> is not permitted in supertraits");
676 }
677 }
678 }
679 }
680
681 TraitReference *trait_ref = TraitResolver::Resolve (trait);
682 if (trait_ref->is_error ())
683 {
684 infered = new TyTy::ErrorType (trait.get_mappings ().get_hirid ());
685 return;
686 }
687
688 RustIdent ident{CanonicalPath::create_empty (), trait.get_locus ()};
689 infered = new TyTy::DynamicObjectType (
690 trait.get_mappings ().get_hirid (), ident,
691 {TyTy::TypeBoundPredicate (*trait_ref, BoundPolarity::RegularBound,
692 trait.get_locus ())});
693 }
694
695 void
696 TypeCheckItem::visit (HIR::ExternBlock &extern_block)
697 {
698 for (auto &item : extern_block.get_extern_items ())
699 {
700 TypeCheckTopLevelExternItem::Resolve (*item, extern_block);
701 }
702 }
703
704 std::pair<std::vector<TyTy::SubstitutionParamMapping>, TyTy::RegionConstraints>
705 TypeCheckItem::resolve_impl_block_substitutions (HIR::ImplBlock &impl_block,
706 bool &failure_flag)
707 {
708 std::vector<TyTy::SubstitutionParamMapping> substitutions;
709 if (impl_block.has_generics ())
710 resolve_generic_params (impl_block.get_generic_params (), substitutions);
711
712 TyTy::RegionConstraints region_constraints;
713 for (auto &where_clause_item : impl_block.get_where_clause ().get_items ())
714 {
715 ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints);
716 }
717
718 auto specified_bound = TyTy::TypeBoundPredicate::error ();
719 TraitReference *trait_reference = &TraitReference::error_node ();
720 if (impl_block.has_trait_ref ())
721 {
722 auto &ref = impl_block.get_trait_ref ();
723 trait_reference = TraitResolver::Resolve (ref);
724 rust_assert (!trait_reference->is_error ());
725
726 // we don't error out here see: gcc/testsuite/rust/compile/traits2.rs
727 // for example
728 specified_bound = get_predicate_from_bound (ref, impl_block.get_type (),
729 impl_block.get_polarity ());
730 }
731
732 TyTy::BaseType *self = TypeCheckType::Resolve (impl_block.get_type ());
733 if (self->is<TyTy::ErrorType> ())
734 {
735 // we cannot check for unconstrained type arguments when the Self type is
736 // not resolved it will just add extra errors that dont help as well as
737 // the case where this could just be a recursive type query that should
738 // fail and will work later on anyway
739 return {substitutions, region_constraints};
740 }
741
742 // inherit the bounds
743 if (!specified_bound.is_error ())
744 self->inherit_bounds ({specified_bound});
745
746 // check for any unconstrained type-params
747 const TyTy::SubstitutionArgumentMappings trait_constraints
748 = specified_bound.get_substitution_arguments ();
749 const TyTy::SubstitutionArgumentMappings impl_constraints
750 = GetUsedSubstArgs::From (self);
751
752 failure_flag = check_for_unconstrained (substitutions, trait_constraints,
753 impl_constraints, self);
754
755 return {substitutions, region_constraints};
756 }
757
758 void
759 TypeCheckItem::validate_trait_impl_block (
760 TraitReference *trait_reference, HIR::ImplBlock &impl_block,
761 TyTy::BaseType *self,
762 std::vector<TyTy::SubstitutionParamMapping> &substitutions)
763 {
764 auto specified_bound = TyTy::TypeBoundPredicate::error ();
765 if (impl_block.has_trait_ref ())
766 {
767 auto &ref = impl_block.get_trait_ref ();
768 trait_reference = TraitResolver::Resolve (ref);
769 if (trait_reference->is_error ())
770 return;
771
772 // we don't error out here see: gcc/testsuite/rust/compile/traits2.rs
773 // for example
774 specified_bound = get_predicate_from_bound (ref, impl_block.get_type (),
775 impl_block.get_polarity ());
776
777 // need to check that if this specified bound has super traits does this
778 // Self
779 // implement them?
780 specified_bound.validate_type_implements_super_traits (
781 *self, impl_block.get_type (), impl_block.get_trait_ref ());
782 }
783
784 bool is_trait_impl_block = !trait_reference->is_error ();
785 std::vector<const TraitItemReference *> trait_item_refs;
786 for (auto &impl_item : impl_block.get_impl_items ())
787 {
788 if (!specified_bound.is_error ())
789 {
790 auto trait_item_ref
791 = TypeCheckImplItemWithTrait::Resolve (impl_block, *impl_item, self,
792 specified_bound,
793 substitutions);
794 if (!trait_item_ref.is_error ())
795 trait_item_refs.push_back (trait_item_ref.get_raw_item ());
796 }
797 }
798
799 bool impl_block_missing_trait_items
800 = !specified_bound.is_error ()
801 && trait_reference->size () != trait_item_refs.size ();
802 if (impl_block_missing_trait_items
803 && impl_block.get_polarity () == BoundPolarity::RegularBound)
804 {
805 // filter the missing impl_items
806 std::vector<std::reference_wrapper<const TraitItemReference>>
807 missing_trait_items;
808 for (const auto &trait_item_ref : trait_reference->get_trait_items ())
809 {
810 bool found = false;
811 for (auto implemented_trait_item : trait_item_refs)
812 {
813 std::string trait_item_name = trait_item_ref.get_identifier ();
814 std::string impl_item_name
815 = implemented_trait_item->get_identifier ();
816 found = trait_item_name == impl_item_name;
817 if (found)
818 break;
819 }
820
821 bool is_required_trait_item = !trait_item_ref.is_optional ();
822 if (!found && is_required_trait_item)
823 missing_trait_items.emplace_back (trait_item_ref);
824 }
825
826 if (!missing_trait_items.empty ())
827 {
828 std::string missing_items_buf;
829 rich_location r (line_table, impl_block.get_locus ());
830 for (size_t i = 0; i < missing_trait_items.size (); i++)
831 {
832 bool has_more = (i + 1) < missing_trait_items.size ();
833 const TraitItemReference &missing_trait_item
834 = missing_trait_items.at (i);
835 missing_items_buf += missing_trait_item.get_identifier ()
836 + (has_more ? ", " : "");
837 r.add_range (missing_trait_item.get_locus ());
838 }
839
840 rust_error_at (r, ErrorCode::E0046,
841 "missing %s in implementation of trait %qs",
842 missing_items_buf.c_str (),
843 trait_reference->get_name ().c_str ());
844 }
845 }
846
847 if (is_trait_impl_block)
848 {
849 trait_reference->clear_associated_types ();
850
851 AssociatedImplTrait associated (trait_reference, specified_bound,
852 &impl_block, self, context);
853 context->insert_associated_trait_impl (
854 impl_block.get_mappings ().get_hirid (), std::move (associated));
855 context->insert_associated_impl_mapping (
856 trait_reference->get_mappings ().get_hirid (), self,
857 impl_block.get_mappings ().get_hirid ());
858 }
859 }
860
861 TyTy::BaseType *
862 TypeCheckItem::resolve_impl_block_self (HIR::ImplBlock &impl_block)
863 {
864 return TypeCheckType::Resolve (impl_block.get_type ());
865 }
866
867 } // namespace Resolver
868 } // namespace Rust