public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Merge #1219
@ 2022-06-08 12:45 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:45 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:140f6a698b4d7157e6a33cd7b41c27b39ecbf76e
commit 140f6a698b4d7157e6a33cd7b41c27b39ecbf76e
Merge: dd5a7654d32 b088d47cdc1
Author: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
Date: Mon May 9 11:09:47 2022 +0000
Merge #1219
1219: Add `Optional<T>` type r=CohenArthur a=CohenArthur
This adds a tagged union to try and simulate a sum type. This is safer and more ergonomic
than one of the two alternatives we're currently using in the compiler:
1. Storing a raw pointer, which can be `nullptr` or valid
This is wildly unsafe, and usable in conjunction with local references, stack
variables, or pointers managed elsewhere, which can cause crashes, hard to
debug issues or undefined behavior. Likewise, if you do not check for the
pointer's validity, this will cause a crash.
2. Storing an extra boolean alongside the object
This causes implementors to use a "dummy object": Either an empty version or
an error version. But what happens if what you really wanted to store was
the empty or error version? You can also easily incorporate logic bugs if you
forget to check for the associated boolean.
The `Optional<T>` type has the same "ergonomic" cost: You need to check
whether your option is valid or not. However, the main advantage is that it
is more restrictive: You can only acess the member it contains "safely".
It is similar to storing a value + an associated boolean, but has the
advantage of making up only one member in your class.
You also benefit from some helper methods such as `map()`.
You also get helper functions and operator overloading to "seamlessly"
replace raw pointer alternatives.
```c++
MyType *raw_pointer = something_that_can_fail();
if (raw_pointer)
raw_pointer->method();
// or
Optional<MyType> opt = something_that_can_fail2();
if (opt)
opt->method();
// equivalent to
if (opt.is_some())
opt.get().method();
```
This will be very useful for parent modules when resolving `super` paths :)
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diff:
gcc/rust/Make-lang.in | 3 +-
gcc/rust/parse/rust-parse-impl.h | 2 +-
gcc/rust/rust-lang.cc | 2 +
gcc/rust/util/rust-make-unique.h | 20 ++++
gcc/rust/util/rust-optional.h | 241 +++++++++++++++++++++++++++++++++++++++
5 files changed, 266 insertions(+), 2 deletions(-)
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-08 12:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:45 [gcc/devel/rust/master] Merge #1219 Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).