aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-pattern.cc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-05-09 11:09:47 +0000
committerGitHub <noreply@github.com>2022-05-09 11:09:47 +0000
commit140f6a698b4d7157e6a33cd7b41c27b39ecbf76e (patch)
tree1234b06acf2b168665b79f94ede82c8081c20015 /gcc/rust/backend/rust-compile-pattern.cc
parentdd5a7654d32134d9bfbe25180dad114367e77767 (diff)
parentb088d47cdc1514d5f92801481cbb412d1e01aeeb (diff)
downloadgcc-140f6a698b4d7157e6a33cd7b41c27b39ecbf76e.zip
gcc-140f6a698b4d7157e6a33cd7b41c27b39ecbf76e.tar.gz
gcc-140f6a698b4d7157e6a33cd7b41c27b39ecbf76e.tar.bz2
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>
Diffstat (limited to 'gcc/rust/backend/rust-compile-pattern.cc')
0 files changed, 0 insertions, 0 deletions