Skip to content

Commit 217668f

Browse files
authored
[nfc] Allow forwarding Error returns from Expected callers (#92208)
On a few compilers (clang 11/12 for example [1]), the following does not result in a copy elision, and since `Error`'s copy dtor is elided, results in a compile error: ``` Expect<Something> foobar() { ... if (Error E = aCallReturningError()) return E; ... } ``` Moving `E` would, conversely, result in the pessimizing-move warning on more recent clangs ("moving a local object in a return statement prevents copy elision") We just need to make the `Expected` ctor taking an `Error` take it as a r-value reference. [1] https://lab.llvm.org/buildbot/#/builders/54/builds/10505
1 parent d92c677 commit 217668f

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

llvm/include/llvm/Support/Error.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ template <class T> class [[nodiscard]] Expected {
493493

494494
public:
495495
/// Create an Expected<T> error value from the given Error.
496-
Expected(Error Err)
496+
Expected(Error &&Err)
497497
: HasError(true)
498498
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
499499
// Expected is unchecked upon construction in Debug builds.

llvm/lib/Bitstream/Reader/BitstreamReader.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
167167
if (Error Err =
168168
JumpToBit(GetCurrentBitNo() + static_cast<uint64_t>(NumElts) *
169169
EltEnc.getEncodingData()))
170-
return std::move(Err);
170+
return Err;
171171
break;
172172
case BitCodeAbbrevOp::VBR:
173173
assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
@@ -180,7 +180,7 @@ Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
180180
break;
181181
case BitCodeAbbrevOp::Char6:
182182
if (Error Err = JumpToBit(GetCurrentBitNo() + NumElts * 6))
183-
return std::move(Err);
183+
return Err;
184184
break;
185185
}
186186
continue;
@@ -206,7 +206,7 @@ Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
206206

207207
// Skip over the blob.
208208
if (Error Err = JumpToBit(NewEnd))
209-
return std::move(Err);
209+
return Err;
210210
}
211211
return Code;
212212
}
@@ -344,7 +344,7 @@ Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
344344
// over tail padding first, in case jumping to NewEnd invalidates the Blob
345345
// pointer.
346346
if (Error Err = JumpToBit(NewEnd))
347-
return std::move(Err);
347+
return Err;
348348
const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
349349

350350
// If we can return a reference to the data, do so to avoid copying it.
@@ -421,7 +421,7 @@ Error BitstreamCursor::ReadAbbrevRecord() {
421421
Expected<std::optional<BitstreamBlockInfo>>
422422
BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
423423
if (llvm::Error Err = EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID))
424-
return std::move(Err);
424+
return Err;
425425

426426
BitstreamBlockInfo NewBlockInfo;
427427

@@ -452,7 +452,7 @@ BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
452452
if (!CurBlockInfo)
453453
return std::nullopt;
454454
if (Error Err = ReadAbbrevRecord())
455-
return std::move(Err);
455+
return Err;
456456

457457
// ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
458458
// appropriate BlockInfo.

llvm/lib/Object/COFFObjectFile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ COFFObjectFile::getSectionContents(DataRefImpl Ref) const {
294294
const coff_section *Sec = toSec(Ref);
295295
ArrayRef<uint8_t> Res;
296296
if (Error E = getSectionContents(Sec, Res))
297-
return std::move(E);
297+
return E;
298298
return Res;
299299
}
300300

@@ -807,7 +807,7 @@ Expected<std::unique_ptr<COFFObjectFile>>
807807
COFFObjectFile::create(MemoryBufferRef Object) {
808808
std::unique_ptr<COFFObjectFile> Obj(new COFFObjectFile(std::move(Object)));
809809
if (Error E = Obj->initialize())
810-
return std::move(E);
810+
return E;
811811
return std::move(Obj);
812812
}
813813

@@ -1959,7 +1959,7 @@ ResourceSectionRef::getContents(const coff_resource_data_entry &Entry) {
19591959
uint64_t Offset = Entry.DataRVA + Sym->getValue();
19601960
ArrayRef<uint8_t> Contents;
19611961
if (Error E = Obj->getSectionContents(*Section, Contents))
1962-
return std::move(E);
1962+
return E;
19631963
if (Offset + Entry.DataSize > Contents.size())
19641964
return createStringError(object_error::parse_failed,
19651965
"data outside of section");

llvm/lib/Object/WindowsResource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Expected<ResourceEntryRef>
8080
ResourceEntryRef::create(BinaryStreamRef BSR, const WindowsResource *Owner) {
8181
auto Ref = ResourceEntryRef(BSR, Owner);
8282
if (auto E = Ref.loadNext())
83-
return std::move(E);
83+
return E;
8484
return Ref;
8585
}
8686

@@ -1006,7 +1006,7 @@ writeWindowsResourceCOFF(COFF::MachineTypes MachineType,
10061006
Error E = Error::success();
10071007
WindowsResourceCOFFWriter Writer(MachineType, Parser, E);
10081008
if (E)
1009-
return std::move(E);
1009+
return E;
10101010
return Writer.write(TimeDateStamp);
10111011
}
10121012

0 commit comments

Comments
 (0)