This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Explicitly delete copy/move constructors and operator= in IntervalMap
ClosedPublic

Authored by kparzysz on May 25 2022, 11:24 AM.

Details

Summary

The default ones may do the wrong thing, for example perform a shallow copy instead of a deep copy. Disable them so they don't get accidentally used.

Diff Detail

Event Timeline

kparzysz created this revision.May 25 2022, 11:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 25 2022, 11:24 AM
kparzysz requested review of this revision.May 25 2022, 11:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 25 2022, 11:24 AM
dblaikie accepted this revision.May 25 2022, 12:38 PM

Sounds good - maybe add some static_asserts in the unit test to validate that these operations are appropriately deleted? & could you make the comment more definitive - "Delete these operations as the defaults would be shallow/cause incorrect shared state. Implement these if/when needed/worthwhile to do so." or something like that.

This revision is now accepted and ready to land.May 25 2022, 12:38 PM
This revision was landed with ongoing or failed builds.May 26 2022, 7:58 AM
This revision was automatically updated to reflect the committed changes.
dblaikie added inline comments.May 26 2022, 11:43 AM
llvm/include/llvm/ADT/IntervalMap.h
1052

This isn't quite accurate - the "std::pair" situation came up only on FreeBSD when std::pair of trivially copyable types wasn't itself trivially copyable. The general statement is true for any case where the value type is not trivially copyable, but maybe that's not worth mentioning in a comment here? Enough to say that these operations wouldn't be correct (regardless of whether the incorrectness/failure is a compile time or runtime issue?)

So I'd probably just remove this two line comment, keep the one above.

kparzysz added inline comments.May 30 2022, 7:15 AM
llvm/include/llvm/ADT/IntervalMap.h
1052

I mentioned it because the deletions of operator= here don't actually do anything, and the static asserts in the test will succeed regardless of whether they are present or not.
The reason why RootLeaf has a non-trivial copy operator is because it contains an array of std::pair, which is not trivially copy-assignable (including on Linux, so it isn't specific to FreeBSD). In particular, std::is_trivially_copy_assignable<std::pair<int, int>>::value is false. I mentioned std::pair explicitly to point to it as the cause, because it isn't always obvious out why some type has some property.

dblaikie added inline comments.May 30 2022, 4:25 PM
llvm/include/llvm/ADT/IntervalMap.h
1052

Fair enough - thanks for the context