public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100134] New: ICE when using a vector in a mdoule
@ 2021-04-18  9:48 patrick.kox at commandoregel dot be
  2021-06-20  9:03 ` [Bug c++/100134] [modules] ICE when using a vector in a module ensadc at mailnesia dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: patrick.kox at commandoregel dot be @ 2021-04-18  9:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

            Bug ID: 100134
           Summary: ICE when using a vector in a mdoule
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patrick.kox at commandoregel dot be
  Target Milestone: ---

Using the following code from the book (Professional C++ 5th Edition) by Marc
Grergoire causes an ICE when trying to compile the "Database" module.

I have 2 files:
Database.cpp (the Interface, called Database.cppm in the book):
===
export module database;
import<string>;
import<vector>;
import employee;

namespace Records {
// const int FirstEmployeeNumber{1'000};
int FirstEmployeeNumber{1'000};

export class Database{
public:
        Employee& addEmployee(const std::string& firstName, const std::string&
lastName);
        Employee& getEmployee(int employeeNumber);
        Employee& getEmployee(const std::string& firstName, const std::string&
lastName);

        void displayAll()const;
        void displayCurrent()const;
        void displayFormer()const;

private:
        std::vector<Employee> m_employees;
        int m_nextEmployeeNumber{FirstEmployeeNumber};
};
}
===
And the file Database-i.cpp (called Database.cpp in the book)
===
module database;
import<stdexcept>;

using namespace std;

namespace Records {
Employee& Database::addEmployee(const string& firstName, const string&
lastName)
{
        Employee theEmployee{firstName, lastName};
        theEmployee.setEmployeeNumber(m_nextEmployeeNumber++);
        theEmployee.hire();
        m_employees.push_back(theEmployee);
        return m_employees.back();
}

Employee& Database::getEmployee(int employeeNumber)
{
        for (auto& employee : m_employees) {
                if (employee.getEmployeeNumber()==employeeNumber) {
                        return employee;
                }
        }
        throw logic_error{"No employee found"};
}

void Database::displayAll() const
{
        for (const auto& employee : m_employees) {
                employee.display();
        }
}

void Database::displayCurrent() const
{
        for (const auto& employee : m_employees) {
                if (employee.isHired()) {
                        employee.display();
                }
        }
}

void Database::displayFormer() const
{
        for (const auto& employee : m_employees) {
                if (!employee.isHired(  )) {
                        employee.display();
                }
        }
}
}
===

If I try to compile this module I get the following error message:
Database.cpp:4:8: internal compiler error: in add_binding_entity, at
cp/module.cc:12704
    4 | export module database;
      |        ^~~~~~
0x69ce1f depset::hash::add_binding_entity(tree_node*, WMB_Flags, void*)
        ../../gcc/cp/module.cc:12704
0xa3ed8c walk_module_binding(tree_node*, bitmap_head*, bool (*)(tree_node*,
WMB_Flags, void*), void*)
        ../../gcc/cp/name-lookup.c:3964
0xa10b58 depset::hash::add_namespace_entities(tree_node*, bitmap_head*)
        ../../gcc/cp/module.cc:12744
0xa29364 module_state::write(elf_out*, cpp_reader*)
        ../../gcc/cp/module.cc:17593
0xa2a9b8 finish_module_processing(cpp_reader*)
        ../../gcc/cp/module.cc:19857
0x9bdb5b c_parse_final_cleanups()
        ../../gcc/cp/decl2.c:5175
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
In module imported at Database-i.cpp:5:1:
database: error: failed to read compiled module: No such file or directory
database: note: compiled module file is ‘gcm.cache/database.gcm’
database: note: imports must be built before being imported
database: fatal error: returning to the gate for a mechanical issue
compilation terminated.
make: *** [Makefile:55: gcm] Error 1

Commenting out the line std::vector<Employee> m_employees; makes the error go
away, but will offcourse cause errors since m_employees cannot be found.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
@ 2021-06-20  9:03 ` ensadc at mailnesia dot com
  2022-03-10 14:49 ` sandipan.mohanty at gmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ensadc at mailnesia dot com @ 2021-06-20  9:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

--- Comment #1 from ensadc at mailnesia dot com ---
Reduced:

========
vector
--------
namespace std {
template <typename _Tp, typename _Up> struct __replace_first_arg;
template <template <typename, typename...> class _Template, typename _Up,
          typename _Tp, typename... _Types>
struct __replace_first_arg<_Template<_Tp, _Types...>, _Up> {
  using type = _Template<_Up, _Types...>;
};
template <typename _Tp> struct allocator {
  typedef _Tp value_type;
  friend constexpr bool operator==(const allocator &,
                                   const allocator &) noexcept {
    return true;
  }
};
} // namespace std
namespace __gnu_cxx {
template <typename _Alloc, typename = typename _Alloc::value_type>
struct __alloc_traits {
  template <typename _Tp> struct rebind {
    typedef std::__replace_first_arg<_Alloc, _Tp> other;
  };
};
} // namespace __gnu_cxx
namespace std {
template <typename _Tp, typename _Alloc = std::allocator<_Tp>> class vector {
  typedef
      typename __gnu_cxx::__alloc_traits<_Alloc>::template rebind<_Tp>::other
          _Tp_alloc_type;
};
} // namespace std
========
foo.cpp
--------
export module foo;
import <vector>;
export struct Foo {
  std::vector<int> v;
};
========
build commands:

g++ -I . -x c++-system-header -fmodules-ts -std=c++20 vector
g++ -I . -std=c++20 -fmodules-ts foo.cpp

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
  2021-06-20  9:03 ` [Bug c++/100134] [modules] ICE when using a vector in a module ensadc at mailnesia dot com
