aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
blob: 8a9a3f35a321b112f3cff3764466548337c67693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// Copyright (C) 2020-2024 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include "rust-hir-type-check-implitem.h"
#include "rust-diagnostics.h"
#include "rust-hir-type-check-base.h"
#include "rust-hir-type-check-type.h"
#include "rust-hir-type-check-expr.h"
#include "rust-hir-type-check-pattern.h"
#include "rust-type-util.h"
#include "rust-tyty.h"

namespace Rust {
namespace Resolver {

TypeCheckTopLevelExternItem::TypeCheckTopLevelExternItem (
  const HIR::ExternBlock &parent)
  : TypeCheckBase (), parent (parent)
{}

TyTy::BaseType *
TypeCheckTopLevelExternItem::Resolve (HIR::ExternalItem *item,
				      const HIR::ExternBlock &parent)
{
  // is it already resolved?
  auto context = TypeCheckContext::get ();
  TyTy::BaseType *resolved = nullptr;
  bool already_resolved
    = context->lookup_type (item->get_mappings ().get_hirid (), &resolved);
  if (already_resolved)
    return resolved;

  TypeCheckTopLevelExternItem resolver (parent);
  item->accept_vis (resolver);
  return resolver.resolved;
}

void
TypeCheckTopLevelExternItem::visit (HIR::ExternalStaticItem &item)
{
  TyTy::BaseType *actual_type
    = TypeCheckType::Resolve (item.get_item_type ().get ());

  context->insert_type (item.get_mappings (), actual_type);
  resolved = actual_type;
}

void
TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)
{
  auto binder_pin = context->push_clean_lifetime_resolver ();

  std::vector<TyTy::SubstitutionParamMapping> substitutions;
  if (function.has_generics ())
    {
      for (auto &generic_param : function.get_generic_params ())
	{
	  switch (generic_param.get ()->get_kind ())
	    {
	    case HIR::GenericParam::GenericKind::LIFETIME:
	      context->intern_and_insert_lifetime (
		static_cast<HIR::LifetimeParam &> (*generic_param)
		  .get_lifetime ());
	      // TODO: handle bounds
	      break;
	    case HIR::GenericParam::GenericKind::CONST:
	      // FIXME: Skipping Lifetime and Const completely until better
	      // handling.
	      break;

	      case HIR::GenericParam::GenericKind::TYPE: {
		auto param_type
		  = TypeResolveGenericParam::Resolve (generic_param.get ());
		context->insert_type (generic_param->get_mappings (),
				      param_type);

		substitutions.push_back (TyTy::SubstitutionParamMapping (
		  static_cast<HIR::TypeParam &> (*generic_param), param_type));
	      }
	      break;
	    }
	}
    }

  TyTy::RegionConstraints region_constraints;
  if (function.has_where_clause ())
    {
      for (auto &where_clause_item : function.get_where_clause ().get_items ())
	{
	  ResolveWhereClauseItem::Resolve (*where_clause_item.get (),
					   region_constraints);
	}
    }

  TyTy::BaseType *ret_type = nullptr;
  if (!function.has_return_type ())
    ret_type
      = TyTy::TupleType::get_unit_type (function.get_mappings ().get_hirid ());
  else
    {
      auto resolved
	= TypeCheckType::Resolve (function.get_return_type ().get ());
      if (resolved == nullptr)
	{
	  rust_error_at (function.get_locus (),
			 "failed to resolve return type");
	  return;
	}

      ret_type = resolved->clone ();
      ret_type->set_ref (
	function.get_return_type ()->get_mappings ().get_hirid ());
    }

  std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
  for (auto &param : function.get_function_params ())
    {
      // get the name as well required for later on
      auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ());

      // these are implicit mappings and not used
      auto crate_num = mappings->get_current_crate ();
      Analysis::NodeMapping mapping (crate_num, mappings->get_next_node_id (),
				     mappings->get_next_hir_id (crate_num),
				     UNKNOWN_LOCAL_DEFID);

      HIR::IdentifierPattern *param_pattern
	= new HIR::IdentifierPattern (mapping, param.get_param_name (),
				      UNDEF_LOCATION, false, Mutability::Imm,
				      std::unique_ptr<HIR::Pattern> (nullptr));

      params.push_back (
	std::pair<HIR::Pattern *, TyTy::BaseType *> (param_pattern,
						     param_tyty));

      context->insert_type (param.get_mappings (), param_tyty);

      // FIXME do we need error checking for patterns here?
      // see https://github.com/Rust-GCC/gccrs/issues/995
    }

