public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Merge #1219 Date: Wed, 8 Jun 2022 12:45:09 +0000 (GMT) [thread overview] Message-ID: <20220608124509.B779C3895FD7@sourceware.org> (raw) 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(-)
reply other threads:[~2022-06-08 12:45 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220608124509.B779C3895FD7@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).