@@ -385,9 +385,9 @@ class TypeCheckImplItemWithTrait : public TypeCheckImplItem
385
385
using Rust::Resolver::TypeCheckBase::visit;
386
386
387
387
public:
388
- static const TraitItemReference *
388
+ static TyTy::TypeBoundPredicateItem
389
389
Resolve (HIR::ImplBlock *parent, HIR::ImplItem *item, TyTy::BaseType *self,
390
- TraitReference &trait_reference,
390
+ TyTy::TypeBoundPredicate &trait_reference,
391
391
std::vector<TyTy::SubstitutionParamMapping> substitutions)
392
392
{
393
393
TypeCheckImplItemWithTrait resolver (parent, self, trait_reference,
@@ -398,38 +398,46 @@ class TypeCheckImplItemWithTrait : public TypeCheckImplItem
398
398
399
399
void visit (HIR::ConstantItem &constant) override
400
400
{
401
- trait_reference.lookup_trait_item_by_type (
401
+ // normal resolution of the item
402
+ TypeCheckImplItem::visit (constant);
403
+ TyTy::BaseType *lookup;
404
+ if (!context->lookup_type (constant.get_mappings ().get_hirid (), &lookup))
405
+ return ;
406
+
407
+ // map the impl item to the associated trait item
408
+ const auto tref = trait_reference.get ();
409
+ const TraitItemReference *raw_trait_item = nullptr ;
410
+ bool found = tref->lookup_trait_item_by_type (
402
411
constant.get_identifier (), TraitItemReference::TraitItemType::CONST,
403
- &resolved_trait_item );
412
+ &raw_trait_item );
404
413
405
414
// unknown trait item
406
- if (resolved_trait_item ->is_error ())
415
+ if (!found || raw_trait_item ->is_error ())
407
416
{
408
417
RichLocation r (constant.get_locus ());
409
418
r.add_range (trait_reference.get_locus ());
410
419
rust_error_at (r, " constant %<%s%> is not a member of trait %<%s%>" ,
411
420
constant.get_identifier ().c_str (),
412
421
trait_reference.get_name ().c_str ());
422
+ return ;
413
423
}
414
424
415
- // normal resolution of the item
416
- TypeCheckImplItem::visit (constant);
417
- TyTy::BaseType *lookup;
418
- if (!context->lookup_type (constant.get_mappings ().get_hirid (), &lookup))
419
- return ;
420
- if (resolved_trait_item->is_error ())
421
- return ;
425
+ // get the item from the predicate
426
+ resolved_trait_item
427
+ = trait_reference.lookup_associated_item (raw_trait_item);
428
+ rust_assert (!resolved_trait_item.is_error ());
422
429
423
430
// merge the attributes
424
431
const HIR::TraitItem *hir_trait_item
425
- = resolved_trait_item->get_hir_trait_item ();
432
+ = resolved_trait_item. get_raw_item () ->get_hir_trait_item ();
426
433
merge_attributes (constant.get_outer_attrs (), *hir_trait_item);
427
434
428
435
// check the types are compatible
429
- if (!resolved_trait_item->get_tyty ()->can_eq (lookup, true ))
436
+ auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
437
+ if (!trait_item_type->can_eq (lookup, true ))
430
438
{
431
439
RichLocation r (constant.get_locus ());
432
- r.add_range (resolved_trait_item-> get_locus ());
440
+ r.add_range (resolved_trait_item. get_locus ());
433
441
434
442
rust_error_at (
435
443
r, " constant %<%s%> has an incompatible type for trait %<%s%>" ,
@@ -440,38 +448,46 @@ class TypeCheckImplItemWithTrait : public TypeCheckImplItem
440
448
441
449
void visit (HIR::TypeAlias &type) override
442
450
{
443
- trait_reference.lookup_trait_item_by_type (
451
+ // normal resolution of the item
452
+ TypeCheckImplItem::visit (type);
453
+ TyTy::BaseType *lookup;
454
+ if (!context->lookup_type (type.get_mappings ().get_hirid (), &lookup))
455
+ return ;
456
+
457
+ // map the impl item to the associated trait item
458
+ const auto tref = trait_reference.get ();
459
+ const TraitItemReference *raw_trait_item = nullptr ;
460
+ bool found = tref->lookup_trait_item_by_type (
444
461
type.get_new_type_name (), TraitItemReference::TraitItemType::TYPE,
445
- &resolved_trait_item );
462
+ &raw_trait_item );
446
463
447
464
// unknown trait item
448
- if (resolved_trait_item ->is_error ())
465
+ if (!found || raw_trait_item ->is_error ())
449
466
{
450
467
RichLocation r (type.get_locus ());
451
468
r.add_range (trait_reference.get_locus ());
452
469
rust_error_at (r, " type alias %<%s%> is not a member of trait %<%s%>" ,
453
470
type.get_new_type_name ().c_str (),
454
471
trait_reference.get_name ().c_str ());
472
+ return ;
455
473
}
456
474
457
- // normal resolution of the item
458
- TypeCheckImplItem::visit (type);
459
- TyTy::BaseType *lookup;
460
- if (!context->lookup_type (type.get_mappings ().get_hirid (), &lookup))
461
- return ;
462
- if (resolved_trait_item->is_error ())
463
- return ;
475
+ // get the item from the predicate
476
+ resolved_trait_item
477
+ = trait_reference.lookup_associated_item (raw_trait_item);
478
+ rust_assert (!resolved_trait_item.is_error ());
464
479
465
480
// merge the attributes
466
481
const HIR::TraitItem *hir_trait_item
467
- = resolved_trait_item->get_hir_trait_item ();
482
+ = resolved_trait_item. get_raw_item () ->get_hir_trait_item ();
468
483
merge_attributes (type.get_outer_attrs (), *hir_trait_item);
469
484
470
485
// check the types are compatible
471
- if (!resolved_trait_item->get_tyty ()->can_eq (lookup, true ))
486
+ auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
487
+ if (!trait_item_type->can_eq (lookup, true ))
472
488
{
473
489
RichLocation r (type.get_locus ());
474
- r.add_range (resolved_trait_item-> get_locus ());
490
+ r.add_range (resolved_trait_item. get_locus ());
475
491
476
492
rust_error_at (
477
493
r, " type alias %<%s%> has an incompatible type for trait %<%s%>" ,
@@ -481,83 +497,63 @@ class TypeCheckImplItemWithTrait : public TypeCheckImplItem
481
497
482
498
// its actually a projection, since we need a way to actually bind the
483
499
// generic substitutions to the type itself
484
- TyTy::ProjectionType *projection = new TyTy::ProjectionType (
485
- type.get_mappings ().get_hirid (), lookup, &trait_reference,
486
- resolved_trait_item->get_mappings ().get_defid (), substitutions);
500
+ TyTy::ProjectionType *projection
501
+ = new TyTy::ProjectionType (type.get_mappings ().get_hirid (), lookup,
502
+ tref,
503
+ raw_trait_item->get_mappings ().get_defid (),
504
+ substitutions);
487
505
488
506
context->insert_type (type.get_mappings (), projection);
489
- resolved_trait_item ->associated_type_set (projection);
507
+ raw_trait_item ->associated_type_set (projection);
490
508
}
491
509
492
510
void visit (HIR::Function &function) override
493
511
{
494
- // resolved_trait_item = trait_reference.lookup_trait_item (
495
- // function.get_function_name (), TraitItemReference::TraitItemType::FN);
496
- trait_reference.lookup_trait_item_by_type (
497
- function.get_function_name (), TraitItemReference::TraitItemType::FN,
498
- &resolved_trait_item);
512
+ // we get the error checking from the base method here
513
+ TypeCheckImplItem::visit (function);
514
+ TyTy::BaseType *lookup;
515
+ if (!context->lookup_type (function.get_mappings ().get_hirid (), &lookup))
516
+ return ;
517
+
518
+ // map the impl item to the associated trait item
519
+ const auto tref = trait_reference.get ();
520
+ const TraitItemReference *raw_trait_item = nullptr ;
521
+ bool found
522
+ = tref->lookup_trait_item_by_type (function.get_function_name (),
523
+ TraitItemReference::TraitItemType::FN,
524
+ &raw_trait_item);
499
525
500
526
// unknown trait item
501
- if (resolved_trait_item ->is_error ())
527
+ if (!found || raw_trait_item ->is_error ())
502
528
{
503
529
RichLocation r (function.get_locus ());
504
530
r.add_range (trait_reference.get_locus ());
505
531
rust_error_at (r, " method %<%s%> is not a member of trait %<%s%>" ,
506
532
function.get_function_name ().c_str (),
507
533
trait_reference.get_name ().c_str ());
534
+ return ;
508
535
}
509
536
510
- // we get the error checking from the base method here
511
- TypeCheckImplItem::visit (function);
512
- TyTy::BaseType *lookup;
513
- if (!context->lookup_type (function.get_mappings ().get_hirid (), &lookup))
514
- return ;
515
- if (resolved_trait_item->is_error ())
516
- return ;
537
+ // get the item from the predicate
538
+ resolved_trait_item
539
+ = trait_reference.lookup_associated_item (raw_trait_item);
540
+ rust_assert (!resolved_trait_item.is_error ());
517
541
518
542
// merge the attributes
519
543
const HIR::TraitItem *hir_trait_item
520
- = resolved_trait_item->get_hir_trait_item ();
544
+ = resolved_trait_item. get_raw_item () ->get_hir_trait_item ();
521
545
merge_attributes (function.get_outer_attrs (), *hir_trait_item);
522
546
523
- rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
524
- rust_assert (resolved_trait_item->get_tyty ()->get_kind ()
525
- == TyTy::TypeKind::FNDEF);
526
-
527
- TyTy::FnType *fntype = static_cast <TyTy::FnType *> (lookup);
528
- TyTy::FnType *trait_item_fntype
529
- = static_cast <TyTy::FnType *> (resolved_trait_item->get_tyty ());
530
-
531
- // sets substitute self into the trait_item_ref->tyty
532
- TyTy::SubstitutionParamMapping *self_mapping = nullptr ;
533
- for (auto ¶m_mapping : trait_item_fntype->get_substs ())
534
- {
535
- const HIR::TypeParam &type_param = param_mapping.get_generic_param ();
536
- if (type_param.get_type_representation ().compare (" Self" ) == 0 )
537
- {
538
- self_mapping = ¶m_mapping;
539
- break ;
540
- }
541
- }
542
- rust_assert (self_mapping != nullptr );
543
-
544
- std::vector<TyTy::SubstitutionArg> mappings;
545
- mappings.push_back (TyTy::SubstitutionArg (self_mapping, self));
546
-
547
- TyTy::SubstitutionArgumentMappings implicit_self_substs (
548
- mappings, function.get_locus ());
549
- trait_item_fntype
550
- = trait_item_fntype->handle_substitions (implicit_self_substs);
551
-
552
547
// check the types are compatible
553
- if (!trait_item_fntype->can_eq (fntype, true ))
548
+ auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
549
+ if (!trait_item_type->can_eq (lookup, true ))
554
550
{
555
551
RichLocation r (function.get_locus ());
556
- r.add_range (resolved_trait_item-> get_locus ());
552
+ r.add_range (resolved_trait_item. get_locus ());
557
553
558
554
rust_error_at (
559
555
r, " method %<%s%> has an incompatible type for trait %<%s%>" ,
560
- fntype-> get_identifier ().c_str (),
556
+ function. get_function_name ().c_str (),
561
557
trait_reference.get_name ().c_str ());
562
558
}
563
559
}
@@ -577,19 +573,19 @@ class TypeCheckImplItemWithTrait : public TypeCheckImplItem
577
573
private:
578
574
TypeCheckImplItemWithTrait (
579
575
HIR::ImplBlock *parent, TyTy::BaseType *self,
580
- TraitReference &trait_reference,
576
+ TyTy::TypeBoundPredicate &trait_reference,
581
577
std::vector<TyTy::SubstitutionParamMapping> substitutions)
582
578
: TypeCheckImplItem (parent, self), trait_reference (trait_reference),
583
- resolved_trait_item (& TraitItemReference::error_node ()),
579
+ resolved_trait_item (TyTy::TypeBoundPredicateItem::error ()),
584
580
substitutions (substitutions)
585
581
{
586
582
rust_assert (is_trait_impl_block ());
587
583
}
588
584
589
585
bool is_trait_impl_block () const { return !trait_reference.is_error (); }
590
586
591
- TraitReference &trait_reference;
592
- TraitItemReference * resolved_trait_item;
587
+ TyTy::TypeBoundPredicate &trait_reference;
588
+ TyTy::TypeBoundPredicateItem resolved_trait_item;
593
589
std::vector<TyTy::SubstitutionParamMapping> substitutions;
594
590
};
595
591
0 commit comments