You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since Infallible is an empty type, I expect (T, Infallible) also be an empty type, thus they should have the same size. But I noticed that (T, Infallible) have the same size as T, so currently, we have
mem::size_of::<Option<Infallible>>() == 0
mem::size_of::<Option<(u32, Infallible)>>() = 8
I guess this is because Rust treats empty type to have size 0, so the size of (T, Infallible) is size of T plus size of Infallible equals size of T. May be we can use something like Option<usize> to represent the size of a type internally, where None means the size of an empty type, in this way, we can distinguish empty types from zero sized types. For example, internally, we can have a function like:
fninternal_size_of<T>() -> Option<usize>{ ...}
To keep the current behavior of mem::size_of, it can be implemented as:
(I don't claim to be authoritative on this topic)
(I'm using ! but this applies to Infallible just as well)
tl;dr:
If you were thinking that size_of::<Option<!>>() == 0 should imply size_of::<Option<(u32, !)>>() = 0, I don't believe there is currently any guarantee for that, and, in any case, the logic for each would be distinct.
The Rustonomicon, which you linked, doesn't seem to guarantee anything about the size of (T, !), and the doc page certainly doesn't. In any case, I don't think it would make sense for the size of (T, !) to be 0. Just like how Result<T, !> effectively removes the Err variant because Err(!) can't be instantiated, (T, !) should effectively be a one-member tuple because the programmer is effectively telling the compiler that the second member can't be instantiated. Monomorphization doesn't completely eliminate Result<T, !> because no such statement has been made about the Ok(_) variant, and similarly, (T, !) makes no statement about the instantiability (lol) of the first member.
Indeed, the size of (T, Infallible) was 0 at some point and that was critical soundness issue and got fixed.
So I think this issue should probably be closed. The one thing we could do here is improve documentation of this problem, but I am not sure where such docs would go.
As @Ericson2314 pointed out, the example that made this unsound does not compile any more. NLL does not accept thus code so when NLL became mandatory on all editions, this stopped working. That decision was made here; there is an open issue at #54987 to re-accept such code.
I will hence re-open this issue to track potentially doing the layout optimization again. That said, clearly that is in conflict with #54987, and there might be other soundness issues as well that I am missing.
Also see the discussion starting at #54987 (comment) for other potential soundness concerns with making (!, T) have size 0. In particular, code like this still works:
let x:(String, !) = (mk_string(),panic!());
and it needs to store the string somewhere (and drop it).
Currently a temporary is created for this (and the tuple constructor is not even anywhere to be seen in the MIR), but I am not sure how intentional that is.
And anyway computing the layout of a type involves a lot more than computing its size, so it's not like somehow changing the size of ! would magically make anything easier. The trouble isn't having layout computation make (!, i32) a ZST -- that in fact was already implemented at some point in the past, but got reverted due to soundness issues (and that was referenced above: #49298). The trouble is figuring out if doing so will cause problems elsewhere.
-∞ is I still think morally on the right track, namely we should not conflate the layout of ! and (). Calling them both "ZSTs" just adds more special cases later. What we have is something like
My understanding is we evaluate the right and side, it diverges, and so we don't care what the left hand side looks like.
let(a, b):(String, !) = (mk_string(),panic!());
Splitting into separate lets doesn't change the behavior of the program, even if it does expose a new location where we didn't have one before.
#![feature(never_type)]fnmain(){let x: !;println!("asdf");
x = panic!();}
Does work today, and cracks me up.
Basically, for spooky reasons of polarity of the sort discussed in many papers of at https://www.pauldownen.com/#publications , I think ! really is just a valid type for "(r) values" (not unlike the original restriction, ironically enough!) and so it "nukes" any locations it touches, per the homormophic layout rules.
The problem with things like let x: !; is that an unitialized variable I think is "morally" a different type Uninitialized<A> instead of A (and the "moral types" of locations changes throughout the control flow graph). When A is inhabited, Uninitialized<A> and A have the same layout, but when A isn't they don't because the whole point of Uninitialized<A> is that it is a "fat" (), and so always inhabited. This absurdity is why uninitialized uninhabitted types should just be banned --- do that, and I will bet a few beers all problems go away.
MaybeUninit, being union, is always inhabited, so MaybeUninit<!> is fine but useless, and MaybeUninit<(!, usize)> is fine but just as useless. (Still cannot do anything with second field of the tuple because the tuple is a ZST, too bad.) This might upset people, but follows from the rules so I am OK with it.
This absurdity is why uninitialized uninhabitted types should just be banned --- do that, and I will bet a few beers all problems go away.
I am suspicious of any calculus that ends up not being able to treat uninhabited types properly. let x: T; should work in generic code for any T, ergo it should work for ! as well. If that is a problem for some formal calculus, IMO that shows a flaw in the calculus.
Reacted by scottmcm, Kolsky, Petr Portnov | PROgrm_JARvis, Eyal Kalderon, zachs18 and Alisa Sireneva
The key word there is "properly". The point of the literature of the sort I linked is not to just ban all the scary stuff, but introduce a little bureaucracy once to get back nice things, like eta rules, de Morgan's laws, etc. etc. I am not proposing that ! locations just be abandoned, just that they must always be initialized. That makes allows us to avoid worrying about what they look like since no code and ever access them. (And *const ! and friends of course doesn't say anything useful about what lies behind the pointer in the type.)
The previous method of breaking the layout algebra by making ! annihilate sums only outside of products I consider a hack to avoid the consequences of what ! "wants to mean". I suppose back in the day making mem::forget kosher because panicking is very analogous to making !'s layout non-algebraic because partial initialization. The "more linearity, less mem::forget" world is pretty well closed off to us at this point, but the "! layout is algebraic" world is still accessible as long as we only reintroduce partial initialization carefully.
If return type is uninhabited there's no reason to place partially initialized return value in return place, function diverges anyway.
The way to think about this is that the foo and bar calls may be worlds apart.
With more components/functions: (a(), b(), c(), d(), e(), ..., z()).
Let's say you learn, during monomorphization/codegen, that z() always diverges, but what does that mean for every call before it? Not much, they still have to execute.
If, say, d panics, then the values returned by a(), b() and c() have to be dropped.
And z itself might not even be a failure mode, but e.g. || std::process::exit(0).
Either way, by the time you know everything about z, MIR optimizations must have executed in absentia (on the generic definition), and so they may then rely on return.{0,...,25} existing, and being useful as general-purpose scratch space (even for monomorphizations where the function doesn't return).
Hi! I was under the impression that this question was well settled in the direction of "(T, !) has space for T". I can't find a good source though, what's the status of this? If this hasn't been done yet, could we get a binding decision from T-lang on this matter in one direction or the other? (I'm hoping to resurrect #54987)
I don't think that was ever officially decided. It just happens to be the case currently and MIR building relies on it so when (T, !) was accidentally made zero-sized that led to a soundness issue.
If you want to get a T-lang decision, I suggest you write up the motivation and what you want to have decided, and nominate that for T-lang discussion.
use core::mem::MaybeUninit;pubfnmake_tuple<A,B>(a:implFnOnce() -> A,b:implFnOnce() -> B) -> (A,B){unsafe{letmut m:MaybeUninit<(A,B)> = MaybeUninit::uninit();
std::ptr::addr_of_mut!((*m.as_mut_ptr()).0).write(a());
std::ptr::addr_of_mut!((*m.as_mut_ptr()).1).write(b());
m.assume_init()}}enumNever{}fnmain(){let x = make_tuple(|| "hello".to_string(), || "world".to_string());dbg!(x);let _y:(String,Never) = make_tuple(|| "yup".to_string(), || panic!());}
should not be UB, even though make_tuple::<String, Never> like that must always diverge. (The example leaks, but that could be fixed -- the point is that it's not UB to write a non-ZST into the storage for m.)
So long as that's the case -- basically meaning that field projecting inside a MaybeUninit like that is legal -- I think that precludes making (String, Never) a ZST.
(Existing layout guarantees https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#layout-1 prevent us from saying that MaybeUninit<(String, Never)> is a non-ZST but (String, Never) is a ZST. And I don't think I'd want to try to split that hair even if we could.)
So if you wanted to write that up in some appropriate document form (RFC, like the provenance one? Dunno) I think it could be accepted relatively easily.
Reacted by Nadrieril, Gábor Lehel, Anton and zachs18
St<u8> and StU8 had different sizes. I think it might be the the first case where substituting generics "by hand" would lead to significantly different codegen.
Maybe a first step would be to add this to the lang team's "frequently requested changes" and explain there that this is not going to happen. That should be sufficient to close this issue.
Maybe a first step would be to add this to the lang team's "frequently requested changes" and explain there that this is not going to happen. That should be sufficient to close this issue.
Activity
vacuus commentedon Jan 18, 2022
(I don't claim to be authoritative on this topic)
(I'm using
!but this applies toInfalliblejust as well)tl;dr:
If you were thinking that
size_of::<Option<!>>() == 0should implysize_of::<Option<(u32, !)>>() = 0, I don't believe there is currently any guarantee for that, and, in any case, the logic for each would be distinct.The Rustonomicon, which you linked, doesn't seem to guarantee anything about the size of
(T, !), and the doc page certainly doesn't. In any case, I don't think it would make sense for the size of(T, !)to be 0. Just like howResult<T, !>effectively removes theErrvariant becauseErr(!)can't be instantiated,(T, !)should effectively be a one-member tuple because the programmer is effectively telling the compiler that the second member can't be instantiated. Monomorphization doesn't completely eliminateResult<T, !>because no such statement has been made about theOk(_)variant, and similarly,(T, !)makes no statement about the instantiability (lol) of the first member.Please correct me if I'm wrong!
scottmcm commentedon Jan 19, 2022
This is intentional; see #49298
RalfJung commentedon Jan 19, 2022
Indeed, the size of
(T, Infallible)was 0 at some point and that was critical soundness issue and got fixed.So I think this issue should probably be closed. The one thing we could do here is improve documentation of this problem, but I am not sure where such docs would go.
vacuus commentedon Jan 19, 2022
Perhaps the same page of the Rustonomicon that
@EFanZhlinked at the top?RalfJung commentedon Mar 18, 2022
As @Ericson2314 pointed out, the example that made this unsound does not compile any more. NLL does not accept thus code so when NLL became mandatory on all editions, this stopped working. That decision was made here; there is an open issue at #54987 to re-accept such code.
I will hence re-open this issue to track potentially doing the layout optimization again. That said, clearly that is in conflict with #54987, and there might be other soundness issues as well that I am missing.
RalfJung commentedon Mar 18, 2022
Also see the discussion starting at #54987 (comment) for other potential soundness concerns with making
(!, T)have size 0. In particular, code like this still works:and it needs to store the string somewhere (and drop it).
Currently a temporary is created for this (and the tuple constructor is not even anywhere to be seen in the MIR), but I am not sure how intentional that is.
taralx commentedon Mar 18, 2022
What is the challenge in making -∞ the size of uninhabited types? It makes the math work:
size_of::<!>()= -∞size_of::<Option<!>>()= LSE(-∞, 0) = 0size_of::<(u32, !)>()= 4 + -∞ = -∞size_of::<Option<(u32,!)>>()= LSE(4 + -∞, 0) = 0RalfJung commentedon Mar 18, 2022
-∞is not a number :)And anyway computing the layout of a type involves a lot more than computing its size, so it's not like somehow changing the size of
!would magically make anything easier. The trouble isn't having layout computation make(!, i32)a ZST -- that in fact was already implemented at some point in the past, but got reverted due to soundness issues (and that was referenced above: #49298). The trouble is figuring out if doing so will cause problems elsewhere.Ericson2314 commentedon Mar 18, 2022
-∞is I still think morally on the right track, namely we should not conflate the layout of!and(). Calling them both "ZSTs" just adds more special cases later. What we have is something likeand thus a
Inhabitted(InhabittedLayout { size: 0, align: 0 })vsUninhabitteddistinction.Per https://en.wikipedia.org/wiki/Tropical_geometry layout and the divisibility lattice, layout computation does have nice algebraic properties.
RalfJung commentedon Mar 18, 2022
Yeah, and they have different
Layoutin Rust. That doesn't need to be reflected in size and alignment though.Ericson2314 commentedon Mar 18, 2022
My understanding is we evaluate the right and side, it diverges, and so we don't care what the left hand side looks like.
Splitting into separate lets doesn't change the behavior of the program, even if it does expose a new location where we didn't have one before.
Does work today, and cracks me up.
Basically, for spooky reasons of polarity of the sort discussed in many papers of at https://www.pauldownen.com/#publications , I think
!really is just a valid type for "(r) values" (not unlike the original restriction, ironically enough!) and so it "nukes" any locations it touches, per the homormophic layout rules.The problem with things like
let x: !;is that an unitialized variable I think is "morally" a different typeUninitialized<A>instead ofA(and the "moral types" of locations changes throughout the control flow graph). WhenAis inhabited,Uninitialized<A>andAhave the same layout, but whenAisn't they don't because the whole point ofUninitialized<A>is that it is a "fat"(), and so always inhabited. This absurdity is why uninitialized uninhabitted types should just be banned --- do that, and I will bet a few beers all problems go away.MaybeUninit, being union, is always inhabited, soMaybeUninit<!>is fine but useless, andMaybeUninit<(!, usize)>is fine but just as useless. (Still cannot do anything with second field of the tuple because the tuple is a ZST, too bad.) This might upset people, but follows from the rules so I am OK with it.RalfJung commentedon Mar 18, 2022
I am suspicious of any calculus that ends up not being able to treat uninhabited types properly.
let x: T;should work in generic code for anyT, ergo it should work for!as well. If that is a problem for some formal calculus, IMO that shows a flaw in the calculus.Ericson2314 commentedon Mar 18, 2022
The key word there is "properly". The point of the literature of the sort I linked is not to just ban all the scary stuff, but introduce a little bureaucracy once to get back nice things, like eta rules, de Morgan's laws, etc. etc. I am not proposing that
!locations just be abandoned, just that they must always be initialized. That makes allows us to avoid worrying about what they look like since no code and ever access them. (And*const !and friends of course doesn't say anything useful about what lies behind the pointer in the type.)The previous method of breaking the layout algebra by making
!annihilate sums only outside of products I consider a hack to avoid the consequences of what!"wants to mean". I suppose back in the day makingmem::forgetkosher because panicking is very analogous to making!'s layout non-algebraic because partial initialization. The "more linearity, lessmem::forget" world is pretty well closed off to us at this point, but the "!layout is algebraic" world is still accessible as long as we only reintroduce partial initialization carefully.17 remaining items
zakarumych commentedon Jul 26, 2022
If return type is uninhabited there's no reason to place partially initialized return value in return place, function diverges anyway.
But I get the idea.
If the goal is to make the following code acceptable and non-panicking
Then keeping
size_of::<Foo>() == 1would be impossible.Because then
1would be stored on stack and address would differ.I wouldn't think about it if you didn't mention
modify_foo(&mut return.0)call ^_^eddyb commentedon Jul 26, 2022
The way to think about this is that the
fooandbarcalls may be worlds apart.With more components/functions:
(a(), b(), c(), d(), e(), ..., z()).Let's say you learn, during monomorphization/codegen, that
z()always diverges, but what does that mean for every call before it? Not much, they still have to execute.If, say,
dpanics, then the values returned bya(),b()andc()have to be dropped.And
zitself might not even be a failure mode, but e.g.|| std::process::exit(0).Either way, by the time you know everything about
z, MIR optimizations must have executed in absentia (on the generic definition), and so they may then rely onreturn.{0,...,25}existing, and being useful as general-purpose scratch space (even for monomorphizations where the function doesn't return).Nadrieril commentedon Jan 23, 2024
Hi! I was under the impression that this question was well settled in the direction of "
(T, !)has space forT". I can't find a good source though, what's the status of this? If this hasn't been done yet, could we get a binding decision from T-lang on this matter in one direction or the other? (I'm hoping to resurrect #54987)RalfJung commentedon Jan 23, 2024
I don't think that was ever officially decided. It just happens to be the case currently and MIR building relies on it so when
(T, !)was accidentally made zero-sized that led to a soundness issue.If you want to get a T-lang decision, I suggest you write up the motivation and what you want to have decided, and nominate that for T-lang discussion.
scottmcm commentedon Jan 25, 2024
@Nadrieril As far as I know, the lang team thinks that an example like this https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a7c3a6f3fce6347f33948a7f86e9761a
should not be UB, even though
make_tuple::<String, Never>like that must always diverge. (The example leaks, but that could be fixed -- the point is that it's not UB to write a non-ZST into the storage form.)So long as that's the case -- basically meaning that field projecting inside a
MaybeUninitlike that is legal -- I think that precludes making(String, Never)a ZST.(Existing layout guarantees https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#layout-1 prevent us from saying that
MaybeUninit<(String, Never)>is a non-ZST but(String, Never)is a ZST. And I don't think I'd want to try to split that hair even if we could.)So if you wanted to write that up in some appropriate document form (RFC, like the provenance one? Dunno) I think it could be accepted relatively easily.
RalfJung commentedon Jan 26, 2024
Note however that that example only decides the case for tuples. We could still make this a ZST
You can't write code that is generic over structs.
But possibly the lang team intents that example to also apply to non-generic cases, for an arbitrary struct.
jplatte commentedon Jan 26, 2024
IMHO, it would be pretty weird if for
St<u8>andStU8had different sizes. I think it might be the the first case where substituting generics "by hand" would lead to significantly different codegen.RalfJung commentedon Jan 26, 2024
Nadrieril commentedon Jan 26, 2024
Pattern types are specifically considering such a difference in fact
RalfJung commentedon Mar 24, 2024
Maybe a first step would be to add this to the lang team's "frequently requested changes" and explain there that this is not going to happen. That should be sufficient to close this issue.
zachs18 commentedon Feb 6, 2026
CC rust-lang/lang-team#346