Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P2166R1 Prohibiting basic_string And basic_string_view Construction From nullptr #1995

Merged
merged 14 commits into from
Jun 29, 2021
Merged

P2166R1 Prohibiting basic_string And basic_string_view Construction From nullptr #1995

merged 14 commits into from
Jun 29, 2021

Conversation

sam20908
Copy link
Contributor

@sam20908 sam20908 commented Jun 9, 2021

Fixes #1979

@sam20908 sam20908 requested a review from a team as a code owner June 9, 2021 23:40
stl/inc/xstring Outdated Show resolved Hide resolved
stl/inc/xstring Outdated Show resolved Hide resolved
stl/inc/xstring Outdated Show resolved Hide resolved
Copy link
Contributor Author

@sam20908 sam20908 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question regarding consistency.

@StephanTLavavej StephanTLavavej added the cxx23 C++23 feature label Jun 10, 2021
stl/inc/xstring Outdated Show resolved Hide resolved
@miscco
Copy link
Contributor

miscco commented Jun 10, 2021

Also while you are in the vicinity, would you be interested in #1977

@sam20908
Copy link
Contributor Author

Also while you are in the vicinity, would you be interested in #1977

Yeah I'm interested to tackle that too if that's available.

Copy link
Member

@CaseyCarter CaseyCarter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: I added an entry to the yvals_core.h list of implemented proposals in a new _HAS_CXX23 directly controls: section. (It seemed easier to just push a commit than to try to explain the proper place since we don't have a _HAS_CXX23 section yet.)

stl/inc/xstring Outdated Show resolved Hide resolved
stl/inc/xstring Outdated Show resolved Hide resolved
stl/inc/xstring Outdated Show resolved Hide resolved
tests/std/tests/P0220R1_string_view/test.cpp Outdated Show resolved Hide resolved
tests/std/tests/P0220R1_string_view/test.cpp Outdated Show resolved Hide resolved
@statementreply
Copy link
Contributor

Is this supposed to be applied retroactively, or to C++23 only? cplusplus/papers#887 is tagged "C++20".

@StephanTLavavej
Copy link
Member

This PR needs to merge main and resolve a conflict in yvals_core.h.

@SuperWig
Copy link
Contributor

SuperWig commented Jun 12, 2021

GitHub's merge conflict editor doesn't like this repo's line endings.

Is this supposed to be applied retroactively, or to C++23 only?

IMO it should go all the way back to C++11. The fact that this compiles is super dumb,

#include <string>

std::string boom()
{
    return 0;
}

@sam20908
Copy link
Contributor Author

Merge finally fixed...

@SuperWig
Copy link
Contributor

yvals_core.h still has mixed line endings. It should be CRLF.

stl/inc/xstring Outdated Show resolved Hide resolved
stl/inc/xstring Outdated Show resolved Hide resolved
stl/inc/xstring Outdated Show resolved Hide resolved
Copy link
Member

@StephanTLavavej StephanTLavavej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I'll validate and push trivial changes to the test.

tests/std/tests/P0220R1_string_view/test.cpp Outdated Show resolved Hide resolved
tests/std/tests/P0220R1_string_view/test.cpp Show resolved Hide resolved
tests/std/tests/P0220R1_string_view/test.cpp Outdated Show resolved Hide resolved
@StephanTLavavej
Copy link
Member

@statementreply

Is this supposed to be applied retroactively, or to C++23 only? cplusplus/papers#887 is tagged "C++20".

Good question. Looking at that issue's history, it appears that it was tagged C++20 because it was considered for retroactive application - however, it appears that they decided against that. The final motion that was sent to the plenary vote was for C++23 only.

@SuperWig

IMO it should go all the way back to C++11.

I agree that this detects totally bad code, although I'd be slightly concerned about customers complaining that this causes compiler errors for code that isn't executed at runtime. It does seem almost like [[nodiscard]] (warning/erroring about extremely questionable code), although our classic criterion for implementing a feature unconditionally is when it poses a low risk of breakage and would be difficult to disentangle - here it's very easy to guard.

@StephanTLavavej StephanTLavavej self-assigned this Jun 24, 2021
@StephanTLavavej StephanTLavavej merged commit a8c7283 into microsoft:main Jun 29, 2021
@StephanTLavavej
Copy link
Member

Thanks for implementing this C++23 feature which has already found incorrect code in the compiler! 😹

@StephanTLavavej

This comment has been minimized.

@jwakely
Copy link

jwakely commented Jan 18, 2022

Good question. Looking at that issue's history, it appears that it was tagged C++20 because it was considered for retroactive application - however, it appears that they decided against that. The final motion that was sent to the plenary vote was for C++23 only.

No, adding the C++20 label there appears to just be an admin error in the paper tracker. This paper was never in scope for C++20 and never intended to be a DR against C++20.

IMO it should go all the way back to C++11.

That's what I did for GCC.

I agree that this detects totally bad code, although I'd be slightly concerned about customers complaining that this causes compiler errors for code that isn't executed at runtime.

I got a number of reports like that, but everybody agreed to fix their bad code. Nobody argued "it's OK, we never actually run this undefined code, we want to keep it!"

The real problem with making it apply back to C++11 is it changes previously-correct code to ill-formed:

#include <string>

struct S
{
  operator const char*() const { return ""; }
  operator std::nullptr_t() const { return {}; }
};

std::string s{ S{} };

This is valid in C++20 and earlier, but ill-formed in C++23. GCC rejects this in C++20, which I suppose is a bug.

@CaseyCarter
Copy link
Member

CaseyCarter commented Jan 18, 2022

This is valid in C++20 and earlier, but ill-formed in C++23. GCC rejects this in C++20, which I suppose is a bug.

It's hard for me not to see a type that converts to both nullptr_t and to pointer type with possibly non-null value as broken regardless of this issue. By all means, convert to multiple different types, but if any of those types share a common type those conversions should be consistent.

@jwakely
Copy link

jwakely commented Jan 18, 2022

No argument here. I think the json issue was actually something more like this, where there's a variant-like type that can be implicitly converted to various types:

struct J
{
  // In C++20 this selects basic_string(const char*),
  // in C++23 it's ambiguous due to basic_string(nullptr_t).
  template<typename T>
    requires (!std::is_same_v<std::allocator<char>, T>)
    && (!std::is_same_v<std::string, T>)
    && (!std::is_same_v<char, T>)
    && (!std::is_same_v<std::string_view, T>)
    operator T() const { return {}; }
};

std::string s3{ J{} };

And this seems extremely fragile too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cxx23 C++23 feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

P2166R1 Prohibiting basic_string And basic_string_view Construction From nullptr
7 participants