Skip to content

Commit 457b3d1

Browse files
committed
gccrs: refactor default infer vars to be its own function
This is just a simple refactor to pull all the logic outside of the closure which makes it more readable. gcc/rust/ChangeLog: * typecheck/rust-hir-type-check.h: new function * typecheck/rust-typecheck-context.cc (TypeCheckContext::compute_inference_variables): call the new helper (TypeCheckContext::compute_infer_var): refactored code Signed-off-by: Philip Herron <herron.philip@googlemail.com>
1 parent c087dd8 commit 457b3d1

2 files changed

Lines changed: 39 additions & 32 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,15 @@ class TypeCheckContext
263263
WARN_UNUSED_RESULT std::vector<TyTy::Region>
264264
regions_from_generic_args (const HIR::GenericArgs &args) const;
265265

266-
void compute_inference_variables (bool error);
266+
void compute_inference_variables (bool emit_error);
267267

268268
TyTy::VarianceAnalysis::CrateCtx &get_variance_analysis_ctx ();
269269

270270
private:
271271
TypeCheckContext ();
272272

273+
bool compute_infer_var (HirId id, TyTy::BaseType *ty, bool emit_error);
274+
273275
std::map<NodeId, HirId> node_id_refs;
274276
std::map<HirId, TyTy::BaseType *> resolved;
275277
std::vector<std::unique_ptr<TyTy::BaseType>> builtins;

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

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -575,43 +575,48 @@ TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const
575575
}
576576

577577
void
578-
TypeCheckContext::compute_inference_variables (bool error)
578+
TypeCheckContext::compute_inference_variables (bool emit_error)
579579
{
580-
auto &mappings = Analysis::Mappings::get ();
581-
582580
// default inference variables if possible
583581
iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
584-
// nothing to do
585-
if (ty->get_kind () != TyTy::TypeKind::INFER)
586-
return true;
582+
return compute_infer_var (id, ty, emit_error);
583+
});
584+
}
587585

588-
TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
589-
TyTy::BaseType *default_type;
590-
591-
rust_debug_loc (mappings.lookup_location (id),
592-
"trying to default infer-var: %s",
593-
infer_var->as_string ().c_str ());
594-
bool ok = infer_var->default_type (&default_type);
595-
if (!ok)
596-
{
597-
if (error)
598-
rust_error_at (mappings.lookup_location (id), ErrorCode::E0282,
599-
"type annotations needed");
600-
return true;
601-
}
602-
603-
auto result
604-
= unify_site (id, TyTy::TyWithLocation (ty),
605-
TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
606-
rust_assert (result);
607-
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
608-
result->set_ref (id);
609-
insert_type (Analysis::NodeMapping (mappings.get_current_crate (), 0, id,
610-
UNKNOWN_LOCAL_DEFID),
611-
result);
586+
bool
587+
TypeCheckContext::compute_infer_var (HirId id, TyTy::BaseType *ty,
588+
bool emit_error)
589+
{
590+
auto &mappings = Analysis::Mappings::get ();
612591

592+
// nothing to do
593+
if (ty->get_kind () != TyTy::TypeKind::INFER)
613594
return true;
614-
});
595+
596+
TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
597+
TyTy::BaseType *default_type;
598+
599+
rust_debug_loc (mappings.lookup_location (id),
600+
"trying to default infer-var: %s",
601+
infer_var->as_string ().c_str ());
602+
bool ok = infer_var->default_type (&default_type);
603+
if (!ok)
604+
{
605+
if (emit_error)
606+
rust_error_at (mappings.lookup_location (id), ErrorCode::E0282,
607+
"type annotations needed");
608+
return true;
609+
}
610+
611+
auto result
612+
= unify_site (id, TyTy::TyWithLocation (ty),
613+
TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
614+
rust_assert (result);
615+
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
616+
result->set_ref (id);
617+
insert_implicit_type (id, result);
618+
619+
return true;
615620
}
616621

617622
TyTy::VarianceAnalysis::CrateCtx &

0 commit comments

Comments
 (0)