Bugs, shortcomings, and other minor issues in the parser

Aperta
#343 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
18/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php
Ambito
compilers

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
PHP
Stelle
1.5k
Fork
81
Merge medio
7h 46m
PR unite (30g)
20

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di phpstan/phpdoc-parser

Tutte le issue di phpstan/phpdoc-parser

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.