Rust-GCC / Rust-GCC/gccrs

Gate or Implement dynamic upcasting conversion

Open
#3,157 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
2.9k
Forks
231
Avg merge
19h 55m
Merged PRs (30d)
67

Description

Summary

This issue is created because of a comment under https://github.com/Rust-GCC/gccrs/pull/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

c5f9d6ddf9363401b10849832c6ebc5e1a70067c

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue and the Rust RFC 3324 dynamic upcasting description, then inspect where gccrs handles the conversion before the gimple verifier crash. Determine whether the conversion should be gated or implemented, and verify that the reproducer no longer crashes or is correctly rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.