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

Investigate representing slices with 3 words, just like vectors #10295

Closed
nikomatsakis opened this issue Nov 5, 2013 · 11 comments
Closed

Investigate representing slices with 3 words, just like vectors #10295

nikomatsakis opened this issue Nov 5, 2013 · 11 comments
Labels
A-codegen Area: Code generation

Comments

@nikomatsakis
Copy link
Contributor

Assuming #8981 is completed (which it looks like it will be), I think we should consider representing slices using 3 words. The goal would be to have an invariant that every instance of the type ~T has the same representation as &T, for all T (in a DST world, T includes types like [U]).

This would simplify trans, but it would also enable us to write a function like borrow_all:

fn borrow_all<'a,T>(x: &'a [~T]) -> &'a [&'a T] { unsafe { transmute(x) } }

The main sticking point is that it would require an extra unused word in slices that takes the place of "capacity" (aka "alloc") for normal vectors. It's unclear what performance impact this would have and presumably merits some investigation.

I envisioned this "deep borrowing" as explicit, though it's conceivable we could integrate this into coercion. But in general I sort of want to remove autocoercion wherever possible.

@nikomatsakis
Copy link
Contributor Author

cc @thestinger @cmr @pcwalton @pnkfelix

@nikomatsakis
Copy link
Contributor Author

Going further, I am now thinking we should guarantee that ~T, &T, and *T have a "binary compatible" representation for all T. Making *T share a representation with ~T and friends seems like it might help to address issues around creating values like Rc<[T]>. This would also simplify trans and transmuting.

@emberian
Copy link
Member

emberian commented Nov 9, 2013

If *T is 3 words, isn't that going to massively break FFI?

On Sat, Nov 9, 2013 at 2:35 PM, Niko Matsakis notifications@gitpro.ttaallkk.topwrote:

Going further, I am now thinking we should guarantee that ~T, &T, and *Thave a "binary compatible" representation for all
T. Making *T share a representation with ~T and friends seems like it
might help to address issues around creating values like Rc<[T]>. This
would also simplify trans and transmuting.


Reply to this email directly or view it on GitHubhttps://github.com//issues/10295#issuecomment-28134965
.

@glaebhoerl
Copy link
Contributor

IIUC *T would be 3 words iff T is a DST, which aren't a thing outside of Rust anyways.

@nikomatsakis
Copy link
Contributor Author

If *T is 3 words, isn't that going to massively break FFI?

No, because it would only be 3 words for types like *[T]. In C,
there is no comparable type. The closest is *T and a (separate)
integer, I guess.

Niko

@huonw
Copy link
Member

huonw commented Nov 11, 2013

How would this interact with trait objects, e.g. given

trait Foo {}

impl Foo for [int] {}
impl Foo for int {}

fn main() {
     let v = (&[1i,2]) as &Foo;
     let w = &1i as &Foo;
}

wouldn't v have a different size to w, since the underlying & pointers are different sizes (so the characterisation of <sigil>Trait as (*vtable, <sigil>SelfType) would break down)?

@Kimundi
Copy link
Member

Kimundi commented Nov 11, 2013

Afaik, without making trait objects take always 4 words, you just wouldn't be able to make such a trait object (such as is the case today)

@nikomatsakis
Copy link
Contributor Author

I have been assuming that trait objects can never hide a dynamically sized type in any case. In other words, even if you implement trait Trait for [U], you could not case ~[U] to ~Trait (for precisely this reason -- the whole point of Trait objects is to hide the type, so if we must know the type to know how big the pointer is, that doesn't work out so well. I suppose making trait objects 4 words might be enough, though even that doesn't work out if you have something like impl Trait2 for Trait and you try to cast a ~Trait as a ~Trait2.

@pnkfelix
Copy link
Member

cc me

@treeman
Copy link
Contributor

treeman commented Sep 15, 2014

Triage bump, carry on.

@nikomatsakis
Copy link
Contributor Author

I...think this is basically a settled question now.

flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 10, 2023
…ious-to-owned, r=Jarcho

fix(suspicious_to_owned): use span_suggestions to suggest both intents

fixes rust-lang#10294

changelog: [`suspicious_to_owned`]: suggestions now produce valid Rust code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation
Projects
None yet
Development

No branches or pull requests

7 participants