Sphinx: Keywords `and`, `or`, `not`, `=`, `<=`, etc. not accepted as template parameters in cpp domain

Created on 23 Mar 2020  路  6Comments  路  Source: sphinx-doc/sphinx

Describe the bug

I have C++ code that has in the template parameters and, which fails. However, && works. I get the following:

sphinx-bugs/issue_templates/index.rst:13: WARNING: Error in template parameter list.
Invalid definition: Expected "=", ",", or ">". [error at 18]
  template <Requires<A and B>> foo
  ------------------^

To Reproduce
Steps to reproduce the behavior:

$ git clone [email protected]:nilsdeppe/sphinx-bugs.git
$ cd ./sphinx-bugs/issue_templates
$ sphinx-build ./ ./build

Expected behavior
Sphinx should produce the same output for and as it does for && when they are used in template arguments.

Your project
I've set up a simple repo with an example of the issue: https://github.com/nilsdeppe/sphinx-bugs

Environment info

  • OS: Linux
  • Python version: 3.8.2
  • Sphinx version: 2.4.4
  • Sphinx extensions:
  • Extra tools:

Additional context
The issue appears to be in the cpp domain.

I've been able to "fix" (I use quotes because I'm not sure this is the right solution) the and, or, and not by adding them to the _id_operator_v1, _id_operator_v2, and _expression_bin_ops (_expression_unary_ops for not). Presumably all the alternative operator representations should be added. Adding operator= to _expression_bin_ops resolves that = is not accepted for defaulting template parameters.

I have not been able to get rid of the error:

./sphinx-bugs/issue_templates/index.rst:37: WARNING: Parsing of expression failed. Using fallback parser. Error was:
Error in postfix expression, expected primary expression or type.
If primary expression:
  Invalid definition: Expected identifier in nested name. [error at 22]
    template <Requires<(A<=B)>> foo
    ----------------------^
If type:
  Invalid definition: Expected identifier in nested name. [error at 22]
    template <Requires<(A<=B)>> foo
    ----------------------^
./sphinx-bugs/issue_templates/index.rst:37: WARNING: Parsing of expression failed. Using fallback parser. Error was:
Error in postfix expression, expected primary expression or type.
If primary expression:
  Invalid definition: Expected identifier in nested name. [error at 22]
    template <Requires<(A<=B)>> foo
    ----------------------^
If type:
  Invalid definition: Expected identifier in nested name. [error at 22]
    template <Requires<(A<=B)>> foo
    ----------------------^
./sphinx-bugs/issue_templates/index.rst:37: WARNING: Parsing of expression failed. Using fallback parser. Error was:
Error in postfix expression, expected primary expression or type.
If primary expression:
  Invalid definition: Expected identifier in nested name. [error at 22]
    template <Requires<(A<=B)>> foo
    ----------------------^
If type:
  Invalid definition: Expected identifier in nested name. [error at 22]
    template <Requires<(A<=B)>> foo
    ----------------------^
./sphinx-bugs/issue_templates/index.rst:37: WARNING: Parsing of expression failed. Using fallback parser. Error was:
Error in cast expression.
If type cast expression:
  Invalid definition: Expected ')' in cast expression. [error at 26]
    template <Requires<(A<=B)>> foo
    --------------------------^
If unary expression:
  Error in postfix expression, expected primary expression or type.
  If primary expression:
    Error in fold expression or parenthesized expression.
    If fold expression:
      Invalid definition: Expected '...' after fold operator in fold expression. [error at 28]
        template <Requires<(A<=B)>> foo
        ----------------------------^
    If parenthesized expression:
      Invalid definition: Expected ')' in end of parenthesized expression. [error at 31]
        template <Requires<(A<=B)>> foo
        -------------------------------^
  If type:
    Invalid definition: Expected identifier in nested name. [error at 19]
      template <Requires<(A<=B)>> foo
      -------------------^
./sphinx-bugs/issue_templates/index.rst:37: WARNING: Error in template parameter list.
Invalid definition: Expected "=", ",", or ">". [error at 18]
  template <Requires<(A<=B)>> foo
  ------------------^

I've attached the build output here:
build.tar.gz

bug cpp

All 6 comments

Thanks! As you nicely found out, this is simply that the alternate operators have not been implemented. I'll try to get them fixed soon.

The additional error, on template <Requires<(A<=B)>> foo, seems to be fixed in the 3.x branch. Can you confirm this?

This should now be fixed in the 3.0.x branch. @nilsdeppe, can you confirm?

@jakobandersen thanks for the super quick fix! I can confirm that all the issues except one are resolved on the 3.0.x branch.

The remaining issue is (also available in the repo in my original issue, if that's helpful at all):

./sphinx-bugs/issue_templates/index.rst:32: WARNING: Error in template parameter list.
Invalid definition: Expected "=", ",", or ">". [error at 18]
  template <Requires<std::enable_if_t<B, std::nullptr_t> = nullptr>> foo
  ------------------^

I think operator= is the only one of the _expression_assignment_ops that really make sense inside a template parameter expression, so it's probably not right to allow all of them.

Again, thank you so much! :smile:

Hmm, but is that legal syntax? Did you mean

.. cpp:class:: template <Requires<std::enable_if_t<B, std::nullptr_t>> = nullptr> foo

instead of

.. cpp:class:: template <Requires<std::enable_if_t<B, std::nullptr_t> = nullptr>> foo

Derp, yep bad example from trimming down things into a minimal example. Thank you :)

So I can confirm that this resolves all the issues I had in 2.4.4 in the original report. Thank you again for fixing this, and especially so quickly!! I really appreciate it a lot :heart:

Great, thanks!

Was this page helpful?
0 / 5 - 0 ratings