From 3ce87345084f963ff7ff08abbad8ca0574547ba3 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Sun, 2 Nov 2025 09:44:10 +0300 Subject: [PATCH] c++/modules: import confict with builtin 27_io/headers/iostream/synopsis.cc was failing with modules because pushdecl called update_binding for a redeclared import while passing the built-in clog as "old". This could also be fixed by discarding the builtin in e.g. import_module_binding, but this seems straightforward. gcc/cp/ChangeLog: * name-lookup.cc (pushdecl): Discard built-in sooner. gcc/testsuite/ChangeLog: * g++.dg/modules/builtin-9_a.C: New test. * g++.dg/modules/builtin-9_b.C: New test. --- gcc/cp/name-lookup.cc | 10 +++++----- gcc/testsuite/g++.dg/modules/builtin-9_a.C | 16 ++++++++++++++++ gcc/testsuite/g++.dg/modules/builtin-9_b.C | 8 ++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/builtin-9_a.C create mode 100644 gcc/testsuite/g++.dg/modules/builtin-9_b.C diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index b7530616ef0..2b34102c730 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -4063,6 +4063,11 @@ pushdecl (tree decl, bool hiding) } } + /* Skip a hidden builtin we failed to match already. There can + only be one. */ + if (old && anticipated_builtin_p (old)) + old = OVL_CHAIN (old); + /* Check for redeclaring an import. */ if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR) if (tree match @@ -4081,11 +4086,6 @@ pushdecl (tree decl, bool hiding) /* We are pushing a new decl. */ - /* Skip a hidden builtin we failed to match already. There can - only be one. */ - if (old && anticipated_builtin_p (old)) - old = OVL_CHAIN (old); - if (hiding) ; /* Hidden bindings don't shadow anything. */ else diff --git a/gcc/testsuite/g++.dg/modules/builtin-9_a.C b/gcc/testsuite/g++.dg/modules/builtin-9_a.C new file mode 100644 index 00000000000..69b0e37917b --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/builtin-9_a.C @@ -0,0 +1,16 @@ +// Test that the built-in clog doesn't interfere with redeclaring the import. + +// { dg-additional-options "-fmodules -Wno-global-module" } + +module; + +namespace std { + class ostream; + extern ostream clog; +} + +export module M; + +namespace std { + export using std::clog; +} diff --git a/gcc/testsuite/g++.dg/modules/builtin-9_b.C b/gcc/testsuite/g++.dg/modules/builtin-9_b.C new file mode 100644 index 00000000000..30ea013d6e8 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/builtin-9_b.C @@ -0,0 +1,8 @@ +// { dg-additional-options -fmodules } + +import M; + +namespace std { + class ostream; + extern ostream clog; +} -- 2.47.3