aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-09-17 16:41:25 +0100
committerPhilip Herron <philip.herron@embecosm.com>2021-09-17 16:41:25 +0100
commitd9a0a5f864b69c68d410a14e0aa7ccbaae7eb144 (patch)
tree590ca34cd83110c08f0d04c29ba41b3d0561e092
parentd144eea86290e8efc700d8042e9b517158b9f916 (diff)
downloadgcc-d9a0a5f864b69c68d410a14e0aa7ccbaae7eb144.zip
gcc-d9a0a5f864b69c68d410a14e0aa7ccbaae7eb144.tar.gz
gcc-d9a0a5f864b69c68d410a14e0aa7ccbaae7eb144.tar.bz2
Allow for coercion of structures over to dynamic objects in type system
This is the initial support for allowing a coercion of something like: ```rust let a = Foo(123); let b:&dyn Bound = &a; ``` The coercion will need to ensure that 'a' meets the specified bounds of the dynamic object. Addresses #197
-rw-r--r--gcc/rust/typecheck/rust-tyty-coercion.h11
-rw-r--r--gcc/testsuite/rust/compile/traits9.rs13
2 files changed, 24 insertions, 0 deletions
diff --git a/gcc/rust/typecheck/rust-tyty-coercion.h b/gcc/rust/typecheck/rust-tyty-coercion.h
index d46f1d4..976997d 100644
--- a/gcc/rust/typecheck/rust-tyty-coercion.h
+++ b/gcc/rust/typecheck/rust-tyty-coercion.h
@@ -24,6 +24,7 @@
#include "rust-tyty-visitor.h"
#include "rust-hir-map.h"
#include "rust-hir-type-check.h"
+#include "rust-hir-type-bounds.h"
extern ::Backend *
rust_get_backend ();
@@ -1390,6 +1391,16 @@ public:
resolved = base->clone ();
}
+ void visit (ADTType &type) override
+ {
+ Location ref_locus = mappings->lookup_location (type.get_ref ());
+ bool ok = base->bounds_compatible (type, ref_locus, true);
+ if (!ok)
+ return;
+
+ resolved = base->clone ();
+ }
+
private:
BaseType *get_base () override { return base; }
diff --git a/gcc/testsuite/rust/compile/traits9.rs b/gcc/testsuite/rust/compile/traits9.rs
new file mode 100644
index 0000000..e1aef539
--- /dev/null
+++ b/gcc/testsuite/rust/compile/traits9.rs
@@ -0,0 +1,13 @@
+struct Foo(i32);
+trait Bar {
+ fn baz(&self);
+}
+
+fn main() {
+ let a;
+ a = Foo(123);
+
+ let b: &dyn Bar = &a;
+ // { dg-error "bounds not satisfied for Foo .Bar. is not satisfied" "" { target *-*-* } .-1 }
+ // { dg-error "expected" "" { target *-*-* } .-2 }
+}