mirror of
https://github.com/typst/typst
synced 2025-05-23 13:35:28 +08:00
Update field mutation error message (#1742)
This commit is contained in:
parent
8c6addeb9b
commit
5bd97e218b
@ -68,7 +68,7 @@ use unicode_segmentation::UnicodeSegmentation;
|
|||||||
|
|
||||||
use self::func::{CapturesVisitor, Closure};
|
use self::func::{CapturesVisitor, Closure};
|
||||||
use crate::diag::{
|
use crate::diag::{
|
||||||
bail, error, warning, At, FileError, SourceDiagnostic, SourceResult, StrResult,
|
bail, error, warning, At, FileError, Hint, SourceDiagnostic, SourceResult, StrResult,
|
||||||
Trace, Tracepoint,
|
Trace, Tracepoint,
|
||||||
};
|
};
|
||||||
use crate::model::{
|
use crate::model::{
|
||||||
@ -1962,11 +1962,24 @@ fn access_dict<'a>(
|
|||||||
) -> SourceResult<&'a mut Dict> {
|
) -> SourceResult<&'a mut Dict> {
|
||||||
match access.target().access(vm)? {
|
match access.target().access(vm)? {
|
||||||
Value::Dict(dict) => Ok(dict),
|
Value::Dict(dict) => Ok(dict),
|
||||||
value => bail!(
|
value => {
|
||||||
access.target().span(),
|
let type_name = value.type_name();
|
||||||
"expected dictionary, found {}",
|
let span = access.target().span();
|
||||||
value.type_name(),
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,10 +92,3 @@
|
|||||||
---
|
---
|
||||||
// Error: 3-15 cannot mutate a temporary value
|
// Error: 3-15 cannot mutate a temporary value
|
||||||
#((key: "val").other = "some")
|
#((key: "val").other = "some")
|
||||||
|
|
||||||
---
|
|
||||||
#{
|
|
||||||
let object = none
|
|
||||||
// Error: 3-9 expected dictionary, found none
|
|
||||||
object.property = "value"
|
|
||||||
}
|
|
||||||
|
@ -148,3 +148,53 @@
|
|||||||
#test((top + start).y, top)
|
#test((top + start).y, top)
|
||||||
#test((bottom + end).y, bottom)
|
#test((bottom + end).y, bottom)
|
||||||
#test((horizon + center).y, horizon)
|
#test((horizon + center).y, horizon)
|
||||||
|
|
||||||
|
---
|
||||||
|
#{
|
||||||
|
let object = sym.eq.not
|
||||||
|
// Error: 3-9 cannot mutate fields on symbol
|
||||||
|
object.property = "value"
|
||||||
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
#{
|
||||||
|
let object = [hi]
|
||||||
|
// Error: 3-9 cannot mutate fields on content
|
||||||
|
object.property = "value"
|
||||||
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
#{
|
||||||
|
let object = calc
|
||||||
|
// Error: 3-9 cannot mutate fields on module
|
||||||
|
object.property = "value"
|
||||||
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
#{
|
||||||
|
let object = calc.sin
|
||||||
|
// Error: 3-9 cannot mutate fields on function
|
||||||
|
object.property = "value"
|
||||||
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
#{
|
||||||
|
let object = none
|
||||||
|
// Error: 3-9 none does not have accessible fields
|
||||||
|
object.property = "value"
|
||||||
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
#{
|
||||||
|
let object = 10
|
||||||
|
// Error: 3-9 integer does not have accessible fields
|
||||||
|
object.property = "value"
|
||||||
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
#{
|
||||||
|
let s = 1pt + red
|
||||||
|
// Error: 3-4 fields on stroke are not yet mutable
|
||||||
|
// Hint: 3-4 try creating a new stroke with the updated field value instead
|
||||||
|
s.thickness = 5pt
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user