Skip to content

Commit 649058f

Browse files
committed
Convert lookup return type to optional
Remove usage of error state (but not error state itelf) and use an optional to convey the missing value meaning instead. gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::generate_closure_fntype): Unwrap the optional. * backend/rust-compile.cc: Change return type container. Adapt code to new return type. * typecheck/rust-hir-dot-operator.cc: Likewise. * typecheck/rust-hir-path-probe.cc: Likewise. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): Likewise. * typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::contains_item): Likewise. (TypeBoundPredicate::lookup_associated_item): Likewise. (TypeBoundPredicateItem::get_parent): Likewise. (TypeBoundPredicate::lookup_associated_type): Likewise. * typecheck/rust-tyty.cc (BaseType::satisfies_bound): Likewise. * typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Change return type. * typecheck/rust-tyty.h: Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
1 parent 00d20c9 commit 649058f

File tree

10 files changed

+53
-48
lines changed

10 files changed

+53
-48
lines changed

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,15 +2617,15 @@ CompileExpr::generate_closure_fntype (HIR::ClosureExpr &expr,
26172617
TyTy::TypeBoundPredicateItem item = TyTy::TypeBoundPredicateItem::error ();
26182618
if (predicate.get_name ().compare ("FnOnce") == 0)
26192619
{
2620-
item = predicate.lookup_associated_item ("call_once");
2620+
item = predicate.lookup_associated_item ("call_once").value ();
26212621
}
26222622
else if (predicate.get_name ().compare ("FnMut") == 0)
26232623
{
2624-
item = predicate.lookup_associated_item ("call_mut");
2624+
item = predicate.lookup_associated_item ("call_mut").value ();
26252625
}
26262626
else if (predicate.get_name ().compare ("Fn") == 0)
26272627
{
2628-
item = predicate.lookup_associated_item ("call");
2628+
item = predicate.lookup_associated_item ("call").value ();
26292629
}
26302630
else
26312631
{

gcc/rust/backend/rust-compile.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,13 @@ HIRCompileBase::compute_address_for_trait_item (
241241
&receiver_bounds,
242242
const TyTy::BaseType *receiver, const TyTy::BaseType *root, location_t locus)
243243
{
244-
TyTy::TypeBoundPredicateItem predicate_item
244+
tl::optional<TyTy::TypeBoundPredicateItem> predicate_item
245245
= predicate->lookup_associated_item (ref->get_identifier ());
246-
rust_assert (!predicate_item.is_error ());
246+
rust_assert (predicate_item.has_value ());
247247

248248
// This is the expected end type
249-
TyTy::BaseType *trait_item_type = predicate_item.get_tyty_for_receiver (root);
249+
TyTy::BaseType *trait_item_type
250+
= predicate_item->get_tyty_for_receiver (root);
250251
rust_assert (trait_item_type->get_kind () == TyTy::TypeKind::FNDEF);
251252
TyTy::FnType *trait_item_fntype
252253
= static_cast<TyTy::FnType *> (trait_item_type);

gcc/rust/typecheck/rust-hir-dot-operator.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,17 @@ MethodResolver::get_predicate_items (
471471
std::vector<predicate_candidate> predicate_items;
472472
for (auto &bound : specified_bounds)
473473
{
474-
TyTy::TypeBoundPredicateItem lookup
474+
tl::optional<TyTy::TypeBoundPredicateItem> lookup
475475
= bound.lookup_associated_item (segment_name.as_string ());
476-
if (lookup.is_error ())
476+
if (!lookup.has_value ())
477477
continue;
478478

479-
TyTy::BaseType *ty = lookup.get_tyty_for_receiver (&receiver);
479+
TyTy::BaseType *ty = lookup->get_tyty_for_receiver (&receiver);
480480
if (ty->get_kind () == TyTy::TypeKind::FNDEF)
481481
{
482482
TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
483483
if (fnty->is_method ())
484-
predicate_items.emplace_back (lookup, fnty);
484+
predicate_items.emplace_back (lookup.value (), fnty);
485485
}
486486
}
487487

gcc/rust/typecheck/rust-hir-path-probe.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,15 @@ PathProbeType::process_predicate_for_candidates (
367367
{
368368
const TraitReference *trait_ref = predicate.get ();
369369

370-
TyTy::TypeBoundPredicateItem item
370+
tl::optional<TyTy::TypeBoundPredicateItem> item
371371
= predicate.lookup_associated_item (search.as_string ());
372-
if (item.is_error ())
372+
if (!item.has_value ())
373373
return;
374374

375-
if (ignore_mandatory_trait_items && item.needs_implementation ())
375+
if (ignore_mandatory_trait_items && item->needs_implementation ())
376376
return;
377377

378-
const TraitItemReference *trait_item_ref = item.get_raw_item ();
378+
const TraitItemReference *trait_item_ref = item->get_raw_item ();
379379
PathProbeCandidate::CandidateType candidate_type;
380380
switch (trait_item_ref->get_trait_item_type ())
381381
{
@@ -395,9 +395,9 @@ PathProbeType::process_predicate_for_candidates (
395395
break;
396396
}
397397

398-
TyTy::BaseType *trait_item_tyty = item.get_raw_item ()->get_tyty ();
398+
TyTy::BaseType *trait_item_tyty = item->get_raw_item ()->get_tyty ();
399399
if (receiver->get_kind () != TyTy::DYNAMIC)
400-
trait_item_tyty = item.get_tyty_for_receiver (receiver);
400+
trait_item_tyty = item->get_tyty_for_receiver (receiver);
401401

402402
PathProbeCandidate::TraitItemCandidate trait_item_candidate{trait_ref,
403403
trait_item_ref,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ TypeCheckImplItemWithTrait::visit (HIR::ConstantItem &constant)
469469
}
470470

471471
// get the item from the predicate
472-
resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
473-
rust_assert (!resolved_trait_item.is_error ());
472+
resolved_trait_item
473+
= trait_reference.lookup_associated_item (raw_trait_item).value ();
474474

475475
// merge the attributes
476476
const HIR::TraitItem *hir_trait_item
@@ -519,8 +519,8 @@ TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type)
519519
}
520520

521521
// get the item from the predicate
522-
resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
523-
rust_assert (!resolved_trait_item.is_error ());
522+
resolved_trait_item
523+
= trait_reference.lookup_associated_item (raw_trait_item).value ();
524524

525525
// merge the attributes
526526
const HIR::TraitItem *hir_trait_item
@@ -578,8 +578,8 @@ TypeCheckImplItemWithTrait::visit (HIR::Function &function)
578578
}
579579

580580
// get the item from the predicate
581-
resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
582-
rust_assert (!resolved_trait_item.is_error ());
581+
resolved_trait_item
582+
= trait_reference.lookup_associated_item (raw_trait_item).value ();
583583

584584
// merge the attributes
585585
const HIR::TraitItem *hir_trait_item

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
7777
// lookup the associated item from the specified bound
7878
HIR::PathExprSegment &item_seg = expr.get_segments ().at (0);
7979
HIR::PathIdentSegment item_seg_identifier = item_seg.get_segment ();
80-
TyTy::TypeBoundPredicateItem item
80+
tl::optional<TyTy::TypeBoundPredicateItem> item
8181
= specified_bound.lookup_associated_item (item_seg_identifier.as_string ());
82-
if (item.is_error ())
82+
if (!item.has_value ())
8383
{
8484
rust_error_at (item_seg.get_locus (), "unknown associated item");
8585
return;
@@ -116,9 +116,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
116116
// and we dont need to worry if the trait item is actually implemented or
117117
// not because this will have already been validated as part of the trait
118118
// impl block
119-
infered = item.get_tyty_for_receiver (root);
119+
infered = item->get_tyty_for_receiver (root);
120120
root_resolved_node_id
121-
= item.get_raw_item ()->get_mappings ().get_nodeid ();
121+
= item->get_raw_item ()->get_mappings ().get_nodeid ();
122122
}
123123
else
124124
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
211211
// lookup the associated item from the specified bound
212212
HIR::TypePathSegment &item_seg = path.get_associated_segment ();
213213
HIR::PathIdentSegment item_seg_identifier = item_seg.get_ident_segment ();
214-
TyTy::TypeBoundPredicateItem item
214+
tl::optional<TyTy::TypeBoundPredicateItem> item
215215
= specified_bound.lookup_associated_item (item_seg_identifier.as_string ());
216-
if (item.is_error ())
216+
if (!item.has_value ())
217217
{
218218
std::string item_seg_ident_name, rich_msg;
219219
item_seg_ident_name = qual_path_type.get_trait ().as_string ();
@@ -265,7 +265,7 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
265265
// and we dont need to worry if the trait item is actually implemented or
266266
// not because this will have already been validated as part of the trait
267267
// impl block
268-
translated = item.get_tyty_for_receiver (root);
268+
translated = item->get_tyty_for_receiver (root);
269269
}
270270
else
271271
{

gcc/rust/typecheck/rust-tyty-bounds.cc

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,13 @@ TypeBoundPredicate::apply_argument_mappings (
575575
std::string identifier = it.first;
576576
TyTy::BaseType *type = it.second;
577577

578-
TypeBoundPredicateItem item = lookup_associated_item (identifier);
579-
rust_assert (!item.is_error ());
578+
tl::optional<TypeBoundPredicateItem> item
579+
= lookup_associated_item (identifier);
580580

581-
const auto item_ref = item.get_raw_item ();
581+
if (!item.has_value ())
582+
continue;
583+
584+
const auto item_ref = item->get_raw_item ();
582585
item_ref->associated_type_set (type);
583586
}
584587

@@ -599,7 +602,7 @@ TypeBoundPredicate::contains_item (const std::string &search) const
599602
return trait_ref->lookup_trait_item (search, &trait_item_ref);
600603
}
601604

602-
TypeBoundPredicateItem
605+
tl::optional<TypeBoundPredicateItem>
603606
TypeBoundPredicate::lookup_associated_item (const std::string &search) const
604607
{
605608
auto trait_ref = get ();
@@ -611,11 +614,11 @@ TypeBoundPredicate::lookup_associated_item (const std::string &search) const
611614
for (auto &super_trait : super_traits)
612615
{
613616
auto lookup = super_trait.lookup_associated_item (search);
614-
if (!lookup.is_error ())
617+
if (lookup.has_value ())
615618
return lookup;
616619
}
617620

618-
return TypeBoundPredicateItem::error ();
621+
return tl::nullopt;
619622
}
620623

621624
TypeBoundPredicateItem::TypeBoundPredicateItem (
@@ -656,7 +659,7 @@ TypeBoundPredicateItem::get_parent () const
656659
return &parent;
657660
}
658661

659-
TypeBoundPredicateItem
662+
tl::optional<TypeBoundPredicateItem>
660663
TypeBoundPredicate::lookup_associated_item (
661664
const Resolver::TraitItemReference *ref) const
662665
{
@@ -732,10 +735,11 @@ TypeBoundPredicate::handle_substitions (
732735
std::string identifier = it.first;
733736
TyTy::BaseType *type = it.second;
734737

735-
TypeBoundPredicateItem item = lookup_associated_item (identifier);
736-
if (!item.is_error ())
738+
tl::optional<TypeBoundPredicateItem> item
739+
= lookup_associated_item (identifier);
740+
if (item.has_value ())
737741
{
738-
const auto item_ref = item.get_raw_item ();
742+
const auto item_ref = item->get_raw_item ();
739743
item_ref->associated_type_set (type);
740744
}
741745
}
@@ -799,18 +803,18 @@ TypeBoundPredicate::get_trait_hierachy (
799803
TypeBoundPredicateItem
800804
TypeBoundPredicate::lookup_associated_type (const std::string &search)
801805
{
802-
TypeBoundPredicateItem item = lookup_associated_item (search);
806+
tl::optional<TypeBoundPredicateItem> item = lookup_associated_item (search);
803807

804808
// only need to check that it is infact an associated type because other
805809
// wise if it was not found it will just be an error node anyway
806-
if (!item.is_error ())
810+
if (item.has_value ())
807811
{
808-
const auto raw = item.get_raw_item ();
812+
const auto raw = item->get_raw_item ();
809813
if (raw->get_trait_item_type ()
810814
!= Resolver::TraitItemReference::TraitItemType::TYPE)
811815
return TypeBoundPredicateItem::error ();
812816
}
813-
return item;
817+
return item.value ();
814818
}
815819

816820
std::vector<TypeBoundPredicateItem>

gcc/rust/typecheck/rust-tyty.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,12 @@ BaseType::satisfies_bound (const TypeBoundPredicate &predicate,
351351
return false;
352352

353353
std::string item_name = item->get_impl_item_name ();
354-
TypeBoundPredicateItem lookup
354+
tl::optional<TypeBoundPredicateItem> lookup
355355
= predicate.lookup_associated_item (item_name);
356-
if (lookup.is_error ())
356+
if (!lookup.has_value ())
357357
return false;
358358

359-
const auto *item_ref = lookup.get_raw_item ();
359+
const auto *item_ref = lookup->get_raw_item ();
360360
TyTy::BaseType *bound_ty = item_ref->get_tyty ();
361361

362362
// compare the types

gcc/rust/typecheck/rust-tyty.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,10 @@ class TypeBoundPredicate : public SubstitutionRef
617617

618618
bool contains_item (const std::string &search) const;
619619

620-
TypeBoundPredicateItem
620+
tl::optional<TypeBoundPredicateItem>
621621
lookup_associated_item (const std::string &search) const;
622622

623-
TypeBoundPredicateItem
623+
tl::optional<TypeBoundPredicateItem>
624624
lookup_associated_item (const Resolver::TraitItemReference *ref) const;
625625

626626
// WARNING THIS WILL ALWAYS RETURN NULLPTR

0 commit comments

Comments
 (0)