Comparison with named integer type fails

オープン
#632 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
65/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
go
領域
compilers

調査の方向性

まず、名前付きの uint32 フィールドと Named == 4 式を使用して、skan_assignment_test.go の再現テストを実行します。名前付き整数値の比較処理を追跡します。式が true と評価され、既存の単純な uint32 の比較も引き続き成功すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

docs needed

Hey @antonmedv. Thanks for this amazing library, it is a lot of fun to work with.

After deciding to use it for a new feature with dynamic configuration, we unfortunately ran into some unexpected behavior. The problem is that the expression Named == 4 returns false with this environment definition:

type NamedUint uint32
type Env struct {
    Named NamedUint
}
env := &Env{Named: 4}

It seems to be because we use a named type NamedUint instead of the basic type uint32 in our struct.

Is this behavior expected?

Reproducing Test

Version: github.com/expr-lang/expr v1.16.1

func TestExprUint(t *testing.T) {
	type NamedUint uint32

	type Env struct {
		Plain uint32
		Named NamedUint
	}

	testEnv := &Env{
		Plain: 3,
		Named: 4,
	}

	evaluateCode := func(t *testing.T, code string, expected interface{}) {
		program, err := expr.Compile(code, expr.Env(&Env{}))
		if err != nil {
			t.Error("Compile", err)
		}

		output, err := expr.Run(program, testEnv)
		if err != nil {
			t.Error("Run", err)
		}
		formatted := fmt.Sprintf("%v", output)
		if formatted != fmt.Sprintf("%v", expected) {
			t.Errorf("expected %v, got %v. Code: %q Env: %+v", expected, output, code, testEnv)
		}
	}

	t.Run("Plain return", func(t *testing.T) {
		code := `Plain`
		evaluateCode(t, code, 3)
	})

	t.Run("Named return", func(t *testing.T) {
		code := `Named`
		evaluateCode(t, code, 4)
	})

	t.Run("Plain equal", func(t *testing.T) {
		code := `Plain == 3`
		evaluateCode(t, code, true)
	})

	// --- FAIL: TestExprUint (0.00s)
	//     --- FAIL: TestExprUint/Named_equal (0.00s)
	//         skan_assignment_test.go:394: expected true, got false. Code: "Named == 4" Env: &{Plain:3 Named:4}
	t.Run("Named equal", func(t *testing.T) {
		code := `Named == 4`
		evaluateCode(t, code, true)
	})
}
主要言語
Go
スター
8.1k
フォーク
544
平均マージ
1日 9時間
マージ済み PR(30日)
25

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

expr-lang/expr のほかの issue

expr-lang/expr の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。