diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check.cc | 41 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.cc | 23 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.h | 2 |
3 files changed, 34 insertions, 32 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index fff3087..d5cb0d5 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -58,44 +58,21 @@ TypeResolution::Resolve (HIR::Crate &crate) // nothing to do if (ty->get_kind () != TyTy::TypeKind::INFER) return true; - TyTy::InferType *infer_var = (TyTy::InferType *) ty; - switch (infer_var->get_infer_kind ()) + TyTy::BaseType *default_type; + bool ok = infer_var->default_type (&default_type); + if (!ok) { - case TyTy::InferType::GENERAL: rust_error_at (mappings->lookup_location (id), "unable to determine type: please give this a type: %u", id); - break; - - case TyTy::InferType::INTEGRAL: { - TyTy::BaseType *default_integer; - bool ok = context->lookup_builtin ("i32", &default_integer); - rust_assert (ok); - - auto result = ty->unify (default_integer); - result->set_ref (id); - context->insert_type ( - Analysis::NodeMapping (mappings->get_current_crate (), 0, id, - UNKNOWN_LOCAL_DEFID), - result); - } - break; - - case TyTy::InferType::FLOAT: { - TyTy::BaseType *default_float; - bool ok = context->lookup_builtin ("f32", &default_float); - rust_assert (ok); - - auto result = ty->unify (default_float); - result->set_ref (id); - context->insert_type ( - Analysis::NodeMapping (mappings->get_current_crate (), 0, id, - UNKNOWN_LOCAL_DEFID), - result); - } - break; + return true; } + auto result = ty->unify (default_type); + result->set_ref (id); + context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (), + 0, id, UNKNOWN_LOCAL_DEFID), + result); return true; }); diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 1130212..034bc2d 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -106,6 +106,29 @@ InferType::clone () get_combined_refs ()); } +bool +InferType::default_type (BaseType **type) const +{ + auto context = Resolver::TypeCheckContext::get (); + bool ok = false; + switch (infer_kind) + { + case GENERAL: + return false; + case INTEGRAL: { + ok = context->lookup_builtin ("i32", type); + rust_assert (ok); + return ok; + } + case FLOAT: { + ok = context->lookup_builtin ("f64", type); + rust_assert (ok); + return ok; + } + } + return false; +} + void ErrorType::accept_vis (TyVisitor &vis) { diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 2d97a0b..f0a450e 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -187,6 +187,8 @@ public: std::string get_name () const override final { return as_string (); } + bool default_type (BaseType **type) const; + private: InferTypeKind infer_kind; }; |