diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp index 9325a09faaea0..4aaf59be2ce50 100644 --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -156,6 +156,8 @@ bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault, if (!CurrentNode) { if (Required) EC = make_error_code(errc::invalid_argument); + else + UseDefault = true; return false; } diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp index 90c09ed7f79ee..745d743b2b244 100644 --- a/llvm/unittests/Support/YAMLIOTest.cpp +++ b/llvm/unittests/Support/YAMLIOTest.cpp @@ -2392,6 +2392,7 @@ TEST(YAMLIO, TestMalformedMapFailsGracefully) { struct OptionalTest { std::vector Numbers; + std::optional MaybeNumber; }; struct OptionalTestSeq { @@ -2405,6 +2406,7 @@ namespace yaml { struct MappingTraits { static void mapping(IO& IO, OptionalTest &OT) { IO.mapOptional("Numbers", OT.Numbers); + IO.mapOptional("MaybeNumber", OT.MaybeNumber); } }; @@ -2466,6 +2468,7 @@ TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) { Input yin(""); yin >> doc; EXPECT_FALSE(yin.error()); + EXPECT_FALSE(doc.MaybeNumber.has_value()); } TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {