Bug 97591 - Segmentation fault by non-type template parameters
Summary: Segmentation fault by non-type template parameters
Status: RESOLVED DUPLICATE of bug 95291
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-27 14:10 UTC by Vladimir Meremyanin
Modified: 2020-10-27 15:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vladimir Meremyanin 2020-10-27 14:10:22 UTC
I was experimenting with new C++20 features and stumbled on segfault in GCC 10.2.0:

type-level-routes.cpp:45:25: internal compiler error: Segmentation fault: 11
 45 | struct Route<Slug<Path()>, Rest...> {
 | ^
Comment 1 Vladimir Meremyanin 2020-10-27 14:10:35 UTC
g++-10 -v

Using built-in specs.
COLLECT_GCC=/usr/local/Cellar/gcc/10.2.0/bin/g++-10
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/10.2.0/libexec/gcc/x86_64-apple-darwin19/10.2.0/lto-wrapper
Target: x86_64-apple-darwin19
Configured with: ../configure --build=x86_64-apple-darwin19 --prefix=/usr/local/Cellar/gcc/10.2.0 --libdir=/usr/local/Cellar/gcc/10.2.0/lib/gcc/10 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-10 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --with-pkgversion='Homebrew GCC 10.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk SED=/usr/bin/sed
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (Homebrew GCC 10.2.0)
Comment 2 Vladimir Meremyanin 2020-10-27 14:11:13 UTC
Program source:


// /usr/local/Cellar/gcc/10.2.0/bin/g++-10 -std=c++20 type-level-routes.cpp

#include <iostream>
#include <string>

// FixedString from https://www.reddit.com/r/cpp/comments/bhxx49/c20_string_literals_as_nontype_template/
template<unsigned N>
struct FixedString {
  char buf[N + 1]{};
  constexpr FixedString(char const* s) {
    for (unsigned i = 0; i != N; ++i) buf[i] = s[i];
  }
  constexpr operator char const*() const { return buf; }
};
template<unsigned N> FixedString(char const (&)[N]) -> FixedString<N - 1>;

// own code

// Static url part
template <FixedString Path>
struct Slug {
  static constexpr char const* path = Path;
};

// Dynamic url part
template <typename Value>
struct Capture {
  using value_t = Value;
};

// Route
template <typename... Args>
struct Route;

template<typename ValueT, typename... Rest>
struct Route<Capture<ValueT>, Rest...> {

  static std::string toString(ValueT value, auto... rest) {
    return "/" + std::to_string(value) + Route<Rest...>::toString(rest...);
  }

};

template<FixedString Path, typename... Rest>
struct Route<Slug<Path()>, Rest...> {       /* <------- Path() here causes compiler segfault */

  static std::string toString(auto... rest) {
    return "/" + std::string(SlugT::path) + Route<Rest...>::toString(rest...);
  }

};

template <>
struct Route<> {
  static std::string toString() {
    return "";
  }
};


using BlogRoot = Route< "blog" >;
using AllPosts = Route< "blog", "posts" >;
using SinglePost = Route< "blog", "posts", Capture<int> >;

int main() {

  // /blog
  std::cout << "blog = " << BlogRoot::toString() << std::endl;

  // /blog/posts
  std::cout << "allPosts = " << AllPosts::toString() << std::endl;

  // invalid path, compilation error
  // std::cout << "singlePost = " << SinglePost::toString() << std::endl;

  // /blog/posts/42
  std::cout << "singlePost = " << SinglePost::toString(42) << std::endl;

  return 0;
}
Comment 3 Marek Polacek 2020-10-27 14:21:58 UTC
Looks like a dup.

*** This bug has been marked as a duplicate of bug 95291 ***
Comment 4 Vladimir Meremyanin 2020-10-27 15:07:40 UTC
Maybe but in the #95291 crash occurs when access member, and here when operator() is called. So probably both issues have a single cause.