Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Save small move on Variant.__copyinit__ ( init_pointee_*). #4136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mojo/stdlib/src/utils/variant.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct Variant[*Ts: CollectionElement](
for i in range(len(VariadicList(Ts))):
alias T = Ts[i]
if copy._get_discr() == i:
copy._get_ptr[T]().init_pointee_move(self._get_ptr[T]()[])
copy._get_ptr[T]().init_pointee_copy(self._get_ptr[T]()[])
return

fn __copyinit__(out self, other: Self):
Expand Down
27 changes: 21 additions & 6 deletions mojo/stdlib/test/utils/test_variant.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,50 @@ def test_basic():


def test_copy():
var v1 = TestVariant(MoveCopyCounter())
var v1 = TestVariant(MoveCopyCounter()) # initial move value in
assert_equal(v1[MoveCopyCounter].moved, 1)
var v2 = v1
# didn't call copyinit

assert_equal(v1[MoveCopyCounter].copied, 0)
assert_equal(v2[MoveCopyCounter].copied, 1)
assert_equal(v1[MoveCopyCounter].moved, 1) # same initial move value in
assert_equal(
v2[MoveCopyCounter].moved, 1
) # carry the initial move value in

# test that we didn't call the other copyinit too!
assert_no_poison()


def test_explicit_copy():
var v1 = TestVariant(MoveCopyCounter())
var v1 = TestVariant(MoveCopyCounter()) # initial move value in
assert_equal(v1[MoveCopyCounter].moved, 1)
assert_equal(v1[MoveCopyCounter].copied, 0)

# Perform explicit copy
var v2 = v1.copy()

# Test copy counts
assert_equal(v1[MoveCopyCounter].copied, 0)
assert_equal(v2[MoveCopyCounter].copied, 1)
assert_equal(v1[MoveCopyCounter].moved, 1) # same initial move value in
assert_equal(
v2[MoveCopyCounter].moved, 1
) # carry the initial move value in

# test that we didn't call the other copyinit too!
assert_no_poison()


def test_move():
var v1 = TestVariant(MoveCopyCounter())
var v2 = v1
# didn't call moveinit
var v1 = TestVariant(MoveCopyCounter()) # initial move value in
assert_equal(v1[MoveCopyCounter].moved, 1)

var v2 = v1^

assert_equal(v2[MoveCopyCounter].moved, 2)
assert_equal(v2[MoveCopyCounter].copied, 0)

# test that we didn't call the other moveinit too!
assert_no_poison()

Expand Down