From 6e230a3f70594bf25aab067b97beee1c3cc55c14 Mon Sep 17 00:00:00 2001
From: Tom Jackson <tjackson@fb.com>
Date: Thu, 25 Oct 2012 00:03:35 -0700
Subject: [PATCH] Optional bugfixes

Summary: Tests were run in 'opt', which masked issues alterted by asserts.

Test Plan: Unit tests

Reviewed By: delong.j@fb.com

FB internal diff: D611957
---
 folly/Optional.h            | 16 ++++++++++++----
 folly/test/OptionalTest.cpp |  9 +++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/folly/Optional.h b/folly/Optional.h
index 311f1642..98b8775d 100644
--- a/folly/Optional.h
+++ b/folly/Optional.h
@@ -90,12 +90,20 @@ class Optional : boost::totally_ordered<Optional<Value>,
   }
 
   Optional(const Optional& src) {
-    construct(src.value());
+    if (src.hasValue()) {
+      construct(src.value());
+    } else {
+      hasValue_ = false;
+    }
   }
 
   Optional(Optional&& src) {
-    construct(std::move(src.value()));
-    src.clear();
+    if (src.hasValue()) {
+      construct(std::move(src.value()));
+      src.clear();
+    } else {
+      hasValue_ = false;
+    }
   }
 
   /* implicit */ Optional(const None& empty)
@@ -192,7 +200,7 @@ class Optional : boost::totally_ordered<Optional<Value>,
   void clear() {
     if (hasValue()) {
       hasValue_ = false;
-      value().~Value();
+      value_.~Value();
     }
   }
 
diff --git a/folly/test/OptionalTest.cpp b/folly/test/OptionalTest.cpp
index 1268ff66..0e240a01 100644
--- a/folly/test/OptionalTest.cpp
+++ b/folly/test/OptionalTest.cpp
@@ -95,6 +95,15 @@ TEST(Optional, Simple) {
   EXPECT_FALSE(opt);
 }
 
+TEST(Optional, EmptyConstruct) {
+  Optional<int> opt;
+  EXPECT_FALSE(opt);
+  Optional<int> test1(opt);
+  EXPECT_FALSE(test1);
+  Optional<int> test2(std::move(opt));
+  EXPECT_FALSE(test2);
+}
+
 TEST(Optional, Unique) {
   Optional<unique_ptr<int>> opt;
 
-- 
2.34.1