diff --git a/libc/src/__support/CPP/expected.h b/libc/src/__support/CPP/expected.h --- a/libc/src/__support/CPP/expected.h +++ b/libc/src/__support/CPP/expected.h @@ -38,7 +38,6 @@ constexpr T value() { return exp; } constexpr E error() { return unexp; } - constexpr operator T() { return exp; } constexpr operator bool() { return is_expected; } constexpr T &operator*() { return exp; } diff --git a/libc/src/__support/File/dir.cpp b/libc/src/__support/File/dir.cpp --- a/libc/src/__support/File/dir.cpp +++ b/libc/src/__support/File/dir.cpp @@ -20,7 +20,7 @@ return __llvm_libc::Error(fd.error()); __llvm_libc::AllocChecker ac; - Dir *dir = new (ac) Dir(fd); + Dir *dir = new (ac) Dir(fd.value()); if (!ac) return __llvm_libc::Error(ENOMEM); return dir; @@ -32,7 +32,7 @@ auto readsize = platform_fetch_dirents(fd, buffer); if (!readsize) return __llvm_libc::Error(readsize.error()); - fillsize = readsize; + fillsize = readsize.value(); readptr = 0; } if (fillsize == 0) diff --git a/libc/src/dirent/readdir.cpp b/libc/src/dirent/readdir.cpp --- a/libc/src/dirent/readdir.cpp +++ b/libc/src/dirent/readdir.cpp @@ -23,7 +23,7 @@ libc_errno = dirent_val.error(); return nullptr; } - return dirent_val; + return dirent_val.value(); } } // namespace __llvm_libc diff --git a/libc/test/src/__support/File/platform_file_test.cpp b/libc/test/src/__support/File/platform_file_test.cpp --- a/libc/test/src/__support/File/platform_file_test.cpp +++ b/libc/test/src/__support/File/platform_file_test.cpp @@ -15,14 +15,19 @@ constexpr char TEXT[] = "Hello, File"; constexpr size_t TEXT_SIZE = sizeof(TEXT) - 1; // Ignore the null terminator +LIBC_INLINE File *openfile(const char *file_name, const char *mode) { + auto error_or_file = __llvm_libc::openfile(file_name, mode); + return error_or_file.has_value() ? error_or_file.value() : nullptr; +} + TEST(LlvmLibcPlatformFileTest, CreateWriteCloseAndReadBack) { constexpr char FILENAME[] = "testdata/create_write_close_and_readback.test"; - File *file = __llvm_libc::openfile(FILENAME, "w"); + File *file = openfile(FILENAME, "w"); ASSERT_FALSE(file == nullptr); ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE); ASSERT_EQ(File::cleanup(file), 0); - file = __llvm_libc::openfile(FILENAME, "r"); + file = openfile(FILENAME, "r"); ASSERT_FALSE(file == nullptr); char data[sizeof(TEXT)]; ASSERT_EQ(file->read(data, TEXT_SIZE).value, TEXT_SIZE); @@ -38,7 +43,7 @@ TEST(LlvmLibcPlatformFileTest, CreateWriteSeekAndReadBack) { constexpr char FILENAME[] = "testdata/create_write_seek_and_readback.test"; - File *file = __llvm_libc::openfile(FILENAME, "w+"); + File *file = openfile(FILENAME, "w+"); ASSERT_FALSE(file == nullptr); ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE); @@ -58,19 +63,19 @@ TEST(LlvmLibcPlatformFileTest, CreateAppendCloseAndReadBack) { constexpr char FILENAME[] = "testdata/create_append_close_and_readback.test"; - File *file = __llvm_libc::openfile(FILENAME, "w"); + File *file = openfile(FILENAME, "w"); ASSERT_FALSE(file == nullptr); ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE); ASSERT_EQ(File::cleanup(file), 0); - file = __llvm_libc::openfile(FILENAME, "a"); + file = openfile(FILENAME, "a"); ASSERT_FALSE(file == nullptr); constexpr char APPEND_TEXT[] = " Append Text"; constexpr size_t APPEND_TEXT_SIZE = sizeof(APPEND_TEXT) - 1; ASSERT_EQ(file->write(APPEND_TEXT, APPEND_TEXT_SIZE).value, APPEND_TEXT_SIZE); ASSERT_EQ(File::cleanup(file), 0); - file = __llvm_libc::openfile(FILENAME, "r"); + file = openfile(FILENAME, "r"); ASSERT_FALSE(file == nullptr); constexpr size_t READ_SIZE = TEXT_SIZE + APPEND_TEXT_SIZE; char data[READ_SIZE + 1]; @@ -87,12 +92,12 @@ TEST(LlvmLibcPlatformFileTest, CreateAppendSeekAndReadBack) { constexpr char FILENAME[] = "testdata/create_append_seek_and_readback.test"; - File *file = __llvm_libc::openfile(FILENAME, "w"); + File *file = openfile(FILENAME, "w"); ASSERT_FALSE(file == nullptr); ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE); ASSERT_EQ(File::cleanup(file), 0); - file = __llvm_libc::openfile(FILENAME, "a+"); + file = openfile(FILENAME, "a+"); ASSERT_FALSE(file == nullptr); constexpr char APPEND_TEXT[] = " Append Text"; constexpr size_t APPEND_TEXT_SIZE = sizeof(APPEND_TEXT) - 1; @@ -119,7 +124,7 @@ write_data[i] = BYTE; constexpr char FILENAME[] = "testdata/large_file.test"; - File *file = __llvm_libc::openfile(FILENAME, "w"); + File *file = openfile(FILENAME, "w"); ASSERT_FALSE(file == nullptr); constexpr int REPEAT = 5; @@ -128,7 +133,7 @@ } ASSERT_EQ(File::cleanup(file), 0); - file = __llvm_libc::openfile(FILENAME, "r"); + file = openfile(FILENAME, "r"); ASSERT_FALSE(file == nullptr); constexpr size_t READ_SIZE = DATA_SIZE * REPEAT; char data[READ_SIZE] = {0}; @@ -146,14 +151,14 @@ TEST(LlvmLibcPlatformFileTest, ReadSeekCurAndRead) { constexpr char FILENAME[] = "testdata/read_seek_cur_and_read.test"; - File *file = __llvm_libc::openfile(FILENAME, "w"); + File *file = openfile(FILENAME, "w"); ASSERT_FALSE(file == nullptr); constexpr char CONTENT[] = "1234567890987654321"; ASSERT_EQ(sizeof(CONTENT) - 1, file->write(CONTENT, sizeof(CONTENT) - 1).value); ASSERT_EQ(0, File::cleanup(file)); - file = __llvm_libc::openfile(FILENAME, "r"); + file = openfile(FILENAME, "r"); ASSERT_FALSE(file == nullptr); constexpr size_t READ_SIZE = 5; @@ -175,21 +180,21 @@ constexpr char FILENAME[] = "testdata/incorrect_operation.test"; char data[1] = {123}; - File *file = __llvm_libc::openfile(FILENAME, "w"); + File *file = openfile(FILENAME, "w"); ASSERT_FALSE(file == nullptr); ASSERT_EQ(file->read(data, 1).value, size_t(0)); // Cannot read ASSERT_FALSE(file->iseof()); ASSERT_TRUE(file->error()); ASSERT_EQ(File::cleanup(file), 0); - file = __llvm_libc::openfile(FILENAME, "r"); + file = openfile(FILENAME, "r"); ASSERT_FALSE(file == nullptr); ASSERT_EQ(file->write(data, 1).value, size_t(0)); // Cannot write ASSERT_FALSE(file->iseof()); ASSERT_TRUE(file->error()); ASSERT_EQ(File::cleanup(file), 0); - file = __llvm_libc::openfile(FILENAME, "a"); + file = openfile(FILENAME, "a"); ASSERT_FALSE(file == nullptr); ASSERT_EQ(file->read(data, 1).value, size_t(0)); // Cannot read ASSERT_FALSE(file->iseof());