Skip to content

Trait objects can call nonexistent concrete methods #50781

Closed
@matthewjasper

Description

@matthewjasper

This code segfaults at runtime

use std::{
    fmt::{self, Debug},
};

trait X {
    fn foo(&self) where Self: Debug;
}

impl X for fn(&()) {
    fn foo(&self) where fn(&()): Debug {}
}

impl Debug for dyn X {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "hi!")
    }
}

pub fn main() {
    let x: fn(&()) = |_| ();
    let y = &x as &dyn X;
    y.foo(); // Segfault at opt-level 0, SIGILL otherwise.
}

cc #48557 - Makes this easier to come across.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dyn-traitArea: trait objects, vtable layoutA-trait-systemArea: Trait systemC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions