From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id B779C3895FD7; Wed, 8 Jun 2022 12:45:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B779C3895FD7 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Merge #1219 X-Act-Checkin: gcc X-Git-Author: bors[bot] <26634292+bors[bot]@users.noreply.github.com> X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: dd5a7654d32134d9bfbe25180dad114367e77767 X-Git-Newrev: 140f6a698b4d7157e6a33cd7b41c27b39ecbf76e Message-Id: <20220608124509.B779C3895FD7@sourceware.org> Date: Wed, 8 Jun 2022 12:45:09 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:45:09 -0000 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` 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` 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 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 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(-)