`find`, `findIndex`, `first` and `get` are type-checked as the element type where they return nil

Aperta
#989 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #992 di @TastyHeadphones — chiusa senza merge

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
compilers

Direzione di ricerca

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.

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

Descrizione

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.

Lingua principale
Go
Stelle
8.1k
Fork
544
Merge medio
1g 9h
PR unite (30g)
25

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 expr-lang/expr

Tutte le issue di expr-lang/expr

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.