Skip to content

Commit bf39e7d

Browse files
committed
[libc++] Fix wrongly non-inline basic_string::shrink_to_fit
As explained in https://stackoverflow.com/a/70339311/627587, the fact that shrink_to_fit wasn't defined as inline lead to issues when explicitly instantiating basic_string. While explicit instantiations are always somewhat brittle, this one was clearly a bug on our end. Differential Revision: https://reviews.llvm.org/D115656
1 parent 61bb8b5 commit bf39e7d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

libcxx/include/string

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,7 @@ basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacit
33033303
}
33043304

33053305
template <class _CharT, class _Traits, class _Allocator>
3306+
inline
33063307
void
33073308
basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
33083309
{
@@ -3313,6 +3314,7 @@ basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
33133314
}
33143315

33153316
template <class _CharT, class _Traits, class _Allocator>
3317+
inline
33163318
void
33173319
basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
33183320
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// This test checks that we can explicitly instantiate std::string with a custom
10+
// character type and traits and then use `shrink_to_fit`. In particular, this is
11+
// a regression test for the bug that was reported at https://stackoverflow.com/q/69520633/627587
12+
// and https://seedcentral.apple.com/sm/feedback_collector/radar/85053279.
13+
14+
// RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU1 -c -o %t.tu1.o
15+
// RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU2 -c -o %t.tu2.o
16+
// RUN: %{cxx} %{flags} %t.tu1.o %t.tu2.o %{link_flags} -o %t.exe
17+
18+
// UNSUPPORTED: libcpp-has-no-localization
19+
20+
#include <cstdint>
21+
#include <ios>
22+
#include <string>
23+
24+
typedef std::uint16_t char16;
25+
26+
struct string16_char_traits {
27+
typedef char16 char_type;
28+
typedef int int_type;
29+
30+
typedef std::streamoff off_type;
31+
typedef std::mbstate_t state_type;
32+
typedef std::fpos<state_type> pos_type;
33+
34+
static void assign(char_type&, const char_type&) { }
35+
static bool eq(const char_type&, const char_type&) { return false; }
36+
static bool lt(const char_type&, const char_type&) { return false; }
37+
static int compare(const char_type*, const char_type*, size_t) { return 0; }
38+
static size_t length(const char_type*) { return 0; }
39+
static const char_type* find(const char_type*, size_t, const char_type&) { return nullptr; }
40+
static char_type* move(char_type*, const char_type*, size_t) { return nullptr; }
41+
static char_type* copy(char_type*, const char_type*, size_t) { return nullptr; }
42+
static char_type* assign(char_type*, size_t, char_type) { return nullptr; }
43+
static int_type not_eof(const int_type&) { return 0; }
44+
static char_type to_char_type(const int_type&) { return char_type(); }
45+
static int_type to_int_type(const char_type&) { return int_type(); }
46+
static bool eq_int_type(const int_type&, const int_type&) { return false; }
47+
static int_type eof() { return int_type(); }
48+
};
49+
50+
#if defined(TU1)
51+
template class std::basic_string<char16, string16_char_traits>;
52+
#else
53+
extern template class std::basic_string<char16, string16_char_traits>;
54+
55+
int main() {
56+
std::basic_string<char16, string16_char_traits> s;
57+
s.shrink_to_fit();
58+
}
59+
#endif

0 commit comments

Comments
 (0)