Skip to content

Commit 3ab6ac6

Browse files
committed
gccrs: refactor inference variable computation into a seperate method
gcc/rust/ChangeLog: * typecheck/rust-hir-type-check.cc (TypeResolution::Resolve): refactor * typecheck/rust-hir-type-check.h: new prototype * typecheck/rust-typecheck-context.cc (TypeCheckContext::compute_inference_variables): x
1 parent d333fa1 commit 3ab6ac6

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

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

+1-33
Original file line numberDiff line numberDiff line change
@@ -76,40 +76,8 @@ TypeResolution::Resolve (HIR::Crate &crate)
7676
if (saw_errors ())
7777
return;
7878

79-
auto mappings = Analysis::Mappings::get ();
8079
auto context = TypeCheckContext::get ();
81-
82-
// default inference variables if possible
83-
context->iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
84-
// nothing to do
85-
if (ty->get_kind () != TyTy::TypeKind::INFER)
86-
return true;
87-
88-
TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
89-
TyTy::BaseType *default_type;
90-
bool ok = infer_var->default_type (&default_type);
91-
if (!ok)
92-
{
93-
rust_error_at (mappings->lookup_location (id), ErrorCode::E0282,
94-
"type annotations needed");
95-
return true;
96-
}
97-
else
98-
{
99-
auto result
100-
= unify_site (id, TyTy::TyWithLocation (ty),
101-
TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
102-
rust_assert (result);
103-
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
104-
result->set_ref (id);
105-
context->insert_type (
106-
Analysis::NodeMapping (mappings->get_current_crate (), 0, id,
107-
UNKNOWN_LOCAL_DEFID),
108-
result);
109-
}
110-
111-
return true;
112-
});
80+
context->compute_inference_variables (true);
11381
}
11482

11583
// rust-hir-trait-ref.h

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

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class TypeCheckContext
231231
WARN_UNUSED_RESULT std::vector<TyTy::Region>
232232
regions_from_generic_args (const HIR::GenericArgs &args) const;
233233

234+
void compute_inference_variables (bool error);
235+
234236
private:
235237
TypeCheckContext ();
236238

gcc/rust/typecheck/rust-typecheck-context.cc

+41
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-hir-type-check.h"
20+
#include "rust-type-util.h"
2021

2122
namespace Rust {
2223
namespace Resolver {
@@ -576,6 +577,46 @@ TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const
576577
return regions;
577578
}
578579

580+
void
581+
TypeCheckContext::compute_inference_variables (bool error)
582+
{
583+
auto mappings = Analysis::Mappings::get ();
584+
585+
// default inference variables if possible
586+
iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
587+
// nothing to do
588+
if (ty->get_kind () != TyTy::TypeKind::INFER)
589+
return true;
590+
591+
TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
592+
TyTy::BaseType *default_type;
593+
594+
rust_debug_loc (mappings->lookup_location (id),
595+
"XXXX trying to default: %s",
596+
infer_var->as_string ().c_str ());
597+
bool ok = infer_var->default_type (&default_type);
598+
if (!ok)
599+
{
600+
if (error)
601+
rust_error_at (mappings->lookup_location (id), ErrorCode::E0282,
602+
"type annotations needed");
603+
return true;
604+
}
605+
606+
auto result
607+
= unify_site (id, TyTy::TyWithLocation (ty),
608+
TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
609+
rust_assert (result);
610+
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
611+
result->set_ref (id);
612+
insert_type (Analysis::NodeMapping (mappings->get_current_crate (), 0, id,
613+
UNKNOWN_LOCAL_DEFID),
614+
result);
615+
616+
return true;
617+
});
618+
}
619+
579620
// TypeCheckContextItem
580621

581622
TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {}

0 commit comments

Comments
 (0)