diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index cb28b9ac8..103ea0fe4 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -45,6 +45,7 @@ END TEMPLATE--> ### Bugfixes * Fix ValueList TryPop not clearing element references. +* Fix ValueList Peek always throwing by referencing the wrong index. ### Other diff --git a/Robust.Shared.Tests/Collections/ValueListTest.cs b/Robust.Shared.Tests/Collections/ValueListTest.cs index 6e139dd36..e8f7024c0 100644 --- a/Robust.Shared.Tests/Collections/ValueListTest.cs +++ b/Robust.Shared.Tests/Collections/ValueListTest.cs @@ -9,6 +9,27 @@ namespace Robust.Shared.Tests.Collections; [TestFixture, TestOf(typeof(ValueList<>))] internal sealed class ValueListTest { + [Test] + public void PeekCorrectReference() + { + var list = new ValueList(2) + { + false, + true, + }; + + Assert.That(list.TryPeek(out var popped), Is.True); + Assert.That(popped, Is.True); + + list.TryPop(out _); + + Assert.That(list.TryPeek(out popped), Is.True); + Assert.That(popped, Is.False); + + list.TryPop(out _); + Assert.That(list.TryPeek(out popped), Is.False); + } + [Test] public void TryPopClearsRemovedReference() { diff --git a/Robust.Shared/Collections/ValueList.cs b/Robust.Shared/Collections/ValueList.cs index 71f38bb17..f115356c5 100644 --- a/Robust.Shared/Collections/ValueList.cs +++ b/Robust.Shared/Collections/ValueList.cs @@ -736,7 +736,7 @@ public bool TryPeek([MaybeNullWhen(false)] out T value) return false; } - value = _items![Count]; + value = _items![Count - 1]; return true; } }