Add hint for missing method error for dictionary where a field with a function is present (#1922)

This commit is contained in:
Simon Rask 2023-08-21 16:26:05 +02:00 committed by GitHub
parent f6a4b8f97b
commit 046029b1e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -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 {

View File

@ -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()
}