@@ -472,6 +472,9 @@ pub(crate) fn the_arguments_a_braced_initialisation_deduces(
472472 // query the deduction walks anyway, and every member of a class template carries the class's parameters
473473 // (`DeclFact::parameters` is what pairs them with a use's arguments), so the fallback is not a second guess: it is the
474474 // same field, read from a fact that is in hand.
475+ //
476+ // **Two lookups, and no walk, before the standard's own guides are consulted** — see below for why the order is the
477+ // whole of this change.
475478 let declared = index
476479 . definitions ( name, path)
477480 . value ( )
@@ -486,6 +489,25 @@ pub(crate) fn the_arguments_a_braced_initialisation_deduces(
486489 Some ( declaration) => declaration. fact . qualified_name ( ) ,
487490 None => name. clone ( ) ,
488491 } ;
492+
493+ // **The standard's own guides, before the walk that reads a class's members.** The library's constructors are
494+ // constrained templates (`tuple(_UElements&&...) requires …`), which no pairing of the class's parameters can read;
495+ // the guide is a fact about the standard rather than about the text, so it needs neither the declaration's
496+ // parameters nor the member list.
497+ //
498+ // **It used to sit below `members_of`, and that is the defect this fixes.** `members_of` is the one call here that
499+ // costs more than a query: measured on `std::tuple tuple{1, 2.5, std::string{"three"}}` in a nine-line file, listing
500+ // the members of a class template whose base is written in the pack (`tuple` inherits `_Tuple_impl<0, _Elements...>`,
501+ // which inherits `_Tuple_impl<1, …>`) took the server **16–20 seconds and 1–2 GB** for one inlay hint, and the
502+ // request never came back inside the client's deadline. The answer the table holds was available before the walk
503+ // started: a table that exists so that a walk is unnecessary cannot be read after it.
504+ if let Some ( deduced) = the_deduction_the_standard_fixes ( name, & class_as_written, & element_types)
505+ {
506+ return Some ( crate :: sema:: types:: Type :: Named {
507+ name : class_as_written,
508+ arguments : deduced,
509+ } ) ;
510+ }
489511 let constructor_name = class_as_written
490512 . rsplit ( "::" )
491513 . next ( )
@@ -502,16 +524,6 @@ pub(crate) fn the_arguments_a_braced_initialisation_deduces(
502524 . find ( |member| !member. fact . parameters . is_empty ( ) )
503525 . map ( |member| member. fact . parameters . clone ( ) ) ?,
504526 } ;
505- // **The standard's own guides, before the constructors are walked.** The library's constructors are constrained
506- // templates (`tuple(_UElements&&...) requires …`), which no pairing of the class's parameters can read; the guide is a
507- // fact about the standard rather than about the text. See [`the_deduction_the_standard_fixes`].
508- if let Some ( deduced) = the_deduction_the_standard_fixes ( name, & class_as_written, & element_types)
509- {
510- return Some ( crate :: sema:: types:: Type :: Named {
511- name : class_as_written,
512- arguments : deduced,
513- } ) ;
514- }
515527
516528 if parameters. is_empty ( ) {
517529 return None ;
@@ -1397,12 +1409,23 @@ pub(crate) fn members_of_inner(
13971409 // kept `A`, so the trait was asked about the parameter instead of `allocator<int>`, matched no
13981410 // specialization, and took the primary `false_type` branch — `size_type` came from
13991411 // `_Normal_allocator_traits` where the right answer was `_Default_allocator_traits`'s.
1400- . map ( |written| {
1401- (
1402- substitute_a_base_clause ( index, path, & with_arguments, & written) ,
1403- class. to_string ( ) ,
1404- )
1405- } )
1412+ . map ( |written| substitute_a_base_clause ( index, path, & with_arguments, & written) )
1413+ // **A clause that is not a type is a gap, not a level** — the same rule the walk below applies, and
1414+ // it has to be applied here too: `std::tuple`'s own base is `_Tuple_impl<0, _Elements...>`, so this is
1415+ // the clause that started the unbounded walk. See [`why_a_clause_cannot_be_walked`].
1416+ . filter (
1417+ |substituted| match why_a_clause_cannot_be_walked ( substituted) {
1418+ Some ( reason) => {
1419+ list. unlisted . push ( UnlistedBase {
1420+ spelling : substituted. clone ( ) ,
1421+ reason,
1422+ } ) ;
1423+ false
1424+ }
1425+ None => true ,
1426+ } ,
1427+ )
1428+ . map ( |substituted| ( substituted, class. to_string ( ) ) )
14061429 . collect ( ) ,
14071430 Known :: Unknown ( reason) => {
14081431 list. unlisted . push ( UnlistedBase {
@@ -1432,6 +1455,13 @@ pub(crate) fn members_of_inner(
14321455 if !visited. insert ( shown. clone ( ) ) {
14331456 continue ;
14341457 }
1458+ // **Every level, when the walk is being read.** A member list that never returns is a walk whose *spellings*
1459+ // never repeat, and the spellings are the thing to look at: `_Tuple_impl<0, _Elements...>` is a base that
1460+ // becomes `_Tuple_impl<0 + 1, _Elements...>` one level down, which is a new type every time and therefore an
1461+ // unbounded number of levels. `CPPLS_TRACE_MEMBERS=1`.
1462+ if crate :: probes:: members ( ) {
1463+ eprintln ! ( "[members] {class} level {depth}: {shown}" ) ;
1464+ }
14351465
14361466 // **A base clause whose head is an *alias template* is not a class.** `conditional_t<…>` is a Type fact
14371467 // with a `type_of` target and no members, so `direct_members("conditional_t")` answered an **empty class**
@@ -1519,7 +1549,18 @@ pub(crate) fn members_of_inner(
15191549 . filter ( |clause| {
15201550 !names_a_template_parameter ( base_type_name ( clause) , & parameters)
15211551 } )
1522- . map ( |clause| ( clause, base. clone ( ) ) ) ,
1552+ // **The same rule as the first level** — see [`why_a_clause_cannot_be_walked`], which is
1553+ // where the measurement that makes this necessary is written down.
1554+ . filter_map ( |clause| match why_a_clause_cannot_be_walked ( & clause) {
1555+ Some ( reason) => {
1556+ list. unlisted . push ( UnlistedBase {
1557+ spelling : clause,
1558+ reason,
1559+ } ) ;
1560+ None
1561+ }
1562+ None => Some ( ( clause, base. clone ( ) ) ) ,
1563+ } ) ,
15231564 ) ,
15241565 Known :: Unknown ( reason) => list. unlisted . push ( UnlistedBase {
15251566 spelling : base,
@@ -1611,6 +1652,45 @@ pub(crate) fn the_base_clauses_of(
16111652 bases_of ( index, scopes, root, path, class)
16121653}
16131654
1655+ /// **Why a substituted base clause cannot be walked into** — the two shapes substitution produces that are not types.
1656+ ///
1657+ /// The base walk terminates on a **repeated spelling**: a level is skipped when `visited` already holds it. That is a
1658+ /// proof of termination only if the spellings a level can produce are finite, and substitution can produce an *unbounded
1659+ /// family* of them:
1660+ ///
1661+ /// ```text
1662+ /// level 1 std::_Tuple_impl<0, _Elements...>
1663+ /// level 2 std::_Tuple_impl<0 + 1, _Tail...>
1664+ /// level 3 std::_Tuple_impl<0 + 1 + 1, _Tail...>
1665+ /// level 4 std::_Tuple_impl<0 + 1 + 1 + 1, _Tail...> …
1666+ /// ```
1667+ ///
1668+ /// — the log of `members_of("std::tuple")` on `test_cpp/src/containers.cpp`, one `_Head_base<0 + 1 + 1, _Head>` beside
1669+ /// each level. The two reasons the walk cannot stop are also the two reasons it should never have started:
1670+ ///
1671+ /// * **`_Elements...` is a pack with nothing to expand it.** The clause names a type per argument, and a class asked
1672+ /// about **without** its arguments has none: no level of this walk will ever supply them, so the clause is unwritable
1673+ /// at every depth.
1674+ /// * **`0 + 1` is an expression where a type belongs.** `_Idx + 1` was substituted as *text*, because this layer does
1675+ /// not constant-fold, and each level then writes one more `+ 1` into the spelling. (Substitution joins with spaces, so
1676+ /// the operator is spelled ` + `; no *type* argument has one.)
1677+ ///
1678+ /// Both are recorded as a gap — [`UnlistedBase`] is the field that exists to say where an answer stops — instead of
1679+ /// being walked. This is not a bound on the work: it is the observation that the clause names nothing, so there is no
1680+ /// level to enter. Measured before it: `inlayHint` on a nine-line file containing `std::tuple tuple{1, 2.5, s}` never
1681+ /// returned (120 s, the client's deadline) and the server reached **2 GB**; `members_of("std::tuple")` was still walking
1682+ /// **ten minutes** in with **843 MB** held and no answer.
1683+ fn why_a_clause_cannot_be_walked ( clause : & str ) -> Option < UnknownReason > {
1684+ if clause. contains ( "..." ) {
1685+ return Some ( UnknownReason :: UnknownType ( Box :: from ( clause) ) ) ;
1686+ }
1687+ const OPERATORS : [ & str ; 6 ] = [ " + " , " - " , " * " , " / " , " % " , " << " ] ;
1688+ if OPERATORS . iter ( ) . any ( |operator| clause. contains ( operator) ) {
1689+ return Some ( UnknownReason :: UnknownType ( Box :: from ( clause) ) ) ;
1690+ }
1691+ None
1692+ }
1693+
16141694/// **A base clause with the current level's arguments substituted into it** — `_Vector_base<_Tp, _Alloc>` asked of
16151695/// `vector<int>` is `_Vector_base<int, allocator<int>>`.
16161696///
0 commit comments