Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions gcc/rust/typecheck/rust-casts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-casts.h"
#include "rust-tyty-util.h"

namespace Rust {
namespace Resolver {
Expand All @@ -28,15 +29,20 @@ TypeCastRules::TypeCastRules (location_t locus, TyTy::TyWithLocation from,

TypeCoercionRules::CoercionResult
TypeCastRules::resolve (location_t locus, TyTy::TyWithLocation from,
TyTy::TyWithLocation to)
TyTy::TyWithLocation to, bool emit_error)
{
TypeCastRules cast_rules (locus, from, to);
return cast_rules.check ();
return cast_rules.check (emit_error);
}

TypeCoercionRules::CoercionResult
TypeCastRules::check ()
TypeCastRules::check (bool emit_error)
{
// try the simple cast rules
auto simple_cast = cast_rules ();
if (!simple_cast.is_error ())
return simple_cast;

// https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L565-L582
auto possible_coercion
= TypeCoercionRules::TryCoerce (from.get_ty (), to.get_ty (), locus,
Expand All @@ -51,13 +57,9 @@ TypeCastRules::check ()
true /*is_cast_site*/);
}

// try the simple cast rules
auto simple_cast = cast_rules ();
if (!simple_cast.is_error ())
return simple_cast;
if (emit_error)
TypeCastRules::emit_cast_error (locus, from, to);

// failed to cast
emit_cast_error ();
return TypeCoercionRules::CoercionResult::get_error ();
}

Expand Down Expand Up @@ -329,7 +331,27 @@ TypeCastRules::check_ptr_ptr_cast ()
}
else if (from_is_ref && to_is_ref)
{
// mutability must be coercedable
const auto &from_ref = *from.get_ty ()->as<TyTy::ReferenceType> ();
const auto &to_ref = *to.get_ty ()->as<TyTy::ReferenceType> ();

if (from_ref.is_dyn_object () != to_ref.is_dyn_object ())
{
// this needs to be handled by coercion logic
return TypeCoercionRules::CoercionResult::get_error ();
}

// are the underlying types safely simple castable?
const auto to_underly = to_ref.get_base ();
const auto from_underly = from_ref.get_base ();
auto res = resolve (locus, TyTy::TyWithLocation (from_underly),
TyTy::TyWithLocation (to_underly), false);
if (res.is_error ())
{
// this needs to be handled by coercion logic
return TypeCoercionRules::CoercionResult::get_error ();
}

// mutability must be coerceable
TyTy::ReferenceType &f
= static_cast<TyTy::ReferenceType &> (*from.get_ty ());
TyTy::ReferenceType &t
Expand All @@ -346,7 +368,8 @@ TypeCastRules::check_ptr_ptr_cast ()
}

void
TypeCastRules::emit_cast_error () const
TypeCastRules::emit_cast_error (location_t locus, TyTy::TyWithLocation from,
TyTy::TyWithLocation to)
{
rich_location r (line_table, locus);
r.add_range (from.get_locus ());
Expand Down
10 changes: 6 additions & 4 deletions gcc/rust/typecheck/rust-casts.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ class TypeCastRules
public:
static TypeCoercionRules::CoercionResult resolve (location_t locus,
TyTy::TyWithLocation from,
TyTy::TyWithLocation to);
TyTy::TyWithLocation to,
bool emit_error = true);

static void emit_cast_error (location_t locus, TyTy::TyWithLocation from,
TyTy::TyWithLocation to);

protected:
TypeCoercionRules::CoercionResult check ();
TypeCoercionRules::CoercionResult check (bool emit_error);
TypeCoercionRules::CoercionResult cast_rules ();
TypeCoercionRules::CoercionResult check_ptr_ptr_cast ();

void emit_cast_error () const;

protected:
TypeCastRules (location_t locus, TyTy::TyWithLocation from,
TyTy::TyWithLocation to);
Expand Down
6 changes: 6 additions & 0 deletions gcc/testsuite/rust/compile/issue-2680.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// { dg-additional-options "-fdump-tree-gimple" }
pub fn test_cast() {
let i = 1;
// { dg-final { scan-tree-dump-times {const i32 i;} 1 gimple } }
let _j = i as i64;
}
Loading