  uint8_t flags = TyTy::FnType::FNTYPE_IS_EXTERN_FLAG;
  if (function.is_variadic ())
    {
      flags |= TyTy::FnType::FNTYPE_IS_VARADIC_FLAG;
      if (parent.get_abi () != Rust::ABI::C)
	{
	  rust_error_at (
	    function.get_locus (), ErrorCode::E0045,
	    "C-variadic function must have C or cdecl calling convention");
	}
    }

  RustIdent ident{
    CanonicalPath::new_seg (function.get_mappings ().get_nodeid (),
			    function.get_item_name ().as_string ()),
    function.get_locus ()};

  auto fnType = new TyTy::FnType (
    function.get_mappings ().get_hirid (),
    function.get_mappings ().get_defid (),
    function.get_item_name ().as_string (), ident, flags, parent.get_abi (),
    std::move (params), ret_type, std::move (substitutions),
    TyTy::SubstitutionArgumentMappings::empty (
      context->get_lifetime_resolver ().get_num_bound_regions ()),
    region_constraints);

  context->insert_type (function.get_mappings (), fnType);
  resolved = fnType;
}

void
TypeCheckTopLevelExternItem::visit (HIR::ExternalTypeItem &type)
{
  rust_sorry_at (type.get_locus (), "extern types are not supported yet");
  //  auto binder_pin = context->push_clean_lifetime_resolver ();

  //  std::vector<TyTy::SubstitutionParamMapping> substitutions;
  //  if (function.has_generics ())
  //    {
  //      for (auto &generic_param : function.get_generic_params ())
  // {
  //   switch (generic_param.get ()->get_kind ())
  //     {
  //     case HIR::GenericParam::GenericKind::LIFETIME:
  //       context->intern_and_insert_lifetime (
  // 	static_cast<HIR::LifetimeParam &> (*generic_param)
  // 	  .get_lifetime ());
  //       // TODO: handle bounds
  //       break;
  //     case HIR::GenericParam::GenericKind::CONST:
  //       // FIXME: Skipping Lifetime and Const completely until better
  //       // handling.
  //       break;

  //       case HIR::GenericParam::GenericKind::TYPE: {
  // 	auto param_type
  // 	  = TypeResolveGenericParam::Resolve (generic_param.get ());
  // 	context->insert_type (generic_param->get_mappings (),
  // 			      param_type);

  // 	substitutions.push_back (TyTy::SubstitutionParamMapping (
  // 	  static_cast<HIR::TypeParam &> (*generic_param), param_type));
  //       }
  //       break;
  //     }
  // }
  //    }

  //  TyTy::RegionConstraints region_constraints;
  //  if (function.has_where_clause ())
  //    {
  //      for (auto &where_clause_item : function.get_where_clause ().get_items
  //      ())
  // {
  //   ResolveWhereClauseItem::Resolve (*where_clause_item.get (),
  // 				   region_constraints);
  // }
  //    }

  //  TyTy::BaseType *ret_type = nullptr;
  //  if (!function.has_return_type ())
  //    ret_type
  //      = TyTy::TupleType::get_unit_type (function.get_mappings ().get_hirid
  //      ());
  //  else
  //    {
  //      auto resolved
  // = TypeCheckType::Resolve (function.get_return_type ().get ());
  //      if (resolved == nullptr)
  // {
  //   rust_error_at (function.get_locus (),
  // 		 "failed to resolve return type");
  //   return;
  // }

  //      ret_type = resolved->clone ();
  //      ret_type->set_ref (
  // function.get_return_type ()->get_mappings ().get_hirid ());
  //    }

  //  std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
  //  for (auto &param : function.get_function_params ())
  //    {
  //      // get the name as well required for later on
  //      auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ());

  //      // these are implicit mappings and not used
  //      auto crate_num = mappings->get_current_crate ();
  //      Analysis::NodeMapping mapping (crate_num, mappings->get_next_node_id
  //      (),
  // 			     mappings->get_next_hir_id (crate_num),
  // 			     UNKNOWN_LOCAL_DEFID);

  //      HIR::IdentifierPattern *param_pattern
  // = new HIR::IdentifierPattern (mapping, param.get_param_name (),
  // 			      UNDEF_LOCATION, false, Mutability::Imm,
  // 			      std::unique_ptr<HIR::Pattern> (nullptr));

  //      params.push_back (
  // std::pair<HIR::Pattern *, TyTy::BaseType *> (param_pattern,
  // 					     param_tyty));

  //      context->insert_type (param.get_mappings (), param_tyty);

  //      // FIXME do we need error checking for patterns here?
  //      // see https://github.com/Rust-GCC/gccrs/issues/995
  //    }

  //  uint8_t flags = TyTy::FnType::FNTYPE_IS_EXTERN_FLAG;
  //  if (function.is_variadic ())
  //    {
  //      flags |= TyTy::FnType::FNTYPE_IS_VARADIC_FLAG;
  //      if (parent.get_abi () != Rust::ABI::C)
  // {
  //   rust_error_at (
  //     function.get_locus (), ErrorCode::E0045,
  //     "C-variadic function must have C or cdecl calling convention");
  // }
  //    }

  //  RustIdent ident{
  //    CanonicalPath::new_seg (function.get_mappings ().get_nodeid (),
  // 		    function.get_item_name ().as_string ()),
  //    function.get_locus ()};

  //  auto fnType = new TyTy::FnType (
  //    function.get_mappings ().get_hirid (),
  //    function.get_mappings ().get_defid (),
  //    function.get_item_name ().as_string (), ident, flags, parent.get_abi (),
  //    std::move (params), ret_type, std::move (substitutions),
  //    TyTy::SubstitutionArgumentMappings::empty (
  //      context->get_lifetime_resolver ().get_num_bound_regions ()),
  //    region_constraints);

  //  context->insert_type (function.get_mappings (), fnType);
  //  resolved = fnType;
}

