diff options
| author | Pg Biel <9021226+PgBiel@users.noreply.github.com> | 2023-07-26 18:03:33 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-26 23:03:33 +0200 |
| commit | 5bd97e218b698b2ad14fad0470b3b800469ee748 (patch) | |
| tree | cf3bdca070d0cc1f0bf72267fbe117f39eefd1f6 /crates | |
| parent | 8c6addeb9be07eebaefc78ec9e3d3d302e8baf34 (diff) | |
Update field mutation error message (#1742)
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/typst/src/eval/mod.rs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/crates/typst/src/eval/mod.rs b/crates/typst/src/eval/mod.rs index b4048e1b..15f0fddd 100644 --- a/crates/typst/src/eval/mod.rs +++ b/crates/typst/src/eval/mod.rs @@ -68,7 +68,7 @@ use unicode_segmentation::UnicodeSegmentation; use self::func::{CapturesVisitor, Closure}; use crate::diag::{ - bail, error, warning, At, FileError, SourceDiagnostic, SourceResult, StrResult, + bail, error, warning, At, FileError, Hint, SourceDiagnostic, SourceResult, StrResult, Trace, Tracepoint, }; use crate::model::{ @@ -1962,11 +1962,24 @@ fn access_dict<'a>( ) -> SourceResult<&'a mut Dict> { match access.target().access(vm)? { Value::Dict(dict) => Ok(dict), - value => bail!( - access.target().span(), - "expected dictionary, found {}", - value.type_name(), - ), + value => { + let type_name = value.type_name(); + let span = access.target().span(); + if matches!( + value, // those types have their own field getters + Value::Symbol(_) | Value::Content(_) | Value::Module(_) | Value::Func(_) + ) { + bail!(span, "cannot mutate fields on {type_name}"); + } else if fields::fields_on(type_name).is_empty() { + bail!(span, "{type_name} does not have accessible fields"); + } else { + // type supports static fields, which don't yet have + // setters + Err(eco_format!("fields on {type_name} are not yet mutable")) + .hint(eco_format!("try creating a new {type_name} with the updated field value instead")) + .at(span) + } + } } } |
