Skip to content

Conditional operands do not seem to evaluate lazily #85

@ffried

Description

@ffried

Describe the bug

A set of conditional operands as for example if (a || b) seems to evaluate all its operands (a and b) upfront.
This means that b will be evaluated even if a was already true.

To Reproduce

Steps to reproduce the behavior:

  1. Create a leaf file (e.g. page.leaf) with the following contents:
<!DOCTYPE html>
<html>
<body>
#if(!ctx.children || count(ctx.children) <= 0):
<ul>#for(child in ctx.children):<li>#(child)</li>#endfor</ul>
#else:
<p>No children.</p>
#endif
</body>
</html>
  1. Render the page with the following context:
struct Context: Encodable { let ctx: [String]? }
func configure(app: Application) {
    app.get(use: { $0.view.render("page", Context(ctx: nil)) })
}
  1. Rendering fails with "Unable to convert count parameter to LeafData collection" as thrown by the Count tag if the parameter is no collection. In this case, a breakpoint in Count prints the following for the first parameter (given that ctx.children is nil):
(lldb) po ctx.parameters
▿ 1 element
  ▿ 0 : void()?
    ▿ storage : void()?
      ▿ optional : 2 elements
        - .0 : nil
        - .1 : LeafKit.LeafData.NaturalType.void

Expected behavior

The page renders fine, showing "No children." with the following rendered HTML:

<!DOCTYPE html>
<html>
<body>
<p>No children.</p>
</body>
</html>

Environment

  • Vapor Framework version: 4.36.0
  • Leaf Framework version: 4.0.1
  • LeafKit Framework version: 1.0.0
  • Vapor Toolbox version: n/a
  • OS version: macOS 11.0.1 as well as Ubuntu 18.04
  • Swift version: 5.3.1

Additional context

I tried to dig a bit into LeafKit's source, and am pretty sure that the evaluation of all conditional operands is the problem. However, I could also be missing something, in which case my "attempted diagnosis" might be incorrect and the issue is actually something different.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions