[all-commits] [llvm/llvm-project] 1d4396: [llvm][ADT] Allow returning `std::nullopt` in Type...

zero9178 via All-commits all-commits at lists.llvm.org
Sat Dec 17 15:02:13 PST 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 1d43966bc33a55cad1db7758bf4d82526d125db7
      https://github.com/llvm/llvm-project/commit/1d43966bc33a55cad1db7758bf4d82526d125db7
  Author: Markus Böck <markus.boeck02 at gmail.com>
  Date:   2022-12-18 (Sun, 18 Dec 2022)

  Changed paths:
    M llvm/include/llvm/ADT/TypeSwitch.h
    M llvm/unittests/ADT/TypeSwitchTest.cpp

  Log Message:
  -----------
  [llvm][ADT] Allow returning `std::nullopt` in TypeSwitch

Returning `std::nullopt` from the case of a `TypeSwitch` yields broken results, by either falling through to another case, or falling of the switch entirely and hitting an assertion. This is simply due to the use of `operator=` of what is now `std::optional`, which has an overload specifically for `std::nullopt`, causing the internal optional, used for the `TypeSwitch` result to be reset, instead of a value being constructed from the `std::nullopt`.

The fix is to simply use the `emplace` method of `std::optional`, causing a value to always be constructed from the value returned by the case function.

Differential Revision: https://reviews.llvm.org/D140265




More information about the All-commits mailing list