xref: /aosp_15_r20/external/cronet/base/strings/cstring_view_nocompile.nc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
8#include "base/strings/cstring_view.h"
9
10namespace base {
11namespace {
12
13void WontCompileTypeMismatch() {
14  cstring_view(u"123");  // expected-error {{no matching conversion}}
15  cstring_view(U"123");  // expected-error {{no matching conversion}}
16  u16cstring_view("123");  // expected-error {{no matching conversion}}
17  u16cstring_view(U"123");  // expected-error {{no matching conversion}}
18  u32cstring_view("123");  // expected-error {{no matching conversion}}
19  u32cstring_view(u"123");  // expected-error {{no matching conversion}}
20
21#if BUILDFLAG(IS_WIN)
22  cstring_view(L"");  // expected-error {{no matching conversion}}
23  u16cstring_view(L"");  // expected-error {{no matching conversion}}
24  u32cstring_view(L"");  // expected-error {{no matching conversion}}
25  wcstring_view("");  // expected-error {{no matching conversion}}
26  wcstring_view(u"");  // expected-error {{no matching conversion}}
27  wcstring_view(U"");  // expected-error {{no matching conversion}}
28#endif
29}
30
31void WontCompileNoNulInArray() {
32  const char abc_good[] = {'a', 'b', 'c', '\0'};
33  auto v1 = cstring_view(abc_good);  // No error, NUL exists.
34
35#if defined(__clang__)
36  const char abc_bad[] = {'a', 'b', 'c'};
37  const char after = 'd';
38  auto v2 = cstring_view(abc_bad);  // expected-error {{no matching conversion}}
39#endif
40}
41
42void WontCompilePointerInsteadOfArray() {
43  const char good[] = "abc";
44  const char* bad = good;
45  auto v = cstring_view(bad);  // expected-error {{no matching conversion}}
46  auto v2 = cstring_view(nullptr);  // expected-error {{no matching conversion}}
47}
48
49void WontCompileCompareTypeMismatch() {
50  // TODO(crbug.com/330213589): This should be testable with a static_assert on
51  // a concept.
52  (void)(cstring_view() == u16cstring_view());  // expected-error {{invalid operands to binary expression}}
53  (void)(cstring_view() <=> u16cstring_view());  // expected-error {{invalid operands to binary expression}}
54}
55
56void WontCompileSwapTypeMismatch() {
57  auto a = cstring_view("8");
58  auto b = u16cstring_view(u"16");
59  a.swap(b);  // expected-error {{cannot bind to a value of unrelated type}}
60}
61
62void WontCompileStartsEndWithMismatch() {
63  u16cstring_view(u"abc").starts_with("ab");  // expected-error {{no matching member function}}
64  u16cstring_view(u"abc").ends_with("ab");  // expected-error {{no matching member function}}
65}
66
67void WontCompileDanglingInput() {
68  // TODO: construct from string.
69  // auto v1 = cstring_view(std::string("abc"));
70
71  auto v2 = UNSAFE_BUFFERS(cstring_view(
72    std::vector<char>{'a', 'b', 'c', '\0'}.data(),
73    3u));  // This should make a lifetime error but doesn't. :(
74
75  auto v3 = cstring_view();
76  {
77    std::vector<char> abc = {'a', 'b', 'c', '\0'};
78    v3 = UNSAFE_BUFFERS(cstring_view(
79        abc.data(), 3u));  // This should make a lifetime error but doesn't. :(
80  }
81}
82
83}  // namespace
84}  // namespace base
85