From 046029b1e21324c3b10b03423d9bd297b4eafe8f Mon Sep 17 00:00:00 2001 From: Simon Rask <33556894+SimonRask@users.noreply.github.com> Date: Mon, 21 Aug 2023 16:26:05 +0200 Subject: [PATCH] Add hint for missing method error for dictionary where a field with a function is present (#1922) --- crates/typst/src/eval/methods.rs | 12 +++++++++++- tests/typ/compiler/dict.typ | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/typst/src/eval/methods.rs b/crates/typst/src/eval/methods.rs index 6df504b5e..6127a807c 100644 --- a/crates/typst/src/eval/methods.rs +++ b/crates/typst/src/eval/methods.rs @@ -173,7 +173,17 @@ pub fn call( "keys" => dict.keys().into_value(), "values" => dict.values().into_value(), "pairs" => dict.pairs().into_value(), - _ => return missing(), + _ => { + return if matches!(dict.at(method, None), Ok(Value::Func(_))) { + Err(missing_method(name, method)) + .hint(eco_format!( + "to call the function stored in the dictionary, surround the field access with parentheses" + )) + .at(span) + } else { + missing() + } + } }, Value::Func(func) => match method { diff --git a/tests/typ/compiler/dict.typ b/tests/typ/compiler/dict.typ index b257394f9..957ebe922 100644 --- a/tests/typ/compiler/dict.typ +++ b/tests/typ/compiler/dict.typ @@ -92,3 +92,23 @@ --- // Error: 3-15 cannot mutate a temporary value #((key: "val").other = "some") + +--- +#{ + let dict = ( + func: () => 1, + ) + // Error: 3-14 type dictionary has no method `func` + // Hint: 3-14 to call the function stored in the dictionary, surround the field access with parentheses + dict.func() +} + +--- +#{ + let dict = ( + nonfunc: 1 + ) + + // Error: 3-17 type dictionary has no method `nonfunc` + dict.nonfunc() +} \ No newline at end of file