From: Pierre-Emmanuel Patry <[email protected]>
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 <[email protected]>
---
gcc/rust/backend/rust-compile-expr.cc | 6 ++--
gcc/rust/backend/rust-compile.cc | 7 ++--
gcc/rust/typecheck/rust-hir-dot-operator.cc | 8 ++---
gcc/rust/typecheck/rust-hir-path-probe.cc | 12 +++----
.../typecheck/rust-hir-type-check-implitem.cc | 12 +++----
.../typecheck/rust-hir-type-check-path.cc | 8 ++---
.../typecheck/rust-hir-type-check-type.cc | 6 ++--
gcc/rust/typecheck/rust-tyty-bounds.cc | 32 +++++++++++--------
gcc/rust/typecheck/rust-tyty.cc | 6 ++--
gcc/rust/typecheck/rust-tyty.h | 4 +--
10 files changed, 53 insertions(+), 48 deletions(-)
diff --git a/gcc/rust/backend/rust-compile-expr.cc
b/gcc/rust/backend/rust-compile-expr.cc
index 64339236f89..d059eade2ca 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -2617,15 +2617,15 @@ CompileExpr::generate_closure_fntype (HIR::ClosureExpr
&expr,
TyTy::TypeBoundPredicateItem item = TyTy::TypeBoundPredicateItem::error ();
if (predicate.get_name ().compare ("FnOnce") == 0)
{
- item = predicate.lookup_associated_item ("call_once");
+ item = predicate.lookup_associated_item ("call_once").value ();
}
else if (predicate.get_name ().compare ("FnMut") == 0)
{
- item = predicate.lookup_associated_item ("call_mut");
+ item = predicate.lookup_associated_item ("call_mut").value ();
}
else if (predicate.get_name ().compare ("Fn") == 0)
{
- item = predicate.lookup_associated_item ("call");
+ item = predicate.lookup_associated_item ("call").value ();
}
else
{
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index dbd85158aef..a3e047a2dab 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -241,12 +241,13 @@ HIRCompileBase::compute_address_for_trait_item (
&receiver_bounds,
const TyTy::BaseType *receiver, const TyTy::BaseType *root, location_t locus)
{
- TyTy::TypeBoundPredicateItem predicate_item
+ tl::optional<TyTy::TypeBoundPredicateItem> predicate_item
= predicate->lookup_associated_item (ref->get_identifier ());
- rust_assert (!predicate_item.is_error ());
+ rust_assert (predicate_item.has_value ());
// This is the expected end type
- TyTy::BaseType *trait_item_type = predicate_item.get_tyty_for_receiver
(root);
+ TyTy::BaseType *trait_item_type
+ = predicate_item->get_tyty_for_receiver (root);
rust_assert (trait_item_type->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *trait_item_fntype
= static_cast<TyTy::FnType *> (trait_item_type);
diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.cc
b/gcc/rust/typecheck/rust-hir-dot-operator.cc
index 29919aaaec8..296cc440b7f 100644
--- a/gcc/rust/typecheck/rust-hir-dot-operator.cc
+++ b/gcc/rust/typecheck/rust-hir-dot-operator.cc
@@ -471,17 +471,17 @@ MethodResolver::get_predicate_items (
std::vector<predicate_candidate> predicate_items;
for (auto &bound : specified_bounds)
{
- TyTy::TypeBoundPredicateItem lookup
+ tl::optional<TyTy::TypeBoundPredicateItem> lookup
= bound.lookup_associated_item (segment_name.as_string ());
- if (lookup.is_error ())
+ if (!lookup.has_value ())
continue;
- TyTy::BaseType *ty = lookup.get_tyty_for_receiver (&receiver);
+ TyTy::BaseType *ty = lookup->get_tyty_for_receiver (&receiver);
if (ty->get_kind () == TyTy::TypeKind::FNDEF)
{
TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
if (fnty->is_method ())
- predicate_items.emplace_back (lookup, fnty);
+ predicate_items.emplace_back (lookup.value (), fnty);
}
}
diff --git a/gcc/rust/typecheck/rust-hir-path-probe.cc
b/gcc/rust/typecheck/rust-hir-path-probe.cc
index 6ed6e25858f..bd42e906b4d 100644
--- a/gcc/rust/typecheck/rust-hir-path-probe.cc
+++ b/gcc/rust/typecheck/rust-hir-path-probe.cc
@@ -367,15 +367,15 @@ PathProbeType::process_predicate_for_candidates (
{
const TraitReference *trait_ref = predicate.get ();
- TyTy::TypeBoundPredicateItem item
+ tl::optional<TyTy::TypeBoundPredicateItem> item
= predicate.lookup_associated_item (search.as_string ());
- if (item.is_error ())
+ if (!item.has_value ())
return;
- if (ignore_mandatory_trait_items && item.needs_implementation ())
+ if (ignore_mandatory_trait_items && item->needs_implementation ())
return;
- const TraitItemReference *trait_item_ref = item.get_raw_item ();
+ const TraitItemReference *trait_item_ref = item->get_raw_item ();
PathProbeCandidate::CandidateType candidate_type;
switch (trait_item_ref->get_trait_item_type ())
{
@@ -395,9 +395,9 @@ PathProbeType::process_predicate_for_candidates (
break;
}
- TyTy::BaseType *trait_item_tyty = item.get_raw_item ()->get_tyty ();
+ TyTy::BaseType *trait_item_tyty = item->get_raw_item ()->get_tyty ();
if (receiver->get_kind () != TyTy::DYNAMIC)
- trait_item_tyty = item.get_tyty_for_receiver (receiver);
+ trait_item_tyty = item->get_tyty_for_receiver (receiver);
PathProbeCandidate::TraitItemCandidate trait_item_candidate{trait_ref,
trait_item_ref,
diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
index 077a228e52b..e7f66327d02 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
@@ -469,8 +469,8 @@ TypeCheckImplItemWithTrait::visit (HIR::ConstantItem
&constant)
}
// get the item from the predicate
- resolved_trait_item = trait_reference.lookup_associated_item
(raw_trait_item);
- rust_assert (!resolved_trait_item.is_error ());
+ resolved_trait_item
+ = trait_reference.lookup_associated_item (raw_trait_item).value ();
// merge the attributes
const HIR::TraitItem *hir_trait_item
@@ -519,8 +519,8 @@ TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type)
}
// get the item from the predicate
- resolved_trait_item = trait_reference.lookup_associated_item
(raw_trait_item);
- rust_assert (!resolved_trait_item.is_error ());
+ resolved_trait_item
+ = trait_reference.lookup_associated_item (raw_trait_item).value ();
// merge the attributes
const HIR::TraitItem *hir_trait_item
@@ -578,8 +578,8 @@ TypeCheckImplItemWithTrait::visit (HIR::Function &function)
}
// get the item from the predicate
- resolved_trait_item = trait_reference.lookup_associated_item
(raw_trait_item);
- rust_assert (!resolved_trait_item.is_error ());
+ resolved_trait_item
+ = trait_reference.lookup_associated_item (raw_trait_item).value ();
// merge the attributes
const HIR::TraitItem *hir_trait_item
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc
b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index cc5c412f986..3068f0c6e35 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -77,9 +77,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
// lookup the associated item from the specified bound
HIR::PathExprSegment &item_seg = expr.get_segments ().at (0);
HIR::PathIdentSegment item_seg_identifier = item_seg.get_segment ();
- TyTy::TypeBoundPredicateItem item
+ tl::optional<TyTy::TypeBoundPredicateItem> item
= specified_bound.lookup_associated_item (item_seg_identifier.as_string
());
- if (item.is_error ())
+ if (!item.has_value ())
{
rust_error_at (item_seg.get_locus (), "unknown associated item");
return;
@@ -116,9 +116,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
// and we dont need to worry if the trait item is actually implemented or
// not because this will have already been validated as part of the trait
// impl block
- infered = item.get_tyty_for_receiver (root);
+ infered = item->get_tyty_for_receiver (root);
root_resolved_node_id
- = item.get_raw_item ()->get_mappings ().get_nodeid ();
+ = item->get_raw_item ()->get_mappings ().get_nodeid ();
}
else
{
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 4a6c703d764..39772b42a0d 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -211,9 +211,9 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
// lookup the associated item from the specified bound
HIR::TypePathSegment &item_seg = path.get_associated_segment ();
HIR::PathIdentSegment item_seg_identifier = item_seg.get_ident_segment ();
- TyTy::TypeBoundPredicateItem item
+ tl::optional<TyTy::TypeBoundPredicateItem> item
= specified_bound.lookup_associated_item (item_seg_identifier.as_string
());
- if (item.is_error ())
+ if (!item.has_value ())
{
std::string item_seg_ident_name, rich_msg;
item_seg_ident_name = qual_path_type.get_trait ().as_string ();
@@ -265,7 +265,7 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
// and we dont need to worry if the trait item is actually implemented or
// not because this will have already been validated as part of the trait
// impl block
- translated = item.get_tyty_for_receiver (root);
+ translated = item->get_tyty_for_receiver (root);
}
else
{
diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc
b/gcc/rust/typecheck/rust-tyty-bounds.cc
index 91de35976b6..bb2e6d4d708 100644
--- a/gcc/rust/typecheck/rust-tyty-bounds.cc
+++ b/gcc/rust/typecheck/rust-tyty-bounds.cc
@@ -575,10 +575,13 @@ TypeBoundPredicate::apply_argument_mappings (
std::string identifier = it.first;
TyTy::BaseType *type = it.second;
- TypeBoundPredicateItem item = lookup_associated_item (identifier);
- rust_assert (!item.is_error ());
+ tl::optional<TypeBoundPredicateItem> item
+ = lookup_associated_item (identifier);
- const auto item_ref = item.get_raw_item ();
+ if (!item.has_value ())
+ continue;
+
+ const auto item_ref = item->get_raw_item ();
item_ref->associated_type_set (type);
}
@@ -599,7 +602,7 @@ TypeBoundPredicate::contains_item (const std::string
&search) const
return trait_ref->lookup_trait_item (search, &trait_item_ref);
}
-TypeBoundPredicateItem
+tl::optional<TypeBoundPredicateItem>
TypeBoundPredicate::lookup_associated_item (const std::string &search) const
{
auto trait_ref = get ();
@@ -611,11 +614,11 @@ TypeBoundPredicate::lookup_associated_item (const
std::string &search) const
for (auto &super_trait : super_traits)
{
auto lookup = super_trait.lookup_associated_item (search);
- if (!lookup.is_error ())
+ if (lookup.has_value ())
return lookup;
}
- return TypeBoundPredicateItem::error ();
+ return tl::nullopt;
}
TypeBoundPredicateItem::TypeBoundPredicateItem (
@@ -656,7 +659,7 @@ TypeBoundPredicateItem::get_parent () const
return &parent;
}
-TypeBoundPredicateItem
+tl::optional<TypeBoundPredicateItem>
TypeBoundPredicate::lookup_associated_item (
const Resolver::TraitItemReference *ref) const
{
@@ -732,10 +735,11 @@ TypeBoundPredicate::handle_substitions (
std::string identifier = it.first;
TyTy::BaseType *type = it.second;
- TypeBoundPredicateItem item = lookup_associated_item (identifier);
- if (!item.is_error ())
+ tl::optional<TypeBoundPredicateItem> item
+ = lookup_associated_item (identifier);
+ if (item.has_value ())
{
- const auto item_ref = item.get_raw_item ();
+ const auto item_ref = item->get_raw_item ();
item_ref->associated_type_set (type);
}
}
@@ -799,18 +803,18 @@ TypeBoundPredicate::get_trait_hierachy (
TypeBoundPredicateItem
TypeBoundPredicate::lookup_associated_type (const std::string &search)
{
- TypeBoundPredicateItem item = lookup_associated_item (search);
+ tl::optional<TypeBoundPredicateItem> item = lookup_associated_item (search);
// only need to check that it is infact an associated type because other
// wise if it was not found it will just be an error node anyway
- if (!item.is_error ())
+ if (item.has_value ())
{
- const auto raw = item.get_raw_item ();
+ const auto raw = item->get_raw_item ();
if (raw->get_trait_item_type ()
!= Resolver::TraitItemReference::TraitItemType::TYPE)
return TypeBoundPredicateItem::error ();
}
- return item;
+ return item.value ();
}
std::vector<TypeBoundPredicateItem>
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index af5f9777fd4..95e9bc2c17f 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -351,12 +351,12 @@ BaseType::satisfies_bound (const TypeBoundPredicate
&predicate,
return false;
std::string item_name = item->get_impl_item_name ();
- TypeBoundPredicateItem lookup
+ tl::optional<TypeBoundPredicateItem> lookup
= predicate.lookup_associated_item (item_name);
- if (lookup.is_error ())
+ if (!lookup.has_value ())
return false;
- const auto *item_ref = lookup.get_raw_item ();
+ const auto *item_ref = lookup->get_raw_item ();
TyTy::BaseType *bound_ty = item_ref->get_tyty ();
// compare the types
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index 49415ea0b31..b90bc83e8bf 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -617,10 +617,10 @@ public:
bool contains_item (const std::string &search) const;
- TypeBoundPredicateItem
+ tl::optional<TypeBoundPredicateItem>
lookup_associated_item (const std::string &search) const;
- TypeBoundPredicateItem
+ tl::optional<TypeBoundPredicateItem>
lookup_associated_item (const Resolver::TraitItemReference *ref) const;
// WARNING THIS WILL ALWAYS RETURN NULLPTR
--
2.50.1