Bug 82565 - [7 Regression] Concept and lambda return type deduction cause compilation to crash with "mmap: Cannot allocate memory"
Summary: [7 Regression] Concept and lambda return type deduction cause compilation to ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.1.1
: P2 normal
Target Milestone: 8.0
Assignee: Jason Merrill
URL:
Keywords: c++-lambda, ice-on-valid-code
: 81918 81997 (view as bug list)
Depends on:
Blocks: concepts lambdas
  Show dependency treegraph
 
Reported: 2017-10-16 15:20 UTC by Piotr Jastrzebski
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 7.1.0, 8.0
Known to fail: 7.2.0, 7.5.0
Last reconfirmed: 2017-11-01 00:00:00


Attachments
file that crashes compilation "g++ -fconcepts crash.cpp" (512 bytes, text/x-csrc)
2017-10-16 15:20 UTC, Piotr Jastrzebski
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Piotr Jastrzebski 2017-10-16 15:20:50 UTC
Created attachment 42378 [details]
file that crashes compilation "g++ -fconcepts crash.cpp"

The file I attached crashes the following compilation:

g++ -fconcepts crash.cpp

both on GCC 7.1.1 and GCC 7.2.

GCC 7.1.1 gives the following result:

crash.cpp: In instantiation of ‘run_test()::<lambda(auto:1&)> [with auto:1 = const std::__cxx11::basic_string<char>]’:
crash.cpp:9:12:   required from here
crash.cpp:49:58: internal compiler error: Segmentation fault
     return object.visit([] (auto& x) { return x.empty(); });

GCC 7.2 gives the following result:

crash.cpp: In instantiation of 'run_test()::<lambda(auto:1&)> [with auto:1 = const std::__cxx11::basic_string<char>]':
crash.cpp:9:12:   required from here
crash.cpp:49:58: internal compiler error: Segmentation fault
     return object.visit([] (auto& x) { return x.empty(); });
                                                          ^
mmap: Cannot allocate memory

The file compiles correctly after commenting out line 31 which applies Concept to visit method:

requires Concept<Visitor, decltype(std::declval<Visitor>()(std::declval<std::string&>()))>()

or after explicit declaration of the return type to "-> bool" on lambda in line 49:

return object.visit([] (auto& x) -> bool { return x.empty(); });

Below I'm pasting the content of the crash.cpp file:

#include<vector>
#include<string>
#include<memory>
#include<iostream>

template<typename T, typename ReturnType>
concept bool Concept() {
    return requires(T t, const std::string& s, const std::vector<int>& v) {
        { t(s) } -> ReturnType;
        { t(v) } -> ReturnType;
    };
}

struct test {
    enum class kind {
        string, vector
    };
    struct data {
        data() { }
        ~data() { }

        union {
            std::string _str;
            std::vector<int> _v;
        };
    };
    kind _kind;
    std::unique_ptr<data> _data;

    template<typename Visitor>
        requires Concept<Visitor, decltype(std::declval<Visitor>()(std::declval<std::string&>()))>()
    decltype(auto) visit(Visitor&& visitor) const {
        switch (_kind) {
        case kind::string:
            return visitor(_data->_str);
        case kind::vector:
            return visitor(_data->_v);
        }
        abort();
    }

};

bool run_test() {
    test object;
    object._kind = test::kind::string;
    object._data = std::make_unique<test::data>();
    new (&object._data->_str) std::string("hello test");
    return object.visit([] (auto& x) { return x.empty(); });
}

int main() {
    std::cout << run_test() << std::endl;
    return 0;
}
Comment 1 Jonathan Wakely 2017-11-01 11:33:03 UTC
Trunk gives a seemingly bogus error before the ICE:

mozilla_jwakely0/crash.cpp: In instantiation of ‘run_test()::<lambda(auto:1&)> [with auto:1 = const std::__cxx11::basic_string<char>]’:
mozilla_jwakely0/crash.cpp:9:12:   required from here
mozilla_jwakely0/crash.cpp:49:47: error: use of parameter from containing function
     return object.visit([] (auto& x) { return x.empty(); });
                                               ^
mozilla_jwakely0/crash.cpp:49:35: note: ‘const std::__cxx11::basic_string<char>& x’ declared here
     return object.visit([] (auto& x) { return x.empty(); });
                             ~~~~~~^
mozilla_jwakely0/crash.cpp:49:58: internal compiler error: Segmentation fault
     return object.visit([] (auto& x) { return x.empty(); });
                                                          ^
Comment 2 Richard Biener 2018-01-25 08:24:14 UTC
GCC 7.3 is being released, adjusting target milestone.
Comment 3 Jakub Jelinek 2018-03-01 14:27:48 UTC
Started to ICE with r249323, before it was accepted.
Comment 4 Jason Merrill 2018-03-13 20:23:06 UTC
Author: jason
Date: Tue Mar 13 20:22:31 2018
New Revision: 258502

URL: https://gcc.gnu.org/viewcvs?rev=258502&root=gcc&view=rev
Log:
	PR c++/82565 - ICE with concepts and generic lambda.

	* pt.c (instantiate_decl): Clear fn_context for lambdas.

Added:
    trunk/gcc/testsuite/g++.dg/concepts/lambda1.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/pt.c
Comment 5 Jason Merrill 2018-03-13 20:28:22 UTC
*** Bug 81918 has been marked as a duplicate of this bug. ***
Comment 6 Jason Merrill 2018-03-13 20:29:03 UTC
*** Bug 81997 has been marked as a duplicate of this bug. ***
Comment 7 postmaster 2018-12-06 10:52:35 UTC Comment hidden (obsolete)
Comment 8 Richard Biener 2019-11-14 10:46:53 UTC
Fixed in GCC 8.