Skip to content

Crash when calling a closure in context #6800

@HugoForrat

Description

@HugoForrat

Description

The following code crashes:

#let f(fn) = {
  context (size: none) => {
    if size == none { size = text.size }
    fn(size)
  }()
}

#f((size) => [
  Text size is #size
])

with

thread 'main' panicked at crates/typst-eval/src/call.rs:242:47:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I have no experience with the compiler so my investigations were very crude and limited, however here are some things which might be related:

When the closure is parenthesised, it compiles and renders as expected

// this renders correctly
#let f(fn) = {
  context ((size: none) => {
    if size == none { size = text.size }
    fn(size)
  })()
}

#f((size) => [
  Text size is #size
])

A similar behaviour can be observed without context but which doesn't lead to a crash (I am unsure whether this is an expected behaviour or not, and if not, whether this ought to be a separate issue, I apologize).
Consider the following:

#{
  (() => {
    [ Foo ]
  })()
}

#{
  () => {
    [ Bar ]
  }()
}

This is rendered as:

Foo

(..) => ..

In fact the second closure doesn't seem to be evaluated since the code

#{
  (x) => {
    [ Bar ]
  }()
}

also renders as (..) => .. whereas

#{
  ((x) => {
    [ Foo ]
  })()
}

raises a missing argument error as expected.

This parenthesis behaviour is quite confusing since when the closure is stored in a variable, there is no difference in behaviour between parenthesised and not parenthesised.

#{
  let foo = () => {
    [ Foo ]
  }

  foo()
}

#{
  let foo = (() => {
    [ Bar ]
  })

  foo()
}

renders as

Foo

Bar

as expected.

Reproduction URL

No response

Operating system

Linux

Typst version

  • I am using the latest version of Typst

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcrashThe Typst compiler crashes for some input

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions