[libc] Remove operator T from cpp::expected.

The libc's equivalent of std::expected has a non-standard and
non-explicit operator T - https://github.com/llvm/llvm-project/issues/62738

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D152270
This commit is contained in:
Tue Ly 2023-06-06 10:10:49 -04:00
parent 6836a47b7e
commit b95ed8b6d9
4 changed files with 23 additions and 19 deletions

View File

@ -38,7 +38,6 @@ public:
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; }

View File

@ -20,7 +20,7 @@ ErrorOr<Dir *> Dir::open(const char *path) {
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 @@ ErrorOr<struct ::dirent *> Dir::read() {
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)

View File

@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(struct ::dirent *, readdir, (::DIR * dir)) {
libc_errno = dirent_val.error();
return nullptr;
}
return dirent_val;
return dirent_val.value();
}
} // namespace __llvm_libc

View File

@ -15,14 +15,19 @@ using File = __llvm_libc::File;
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, CreateWriteCloseAndReadBack) {
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, CreateWriteSeekAndReadBack) {
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, CreateAppendCloseAndReadBack) {
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 @@ TEST(LlvmLibcPlatformFileTest, LargeFile) {
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 @@ TEST(LlvmLibcPlatformFileTest, LargeFile) {
}
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, LargeFile) {
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 @@ TEST(LlvmLibcPlatformFileTest, IncorrectOperation) {
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());