Bugs, shortcomings, and other minor issues in the parser

Ouverte
#343 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
18/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
Active
Stack technique
php
Domaine
compilers

Piste de recherche

This bundles about twelve separate parser issues, several of which are feature requests or need a maintainer decision on the intended grammar. Start by reproducing one item, such as the variadic-with-default case, through TypeParser::parse() and checking that the whole input is consumed. Ask the maintainers which items to split out before picking one, since several expected behaviours are still open choices.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Hi! As you may recall, I work with parsers, and I've been using type grammars as a benchmark for my projects.

While running benchmarks, developing a fully-fledged, semantically independent type parser, and performing fuzz testing, I encountered several issues in the current PHPStan type parser.

For your convenience, I have listed them below, ranging from clear-cut bugs to minor issues and potential feature requests.

All PHPStan claims were verified against phpstan/phpdoc-parser 2.4.1 (PHP 8.1) in two modes:

  • raw: TypeParser::parse() followed by a check that the whole input is consumed
  • docblock: the same string as /** @return <type> */ through PhpDocParser

If necessary, I can split this into separate issues. Just let me know the relevant numbers, and I will move them into individual issues for your convenience.

1. Variadic combined with optional
callable(int ...=): void     // accepted
callable(int ...$a=): void   // accepted

Current behavior

Accepted. = marks a parameter as optional, but a variadic parameter cannot be optional

Expected

A ... together with = must be rejected

2. Nested conditional types: then branch versus else branch
(T is int ? A : B is C ? D : E)     // accepted, printed as (T is int ? A : (B is C ? D : E))
(T is int ? (A is C ? D : E) : B)   // accepted
(T is int ? A is C ? D : E : B)     // rejected: Unexpected token "is", expected ':'
(T is int ? A : B|C is D ? E : F)   // rejected: Unexpected token "is", expected ')'

Current behavior

An unparenthesized conditional type is allowed only in the else branch, the then branch needs parentheses.

Expected

  1. If unparenthesized nesting is allowed, it is allowed in both branches
  2. (prefer) Otherwise only the parenthesized form is allowed

The rule is currently asymmetric and undocumented

3. Line breaks inside a type

https://phpstan.org/r/43dd2349-937d-4307-8156-18e219294ff0

Rejected:

"int|\nstring"
"?\nint"
"callable(int $a)\n: void"
"callable(int $a) :\nvoid"
"(T is\nint ? A : B)"
"(T\nis\nint\n?\nA\n:\nB)"

But accepted:

"int\n|string"
"array<\n int,\n string\n>"
"array{\n a: int,\n b: string\n}"
"callable(\n int $a\n): void"

Current behavior

A line break works before | and inside <>, {}, (), but not after |, after ?, around the : of a callable, or inside a parenthesized conditional type. A tab is accepted in the same positions ("?\tint", "int\t|\tstring").

Expected

A line break is equivalent to a space in every position between the tokens of a type, so a long union or a conditional type can be wrapped at any operator.

4. Trailing comma in ...<K, V>
array{a: int, ...<K,>}     // rejected: Unexpected token ">", expected type
array{a: int, ...<K>,}     // accepted
Foo<int,>                  // accepted
callable<T,>(): void       // accepted

Current behavior

A trailing comma is rejected inside the arguments of the unsealed part and accepted in every other list.

Expected

A trailing comma is accepted there too.

5. Variance keywords: covariant as a name
Foo<covariant>                              // rejected
Foo<contravariant>                          // rejected
Foo<covariant covariant>                    // accepted
callable<covariant>(covariant): covariant   // accepted

Current behavior

The same word covariant is parsed differently in different positions.

Expected

One defined rule:

  • Either it is a reserved word everywhere
  • Or an ordinary name wherever no type follows it
6. Number with an underscore right after the dot
1._5   // accepted, printed as 1.5

Current behavior

Accepted.

Expected

Rejected (Syntax Error). It is a syntax error in PHP

7. Hyphen (-) and backslash (\\) in a class constant name
Foo::BAR-BAR
Foo::BAR\Baz

Current behavior

Accepted, the hyphen and the backslash become part of the constant name.

Expected

Rejected.

8. Odd template names
callable<T\X>(int): int
callable<\T>(int): int
callable<T-X>(int): int
callable<non-empty-string>(int): int
callable<int>(int): int

// AND

@template T\X of A = Default
@template non-empty-string of B = Default
...etc

Current behavior

Accepted, a template name may be anything, including a builtin or a pseudo type.

Expected

A template name is a single word without \ and -. A builtin name is either rejected or explicitly allowed.

9. Conditional type without parentheses

This is a good solution that requires developers to be explicit. However, there are some debatable situations.

// Error, parentheses are required.
Foo<T is int ? A : B>
array{a: T is int ? A : B}

And bugs (https://phpstan.org/r/853c0920-857a-4d8f-8ff0-384382af1dbf).

The type ends before is, the rest becomes the description:

T is int ? A : B            // Silently "truncates" the type as "T", which can lead to problems
callable(): T is X ? A : B  // Truncates to "callable(): T"
C|T is int ? A : B          // Truncates to "C|T"
?T is int ? A : B           // Truncates to "?T"

The results are T, callable(): T, (C | T) and ?T

10. Space or line break before [, {, <, (
T [K]
array {a: int}
"array\n{a: int}"
"Foo\n<int>"
"callable\n(int): void"

PHPStan truncates to T, array, array, Foo, callable. The rest becomes the description.

BUT a space before < is accepted (array <int>, Foo <T> (T): T), so the rule differs between brackets.

11. Class constant on a variable

(?) Feature request

$this::BAR
$this::class
$a::BAR

Current behavior

Syntax error

12. Space after the sign of a number
- 1
- 42
- 1.5

Current behavior

Syntax error

Expected

In PHP - 42.0 is a valid expression.

Langage dominant
PHP
Étoiles
1.5k
Forks
81
Merge moyen
7 h 46 min
PR mergées (30 j)
20

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de phpstan/phpdoc-parser

Toutes les issues de phpstan/phpdoc-parser

Issues similaires

Plus d'issues PHP

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.