Commit Graph

1 Commits

Author SHA1 Message Date
William S Fulton c2de37f869 C++20: parse a requires-expression as a primary in expression position
A requires-expression of the form 'requires [(parms)] { body }' is one
of the C++20 primary-expression forms.  SWIG previously only handled it
inside a requires-clause (where skip_constraint walks past the body at
the scanner level).  In expression position - for example as the
initialiser of a namespace scope variable template

  template<typename T>
  constexpr bool Addable = requires (T t) { t + t; };

the parser saw REQUIRES, had no production for it as a primary, and
fell through to the EQUAL-error-SEMI recovery rule, emitting Warning
328 ("Value assigned to %s not used due to limited parsing
implementation") and a "Syntax error" - so the whole compilation
failed.

Add a new valexpr alternative that consumes REQUIRES followed by a
small requires_body helper (the optional '(parms)' and the required
'{ body }', both skipped via skip_balanced).  The parsed value is an
opaque empty string typed as T_BOOL; SWIG cannot evaluate the
requirements but the C++ compiler does at instantiation time, and the
generated wrapper just calls into the templated variable.

Bison reports no new shift/reduce or reduce/reduce conflicts.

The cpp20_concepts test case keeps its 'add' function (a requires-clause
whose constraint contains a requires-expression as a primary).  A new
cpp20_variable_templates test case is added for the variable template
form, with Python and Java runme files exercising Addable<int>.  The
C++20 manual chapter is restructured: the variable template from-
requires-expression idiom moves out of the Concepts section into its
own section, since variable templates are independent of concepts.

#3413

Assisted-by: Claude Code (Opus 4.7)
2026-05-10 17:37:51 +01:00