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 10#include <vector> 11 12namespace base { 13namespace { 14 15void WontCompileTypeMismatch() { 16 cstring_view(u"123"); // expected-error {{no matching conversion}} 17 cstring_view(U"123"); // expected-error {{no matching conversion}} 18 u16cstring_view("123"); // expected-error {{no matching conversion}} 19 u16cstring_view(U"123"); // expected-error {{no matching conversion}} 20 u32cstring_view("123"); // expected-error {{no matching conversion}} 21 u32cstring_view(u"123"); // expected-error {{no matching conversion}} 22 23#if BUILDFLAG(IS_WIN) 24 cstring_view(L""); // expected-error {{no matching conversion}} 25 u16cstring_view(L""); // expected-error {{no matching conversion}} 26 u32cstring_view(L""); // expected-error {{no matching conversion}} 27 wcstring_view(""); // expected-error {{no matching conversion}} 28 wcstring_view(u""); // expected-error {{no matching conversion}} 29 wcstring_view(U""); // expected-error {{no matching conversion}} 30#endif 31} 32 33void WontCompileNoNulInArray() { 34 const char abc_good[] = {'a', 'b', 'c', '\0'}; 35 auto v1 = cstring_view(abc_good); // No error, NUL exists. 36 37#if defined(__clang__) 38 const char abc_bad[] = {'a', 'b', 'c'}; 39 const char after = 'd'; 40 auto v2 = cstring_view(abc_bad); // expected-error {{no matching conversion}} 41#endif 42} 43 44void WontCompileNullptr() { 45 auto v = cstring_view(nullptr); // expected-error {{no matching conversion}} 46} 47 48void WontCompileCompareTypeMismatch() { 49 // TODO(crbug.com/330213589): This should be testable with a static_assert on 50 // a concept. 51 (void)(cstring_view() == u16cstring_view()); // expected-error {{invalid operands to binary expression}} 52 (void)(cstring_view() <=> u16cstring_view()); // expected-error {{invalid operands to binary expression}} 53} 54 55void WontCompileSwapTypeMismatch() { 56 auto a = cstring_view("8"); 57 auto b = u16cstring_view(u"16"); 58 a.swap(b); // expected-error {{cannot bind to a value of unrelated type}} 59} 60 61void WontCompileStartsEndWithMismatch() { 62 u16cstring_view(u"abc").starts_with("ab"); // expected-error {{no matching member function}} 63 u16cstring_view(u"abc").ends_with("ab"); // expected-error {{no matching member function}} 64} 65 66void WontCompileDanglingInput() { 67 // TODO: construct from string. 68 // auto v1 = cstring_view(std::string("abc")); 69 70 auto v2 = UNSAFE_BUFFERS(cstring_view( // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 71 std::vector<char>{'a', 'b', 'c', '\0'}.data(), 72 3u)); 73 74 auto v3 = cstring_view(); 75 { 76 std::vector<char> abc = {'a', 'b', 'c', '\0'}; 77 v3 = UNSAFE_BUFFERS(cstring_view( 78 abc.data(), 3u)); // This should make a lifetime error but doesn't. :( 79 } 80} 81 82} // namespace 83} // namespace base 84