Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions text/0000-non-zero-lang-item.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
- Start Date: 2014-12-05
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary

Add a `NonZero` lang item to be used to hint to the compiler that the wrapped pointer or integral value is never 0/NULL.

# Motivation

We currently take advantage of the fact that we know certain kinds of types either are never NULL themselves (`Box`, `&`, function pointers) or contain some pointer which we know to never be NULL (the pointer in a slice, trait object, closure). Specifically, for certain kinds of enums the contain these types, we don't add in an extra field for the discriminant but rather non-nullness of the type to encode which of two possible variants the enum may be.

This is what enables `size_of::<Box<T>>() == size_of::<Option<Box<T>>>()`.

Unfortunately we can't take advantage of this in library code leading to extra overhead for `Option<Vec<T>>`, `Rc<T>` and the like.

# Detailed design

As part of this we'd introduce a new lang item:
```
#[lang = "non_zero"]
struct NonZero<T>(T);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I put a non-pointer, non-integral type in NonZero?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the initial implementation in rust-lang/rust#19536 it doesn't do anything special. It would just act as any other struct there and the optimization wouldn't kick in.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a reasonable approach, but I think we may want to make it an error to use NonZero for non-supported types.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we could probably actually do that just by limiting the types constructor will accept.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be baked into the compiler, because there is no trait for the types we'd like to fit here. We could possibly write a marker-only trait with unsafe traits and default traits.

```

Then, `trans::adt` can treat `NonZero` fields that wrap a pointer or integral type in the same manner as it currently does for `Box<T>` and the like. Thus, we'd represent such enums as `RawNullablePointer/StructWrappedNullablePointer`.

In using it, creating an instance of `NonZero` would be an unsafe operation (private field, mark `::new` as unsafe). We could also implement `Deref` for convenience (i.e. for `foo: NonZero<*mut i8>`, `*foo` would return `*mut i8` since `*mut i8` is `Copy`).

# Drawbacks



# Alternatives

We could possibly do this with an attribute instead but it's a bit more annoying to mark it as unsafe beyond just the name or hardcoding that specific attribute in rustc.

# Unresolved questions