Skip to content

Enforce object-safety when coercing to an object - #255

Merged
nikomatsakis merged 2 commits into
rust-lang:masterfrom
nrc:objects
Oct 16, 2014
Merged

Enforce object-safety when coercing to an object#255
nikomatsakis merged 2 commits into
rust-lang:masterfrom
nrc:objects

Conversation

@nrc

@nrc nrc commented Sep 22, 2014

Copy link
Copy Markdown
Member

Restrict which objects can be coerced to trait objects, rather than which methods can be called on a trait object.

Restrict which objects can be coerced to trait objects, rather than which methods can be called on a trait object.
@nrc nrc self-assigned this Sep 22, 2014
@nrc

nrc commented Sep 22, 2014

Copy link
Copy Markdown
Member Author

cc @nikomatsakis

@sfackler

Copy link
Copy Markdown
Member

It would be nice to allow Box<Trait> when Trait has methods that take self by value.

@nrc

nrc commented Sep 22, 2014

Copy link
Copy Markdown
Member Author

@sfackler do you have an example? I had not considered having different rules for different kinds of trait objects, I fear that would be too much complexity unless there is a really important use case.

@sfackler

Copy link
Copy Markdown
Member

There's some discussion at rust-lang/rust#10672. Most notably, @pcwalton points out that it's required to allow Box<FnOnce<In, Out>> to replace proc(In) -> Out.

@sfackler

Copy link
Copy Markdown
Member

It'd also seem to me that you couldn't allow &Trait to implement Trait if it contained any &mut self methods for similar reasons to the by value self case.

@nrc

nrc commented Sep 22, 2014

Copy link
Copy Markdown
Member Author

@sfackler thanks for the link!

I need to check, but I think that the Box case is OK - fn foo(box self) is ok, it is literally only fn foo(self) which is not.

Why do you think &mut self will cause problems. My thinking here is that the no self by value rule is a kind of hackey way to allow this for 'all' traits rather than traits which are for Sized?. So the only requirement is that we have fat pointers so we have a vtable. Obviously that is not the case for self by value. However, thinking about it, we probably do need the Sized? requirement for Self since otherwise we can dereference self. (Maybe I am missing something on both sides of the argument though).

… rather than restricting value to reference types.
@CloudiDust

Copy link
Copy Markdown
Contributor

I remember @glaebhoerl arguing that the diffculties around DSTs are signs that we indeed need full-on existential types. I am not sure if this particular problem applies, but here is his (somewhat random) thought about existential types. And I'll leave it here just in case this is relevant. :)

@sfackler

Copy link
Copy Markdown
Member

Oops, I misinterpreted the proposal wrt &mut self.

@nikomatsakis

Copy link
Copy Markdown
Contributor

In general, the rules around Self do not apply to the receiver argument. Box<Self> and other such variations definitely need to work.

@nikomatsakis

Copy link
Copy Markdown
Contributor

Perhaps a more accurate rule would be to say "modified rules around Self apply to the receiver argument". We definitely need to phrase those rules correctly.

@nikomatsakis

Copy link
Copy Markdown
Contributor

This also intersects the proposed DerefMove trait, I've been working on integrating those two designs. It kind of boils down to a special case of supporting unsized types in parameters, which I still favor.

@reem

reem commented Oct 1, 2014

Copy link
Copy Markdown

@CloudiDust That's a pretty interesting set of notes. I hadn't thought much about existential types as they apply to Rust, but it seems like it could be a really fantastic fit.

@nikomatsakis
nikomatsakis merged commit b4738dd into rust-lang:master Oct 16, 2014
nikomatsakis added a commit that referenced this pull request Oct 16, 2014
withoutboats pushed a commit to withoutboats/rfcs that referenced this pull request Jan 15, 2017
@Centril Centril added A-trait-object Proposals relating to trait objects. A-coercions Proposals relating to coercions. A-traits Trait system related proposals & ideas A-typesystem Type system related proposals & ideas A-machine Proposals relating to Rust's abstract machine. labels Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-coercions Proposals relating to coercions. A-machine Proposals relating to Rust's abstract machine. A-trait-object Proposals relating to trait objects. A-traits Trait system related proposals & ideas A-typesystem Type system related proposals & ideas

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants