Stream: wg-traits

Topic: questions from Stack Overflow


Jake Goulding (Jan 31 2019 at 19:27, on Zulip):
trait Works {
    fn foo() -> Self;
}

This is does not report any errors from the compiler. If you add a body (even { unimplemented!() }) it complains about Sized. Is there any possible implementation of this trait method that doesn't require Sized? Should the unimplemented trait method have the same warning?

Jake Goulding (Jan 31 2019 at 19:28, on Zulip):
trait Errors {
    fn foo() -> Option<Self>;
}

this does have the warning

pnkfelix (Jan 31 2019 at 19:41, on Zulip):

I think the latter error is more due to a property of Option itself rather than of the trait

nikomatsakis (Jan 31 2019 at 19:41, on Zulip):

well

pnkfelix (Jan 31 2019 at 19:41, on Zulip):

you can't do Option<[u32]> for example

nikomatsakis (Jan 31 2019 at 19:42, on Zulip):

so @Jake Goulding we intentionally (but confusingly) say that "every function body checks that its return type is sized"; this was mostly to allow us room to expand traits in the future to permit unsized types in more places

nikomatsakis (Jan 31 2019 at 19:42, on Zulip):

notably, we wanted to define

trait FnOnce {
    fn call(self, ...);
}

without requiring FnOnce: Sized

nikomatsakis (Jan 31 2019 at 19:43, on Zulip):

(since we expect that to be implementable for unsized types eventually)

nikomatsakis (Jan 31 2019 at 19:44, on Zulip):

the point is, that trait def'n doesn't have a function body, so it doesn't enforce the requirement that the parameter types are sized, though it's true that to actually implement the trait, the self type must (presently) be sized

nikomatsakis (Jan 31 2019 at 19:44, on Zulip):

(what @pnkfelix said re: Option is also true, that's a separate case)

Jake Goulding (Jan 31 2019 at 19:47, on Zulip):

Ok, let me say that back to you.

Jake Goulding (Jan 31 2019 at 19:49, on Zulip):

There are two sets of checks happening here, which is why the difference appears confusing. Option requires T: Sized. Something that doesn't require Sized would be fine (e.g. Box<Self>)

Jake Goulding (Jan 31 2019 at 19:50, on Zulip):

The second check is that a (trait? any?) function with a body requires all param types (including return type?) to be sized.

Jake Goulding (Jan 31 2019 at 19:52, on Zulip):

I guess only traits can have "bodiless functions".

Jake Goulding (Jan 31 2019 at 19:53, on Zulip):

Can the "unsized locals/rvalue" work not apply to a return type then?

Jake Goulding (Jan 31 2019 at 19:53, on Zulip):

Oh:

// You CANNOT have unsized return types.
nikomatsakis (Jan 31 2019 at 19:56, on Zulip):

The second check is that a (trait? any?) function with a body requires all param types (including return type?) to be sized.

Right. You can think of it like this:

The function body, to type-check, requires its parameters + return-type are Sized, because we wouldn't know how to generate code otherwise (but if there is no body, we don't have to generate code, so no error).

Similarly, calling a function imposes the same requirement, for the same reason.

nikomatsakis (Jan 31 2019 at 19:57, on Zulip):

Can the "unsized locals/rvalue" work not apply to a return type then?

No, it can't, at least, not without more complexity. The problem is that the way return types work, the caller has to allocate stack space for the callee to write into, but without knowing the type of the value that will be returned, we can't know how much stack space to allocate.

nikomatsakis (Jan 31 2019 at 19:58, on Zulip):

(In contrast, we know how big the parameters we're about to pass are, or we can figure it out)

Jake Goulding (Jan 31 2019 at 20:04, on Zulip):

Thank you! I think I will need to do some deeper thinking as to how the actual implementation of Box<FnOnce()> can work.

mikeyhew (Jan 31 2019 at 20:10, on Zulip):

@nikomatsakis About that: I've always thought it would be possible to alloca the return value, and then move it when popping the stack frame. Has that been considered?

mikeyhew (Jan 31 2019 at 20:24, on Zulip):

Screen-Shot-2019-01-31-at-3.23.48-PM.png Like this

nikomatsakis (Jan 31 2019 at 20:54, on Zulip):

I don't know that I'd thought about moving it, I suppose it may be possible

nikomatsakis (Jan 31 2019 at 20:54, on Zulip):

certainly a big change to the ABI

Last update: Nov 12 2019 at 16:55UTC