@@ -2,13 +2,15 @@ use ecow::{EcoVec, eco_vec};
2
2
use typst_library:: diag:: { At , SourceResult , bail, error, warning} ;
3
3
use typst_library:: engine:: Engine ;
4
4
use typst_library:: foundations:: {
5
- Array , Capturer , Closure , Content , ContextElem , Dict , Func , NativeElement , Selector ,
6
- Str , Value , ops,
5
+ Array , Capturer , Closure , Content , ContextElem , Dict , Func , NativeElement , Repr ,
6
+ Selector , Str , Value , ops,
7
7
} ;
8
8
use typst_library:: introspection:: { Counter , State } ;
9
+ use typst_syntax:: Span ;
9
10
use typst_syntax:: ast:: { self , AstNode } ;
10
11
use typst_utils:: singleton;
11
12
13
+ use crate :: methods:: is_mutating_method;
12
14
use crate :: { CapturesVisitor , Eval , FlowEvent , Vm } ;
13
15
14
16
impl Eval for ast:: Code < ' _ > {
@@ -319,7 +321,10 @@ impl Eval for ast::FieldAccess<'_> {
319
321
let field_span = field. span ( ) ;
320
322
321
323
let err = match value. field ( & field, ( & mut vm. engine , field_span) ) . at ( field_span) {
322
- Ok ( value) => return Ok ( value) ,
324
+ Ok ( field) => {
325
+ validate_method_access ( & value, & field, field_span) ?;
326
+ return Ok ( field) ;
327
+ }
323
328
Err ( err) => err,
324
329
} ;
325
330
@@ -389,3 +394,15 @@ fn warn_for_discarded_content(engine: &mut Engine, event: &FlowEvent, joined: &V
389
394
390
395
engine. sink . warn ( warning) ;
391
396
}
397
+
398
+ /// Raises an error if a user tries to access a mutating method outside of a call expression.
399
+ fn validate_method_access ( target : & Value , field : & Value , span : Span ) -> SourceResult < ( ) > {
400
+ let Value :: Func ( f) = field else { return Ok ( ( ) ) } ;
401
+ let Some ( name) = f. name ( ) else { return Ok ( ( ) ) } ;
402
+
403
+ if is_mutating_method ( name) {
404
+ bail ! ( span, "cannot access mutating fields on {}" , target. repr( ) ) ;
405
+ }
406
+
407
+ Ok ( ( ) )
408
+ }
0 commit comments