Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic objects support inheritance #914

Closed
philberty opened this issue Feb 9, 2022 · 4 comments · Fixed by #3124
Closed

Dynamic objects support inheritance #914

philberty opened this issue Feb 9, 2022 · 4 comments · Fixed by #3124
Assignees
Labels

Comments

@philberty
Copy link
Member

I tried this code:

extern "C" {
    fn printf(s: *const i8, ...);
}

struct Foo(i32);
trait Bar {
    fn baz(&self);
}

trait Baz : Bar {
    fn qux(&self);
}

impl Bar for Foo {
    fn baz(&self) {
        unsafe {
            let a = "baz %i\n\0";
            let b = a as *const str;
            let c = b as *const i8;

            printf(c, self.0);
        }
    }
}

impl Baz for Foo {
    fn qux(&self) {
        unsafe {
            let a = "baz %i\n\0";
            let b = a as *const str;
            let c = b as *const i8;

            printf(c, self.0);
        }
    }
}


fn dynamic_dispatch(t: &dyn Baz) {
    t.baz();
    t.qux();
}

pub fn main() {
    let a;
    a = &Foo(123);
    dynamic_dispatch(a);
}

I expected to see this happen: compile without error

Instead, this happened:

<source>:40:7: error: failed to resolve method for 'baz'
   40 |     t.baz();
      |       ^
<source>:40:5: error: failed to type resolve expression
   40 |     t.baz();
      |     ^
rust1: error: bounds not satisfied for dyn [example::Baz] 'example::Bar' is not satisfied

Meta

  • What version of Rust GCC were you using, git sha if possible. 83bfbf0
@philberty philberty added the bug label Feb 9, 2022
@philberty philberty added this to the Macro Expansion milestone Feb 9, 2022
@bjorn3
Copy link

bjorn3 commented Feb 9, 2022

You can't get from &dyn Baz to &dyn Bar on stable rustc. What happens here is that the vtable contains all methods of all super traits. On nightly rustc it also contains pointers to vtables of super traits as necessary to allow upcasting trait objects to super traits.

@philberty philberty self-assigned this Feb 18, 2022
@philberty philberty removed this from the Macro Expansion milestone Mar 17, 2022
@philberty philberty added this to the Const Generics milestone Aug 3, 2022
philberty added a commit that referenced this issue Oct 12, 2022
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses #914
@CohenArthur CohenArthur moved this to Additional sprint items in libcore 1.49 Oct 14, 2022
bors bot added a commit that referenced this issue Oct 14, 2022
1583: Support type resolution on super traits on dyn objects r=philberty a=philberty

When checking if specified bounds satisfy other bounds we must lookup the super traits. To finish the support for super traits we need to redo the computation of method addresses to support super traits.

Addresses #914


Co-authored-by: Philip Herron <philip.herron@embecosm.com>
@philberty philberty moved this from Additional sprint items to In Progress in libcore 1.49 Oct 14, 2022
@philberty philberty moved this from In Progress to Additional sprint items in libcore 1.49 Oct 14, 2022
dkm pushed a commit that referenced this issue Dec 17, 2022
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses #914
dkm pushed a commit that referenced this issue Dec 28, 2022
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses #914
dkm pushed a commit that referenced this issue Jan 6, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses #914
dkm pushed a commit that referenced this issue Jan 15, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses #914
CohenArthur pushed a commit that referenced this issue Jan 16, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses #914
CohenArthur pushed a commit to CohenArthur/gccrs that referenced this issue Feb 8, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses Rust-GCC#914

gcc/rust/ChangeLog:

	* backend/rust-compile.cc: Add note about missing support for super
	traits.
	* typecheck/rust-tyty.cc (BaseType::satisfies_bound): New function.
	(BaseType::bounds_compatible): New function.
	(DynamicObjectType::get_object_items): New function.
	* typecheck/rust-hir-trait-ref.h: Use new API to perform type resolution
	on dyn objects.
CohenArthur pushed a commit to CohenArthur/gccrs that referenced this issue Feb 8, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses Rust-GCC#914

gcc/rust/ChangeLog:

	* backend/rust-compile.cc: Add note about missing support for super
	traits.
	* typecheck/rust-tyty.cc (BaseType::satisfies_bound): New function.
	(BaseType::bounds_compatible): New function.
	(DynamicObjectType::get_object_items): New function.
	* typecheck/rust-hir-trait-ref.h: Use new API to perform type resolution
	on dyn objects.
CohenArthur pushed a commit to CohenArthur/gccrs that referenced this issue Feb 9, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses Rust-GCC#914

gcc/rust/ChangeLog:

	* backend/rust-compile.cc: Add note about missing support for super
	traits.
	* typecheck/rust-tyty.cc (BaseType::satisfies_bound): New function.
	(BaseType::bounds_compatible): New function.
	(DynamicObjectType::get_object_items): New function.
	* typecheck/rust-hir-trait-ref.h: Use new API to perform type resolution
	on dyn objects.
