This is an archive of the discontinued LLVM Phabricator instance.

[libc++] [c++2b] [P2166] Prohibit string and string_view construction from nullptr.
ClosedPublic

Authored by curdeius on Jul 26 2021, 8:54 AM.

Diff Detail

Event Timeline

curdeius requested review of this revision.Jul 26 2021, 8:54 AM
curdeius created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJul 26 2021, 8:54 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
curdeius retitled this revision from [libc++] [c++2b] Implement P2166: prohibit string and string_view construction from nullptr. to [libc++] [c++2b] [P2166] Prohibit string and string_view construction from nullptr..Jul 26 2021, 8:59 AM
Quuxplusone added inline comments.
libcxx/test/std/strings/basic.string/string.cons/nullptr.fail.cpp
18 ↗(On Diff #361680)

(1) I think we should also test at least one of the implicit-conversion syntaxes: std::string s = nullptr; and/or void f(std::string); f(nullptr);
(2) Personally I see no problem with testing explicit construction, implicit conversion, and assignment all in the same test file (but defer to @ldionne if he has an opinion).
(3) The wording of this error message may depend on details of Clang and/or libc++; does that mean this test needs to go in libcxx/test/libcxx/? If so, then it might be better simply to write a compile-only (non-fail) test that just does

static_assert(!std::is_convertible_v<decltype(nullptr), std::string>);
static_assert(!std::is_constructible_v<std::string, decltype(nullptr)>);
static_assert(!std::is_assignable_v<std::string, decltype(nullptr)>);

and leave it at that.

I was going to suggest an overload-resolution test like

void f(std::unique_ptr<int>) { }
void f(const std::string&);
int main() { f(nullptr); }

which I'm sure programmers would like to Just Work... but [p2166] deliberately keeps that as ambiguous. (The user-defined conversion from nullptr to std::string ranks lower in preference than a standard conversion such as nullptr-to-int*, but delete'ing it doesn't make it go away; it just says "The API designer knows what you're trying to do, and you're wrong to try it." So we still have an ambiguity here between the right thing (conversion to unique_ptr) and the wrong thing (conversion to string), with no tiebreaker for picking between them.)

ldionne added inline comments.Jul 26 2021, 12:02 PM
libcxx/test/std/strings/basic.string/string.cons/nullptr.fail.cpp
18 ↗(On Diff #361680)

I would go for the static_assert tests in a .compile.pass.cpp test file too. All in the same file IMO.

ldionne requested changes to this revision.Jul 26 2021, 12:03 PM

Requesting changes so it shows up right in the review queue, but feel free to ping me on Discord for a pretty trivial review once you update this and CI passes.

This revision now requires changes to proceed.Jul 26 2021, 12:03 PM
curdeius updated this revision to Diff 361918.Jul 27 2021, 12:09 AM
  • Fix tests.
  • Address review comments.
ldionne accepted this revision.Jul 27 2021, 7:10 AM

LGTM but please remove main()s!

libcxx/test/std/strings/string.view/string.view.cons/nullptr.compile.pass.cpp
23

You don't need to define a main() for compile.pass.cpp tests. Not defining a main makes it clear that the test was never intended to be run. Applies above too.

This revision is now accepted and ready to land.Jul 27 2021, 7:10 AM