@ 2022-03-10 14:49 ` sandipan.mohanty at gmail dot com
  2022-04-06  7:47 ` sandipan.mohanty at gmail dot com
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: sandipan.mohanty at gmail dot com @ 2022-03-10 14:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

Sandipan Mohanty <sandipan.mohanty at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sandipan.mohanty at gmail dot com

--- Comment #2 from Sandipan Mohanty <sandipan.mohanty at gmail dot com> ---
I was about to report something very similar, so I will add my observations to
this report:

(i) I have reproduced this after pulling and rebuilding today (10 March 2022)
from gcc git repository. The commit id for my testing version is
3357878ef56d1e47666fc697cfd7cb5cd9c1dfc9.

(ii) The ICE happens even if you don't create a vector instance:

// minimal code to reproduce
module;
import <vector>;
export module SomeModule;

struct SomeClass {
    using iterator = std::vector<int>::iterator;
};
// end code

Compilation steps are as in the original report. In order to avoid possible
interference from other header units, I performed each of my tests by first
removing gcm.cache/.

$ rm -rf gcm.cache
$ g++ -std=c++20 -fmodules-ts -xc++-system-header vector
$ g++ -std=c++20 -fmodules-ts -c module_ICE_1.cc -freport-bug


(iii) One can change vector to deque, list, forward_list or a set, and still
have the same error. Changing the container to std::array does not cause this
specific ICE.

(iv) Replacing the container with std::map<int, int> still has the same error,
but std::unordered_map<int, int> does not! With an unordered_set, one can get
further and create objects in the module.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
  2021-06-20  9:03 ` [Bug c++/100134] [modules] ICE when using a vector in a module ensadc at mailnesia dot com
  2022-03-10 14:49 ` sandipan.mohanty at gmail dot com
@ 2022-04-06  7:47 ` sandipan.mohanty at gmail dot com
  2022-04-06  7:52 ` sandipan.mohanty at gmail dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: sandipan.mohanty at gmail dot com @ 2022-04-06  7:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

--- Comment #3 from Sandipan Mohanty <sandipan.mohanty at gmail dot com> ---
Created attachment 52759
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52759&action=edit
Preprocessed source generated by gcc

Preprocessed source generated by gcc

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
                   ` (2 preceding siblings ...)
  2022-04-06  7:47 ` sandipan.mohanty at gmail dot com
@ 2022-04-06  7:52 ` sandipan.mohanty at gmail dot com
  2022-09-26 23:33 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: sandipan.mohanty at gmail dot com @ 2022-04-06  7:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

--- Comment #4 from Sandipan Mohanty <sandipan.mohanty at gmail dot com> ---
Comment on attachment 52759
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52759
Preprocessed source generated by gcc

This bug remains unhandled as of 6 April 2022, making it difficult to use
modules at all with gcc. The attachment is the preprocessed source generated by
gcc for the minimized example in my previous comment. The ICE continues to
happen when using a freshly compiled gcc after pulling from git on 6 April
2022. git commit id: 9d84ed6812dce4a50e64334e7cc4abdeebe41523

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
                   ` (3 preceding siblings ...)
  2022-04-06  7:52 ` sandipan.mohanty at gmail dot com
@ 2022-09-26 23:33 ` pinskia at gcc dot gnu.org
  2022-09-26 23:34 ` johelegp at gmail dot com
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-26 23:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |johelegp at gmail dot com

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 107045 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
                   ` (4 preceding siblings ...)
  2022-09-26 23:33 ` pinskia at gcc dot gnu.org
@ 2022-09-26 23:34 ` johelegp at gmail dot com
  2022-10-07 19:11 ` ppalka at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: johelegp at gmail dot com @ 2022-09-26 23:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

--- Comment #6 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107045#c1 for a minimal
reproducer:

Minimal: https://godbolt.org/z/hxrPvPPhs.

```C++
namespace std {
template<class> struct X {
  friend void f();
};
}
```

```C++
export module mod;
import "std.hpp";
export std::X<int> v;
```

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
                   ` (5 preceding siblings ...)
  2022-09-26 23:34 ` johelegp at gmail dot com
@ 2022-10-07 19:11 ` ppalka at gcc dot gnu.org
  2022-10-11 19:03 ` cvs-commit at gcc dot gnu.org
  2022-10-12 14:36 ` ppalka at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-10-07 19:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-10-07
           Keywords|                            |ice-on-valid-code
     Ever confirmed|0                           |1
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #7 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Adding 'namespace std { }' alongside the exported entity seems to be a
workaround, e.g.

