c++: Incorrect module-number ordering [PR 98741]
authorNathan Sidwell <nathan@acm.org>
Fri, 19 Feb 2021 21:10:27 +0000 (13:10 -0800)
committerNathan Sidwell <nathan@acm.org>
Fri, 19 Feb 2021 21:29:48 +0000 (13:29 -0800)
commit14886cbe300c144a4390245d0515cdf28fc5f68f
tree378a970b311b4f7526a132c26a4b72f0330a96db
parentdfa2f821c18b7e926b5f5d6e394a0c915937db5e
c++: Incorrect module-number ordering [PR 98741]

One of the very strong invariants in modules is that module numbers
are allocated such that (other than the current TU), all imports have
lesser module numbers, and also that the binding vector is only
appended to with increasing module numbers.   This broke down when
module-directives became a thing and the preprocessing became entirely
decoupled from parsing.  We'd load header units and their macros (but
not symbols of course) during preprocessing.  Then we'd load named
modules during parsing.  This could lead to the situation where a
header unit appearing after a named import had a lower module number
than the import.  Consequently, if they both bound the same
identifier, the binding vector would be misorderd and bad things
happen.

This patch restores a pending import queue I previously had, but in
simpler form (hurrah).  During preprocessing we queue all
module-directives and when we meet one for a header unit we do the
minimal loading for all of the queue, so they get appropriate
numbering.  Then we load the preprocessor state for the header unit.

PR c++/98741
gcc/cp/
* module.cc (pending_imports): New.
(declare_module): Adjust test condition.
(name_pending_imports): New.
(preprocess_module): Reimplement using pending_imports.
(preprocessed_module): Move name-getting to name_pending_imports.
* name-lookup.c (append_imported_binding_slot): Assert module
ordering is increasing.
gcc/testsuite/
* g++.dg/modules/pr98741_a.H: New.
* g++.dg/modules/pr98741_b.H: New.
* g++.dg/modules/pr98741_c.C: New.
* g++.dg/modules/pr98741_d.C: New.
gcc/cp/module.cc
gcc/cp/name-lookup.c
gcc/testsuite/g++.dg/modules/pr98741_a.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/pr98741_b.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/pr98741_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/pr98741_d.C [new file with mode: 0644]