This is an archive of the discontinued LLVM Phabricator instance.

[llvm][ADT] Allow returning `std::nullopt` in TypeSwitch
ClosedPublic

Authored by zero9178 on Dec 17 2022, 1:37 PM.

Details

Summary

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.

Diff Detail

Event Timeline

zero9178 created this revision.Dec 17 2022, 1:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 17 2022, 1:37 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
zero9178 requested review of this revision.Dec 17 2022, 1:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 17 2022, 1:37 PM
rriddle accepted this revision.Dec 17 2022, 1:39 PM
This revision is now accepted and ready to land.Dec 17 2022, 1:39 PM
This revision was landed with ongoing or failed builds.Dec 17 2022, 3:02 PM
This revision was automatically updated to reflect the committed changes.