`find`, `findIndex`, `first` and `get` are type-checked as the element type where they return nil
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
- #992 @TastyHeadphones による — マージされずにクローズ
評価
調査の方向性
Look at the type-checking logic for built-in functions like find, findIndex, first, and get in the compiler. The issue is that they return nil but are typed as the element type. Start by examining the type definitions and the WarnOnAny configuration. The test case provided shows the problem; run it to confirm. The fix likely involves adjusting the return type to be nullable or catching the mismatch during compilation.
索引モデルが issue の本文から書いたものです。
説明
first of an empty []int, get with an out-of-range index and find/findIndex with no match are all type-checked as int, so AsInt() with WarnOnAny() accepts them at compile time and Run then fails with invalid operation: int(<nil>); the same find over a []any array is refused at compile time:
package main
import (
"fmt"
"github.com/expr-lang/expr"
)
func main() {
env := map[string]any{
"ints": []int{1, 2, 3},
"anys": []any{1, 2, 3},
"empty": []int{},
}
for _, src := range []string{
"find(anys, # > 5)",
"find(ints, # > 5)",
"findIndex(ints, # > 5)",
"first(empty)",
"get(ints, 9)",
} {
program, err := expr.Compile(src, expr.Env(env), expr.AsInt(), expr.WarnOnAny())
if err != nil {
fmt.Printf("%-23s compile error: %v\n", src, err)
continue
}
_, err = expr.Run(program, env)
fmt.Printf("%-23s compiled as %v; Run: %v\n", src, program.Node().Type(), err)
}
}
Output:
find(anys, # > 5) compile error: expected int, but got interface {}
find(ints, # > 5) compiled as int; Run: invalid operation: int(<nil>) (1:1)
| find(ints, # > 5)
| ^
findIndex(ints, # > 5) compiled as int; Run: invalid operation: int(<nil>) (1:1)
| findIndex(ints, # > 5)
| ^
first(empty) compiled as int; Run: invalid operation: int(<nil>) (1:1)
| first(empty)
| ^
get(ints, 9) compiled as int; Run: invalid operation: int(<nil>) (1:1)
| get(ints, 9)
| ^
first and get are documented to return nil for an empty array and an out-of-range index (language-definition.md), and WarnOnAny() is documented to make the type checker return an error when the return type is any (configuration.md). With a []int array the static type of these calls is int, so the check passes and the nil only shows up when the program runs. I would expect these to be caught at compile time the way the []any case is, or to have a static type that admits nil.
Tested on expr v1.17.8 and on current master (4b31df3), Go 1.27.1.
BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 3 other bugs in expr. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/expr. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.
- 主要言語
- Go
- スター
- 8.1k
- フォーク
- 544
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 25
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
expr-lang/expr のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
expr-lang/expr#1037 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
count() without a predicate compiles for non-bool arrays and fails at run time対応中かも @minilan677-netizen が 2 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 57/100
メンテナーはふだん 1 日以内に返信
-
wontfix
難易度 4/5 1〜2日 初心者へのやさしさ 35/100
expr-lang/expr#995 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
expr-lang/expr#975 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
Correct local module testing instructions for Go sample contributors対応中かも @slievrly が今日担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
-
internal/core: cleanAttachmentName splits a rune at the 255-byte cap and stores invalid UTF-8対応中かも @pacwestnobody が今日担当しました。 オープンgo good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
Podiom/Podiom#427 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
TestLaunchGolden spends about 98 seconds on fake-agent exit delays under -race対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
factory:subtask
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
メンテナーはふだん 1 日以内に返信