export module mod;
import "std.hpp";
namespace std { }
export std::X<int> v;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
                   ` (6 preceding siblings ...)
  2022-10-07 19:11 ` ppalka at gcc dot gnu.org
@ 2022-10-11 19:03 ` cvs-commit at gcc dot gnu.org
  2022-10-12 14:36 ` ppalka at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-11 19:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:9736a42e1fb8df30d72cf28594d9046bf50200c1

commit r13-3236-g9736a42e1fb8df30d72cf28594d9046bf50200c1
Author: Patrick Palka <ppalka@redhat.com>
Date:   Tue Oct 11 15:02:23 2022 -0400

    c++ modules: ICE with templated friend and std namespace [PR100134]

    The function depset::hash::add_binding_entity has an assert verifying
    that if a namespace contains an exported entity, then the namespace must
    have been opened in the module purview:

      if (data->hash->add_namespace_entities (decl, data->partitions))
        {
          /* It contains an exported thing, so it is exported.  */
          gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
          DECL_MODULE_EXPORT_P (decl) = true;
        }

    We're tripping over this assert in the below testcase because by
    instantiating and exporting std::A<int>, we in turn define and export the
    hidden friend std::f(A<int>) without ever having opened the enclosing
    namespace std within the module purview, and thus DECL_MODULE_PURVIEW_P
    for std is false.

    It's important that the enclosing namespace is std here: if we use
    a different namespace then the ICE disappears.  This probably has
    something to do with us predefining std via push_namespace from
    cxx_init_decl_processing (which makes it look like we've opened it
    within the TU), whereas with another namespace we would instead lazily
    create its NAMESPACE_DECL from add_imported_namespace.

    Since templated friend functions are special in that they give us a way
    to introduce a namespace-scope function without having to explicitly
    open the namespace, this patch proposes to fix this ICE by propagating
    DECL_MODULE_PURVIEW_P from the introduced function to the enclosing
    namespace during tsubst_friend_function.

            PR c++/100134

    gcc/cp/ChangeLog:

            * pt.cc (tsubst_friend_function): Propagate DECL_MODULE_PURVIEW_P
            from the introduced namespace-scope function to the namespace.

    gcc/testsuite/ChangeLog:

            * g++.dg/modules/tpl-friend-8_a.H: New test.
            * g++.dg/modules/tpl-friend-8_b.C: New test.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/100134] [modules] ICE when using a vector in a module
  2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
                   ` (7 preceding siblings ...)
  2022-10-11 19:03 ` cvs-commit at gcc dot gnu.org
@ 2022-10-12 14:36 ` ppalka at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-10-12 14:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100134

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0
             Status|NEW                         |RESOLVED
           Assignee|unassigned at gcc dot gnu.org      |ppalka at gcc dot gnu.org
         Resolution|---                         |FIXED

--- Comment #9 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Should be fixed on trunk

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-10-12 14:36 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-18  9:48 [Bug c++/100134] New: ICE when using a vector in a mdoule patrick.kox at commandoregel dot be
2021-06-20  9:03 ` [Bug c++/100134] [modules] ICE when using a vector in a module ensadc at mailnesia dot com
2022-03-10 14:49 ` sandipan.mohanty at gmail dot com
2022-04-06  7:47 ` sandipan.mohanty at gmail dot com
2022-04-06  7:52 ` sandipan.mohanty at gmail dot com
2022-09-26 23:33 ` pinskia at gcc dot gnu.org
2022-09-26 23:34 ` johelegp at gmail dot com
2022-10-07 19:11 ` ppalka at gcc dot gnu.org
2022-10-11 19:03 ` cvs-commit at gcc dot gnu.org
2022-10-12 14:36 ` ppalka at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).