From d9a0a5f864b69c68d410a14e0aa7ccbaae7eb144 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 17 Sep 2021 16:41:25 +0100 Subject: 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 --- gcc/rust/typecheck/rust-tyty-coercion.h | 11 +++++++++++ gcc/testsuite/rust/compile/traits9.rs | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 gcc/testsuite/rust/compile/traits9.rs (limited to 'gcc') 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 } +} -- cgit v1.1