Bugs, shortcomings, and other minor issues in the parser

Đang mở
#343 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
18/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
PHP
Star
1.5k
Fork
81
Merge trung bình
7 giờ 46 phút
Pull request đã merge (30 ngày)
20

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phpstan/phpdoc-parser

Tất cả issue của phpstan/phpdoc-parser

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.