Changing the sublice does not change the original slice #1554

Open
opened 2025-12-28 17:16:49 +00:00 by sami · 1 comment
Owner

Originally created by @Turalchik on GitHub (Sep 16, 2025).

func TestSlice(t *testing.T) {
	src := `package foo
	func Main() byte {
		a := make([]byte, 2)
		b := a[:1]
		b[0] = 42
		return a[0]
	}
	`
	eval(t, src, big.NewInt(42))
}

Current Behavior

Test returns 0.

Expected Behavior

Test should return 42.

Possible Solution

Perhaps you don't need to create a copy of the original slice when creating a subslice.

Steps to Reproduce

Run the test above.

Your Environment

v0.112.0

Originally created by @Turalchik on GitHub (Sep 16, 2025). <!-- Provide a general summary of the issue in the Title above --> ```golang func TestSlice(t *testing.T) { src := `package foo func Main() byte { a := make([]byte, 2) b := a[:1] b[0] = 42 return a[0] } ` eval(t, src, big.NewInt(42)) } ``` ## Current Behavior Test returns 0. ## Expected Behavior Test should return 42. ## Possible Solution Perhaps you don't need to create a copy of the original slice when creating a subslice. ## Steps to Reproduce Run the test above. ## Your Environment <!-- Include as many relevant details about the environment you experienced the bug in --> v0.112.0
Author
Owner

@roman-khimov commented on GitHub (Oct 1, 2025):

We can emulate subslices efficiently with buffers for known at compile time offsets ([:1], it's just a matter of tracking additional data for variables to emit code properly. But dynamic slicing ([:x]) is a problem requiring additional data, can be emulated with a structure, but this incurs significant performance penalty.

@roman-khimov commented on GitHub (Oct 1, 2025): We can emulate subslices efficiently with buffers for known at compile time offsets (`[:1]`, it's just a matter of tracking additional data for variables to emit code properly. But dynamic slicing (`[:x]`) is a problem requiring additional data, can be emulated with a structure, but this incurs significant performance penalty.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
nspcc-dev/neo-go#1554
No description provided.