TypeCheckImplItem::TypeCheckImplItem (
  HIR::ImplBlock *parent, TyTy::BaseType *self,
  std::vector<TyTy::SubstitutionParamMapping> substitutions)
  : TypeCheckBase (), parent (parent), self (self),
    substitutions (substitutions)
{}

TyTy::BaseType *
TypeCheckImplItem::Resolve (
  HIR::ImplBlock *parent, HIR::ImplItem *item, TyTy::BaseType *self,
  std::vector<TyTy::SubstitutionParamMapping> substitutions)
{
  // is it already resolved?
  auto context = TypeCheckContext::get ();
  TyTy::BaseType *resolved = nullptr;
  bool already_resolved
    = context->lookup_type (item->get_impl_mappings ().get_hirid (), &resolved);
  if (already_resolved)
    return resolved;

  // resolve
  TypeCheckImplItem resolver (parent, self, substitutions);
  item->accept_vis (resolver);
  return resolver.result;
}

void
TypeCheckImplItem::visit (HIR::Function &function)
{
  auto binder_pin = context->push_lifetime_binder ();

  if (function.has_generics ())
    resolve_generic_params (function.get_generic_params (), substitutions);

  TyTy::RegionConstraints region_constraints;
  for (auto &where_clause_item : function.get_where_clause ().get_items ())
    {
      ResolveWhereClauseItem::Resolve (*where_clause_item.get (),
				       region_constraints);
    }

  TyTy::BaseType *ret_type = nullptr;
  if (!function.has_function_return_type ())
    ret_type
      = TyTy::TupleType::get_unit_type (function.get_mappings ().get_hirid ());
  else
    {
      auto resolved
	= TypeCheckType::Resolve (function.get_return_type ().get ());
      if (resolved == nullptr)
	{
	  rust_error_at (function.get_locus (),
			 "failed to resolve return type");
	  return;
	}

      ret_type = resolved->clone ();
      ret_type->set_ref (
	function.get_return_type ()->get_mappings ().get_hirid ());
    }

  std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
  if (function.is_method ())
    {
      // these are implicit mappings and not used
      auto crate_num = mappings->get_current_crate ();
      Analysis::NodeMapping mapping (crate_num, mappings->get_next_node_id (),
				     mappings->get_next_hir_id (crate_num),
				     UNKNOWN_LOCAL_DEFID);

      // add the synthetic self param at the front, this is a placeholder for
      // compilation to know parameter names. The types are ignored but we
      // reuse the HIR identifier pattern which requires it
      HIR::SelfParam &self_param = function.get_self_param ();
      // FIXME: which location should be used for Rust::Identifier for `self`?
      HIR::IdentifierPattern *self_pattern = new HIR::IdentifierPattern (
	mapping, {"self"}, self_param.get_locus (), self_param.is_ref (),
	self_param.get_mut (), std::unique_ptr<HIR::Pattern> (nullptr));

      // might have a specified type
      TyTy::BaseType *self_type = nullptr;
      if (self_param.has_type ())
	{
	  std::unique_ptr<HIR::Type> &specified_type = self_param.get_type ();
	  self_type = TypeCheckType::Resolve (specified_type.get ());
	}
      else
	{
	  switch (self_param.get_self_kind ())
	    {
	    case HIR::SelfParam::IMM:
	    case HIR::SelfParam::MUT:
	      self_type = self->clone ();
	      break;

	      case HIR::SelfParam::IMM_REF: {
		auto region = context->lookup_and_resolve_lifetime (
		  self_param.get_lifetime ());
		if (!region.has_value ())
		  {
		    rust_inform (self_param.get_locus (),
				 "failed to resolve lifetime");
		    region = TyTy::Region::make_anonymous (); // FIXME
		  }
		self_type = new TyTy::ReferenceType (
		  self_param.get_mappings ().get_hirid (),
		  TyTy::TyVar (self->get_ref ()), Mutability::Imm,
		  region.value ());
	      }
	      break;

	      case HIR::SelfParam::MUT_REF: {
		auto region = context->lookup_and_resolve_lifetime (
		  self_param.get_lifetime ());
		if (!region.has_value ())
		  {
		    rust_error_at (self_param.get_locus (),
				   "failed to resolve lifetime");
		    return;
		  }
		self_type = new TyTy::ReferenceType (
		  self_param.get_mappings ().get_hirid (),
		  TyTy::TyVar (self->get_ref ()), Mutability::Mut,
		  region.value ());
	      }
	      break;

	    default:
	      rust_unreachable ();
	      return;
	    }
	}

      context->insert_type (self_param.get_mappings (), self_type);
      params.push_back (
	std::pair<HIR::Pattern *, TyTy::BaseType *> (self_pattern, self_type));
    }

  for (auto &param : function.get_function_params ())
    {
      // get the name as well required for later on
      auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ());
      params.push_back (std::pair<HIR::Pattern *, TyTy::BaseType *> (
	param.get_param_name ().get (), param_tyty));

      context->insert_type (param.get_mappings (), param_tyty);
      TypeCheckPattern::Resolve (param.get_param_name ().get (), param_tyty);
    }

  const CanonicalPath *canonical_path = nullptr;
  bool ok
    = mappings->lookup_canonical_path (function.get_mappings ().get_nodeid (),
				       &canonical_path);
  rust_assert (ok);

  RustIdent ident{*canonical_path, function.get_locus ()};
  auto fnType = new TyTy::FnType (
    function.get_mappings ().get_hirid (),
    function.get_mappings ().get_defid (),
    function.get_function_name ().as_string (), ident,
    function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG
			  : TyTy::FnType::FNTYPE_DEFAULT_FLAGS,
    ABI::RUST, std::move (params), ret_type, std::move (substitutions),
    TyTy::SubstitutionArgumentMappings::empty (
      context->get_lifetime_resolver ().get_num_bound_regions ()),
    region_constraints);

  context->insert_type (function.get_mappings (), fnType);
  result = fnType;

  // need to get the return type from this
  TyTy::FnType *resolve_fn_type = fnType;
  auto expected_ret_tyty = resolve_fn_type->get_return_type ();
  context->push_return_type (TypeCheckContextItem (parent, &function),
			     expected_ret_tyty);

  auto block_expr_ty
    = TypeCheckExpr::Resolve (function.get_definition ().get ());

  location_t fn_return_locus = function.has_function_return_type ()
				 ? function.get_return_type ()->get_locus ()
				 : function.get_locus ();

  coercion_site (function.get_definition ()->get_mappings ().get_hirid (),
		 TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus),
		 TyTy::TyWithLocation (block_expr_ty),
		 function.get_definition ()->get_locus ());

  context->pop_return_type ();
}

