From 36a9255b1b6683bb060021c637997de396445049 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 22 Aug 2022 15:54:14 +0100 Subject: Refactor our casts to follow the Rustc implemention This gets rid of our old visitor system for cast type checking. Casts depend on type coercions as they are meant to attempt a type coercion before trying a simple cast. This explicitly defines the rules which should be allowed for simple casts. In rustc they use match expressions to write a list of casts which should not be allowed. We have likely missed some rules of what should be allowed but this is at least the start of how to implement this. Fixes #1496 --- gcc/rust/backend/rust-compile-expr.cc | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 412ca09..865ad25 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -237,16 +237,34 @@ CompileExpr::visit (HIR::LazyBooleanExpr &expr) void CompileExpr::visit (HIR::TypeCastExpr &expr) { - TyTy::BaseType *tyty = nullptr; + TyTy::BaseType *type_to_cast_to_ty = nullptr; if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), - &tyty)) + &type_to_cast_to_ty)) { translated = error_mark_node; return; } - auto type_to_cast_to = TyTyResolveCompile::compile (ctx, tyty); + TyTy::BaseType *casted_tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + expr.get_casted_expr ()->get_mappings ().get_hirid (), &casted_tyty)) + { + translated = error_mark_node; + return; + } + + auto type_to_cast_to = TyTyResolveCompile::compile (ctx, type_to_cast_to_ty); auto casted_expr = CompileExpr::Compile (expr.get_casted_expr ().get (), ctx); + + std::vector *adjustments = nullptr; + bool ok = ctx->get_tyctx ()->lookup_cast_autoderef_mappings ( + expr.get_mappings ().get_hirid (), &adjustments); + if (ok) + { + casted_expr + = resolve_adjustements (*adjustments, casted_expr, expr.get_locus ()); + } + translated = type_cast_expression (type_to_cast_to, casted_expr, expr.get_locus ()); } -- cgit v1.1