From 12129f01707a3389ef90a4c78a872e1c7897a752 Mon Sep 17 00:00:00 2001 From: Marmare314 <49279081+Marmare314@users.noreply.github.com> Date: Mon, 24 Apr 2023 19:08:07 +0200 Subject: [PATCH] hint for unknown variable containing dash (#924) --- src/eval/scope.rs | 14 ++++++++++++-- tests/typ/compiler/hint.typ | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/eval/scope.rs b/src/eval/scope.rs index 76633bf28..83d1703e9 100644 --- a/src/eval/scope.rs +++ b/src/eval/scope.rs @@ -42,7 +42,7 @@ impl<'a> Scopes<'a> { .chain(self.scopes.iter().rev()) .chain(self.base.map(|base| base.global.scope())) .find_map(|scope| scope.get(var)) - .ok_or_else(|| eco_format!("unknown variable: {}", var)) + .ok_or_else(|| unknown_variable(var)) } /// Try to access a variable immutably in math. @@ -62,12 +62,22 @@ impl<'a> Scopes<'a> { .ok_or_else(|| { match self.base.and_then(|base| base.global.scope().get(var)) { Some(_) => eco_format!("cannot mutate a constant: {}", var), - _ => eco_format!("unknown variable: {}", var), + _ => unknown_variable(var), } })? } } +/// The error message when a variable is not found. +#[cold] +fn unknown_variable(var: &str) -> EcoString { + if var.contains('-') { + eco_format!("unknown variable: {} – if you meant to use subtraction, try adding spaces around the minus sign.", var) + } else { + eco_format!("unknown variable: {}", var) + } +} + /// A map from binding names to values. #[derive(Default, Clone, Hash)] pub struct Scope(BTreeMap, bool); diff --git a/tests/typ/compiler/hint.typ b/tests/typ/compiler/hint.typ index 4fd25abb5..881354438 100644 --- a/tests/typ/compiler/hint.typ +++ b/tests/typ/compiler/hint.typ @@ -3,4 +3,20 @@ --- // Error: 1:17-1:19 expected length, found integer: a length needs a unit – did you mean 12pt? -#set text(size: 12) \ No newline at end of file +#set text(size: 12) + +--- +#{ + let a = 2 + a = 1-a + a = a -1 + + // Error: 7-10 unknown variable: a-1 – if you meant to use subtraction, try adding spaces around the minus sign. + a = a-1 +} + +--- +#{ + // Error: 3-6 unknown variable: a-1 – if you meant to use subtraction, try adding spaces around the minus sign. + a-1 = 2 +}