void
TypeCheckImplItem::visit (HIR::ConstantItem &constant)
{
  TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ().get ());
  TyTy::BaseType *expr_type
    = TypeCheckExpr::Resolve (constant.get_expr ().get ());

  TyTy::BaseType *unified = unify_site (
    constant.get_mappings ().get_hirid (),
    TyTy::TyWithLocation (type, constant.get_type ()->get_locus ()),
    TyTy::TyWithLocation (expr_type, constant.get_expr ()->get_locus ()),
    constant.get_locus ());
  context->insert_type (constant.get_mappings (), unified);
  result = unified;
}

void
TypeCheckImplItem::visit (HIR::TypeAlias &alias)
{
  auto binder_pin = context->push_lifetime_binder ();

  if (alias.has_generics ())
    resolve_generic_params (alias.get_generic_params (), substitutions);

  TyTy::BaseType *actual_type
    = TypeCheckType::Resolve (alias.get_type_aliased ().get ());

  context->insert_type (alias.get_mappings (), actual_type);
  result = actual_type;
  TyTy::RegionConstraints region_constraints;
  for (auto &where_clause_item : alias.get_where_clause ().get_items ())
    {
      ResolveWhereClauseItem::Resolve (*where_clause_item.get (),
				       region_constraints);
    }
}

