Skip to content

transmute safety guidelines handle fat pointers incorrectly #20116

Closed
@nikomatsakis

Description

@nikomatsakis

The code incorrectly assumes that a type parameter which is not known to meet Sized will not meet Sized. So this code fails:

use std::mem::transmute;

fn foo<T, Sized? U>(x: &[T]) -> &U {
    unsafe { transmute(x) }
}

fn main() {
    let x = [1, 2, 3];
    foo::<int, int>(&x);
}

I'm overhauling how we handle Sized and will fix this to be more conservative, I guess.

However, I think that long term this approach of baking in weird rules around the transmute intrinsic is not good. It might be better (and perhaps just as easy or easier?) to have transmute reference a "magic" trait SameSize. This would allow generic code to expose and "bubble up" the transmutable requirement to its parents. It would also be useful for some of the in-place vec methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions