From f3a6f968ac3b0c2c4e92801eaf35c27a8eb9a700 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 5 Aug 2016 12:33:54 +0800 Subject: HIR Typecheck CS - Extended apply --- src/hir_typeck/expr_cs.cpp | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp index dc790c2d..77f48d6a 100644 --- a/src/hir_typeck/expr_cs.cpp +++ b/src/hir_typeck/expr_cs.cpp @@ -1894,10 +1894,12 @@ namespace { class ExprVisitor_Apply: public ::HIR::ExprVisitorDef { - HMTypeInferrence& ivars; + const Context& context; + const HMTypeInferrence& ivars; public: - ExprVisitor_Apply(HMTypeInferrence& ivars): - ivars(ivars) + ExprVisitor_Apply(const Context& context): + context(context), + ivars(context.m_ivars) { } void visit_node_ptr(::HIR::ExprNodeP& node) override { @@ -1938,6 +1940,11 @@ namespace { private: void check_type_resolved_top(const Span& sp, ::HIR::TypeRef& ty) const { check_type_resolved(sp, ty, ty); + ty = this->context.m_resolve.expand_associated_types(sp, mv$(ty)); + } + void check_type_resolved_pp(const Span& sp, ::HIR::PathParams& pp, const ::HIR::TypeRef& top_type) const { + for(auto& ty : pp.m_types) + check_type_resolved(sp, ty, top_type); } void check_type_resolved(const Span& sp, ::HIR::TypeRef& ty, const ::HIR::TypeRef& top_type) const { TU_MATCH(::HIR::TypeRef::Data, (ty.m_data), (e), @@ -1957,12 +1964,32 @@ namespace { // Leaf ), (Path, - // TODO: + TU_MATCH(::HIR::Path::Data, (e.path.m_data), (pe), + (Generic, + check_type_resolved_pp(sp, pe.m_params, top_type); + ), + (UfcsInherent, + check_type_resolved(sp, *pe.type, top_type); + check_type_resolved_pp(sp, pe.params, top_type); + ), + (UfcsKnown, + check_type_resolved(sp, *pe.type, top_type); + check_type_resolved_pp(sp, pe.trait.m_params, top_type); + check_type_resolved_pp(sp, pe.params, top_type); + ), + (UfcsUnknown, + ERROR(sp, E0000, "UfcsUnknown " << ty << " left in " << top_type); + ) + ) ), (Generic, - // Leaf + // Leaf - no ivars ), (TraitObject, + check_type_resolved_pp(sp, e.m_trait.m_path.m_params, top_type); + for(auto& marker : e.m_markers) { + check_type_resolved_pp(sp, marker.m_params, top_type); + } // TODO: ), (Array, @@ -3771,7 +3798,7 @@ void Typecheck_Code_CS(const typeck::ModuleState& ms, t_args& args, const ::HIR: DEBUG("==== VALIDATE ==== (" << count << " rounds)"); context.dump(); - ExprVisitor_Apply visitor { context.m_ivars }; + ExprVisitor_Apply visitor { context }; visitor.visit_node_ptr( root_ptr ); } expr = ::HIR::ExprPtr( mv$(root_ptr) ); -- cgit v1.2.3 From d3a7b742572e23a535571eb126a84a394c37dfe2 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 5 Aug 2016 12:51:11 +0800 Subject: HIR Typecheck Check - ! matches anything --- src/hir_typeck/expr_check.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/hir_typeck/expr_check.cpp b/src/hir_typeck/expr_check.cpp index 7b6b3705..f8cd8202 100644 --- a/src/hir_typeck/expr_check.cpp +++ b/src/hir_typeck/expr_check.cpp @@ -545,9 +545,16 @@ namespace { } void check_types_equal(const Span& sp, const ::HIR::TypeRef& l, const ::HIR::TypeRef& r) const { - if( l != r ) { + if( l.m_data.is_Diverge() || r.m_data.is_Diverge() ) { + // Diverge, matches everything. + // TODO: Is this always true? + } + else if( l != r ) { ERROR(sp, E0000, "Type mismatch - " << l << " != " << r); } + else { + // All good + } } void check_associated_type(const Span& sp, const ::HIR::TypeRef& res, -- cgit v1.2.3 From 0459a4ee12ad98be68f92645c9a1483d117c0a15 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 5 Aug 2016 12:51:26 +0800 Subject: HIR Typecheck CS - Add cast when going from &T to *const T --- src/hir_typeck/expr_cs.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp index 77f48d6a..887c309b 100644 --- a/src/hir_typeck/expr_cs.cpp +++ b/src/hir_typeck/expr_cs.cpp @@ -3226,7 +3226,18 @@ namespace { (Pointer, // Pointers coerce from borrows and similar pointers TU_IFLET(::HIR::TypeRef::Data, ty_src.m_data, Borrow, r_e, + if( r_e.type != l_e.type ) { + ERROR(sp, E0000, "Type mismatch between " << ty_dst << " and " << ty_src << " - Mutability differs"); + } context.equate_types(sp, *l_e.inner, *r_e.inner); + + // Add downcast + auto span = node_ptr->span(); + node_ptr->m_res_type = ty_src.clone(); + node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_Cast( mv$(span), mv$(node_ptr), ty_dst.clone() )); + node_ptr->m_res_type = ty_dst.clone(); + + context.m_ivars.mark_change(); return true; ) else TU_IFLET(::HIR::TypeRef::Data, ty_src.m_data, Pointer, r_e, -- cgit v1.2.3 From 58e5acc9ca0ef49c2b24d749603790ea3f255019 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 5 Aug 2016 15:04:51 +0800 Subject: HIR Typecheck CS - Apply ivars to paths in various locations --- src/hir_typeck/expr_cs.cpp | 54 ++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp index 887c309b..7bbcc1c9 100644 --- a/src/hir_typeck/expr_cs.cpp +++ b/src/hir_typeck/expr_cs.cpp @@ -1797,7 +1797,6 @@ namespace { // TODO: Apply derefs! - node.m_method_path = mv$(fcn_path); this->m_completed = true; } } @@ -1924,11 +1923,13 @@ namespace { void visit(::HIR::ExprNode_CallPath& node) override { for(auto& ty : node.m_cache.m_arg_types) this->check_type_resolved_top(node.span(), ty); + this->check_type_resolved_path(node.span(), node.m_path); ::HIR::ExprVisitorDef::visit(node); } void visit(::HIR::ExprNode_CallMethod& node) override { for(auto& ty : node.m_cache.m_arg_types) this->check_type_resolved_top(node.span(), ty); + this->check_type_resolved_path(node.span(), node.m_method_path); ::HIR::ExprVisitorDef::visit(node); } void visit(::HIR::ExprNode_CallValue& node) override { @@ -1937,6 +1938,14 @@ namespace { ::HIR::ExprVisitorDef::visit(node); } + void visit(::HIR::ExprNode_PathValue& node) override { + this->check_type_resolved_path(node.span(), node.m_path); + } + void visit(::HIR::ExprNode_StructLiteral& node) override { + this->check_type_resolved_pp(node.span(), node.m_path.m_params, ::HIR::TypeRef()); + + ::HIR::ExprVisitorDef::visit(node); + } private: void check_type_resolved_top(const Span& sp, ::HIR::TypeRef& ty) const { check_type_resolved(sp, ty, ty); @@ -1946,6 +1955,30 @@ namespace { for(auto& ty : pp.m_types) check_type_resolved(sp, ty, top_type); } + void check_type_resolved_path(const Span& sp, ::HIR::Path& path) const { + //auto tmp = ::HIR::TypeRef(path.clone()); + auto tmp = ::HIR::TypeRef(); + check_type_resolved_path(sp, path, tmp); + } + void check_type_resolved_path(const Span& sp, ::HIR::Path& path, const ::HIR::TypeRef& top_type) const { + TU_MATCH(::HIR::Path::Data, (path.m_data), (pe), + (Generic, + check_type_resolved_pp(sp, pe.m_params, top_type); + ), + (UfcsInherent, + check_type_resolved(sp, *pe.type, top_type); + check_type_resolved_pp(sp, pe.params, top_type); + ), + (UfcsKnown, + check_type_resolved(sp, *pe.type, top_type); + check_type_resolved_pp(sp, pe.trait.m_params, top_type); + check_type_resolved_pp(sp, pe.params, top_type); + ), + (UfcsUnknown, + ERROR(sp, E0000, "UfcsUnknown " << path << " left in " << top_type); + ) + ) + } void check_type_resolved(const Span& sp, ::HIR::TypeRef& ty, const ::HIR::TypeRef& top_type) const { TU_MATCH(::HIR::TypeRef::Data, (ty.m_data), (e), (Infer, @@ -1964,23 +1997,7 @@ namespace { // Leaf ), (Path, - TU_MATCH(::HIR::Path::Data, (e.path.m_data), (pe), - (Generic, - check_type_resolved_pp(sp, pe.m_params, top_type); - ), - (UfcsInherent, - check_type_resolved(sp, *pe.type, top_type); - check_type_resolved_pp(sp, pe.params, top_type); - ), - (UfcsKnown, - check_type_resolved(sp, *pe.type, top_type); - check_type_resolved_pp(sp, pe.trait.m_params, top_type); - check_type_resolved_pp(sp, pe.params, top_type); - ), - (UfcsUnknown, - ERROR(sp, E0000, "UfcsUnknown " << ty << " left in " << top_type); - ) - ) + check_type_resolved_path(sp, e.path, top_type); ), (Generic, // Leaf - no ivars @@ -1990,7 +2007,6 @@ namespace { for(auto& marker : e.m_markers) { check_type_resolved_pp(sp, marker.m_params, top_type); } - // TODO: ), (Array, this->check_type_resolved(sp, *e.inner, top_type); -- cgit v1.2.3 From 310c72f5dbd4bdce3c0d0cea59f17a594af5a8ba Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 5 Aug 2016 15:31:35 +0800 Subject: HIR Typecheck CS - Apply ivars to caches --- src/hir_typeck/expr_cs.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp index 7bbcc1c9..777ae1cc 100644 --- a/src/hir_typeck/expr_cs.cpp +++ b/src/hir_typeck/expr_cs.cpp @@ -1943,6 +1943,21 @@ namespace { } void visit(::HIR::ExprNode_StructLiteral& node) override { this->check_type_resolved_pp(node.span(), node.m_path.m_params, ::HIR::TypeRef()); + for(auto& ty : node.m_value_types) { + if( ty != ::HIR::TypeRef() ) { + this->check_type_resolved_top(node.span(), ty); + } + } + + ::HIR::ExprVisitorDef::visit(node); + } + void visit(::HIR::ExprNode_TupleVariant& node) override { + this->check_type_resolved_pp(node.span(), node.m_path.m_params, ::HIR::TypeRef()); + for(auto& ty : node.m_arg_types) { + if( ty != ::HIR::TypeRef() ) { + this->check_type_resolved_top(node.span(), ty); + } + } ::HIR::ExprVisitorDef::visit(node); } -- cgit v1.2.3