TypeCheckImplItemWithTrait::TypeCheckImplItemWithTrait (
  HIR::ImplBlock *parent, TyTy::BaseType *self,
  TyTy::TypeBoundPredicate &trait_reference,
  std::vector<TyTy::SubstitutionParamMapping> substitutions)
  : TypeCheckBase (), trait_reference (trait_reference),
    resolved_trait_item (TyTy::TypeBoundPredicateItem::error ()),
    parent (parent), self (self), substitutions (substitutions)
{
  rust_assert (is_trait_impl_block ());
}

TyTy::TypeBoundPredicateItem
TypeCheckImplItemWithTrait::Resolve (
  HIR::ImplBlock *parent, HIR::ImplItem *item, TyTy::BaseType *self,
  TyTy::TypeBoundPredicate &trait_reference,
  std::vector<TyTy::SubstitutionParamMapping> substitutions)
{
  TypeCheckImplItemWithTrait resolver (parent, self, trait_reference,
				       substitutions);
  item->accept_vis (resolver);
  return resolver.resolved_trait_item;
}

void
TypeCheckImplItemWithTrait::visit (HIR::ConstantItem &constant)
{
  // normal resolution of the item
  TyTy::BaseType *lookup
    = TypeCheckImplItem::Resolve (parent, &constant, self, substitutions);

  // map the impl item to the associated trait item
  const auto tref = trait_reference.get ();
  const TraitItemReference *raw_trait_item = nullptr;
  bool found
    = tref->lookup_trait_item_by_type (constant.get_identifier ().as_string (),
				       TraitItemReference::TraitItemType::CONST,
				       &raw_trait_item);

  // unknown trait item - https://doc.rust-lang.org/error_codes/E0323.html
  if (!found || raw_trait_item->is_error ())
    {
      rich_location r (line_table, constant.get_locus ());
      r.add_range (trait_reference.get_locus ());
      rust_error_at (r, ErrorCode::E0323,
		     "item %qs is an associated const, which does not match "
		     "its trait %qs",
		     constant.get_identifier ().as_string ().c_str (),
		     trait_reference.get_name ().c_str ());
      return;
    }

  // get the item from the predicate
  resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
  rust_assert (!resolved_trait_item.is_error ());

  // merge the attributes
  const HIR::TraitItem *hir_trait_item
    = resolved_trait_item.get_raw_item ()->get_hir_trait_item ();
  merge_attributes (constant.get_outer_attrs (), *hir_trait_item);

  // check the types are compatible
  auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
  if (!types_compatable (TyTy::TyWithLocation (trait_item_type),
			 TyTy::TyWithLocation (lookup), constant.get_locus (),
			 true /*emit_errors*/))
    {
      rich_location r (line_table, constant.get_locus ());
      r.add_range (resolved_trait_item.get_locus ());

      rust_error_at (
	r, "constant %<%s%> has an incompatible type for trait %<%s%>",
	constant.get_identifier ().as_string ().c_str (),
	trait_reference.get_name ().c_str ());
    }
}

