Make mem::uninitialized and mem::zeroed panic for (some) types where 0 is a niche

What does "0 is a niche" mean? I've never heard the term niche used this way. Reading drXor's comment, it looks like your using niche to talk about a value that has meaning on its own regardless of initialization? In this case 0 is a "nullptr" and that is the "niche"? Could you elaborate what you mean by your usage of niche here?

"Niche" refers to unused/invalid values for that type, which may then be used by the compiler to represent other states, per PR45225. The most common example is Option<T>, where the niche for T may be used to represent None, since it wouldn't be a valid Some(T). Then we don't need to use any additional memory for the enum discriminant.

In this context, where 0 is a niche for T, then Some(mem::zeroed::<T>()) would effectively create a None instead.

The UCG Glossary also has a definition of "niche".

4 posts were split to a new topic: The term "niche" when referring to disallowed bit patterns and reusing them in option-like enums

I submitted a PR for a lint at

1 Like

It seems mem:: uninitialized::<char/bool>() is allowed with this lint. Is this desired behavior?

I wouldn't say "allowed". It is just not (yet) linted against. This is a conservative lint. It will anyway never catch 100% of the errors, so it starts with a small and very uncontroversial subset and will grow from there.

What if we got the FromZeros trait in the std and then used that as the basis for the lint?

This is a pre-monomorphization lint. Do we have a trait query asking "does this type definitely not satisfy this trait, for all possible instantiations of its generic parameters"?

This is unlike Freeze and other traits we have, where we ask "does this definitely satisfy this trait, for all possible instantiations of its generic parameters".

1 Like

Looking at code like this, we might want to lint on transmute(0usize) to a reference as well.