Skip to content

Commit 74d30fe

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 refactor (TypeCheckContext::compute_inference_variables): refactor
1 parent d57b04a commit 74d30fe

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
@@ -43,40 +43,8 @@ TypeResolution::Resolve (HIR::Crate &crate)
4343
if (saw_errors ())
4444
return;
4545

46-
auto mappings = Analysis::Mappings::get ();
4746
auto context = TypeCheckContext::get ();
48-
49-
// default inference variables if possible
50-
context->iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
51-
// nothing to do
52-
if (ty->get_kind () != TyTy::TypeKind::INFER)
53-
return true;
54-
55-
TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
56-
TyTy::BaseType *default_type;
57-
bool ok = infer_var->default_type (&default_type);
58-
if (!ok)
59-
{
60-
rust_error_at (mappings->lookup_location (id), ErrorCode::E0282,
61-
"type annotations needed");
62-
return true;
63-
}
64-
else
65-
{
66-
auto result
67-
= unify_site (id, TyTy::TyWithLocation (ty),
68-
TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
69-
rust_assert (result);
70-
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
71-
result->set_ref (id);
72-
context->insert_type (
73-
Analysis::NodeMapping (mappings->get_current_crate (), 0, id,
74-
UNKNOWN_LOCAL_DEFID),
75-
result);
76-
}
77-
78-
return true;
79-
});
47+
context->compute_inference_variables (true);
8048
}
8149

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class TypeCheckContext
173173
void trait_query_completed (DefId id);
174174
bool trait_query_in_progress (DefId id) const;
175175

176+
void compute_inference_variables (bool error);
177+
176178
private:
177179
TypeCheckContext ();
178180

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 {
@@ -496,6 +497,46 @@ TypeCheckContext::trait_query_in_progress (DefId id) const
496497
!= trait_queries_in_progress.end ();
497498
}
498499

500+
void
501+
TypeCheckContext::compute_inference_variables (bool error)
502+
{
503+
auto mappings = Analysis::Mappings::get ();
504+
505+
// default inference variables if possible
506+
iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
507+
// nothing to do
508+
if (ty->get_kind () != TyTy::TypeKind::INFER)
509+
return true;
510+
511+
TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
512+
TyTy::BaseType *default_type;
513+
514+
rust_debug_loc (mappings->lookup_location (id),
515+
"XXXX trying to default: %s",
516+
infer_var->as_string ().c_str ());
517+
bool ok = infer_var->default_type (&default_type);
518+
if (!ok)
519+
{
520+
if (error)
521+
rust_error_at (mappings->lookup_location (id), ErrorCode::E0282,
522+
"type annotations needed");
523+
return true;
524+
}
525+
526+
auto result
527+
= unify_site (id, TyTy::TyWithLocation (ty),
528+
TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
529+
rust_assert (result);
530+
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
531+
result->set_ref (id);
532+
insert_type (Analysis::NodeMapping (mappings->get_current_crate (), 0, id,
533+
UNKNOWN_LOCAL_DEFID),
534+
result);
535+
536+
return true;
537+
});
538+
}
539+
499540
// TypeCheckContextItem
500541

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

0 commit comments

Comments
 (0)