void
TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type)
{
  // normal resolution of the item
  TyTy::BaseType *lookup
    = TypeCheckImplItem::Resolve (parent, &type, self, substitutions);

  // map the impl item to the associated trait item
  const auto tref = trait_reference.get ();
  const TraitItemReference *raw_trait_item = nullptr;
  bool found
    = tref->lookup_trait_item_by_type (type.get_new_type_name ().as_string (),
				       TraitItemReference::TraitItemType::TYPE,
				       &raw_trait_item);

  // unknown trait item
  if (!found || raw_trait_item->is_error ())
    {
      rich_location r (line_table, type.get_locus ());
      r.add_range (trait_reference.get_locus ());
      rust_error_at (r, "type alias %<%s%> is not a member of trait %<%s%>",
		     type.get_new_type_name ().as_string ().c_str (),
		     trait_reference.get_name ().c_str ());
      return;
    }

  // get the item from the predicate
  resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
  rust_assert (!resolved_trait_item.is_error ());

  // merge the attributes
  const HIR::TraitItem *hir_trait_item
    = resolved_trait_item.get_raw_item ()->get_hir_trait_item ();
  merge_attributes (type.get_outer_attrs (), *hir_trait_item);

  // check the types are compatible
  auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
  if (!types_compatable (TyTy::TyWithLocation (trait_item_type),
			 TyTy::TyWithLocation (lookup), type.get_locus (),
			 true /*emit_errors*/))
    {
      rich_location r (line_table, type.get_locus ());
      r.add_range (resolved_trait_item.get_locus ());

      rust_error_at (
	r, "type alias %<%s%> has an incompatible type for trait %<%s%>",
	type.get_new_type_name ().as_string ().c_str (),
	trait_reference.get_name ().c_str ());
    }

  // its actually a projection, since we need a way to actually bind the
  // generic substitutions to the type itself
  TyTy::ProjectionType *projection
    = new TyTy::ProjectionType (type.get_mappings ().get_hirid (), lookup, tref,
				raw_trait_item->get_mappings ().get_defid (),
				substitutions);

  context->insert_type (type.get_mappings (), projection);
  raw_trait_item->associated_type_set (projection);
}

void
TypeCheckImplItemWithTrait::visit (HIR::Function &function)
{
  // normal resolution of the item
  TyTy::BaseType *lookup
    = TypeCheckImplItem::Resolve (parent, &function, self, substitutions);

  // map the impl item to the associated trait item
  const auto tref = trait_reference.get ();
  const TraitItemReference *raw_trait_item = nullptr;
  bool found = tref->lookup_trait_item_by_type (
    function.get_function_name ().as_string (),
    TraitItemReference::TraitItemType::FN, &raw_trait_item);

  // unknown trait item
  if (!found || raw_trait_item->is_error ())
    {
      rich_location r (line_table, function.get_locus ());
      r.add_range (trait_reference.get_locus ());
      rust_error_at (r, "method %<%s%> is not a member of trait %<%s%>",
		     function.get_function_name ().as_string ().c_str (),
		     trait_reference.get_name ().c_str ());
      return;
    }

  // get the item from the predicate
  resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
  rust_assert (!resolved_trait_item.is_error ());

  // merge the attributes
  const HIR::TraitItem *hir_trait_item
    = resolved_trait_item.get_raw_item ()->get_hir_trait_item ();
  merge_attributes (function.get_outer_attrs (), *hir_trait_item);

  // check the types are compatible
  auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
  if (!types_compatable (TyTy::TyWithLocation (trait_item_type),
			 TyTy::TyWithLocation (lookup), function.get_locus (),
			 true /*emit_errors*/))
    {
      rich_location r (line_table, function.get_locus ());
      r.add_range (resolved_trait_item.get_locus ());

      rust_error_at (r, ErrorCode::E0053,
		     "method %<%s%> has an incompatible type for trait %<%s%>",
		     function.get_function_name ().as_string ().c_str (),
		     trait_reference.get_name ().c_str ());
    }
}

void
TypeCheckImplItemWithTrait::merge_attributes (AST::AttrVec &impl_item_attrs,
					      const HIR::TraitItem &trait_item)
{
  for (const auto &attr : trait_item.get_outer_attrs ())
    {
      impl_item_attrs.push_back (attr);
    }
}

bool
TypeCheckImplItemWithTrait::is_trait_impl_block () const
{
  return !trait_reference.is_error ();
}

} // namespace Resolver
} // namespace Rust