nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: cycloidio/terracost
Pull request #93 (2023-05-19 10:58:48)

terraform: Changed the type of Resource.Index from 'int' to 'interface{}'


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c11f44f..729743e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,8 @@
   ([Issue #86](https://github.com/cycloidio/terracost/issues/86))
 - Added weak type conversion for values when calculation price from the user inputs
   ([Pull #89](https://github.com/cycloidio/terracost/pull/89))
+- Resource.Index is now of type `interface{}` as it can be `int` or `string`
+  ([Pull #90](https://github.com/cycloidio/terracost/pull/90))
 
 ## [0.5.1] _2023-03-08_
 
diff --git a/terraform/schema.go b/terraform/schema.go
index a14d2e1..b9a1d73 100644
--- a/terraform/schema.go
+++ b/terraform/schema.go
@@ -70,8 +70,11 @@ type State struct {
 
 // Resource is a single Terraform resource definition.
 type Resource struct {
-	Address      string                 `json:"address"`
-	Index        int                    `json:"index"`
+	Address string `json:"address"`
+	// This value is 99% of the time an integer, but it can also be
+	// a string, the implementation on TF side is of 'addrs.InstanceKey'
+	// which can be of type 'IntKey' or 'StringKey'
+	Index        interface{}            `json:"index"`
 	Mode         string                 `json:"mode"`
 	Type         string                 `json:"type"`
 	Name         string                 `json:"name"`


Test patch:
diff --git a/testdata/aws/terraform-plan.json b/testdata/aws/terraform-plan.json
index 2dd3764..5441095 100644
--- a/testdata/aws/terraform-plan.json
+++ b/testdata/aws/terraform-plan.json
@@ -13,6 +13,7 @@
                             "name": "example",
                             "provider_name": "registry.terraform.io/hashicorp/aws",
                             "schema_version": 1,
+                            "index":"test",
                             "values": {
                                 "ami": "ami-2757f631",
                                 "availability_zone": "us-east-1e",
@@ -38,6 +39,7 @@
                     "type": "aws_lb",
                     "name": "example",
                     "provider_name": "registry.terraform.io/hashicorp/aws",
+                    "index":1,
                     "schema_version": 0,
                     "values": {
                         "load_balancer_type": "application"
Repository: d5/tengo
Pull request #392 (2022-08-30 11:46:02)

fix: copy SourceMap in vm OpClosure branch
It seems that SourceMap should be copied to the new CompiledFunction when executing OpClosure. All other fields are copied but this field is left out.

I've added a test to explain

geseq: Thanks for the PR. LGTM

Patch:
diff --git a/vm.go b/vm.go
index c836525..83e49fd 100644
--- a/vm.go
+++ b/vm.go
@@ -767,6 +767,7 @@ func (v *VM) run() {
 				NumLocals:     fn.NumLocals,
 				NumParameters: fn.NumParameters,
 				VarArgs:       fn.VarArgs,
+				SourceMap:     fn.SourceMap,
 				Free:          free,
 			}
 			v.allocs--


Test patch:
diff --git a/script_test.go b/script_test.go
index 87cbe51..9ab2015 100644
--- a/script_test.go
+++ b/script_test.go
@@ -541,6 +541,38 @@ func (n *customNumber) binaryOpInt(op token.Token, rhs *tengo.Int) (tengo.Object
 	return nil, tengo.ErrInvalidOperator
 }
 
+func TestScript_ImportError(t *testing.T) {
+	m := `
+	exp := import("expression")
+	r := exp(ctx)
+`
+
+	src := `
+export func(ctx) {
+	closure := func() {
+		if ctx.actiontimes < 0 { // an error is thrown here because actiontimes is undefined
+			return true
+		}
+		return false
+	}
+
+	return closure()
+}`
+
+	s := tengo.NewScript([]byte(m))
+	mods := tengo.NewModuleMap()
+	mods.AddSourceModule("expression", []byte(src))
+	s.SetImports(mods)
+
+	err := s.Add("ctx", map[string]interface{}{
+		"ctx": 12,
+	})
+	require.NoError(t, err)
+
+	_, err = s.Run()
+	require.True(t, strings.Contains(err.Error(), "expression:4:6"))
+}
+
 func compile(t *testing.T, input string, vars M) *tengo.Compiled {
 	s := tengo.NewScript([]byte(input))
 	for vn, vv := range vars {
Repository: d5/tengo
Pull request #393 (2022-09-22 12:15:58)

Use Object.Copy when cloning globals in Compiled
An attempt to fix issue #336

geseq: @ipsusila  thanks for the PR. Let's simplify the test but otherwise good to go

Patch:
diff --git a/script.go b/script.go
index 82b02f5..4cca4d0 100644
--- a/script.go
+++ b/script.go
@@ -259,7 +259,7 @@ func (c *Compiled) Clone() *Compiled {
 	// copy global objects
 	for idx, g := range c.globals {
 		if g != nil {
-			clone.globals[idx] = g
+			clone.globals[idx] = g.Copy()
 		}
 	}
 	return clone
diff --git a/vm.go b/vm.go
index c836525..83e49fd 100644
--- a/vm.go
+++ b/vm.go
@@ -767,6 +767,7 @@ func (v *VM) run() {
 				NumLocals:     fn.NumLocals,
 				NumParameters: fn.NumParameters,
 				VarArgs:       fn.VarArgs,
+				SourceMap:     fn.SourceMap,
 				Free:          free,
 			}
 			v.allocs--


Test patch:
diff --git a/script_test.go b/script_test.go
index 87cbe51..a84941c 100644
--- a/script_test.go
+++ b/script_test.go
@@ -541,6 +541,38 @@ func (n *customNumber) binaryOpInt(op token.Token, rhs *tengo.Int) (tengo.Object
 	return nil, tengo.ErrInvalidOperator
 }
 
+func TestScript_ImportError(t *testing.T) {
+	m := `
+	exp := import("expression")
+	r := exp(ctx)
+`
+
+	src := `
+export func(ctx) {
+	closure := func() {
+		if ctx.actiontimes < 0 { // an error is thrown here because actiontimes is undefined
+			return true
+		}
+		return false
+	}
+
+	return closure()
+}`
+
+	s := tengo.NewScript([]byte(m))
+	mods := tengo.NewModuleMap()
+	mods.AddSourceModule("expression", []byte(src))
+	s.SetImports(mods)
+
+	err := s.Add("ctx", map[string]interface{}{
+		"ctx": 12,
+	})
+	require.NoError(t, err)
+
+	_, err = s.Run()
+	require.True(t, strings.Contains(err.Error(), "expression:4:6"))
+}
+
 func compile(t *testing.T, input string, vars M) *tengo.Compiled {
 	s := tengo.NewScript([]byte(input))
 	for vn, vv := range vars {
@@ -608,3 +640,28 @@ func compiledIsDefined(
 ) {
 	require.Equal(t, expected, c.IsDefined(name))
 }
+func TestCompiled_Clone(t *testing.T) {
+	script := tengo.NewScript([]byte(`
+count += 1
+data["b"] = 2
+`))
+
+	err := script.Add("data", map[string]interface{}{"a": 1})
+	require.NoError(t, err)
+
+	err = script.Add("count", 1000)
+	require.NoError(t, err)
+
+	compiled, err := script.Compile()
+	require.NoError(t, err)
+
+	clone := compiled.Clone()
+	err = clone.RunContext(context.Background())
+	require.NoError(t, err)
+
+	require.Equal(t, 1000, compiled.Get("count").Int())
+	require.Equal(t, 1, len(compiled.Get("data").Map()))
+
+	require.Equal(t, 1001, clone.Get("count").Int())
+	require.Equal(t, 2, len(clone.Get("data").Map()))
+}
Repository: d5/tengo
Pull request #421 (2023-04-20 12:16:43)

bugfix: parse expect error when mis match braces
Change-Id: Iba1ad456d5b020375bb0416f44fb54bc2e346d62

sailinglawliet: ![image](https://user-images.githubusercontent.com/1274797/233363664-0b06ab1b-5b21-462d-8e5d-f0043eee49c8.png)

sailinglawliet: plz review if u guys got time thx @d5  @geseq 
sailinglawliet: > Thanks for the PR. You need to account for the import expression as well

yeah, i see the check failed, but its due to the wrong test cases, ill fix it as well

Patch:
diff --git a/parser/parser.go b/parser/parser.go
index eaa9dc9..a12af61 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -143,6 +143,7 @@ func (p *Parser) ParseFile() (file *File, err error) {
 	}
 
 	stmts := p.parseStmtList()
+	p.expect(token.EOF)
 	if p.errors.Len() > 0 {
 		return nil, p.errors.Err()
 	}


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 8c2c530..b0aaa87 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -1631,6 +1631,20 @@ func TestParseFloat(t *testing.T) {
 	}
 }
 
+func TestMismatchBrace(t *testing.T) {
+	expectParseError(t, `
+fmt := import("fmt")
+out := 0
+if 3 == 1 {
+	out = 1
+}
+} else {
+	out = 2
+}
+fmt.println(out)
+	`)
+}
+
 func TestParseNumberExpressions(t *testing.T) {
 	expectParse(t, `0x15e+2`, func(p pfn) []Stmt {
 		return stmts(
diff --git a/vm_test.go b/vm_test.go
index dcde5ec..8c98b5a 100644
--- a/vm_test.go
+++ b/vm_test.go
@@ -2754,10 +2754,10 @@ export func(a) {
 		Opts().Module("mod0", `if 1 { } else { export true }`),
 		tengo.UndefinedValue)
 	expectRun(t, `out = import("mod0")`,
-		Opts().Module("mod0", `for v:=0;;v++ { if v == 3 { export true } } }`),
+		Opts().Module("mod0", `for v:=0;;v++ { if v == 3 { export true } }`),
 		true)
 	expectRun(t, `out = import("mod0")`,
-		Opts().Module("mod0", `for v:=0;;v++ { if v == 3 { break } } }`),
+		Opts().Module("mod0", `for v:=0;;v++ { if v == 3 { break } }`),
 		tengo.UndefinedValue)
 
 	// duplicate compiled functions
Repository: d5/tengo
Pull request #440 (2024-01-26 04:06:05)

fix the import expr string output


Patch:
diff --git a/parser/expr.go b/parser/expr.go
index b6b6c62..ea4b8b1 100644
--- a/parser/expr.go
+++ b/parser/expr.go
@@ -351,7 +351,7 @@ func (e *ImportExpr) End() Pos {
 }
 
 func (e *ImportExpr) String() string {
-	return `import("` + e.ModuleName + `")"`
+	return `import("` + e.ModuleName + `")`
 }
 
 // IndexExpr represents an index expression.
diff --git a/vm.go b/vm.go
index 2f8f6fc..2e0240b 100644
--- a/vm.go
+++ b/vm.go
@@ -534,6 +534,8 @@ func (v *VM) run() {
 				}
 				v.stack[v.sp] = val
 				v.sp++
+			default:
+				v.err = fmt.Errorf("not indexable: %s", left.TypeName())
 			}
 		case parser.OpCall:
 			numArgs := int(v.curInsts[v.ip+1])


Test patch:
diff --git a/vm_test.go b/vm_test.go
index 8c98b5a..49c93de 100644
--- a/vm_test.go
+++ b/vm_test.go
@@ -3634,6 +3634,12 @@ func TestSpread(t *testing.T) {
 		"Runtime Error: wrong number of arguments: want=3, got=2")
 }
 
+func TestSliceIndex(t *testing.T) {
+	expectError(t, `undefined[:1]`, nil, "Runtime Error: not indexable")
+	expectError(t, `123[-1:2]`, nil, "Runtime Error: not indexable")
+	expectError(t, `{}[:]`, nil, "Runtime Error: not indexable")
+}
+
 func expectRun(
 	t *testing.T,
 	input string,
Repository: d5/tengo
Pull request #442 (2024-02-24 12:53:49)

Throw a runtime error when trying to slice an unsupported type
Add a missing switch case corresponding to a slice of an unsupported type. Without it Tengo continues execution past the unsupported slice with an invalid stack state, causing unexpected behaviour or obscure errors.

Example:

```golang
test := func() {
    a := 42
    return undefined[:5]
}

fmt := import("fmt")
fmt.println(test())  // -> prints 42
```

This PR changes it to throw a runtime error instead, which is consistent with similar cases such as using a function call operator on an unsupported type.


Patch:
diff --git a/vm.go b/vm.go
index 2f8f6fc..2e0240b 100644
--- a/vm.go
+++ b/vm.go
@@ -534,6 +534,8 @@ func (v *VM) run() {
 				}
 				v.stack[v.sp] = val
 				v.sp++
+			default:
+				v.err = fmt.Errorf("not indexable: %s", left.TypeName())
 			}
 		case parser.OpCall:
 			numArgs := int(v.curInsts[v.ip+1])


Test patch:
diff --git a/vm_test.go b/vm_test.go
index 8c98b5a..49c93de 100644
--- a/vm_test.go
+++ b/vm_test.go
@@ -3634,6 +3634,12 @@ func TestSpread(t *testing.T) {
 		"Runtime Error: wrong number of arguments: want=3, got=2")
 }
 
+func TestSliceIndex(t *testing.T) {
+	expectError(t, `undefined[:1]`, nil, "Runtime Error: not indexable")
+	expectError(t, `123[-1:2]`, nil, "Runtime Error: not indexable")
+	expectError(t, `{}[:]`, nil, "Runtime Error: not indexable")
+}
+
 func expectRun(
 	t *testing.T,
 	input string,
Repository: d5/tengo
Pull request #443 (2024-02-24 21:27:59)

Fix: stop execution when trying to slice an unsupported type
> This is a follow-up to https://github.com/d5/tengo/pull/442

Add a missing `return` to actually stop script execution after detecting an invalid slice operation. Otherwise the execution continues with an invalid stack causing unexpected behaviour:

```golang
a := 123[-1:2]
a += 1  // runtime error: index out of range [-1]
```


Patch:
diff --git a/vm.go b/vm.go
index 2e0240b..74b7742 100644
--- a/vm.go
+++ b/vm.go
@@ -536,6 +536,7 @@ func (v *VM) run() {
 				v.sp++
 			default:
 				v.err = fmt.Errorf("not indexable: %s", left.TypeName())
+				return
 			}
 		case parser.OpCall:
 			numArgs := int(v.curInsts[v.ip+1])


Test patch:
diff --git a/vm_test.go b/vm_test.go
index 49c93de..55a9289 100644
--- a/vm_test.go
+++ b/vm_test.go
@@ -3638,6 +3638,7 @@ func TestSliceIndex(t *testing.T) {
 	expectError(t, `undefined[:1]`, nil, "Runtime Error: not indexable")
 	expectError(t, `123[-1:2]`, nil, "Runtime Error: not indexable")
 	expectError(t, `{}[:]`, nil, "Runtime Error: not indexable")
+	expectError(t, `a := 123[-1:2] ; a += 1`, nil, "Runtime Error: not indexable")
 }
 
 func expectRun(