`find`, `findIndex`, `first` and `get` are type-checked as the element type where they return nil
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 65/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- Go
- Étoiles
- 8k
- Forks
- 535
- Métriques de merge des PR
- Aucune PR mergée en 30 j
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
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de expr-lang/expr
-
Docs: Built in functions shadow user variablesPeut-être pris @sanmaxdev l’a pris il y a 99 jours. Ouvertedocs needed
Difficulté 1/5 1-3 heures Accessibilité débutants 62/100
-
`in` operator on a struct doesn't respect `expr` tagsPeut-être pris @akashchamp l’a pris il y a 19 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 65/100
-
Release latest master branchOuverte
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 55/100
Toutes les issues de expr-lang/expr
Issues similaires
-
darwin zsh
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
carapace-sh/carapace-bin#3846 ·
Les mainteneurs répondent en général sous 1 jour
-
appplication - is a typo?Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 70/100
golang-app/ecommerce#179 ·
Les mainteneurs répondent en général sous 1 jour
-
Simulator process crashes (nil pointer panic) when the dataset returns an errorPeut-être pris @EugeneVon l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
llm-d/llm-d-inference-sim#772 ·
Les mainteneurs répondent en général sous 1 jour
-
refactor: decode numchildren with a lenient int type instead of a hand-written xProp.UnmarshalXMLOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
crazy-goat/xdbg#125 ·
Les mainteneurs répondent en général sous 1 jour
-
150 points ci complexity:medium drips-wave help wanted medium
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
sorotrail/SoroBeacon#449 ·
Les mainteneurs répondent en général sous 1 jour