CohenArthur pushed a commit to CohenArthur/gccrs that referenced this issue Feb 9, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses Rust-GCC#914

gcc/rust/ChangeLog:

	* backend/rust-compile.cc: Add note about missing support for super
	traits.
	* typecheck/rust-tyty.cc (BaseType::satisfies_bound): New function.
	(BaseType::bounds_compatible): New function.
	(DynamicObjectType::get_object_items): New function.
	* typecheck/rust-hir-trait-ref.h: Use new API to perform type resolution
	on dyn objects.
tschwinge pushed a commit that referenced this issue Feb 22, 2023
When checking if specified bounds satisfy other bounds we must lookup the
super traits. To finish the support for super traits we need to redo the
computation of method addresses to support super traits.

Addresses #914

gcc/rust/ChangeLog:

	* backend/rust-compile.cc: Add note about missing support for super
	traits.
	* typecheck/rust-tyty.cc (BaseType::satisfies_bound): New function.
	(BaseType::bounds_compatible): New function.
	(DynamicObjectType::get_object_items): New function.
	* typecheck/rust-hir-trait-ref.h: Use new API to perform type resolution
	on dyn objects.
@CohenArthur CohenArthur moved this from Additional sprint items to Todo in libcore 1.49 Apr 18, 2023
@liamnaddell
Copy link
Contributor

New behavior: ICE!

crab1: internal compiler error: in compute_address_for_trait_item, at rust/backend/rust-compile.cc:362
0x10afce3 Rust::Compile::HIRCompileBase::compute_address_for_trait_item(Rust::Resolver::TraitItemReference const*, Rust::TyTy::TypeBoundPredicate const*, std::vector<std::pair<Rust::Resolver::TraitReference*, Rust::HIR::ImplBlock*>, std::allocator<std::pair<Rust::Resolver::TraitReference*, Rust::HIR::ImplBlock*> > >&, Rust::TyTy::BaseType const*, Rust::TyTy::BaseType const*, unsigned int)
        ../../gccrs/gcc/rust/backend/rust-compile.cc:362
0x10af543 Rust::Compile::HIRCompileBase::coerce_to_dyn_object(tree_node*, Rust::TyTy::BaseType const*, Rust::TyTy::DynamicObjectType const*, unsigned int)
        ../../gccrs/gcc/rust/backend/rust-compile.cc:219
0x13cc47f Rust::Compile::HIRCompileBase::resolve_unsized_dyn_adjustment(Rust::Resolver::Adjustment&, tree_node*, unsigned int)
        ../../gccrs/gcc/rust/backend/rust-compile-expr.cc:2000
0x13cc007 Rust::Compile::HIRCompileBase::resolve_unsized_adjustment(Rust::Resolver::Adjustment&, tree_node*, unsigned int)
        ../../gccrs/gcc/rust/backend/rust-compile-expr.cc:1949
0x13cbc8f Rust::Compile::HIRCompileBase::resolve_adjustements(std::vector<Rust::Resolver::Adjustment, std::allocator<Rust::Resolver::Adjustment> >&, tree_node*, unsigned int)
        ../../gccrs/gcc/rust/backend/rust-compile-expr.cc:1890
0x10aeb53 Rust::Compile::HIRCompileBase::coercion_site(unsigned int, tree_node*, Rust::TyTy::BaseType*, Rust::TyTy::BaseType*, unsigned int, unsigned int)
        ../../gccrs/gcc/rust/backend/rust-compile.cc:66
0x13c96fb Rust::Compile::CompileExpr::visit(Rust::HIR::CallExpr&)
        ../../gccrs/gcc/rust/backend/rust-compile-expr.cc:1269
0x1195ec3 Rust::HIR::CallExpr::accept_vis(Rust::HIR::HIRExpressionVisitor&)
        ../../gccrs/gcc/rust/hir/tree/rust-hir.cc:4865
0x13c5157 Rust::Compile::CompileExpr::Compile(Rust::HIR::Expr*, Rust::Compile::Context*)
        ../../gccrs/gcc/rust/backend/rust-compile-expr.cc:46
0x13c3c5f Rust::Compile::CompileStmt::visit(Rust::HIR::ExprStmt&)
        ../../gccrs/gcc/rust/backend/rust-compile-stmt.cc:43
0x1195ccb Rust::HIR::ExprStmt::accept_vis(Rust::HIR::HIRStmtVisitor&)
        ../../gccrs/gcc/rust/hir/tree/rust-hir.cc:4811
0x13c3be7 Rust::Compile::CompileStmt::Compile(Rust::HIR::Stmt*, Rust::Compile::Context*)
        ../../gccrs/gcc/rust/backend/rust-compile-stmt.cc:36
0x13ef6ff Rust::Compile::HIRCompileBase::compile_function_body(tree_node*, Rust::HIR::BlockExpr&, Rust::TyTy::BaseType*)
        ../../gccrs/gcc/rust/backend/rust-compile-base.cc:575
