From 8f07018b9825cc6914efa0dbfb1c78c105d9bcf5 Mon Sep 17 00:00:00 2001 From: Daveed Vandevoorde Date: Tue, 24 Nov 2026 15:25:04 +0000 Subject: [PATCH] Qualified call via an inline namespace in Microsoft mode finding a global function [GH #588] --- src/il.c | 8 ++- ...588-ms-qualified-call-inline-namespace.txt | 21 +++++++ .../regressions/.gh-588.rto/default.1.1.txt | 1 + .../regressions/.gh-588.rto/default.2.1.txt | 1 + .../regressions/.gh-588.rto/default.3.1.txt | 1 + tests/tests/regressions/gh-588.sft.cpp | 57 +++++++++++++++++++ 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/pending_changes/gh-588-ms-qualified-call-inline-namespace.txt create mode 100644 tests/tests/regressions/.gh-588.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.gh-588.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.gh-588.rto/default.3.1.txt create mode 100644 tests/tests/regressions/gh-588.sft.cpp diff --git a/src/il.c b/src/il.c index 95565082b0..a4591e97b8 100644 --- a/src/il.c +++ b/src/il.c @@ -21498,7 +21498,13 @@ for the copy/substitution. a_symbol_ptr new_sym; make_locator_for_symbol(sym, &locator); locator.specific_symbol = NULL; - if (!symbol_is(sym, sk_namespace_projection) && + /* A namespace projection symbol (e.g., for a function found through + an inline namespace or a using-directive) has the namespace of the + lookup as its parent only if it was created by a qualified + lookup. Otherwise, its parent is the namespace in which an + unqualified lookup was done. */ + if ((!symbol_is(sym, sk_namespace_projection) || + sym->qualified_lookup) && con->source_corresp.parent_scope != NULL && scope_is(con->source_corresp.parent_scope, sck_namespace)) { a_namespace_ptr ns_ptr; diff --git a/src/pending_changes/gh-588-ms-qualified-call-inline-namespace.txt b/src/pending_changes/gh-588-ms-qualified-call-inline-namespace.txt new file mode 100644 index 0000000000..1cac4db7d7 --- /dev/null +++ b/src/pending_changes/gh-588-ms-qualified-call-inline-namespace.txt @@ -0,0 +1,21 @@ +[GH #588] +Microsoft mode: Qualified call via inline namespace finding a global function + +In Microsoft permissive mode (where dependent name processing is not done), +a nonmember function called in a template is looked up again when the +template is instantiated, so that declarations appearing after the template +are found. For a namespace-qualified name, that lookup is done in the +qualifying namespace. However, when the name was found through an inline +namespace or a using-directive, an unqualified lookup was done instead, which +could find a function of the same name in the global namespace. For example, +with --ms_c++17: + + double f(float, double); + namespace N { inline namespace I { template T f(T, T); } } + template struct S { static const int n = 1; }; + template struct S { + static const int n = 2; + }; + static_assert(S::n == 1); // Previously failed. + +The lookup is now done in the qualifying namespace in those cases too. diff --git a/tests/tests/regressions/.gh-588.rto/default.1.1.txt b/tests/tests/regressions/.gh-588.rto/default.1.1.txt new file mode 100644 index 0000000000..79590d790d --- /dev/null +++ b/tests/tests/regressions/.gh-588.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --ms_c++17 --microsoft_version=1937 -DLATE=1 Test_name.c diff --git a/tests/tests/regressions/.gh-588.rto/default.2.1.txt b/tests/tests/regressions/.gh-588.rto/default.2.1.txt new file mode 100644 index 0000000000..eb09aff6d2 --- /dev/null +++ b/tests/tests/regressions/.gh-588.rto/default.2.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=2 --ms_c++20 --microsoft_version=1937 Test_name.c diff --git a/tests/tests/regressions/.gh-588.rto/default.3.1.txt b/tests/tests/regressions/.gh-588.rto/default.3.1.txt new file mode 100644 index 0000000000..d5b7e2da21 --- /dev/null +++ b/tests/tests/regressions/.gh-588.rto/default.3.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=3 --c++17 Test_name.c diff --git a/tests/tests/regressions/gh-588.sft.cpp b/tests/tests/regressions/gh-588.sft.cpp new file mode 100644 index 0000000000..1b9290f494 --- /dev/null +++ b/tests/tests/regressions/gh-588.sft.cpp @@ -0,0 +1,57 @@ +//type:fp +//options:--ms_c++17 --microsoft_version=1937 -DLATE=1:--ms_c++20 --microsoft_version=1937:--c++17 +//remark:qualified call of a function template found through inline namespace + +// In Microsoft permissive mode, the overload set of a function called in a +// template is looked up again when the template is instantiated. For a +// qualified name found through an inline namespace or a using-directive, that +// lookup must still be done in the qualifying namespace (and not find the +// global function). + +template T&& declval(); +template struct void_t_impl { typedef void type; }; + +double fn(float, double); + +namespace lib { + inline namespace v1 { + template T fn(T, T); + } +} + +template +struct can_call { static const bool value = false; }; +template +struct can_call(), + declval()))>::type> { + static const bool value = true; +}; +static_assert(!can_call::value, ""); +static_assert(can_call::value, ""); + +namespace lib2 { + namespace v1 { + template T fn(T, T); + } + using namespace v1; +} + +template constexpr auto test(int) + -> decltype(lib2::fn(declval(), declval()), true) { return true; } +template constexpr bool test(...) { return false; } +static_assert(!test(0), ""); +static_assert(test(0), ""); + +#if LATE +// An overload declared later in the qualifying namespace is found. +template auto h(T t, U u) -> decltype(lib::fn(t, u)) { + return lib::fn(t, u); +} +namespace lib { + inline namespace v1 { + double fn(float, double); + } +} +double d = h(1.0f, 2.0); +#endif