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

Gate or Implement dynamic upcasting conversion #3157

Open
1 task done
liamnaddell opened this issue Sep 10, 2024 · 0 comments
Open
1 task done

Gate or Implement dynamic upcasting conversion #3157

liamnaddell opened this issue Sep 10, 2024 · 0 comments

Comments

@liamnaddell
Copy link
Contributor

liamnaddell commented Sep 10, 2024

Summary

This issue is created because of a comment under #3124.

Background: https://rust-lang.github.io/rfcs/3324-dyn-upcasting.html

As far as I understand, internally, a dynamic reference is a vtable containing all the required struct items, and method pointers. Since rust supports parent and child traits (supertraits), I believe casting from &dyn Child -> &dyn parent requires pruning the Child's vtable to match the Parent's vtable, as far as gccrs is concerned. Rust does not have a specified ABI in this case, so we do not need to conform to some specific vtable layout.

As of writing, rustc stable does not support dynamic upcasting, but the RFC is implemented in nightly rust. Meaning it's likely we will have to support this at some point to remain compliant with rustc.

Reproducer

I tried this code:

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

struct Foo {
    my_int: u32,
}

trait Parent {
    fn parent(&self) -> bool;
}

trait Child : Parent {
    fn child(&self);
}

impl Parent for Foo {
    fn parent(&self) -> bool {
        // Call supertrait method
        return true;
    }
}

impl Child for Foo {
    fn child(&self) {
        let _ = self;
    }
}

pub fn main() {
    let a = Foo{ my_int: 0xf00dfeed};
    let b: &dyn Child = &a;

    let c: &dyn Parent = b;

    c.parent();
}

Does the code make use of any (1.49) nightly feature ?

  • Nightly

Godbolt link

No response

Actual behavior

Crash in the gimple verifier

Expected behavior

Either we need to gate this conversion until rustc stabilizes the behavior, or implement the feature once stabilized.

GCC Version

c5f9d6d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant