From: Alexandre Ganea Date: Fri, 7 Sep 2018 18:32:59 +0000 (+0000) Subject: [Error] Reintroduce type validation in createFileError() X-Git-Tag: android-x86-9.0-r1~13298 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=1b609e06230c2f9bf588bbc8c6f8f2ff463a04c5;p=android-x86%2Fexternal-llvm.git [Error] Reintroduce type validation in createFileError() This prevents from using ErrorSuccess as an argument to createFileError(). Differential Revision: https://reviews.llvm.org/D51490 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341689 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/Error.h b/include/llvm/Support/Error.h index 3ee59b9309f..4e1619019bb 100644 --- a/include/llvm/Support/Error.h +++ b/include/llvm/Support/Error.h @@ -322,7 +322,7 @@ private: /// Subclass of Error for the sole purpose of identifying the success path in /// the type system. This allows to catch invalid conversion to Expected at /// compile time. -class ErrorSuccess : public Error {}; +class ErrorSuccess final : public Error {}; inline ErrorSuccess Error::success() { return ErrorSuccess(); } @@ -1171,8 +1171,7 @@ Error createStringError(std::error_code EC, char const *Msg); /// show more detailed information to the user. class FileError final : public ErrorInfo { - template - friend Error createFileError(std::string, Err); + friend Error createFileError(std::string, Error); public: void log(raw_ostream &OS) const override { @@ -1207,11 +1206,12 @@ private: /// Concatenate a source file path and/or name with an Error. The resulting /// Error is unchecked. -template -inline Error createFileError(std::string F, Err E) { +inline Error createFileError(std::string F, Error E) { return FileError::build(F, std::move(E)); } +Error createFileError(std::string F, ErrorSuccess) = delete; + /// Helper for check-and-exit error handling. /// /// For tool use only. NOT FOR USE IN LIBRARY CODE. diff --git a/unittests/Support/ErrorTest.cpp b/unittests/Support/ErrorTest.cpp index 25102895d20..ed1e2bb458b 100644 --- a/unittests/Support/ErrorTest.cpp +++ b/unittests/Support/ErrorTest.cpp @@ -874,6 +874,9 @@ TEST(Error, FileErrorTest) { }, ""); #endif + // Not allowed, would fail at compile-time + //consumeError(createFileError("file.bin", ErrorSuccess())); + Error E1 = make_error(1); Error FE1 = createFileError("file.bin", std::move(E1)); EXPECT_EQ(toString(std::move(FE1)).compare("'file.bin': CustomError {1}"), 0);