aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile.cc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-05-23 10:46:59 +0100
committerPhilip Herron <philip.herron@embecosm.com>2022-05-23 13:49:01 +0100
commitcd39861da5e1113207193bb8b3e6fb3dde92895f (patch)
treeb12ed1fc0c7f4bcddbb38ff51cae7aea393b7a40 /gcc/rust/backend/rust-compile.cc
parentc7008d3e254786b5e752aa61e067f62c38042b81 (diff)
downloadgcc-cd39861da5e1113207193bb8b3e6fb3dde92895f.zip
gcc-cd39861da5e1113207193bb8b3e6fb3dde92895f.tar.gz
gcc-cd39861da5e1113207193bb8b3e6fb3dde92895f.tar.bz2
Fix Slice Type Layout
Slices in Rust are represented by TypePaths such as '[i32]'. Though if you actually try to use this explicit type-path you will hit errors such as this type has an unknown size at compile time. This is because this is actually what Rust calls a dynamically sized type. This means when you use types such as: '&[i32]' it is not actually a reference type to a slice. Its a slice in its entirety this means for lack of a better word when you use '*const [i32]' or '&mut [i32]' we end up actually passing around a struct by value _not_ at pointer/reference to it. This patch changes the type-layout so that we handle this layout change properly. This patch will also need to be applied to str types which I believe have a similar layout for safety. The patch also sets up TYPE_MAIN_VARIANT so that we can avoid unnessecary view_convert_expressions between *const [i32] and &mut [i32] which will have the same layout. Reference: https://doc.rust-lang.org/reference/dynamically-sized-types.html https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=672adac002939a2dab43b8d231adc1dc Fixes #1232
Diffstat (limited to 'gcc/rust/backend/rust-compile.cc')
-rw-r--r--gcc/rust/backend/rust-compile.cc19
1 files changed, 19 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index d9349d5..18a2df6 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -218,6 +218,11 @@ HIRCompileBase::coercion_site (tree rvalue, const TyTy::BaseType *rval,
= static_cast<const TyTy::ReferenceType *> (expected);
const TyTy::ReferenceType *act
= static_cast<const TyTy::ReferenceType *> (actual);
+ if (act->is_dyn_slice_type ())
+ {
+ // nothing to do
+ return rvalue;
+ }
tree expected_type = TyTyResolveCompile::compile (ctx, act->get_base ());
tree deref_rvalue
@@ -227,6 +232,8 @@ HIRCompileBase::coercion_site (tree rvalue, const TyTy::BaseType *rval,
tree coerced
= coercion_site (deref_rvalue, act->get_base (), exp->get_base (),
lvalue_locus, rvalue_locus);
+ if (exp->is_dyn_slice_type () && SLICE_TYPE_P (TREE_TYPE (coerced)))
+ return coerced;
return address_expression (coerced,
build_reference_type (TREE_TYPE (coerced)),
@@ -249,6 +256,12 @@ HIRCompileBase::coercion_site (tree rvalue, const TyTy::BaseType *rval,
{
const TyTy::ReferenceType *act
= static_cast<const TyTy::ReferenceType *> (actual);
+ if (act->is_dyn_slice_type ())
+ {
+ // nothing to do
+ return rvalue;
+ }
+
actual_base = act->get_base ();
expected_type = TyTyResolveCompile::compile (ctx, act->get_base ());
}
@@ -256,6 +269,12 @@ HIRCompileBase::coercion_site (tree rvalue, const TyTy::BaseType *rval,
{
const TyTy::PointerType *act
= static_cast<const TyTy::PointerType *> (actual);
+ if (act->is_dyn_slice_type ())
+ {
+ // nothing to do
+ return rvalue;
+ }
+
actual_base = act->get_base ();
expected_type = TyTyResolveCompile::compile (ctx, act->get_base ());
}