0x13f00f7 Rust::Compile::HIRCompileBase::compile_function(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Rust::HIR::SelfParam&, std::vector<Rust::HIR::FunctionParam, std::allocator<Rust::HIR::FunctionParam> >&, Rust::HIR::FunctionQualifiers const&, Rust::HIR::Visibility&, std::vector<Rust::AST::Attribute, std::allocator<Rust::AST::Attribute> >&, unsigned int, Rust::HIR::BlockExpr*, Rust::Resolver::CanonicalPath const&, Rust::TyTy::FnType*)
        ../../gccrs/gcc/rust/backend/rust-compile-base.cc:751
0x13c2e9f Rust::Compile::CompileItem::visit(Rust::HIR::Function&)
        ../../gccrs/gcc/rust/backend/rust-compile-item.cc:205
0x11964f3 Rust::HIR::Function::accept_vis(Rust::HIR::HIRStmtVisitor&)
        ../../gccrs/gcc/rust/hir/tree/rust-hir.cc:5033
0x10b0cd3 Rust::Compile::CompileItem::compile(Rust::HIR::Item*, Rust::Compile::Context*, Rust::TyTy::BaseType*, bool, unsigned int)
        ../../gccrs/gcc/rust/backend/rust-compile-item.h:38
0x10aea47 Rust::Compile::CompileCrate::go()
        ../../gccrs/gcc/rust/backend/rust-compile.cc:48
0x10ae98b Rust::Compile::CompileCrate::Compile(Rust::HIR::Crate&, Rust::Compile::Context*)
        ../../gccrs/gcc/rust/backend/rust-compile.cc:41
0x1071e53 Rust::Session::compile_crate(char const*)
        ../../gccrs/gcc/rust/rust-session-manager.cc:710
Please submit a full bug report, with preprocessed source (by using -freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.

@liamnaddell
Copy link
Contributor

liamnaddell commented Aug 5, 2024

This ICE isn't related to either t.baz() or t.qux() invocations, it's related to passing Foo(123) as a dynamic reference to Baz. Here's a minified reproducer of the same issue:

#![allow(dead_code)]

struct Foo(i32);
trait Bar {
    fn baz(&self);
}

trait Baz : Bar {
    fn qux(&self);
}

impl Bar for Foo {
    fn baz(&self) {
    }
}

impl Baz for Foo {
    fn qux(&self) {
    }
}

pub fn main() {
    let a = Foo(123);
    let _b: &dyn Baz = &a;
}

@liamnaddell
Copy link
Contributor

liamnaddell commented Aug 5, 2024

As a note, I think @bjorn3 's comment is out of date now. I'm able to compile the example @philberty posted without trouble on stable rustc:

liam@gentoo ~/super $ rustc --version
rustc 1.79.0 (129f3b996 2024-06-10)
liam@gentoo ~/super $ cat main3.rs
... same code as philberty provided ... 
liam@gentoo ~/super $ rustc main3.rs
liam@gentoo ~/super $

liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Aug 6, 2024
liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Aug 10, 2024
gcc/rust/ChangeLog:

	* gcc/rust/backend/rust-compile.cc:
	Modify compute_address_for_trait_item to support supertraits

gcc/testsuite/ChangeLog:

	* rust/execute/torture/trait14.rs:
	Add test for dynamic dispatch with supertraits

Signed-off-by: Liam Naddell <liam.naddell@mail.utoronto.ca>
liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Aug 10, 2024
gcc/rust/ChangeLog:

	* backend/rust-compile.cc:
	Modify compute_address_for_trait_item to support supertraits
	* typecheck/rust-tyty.cc:
	Remove auto

gcc/testsuite/ChangeLog:

	* rust/execute/torture/trait14.rs:
	Add test for dynamic dispatch with supertraits

Signed-off-by: Liam Naddell <liam.naddell@mail.utoronto.ca>
liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Aug 10, 2024
gcc/rust/ChangeLog:

	* backend/rust-compile.cc:
	Modify compute_address_for_trait_item to support supertraits
	* typecheck/rust-tyty.cc:
	Remove auto

gcc/testsuite/ChangeLog:

	* rust/execute/torture/trait14.rs:
	Add test for dynamic dispatch with supertraits

Signed-off-by: Liam Naddell <liam.naddell@mail.utoronto.ca>
liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Aug 10, 2024
gcc/rust/ChangeLog:

	* backend/rust-compile.cc:
	Modify compute_address_for_trait_item to support supertraits
	* typecheck/rust-tyty.cc:
	Remove auto

gcc/testsuite/ChangeLog:

	* rust/execute/torture/trait14.rs:
	Add test for dynamic dispatch with supertraits

Signed-off-by: Liam Naddell <liam.naddell@mail.utoronto.ca>
@github-project-automation github-project-automation bot moved this from Todo to Done in libcore 1.49 Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants