mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Suggest accessing field if method doesn't exist (#2977)
This commit is contained in:
parent
2dd3af937a
commit
4e5afa672f
@ -1,5 +1,5 @@
|
|||||||
use comemo::{Prehashed, Tracked, TrackedMut};
|
use comemo::{Prehashed, Tracked, TrackedMut};
|
||||||
use ecow::EcoVec;
|
use ecow::{eco_format, EcoVec};
|
||||||
|
|
||||||
use crate::diag::{bail, error, At, HintedStrResult, SourceResult, Trace, Tracepoint};
|
use crate::diag::{bail, error, At, HintedStrResult, SourceResult, Trace, Tracepoint};
|
||||||
use crate::engine::Engine;
|
use crate::engine::Engine;
|
||||||
@ -99,13 +99,27 @@ impl Eval for ast::FuncCall<'_> {
|
|||||||
field.as_str()
|
field.as_str()
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Value::Dict(dict) = target {
|
let mut field_hint = || {
|
||||||
if matches!(dict.get(&field), Ok(Value::Func(_))) {
|
if target.field(&field).is_ok() {
|
||||||
error.hint(
|
error.hint(eco_format!(
|
||||||
"to call the function stored in the dictionary, \
|
"did you mean to access the field `{}`?",
|
||||||
surround the field access with parentheses",
|
field.as_str()
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match target {
|
||||||
|
Value::Dict(ref dict) => {
|
||||||
|
if matches!(dict.get(&field), Ok(Value::Func(_))) {
|
||||||
|
error.hint(
|
||||||
|
"to call the function stored in the dictionary, \
|
||||||
|
surround the field access with parentheses",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
field_hint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => field_hint(),
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!(error);
|
bail!(error);
|
||||||
|
@ -119,6 +119,7 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Error: 8-15 type dictionary has no method `nonfunc`
|
// Error: 8-15 type dictionary has no method `nonfunc`
|
||||||
|
// Hint: 8-15 did you mean to access the field `nonfunc`?
|
||||||
dict.nonfunc()
|
dict.nonfunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +35,12 @@
|
|||||||
#let numbers = ()
|
#let numbers = ()
|
||||||
#numbers.fun()
|
#numbers.fun()
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 2:4-2:10 type content has no method `stroke`
|
||||||
|
// Hint: 2:4-2:10 did you mean to access the field `stroke`?
|
||||||
|
#let l = line(stroke: red)
|
||||||
|
#l.stroke()
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 2:2-2:43 cannot mutate a temporary value
|
// Error: 2:2-2:43 cannot mutate a temporary value
|
||||||
#let numbers = (1, 2, 3)
|
#let numbers = (1, 2, 3)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user