From 3d83960322e1a2053f2e6ef64cf5139b0b04cb47 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 22 May 2023 13:10:27 +0200 Subject: [PATCH] Fix removal order for dictionary Switches to `shift_remove` for now. In the future, we should look into a more efficient implementation. Fixes #1258. --- src/eval/dict.rs | 2 +- tests/typ/compiler/dict.typ | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/eval/dict.rs b/src/eval/dict.rs index 1b28a6baa..49a60147c 100644 --- a/src/eval/dict.rs +++ b/src/eval/dict.rs @@ -88,7 +88,7 @@ impl Dict { /// Remove a mapping by `key` and return the value. pub fn remove(&mut self, key: &str) -> StrResult { - match Arc::make_mut(&mut self.0).remove(key) { + match Arc::make_mut(&mut self.0).shift_remove(key) { Some(value) => Ok(value), None => Err(missing_key(key)), } diff --git a/tests/typ/compiler/dict.typ b/tests/typ/compiler/dict.typ index fd95920b4..6a2f1c7ca 100644 --- a/tests/typ/compiler/dict.typ +++ b/tests/typ/compiler/dict.typ @@ -60,6 +60,12 @@ #test("c" in dict, false) #test(dict, (a: 3, b: 1)) +--- +// Test that removal keeps order. +#let dict = (a: 1, b: 2, c: 3, d: 4) +#dict.remove("b") +#test(dict.keys(), ("a", "c", "d")) + --- // Error: 24-29 duplicate key: first #(first: 1, second: 2, first: 3)