From 9e3cc38975c94a626c6261541bc8b89a44d482dd Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Mon, 18 Sep 2023 23:59:41 +0800 Subject: [PATCH] nixd/Sema: lowering for ExprAssert & ExprWith --- nixd/lib/Sema/Lowering.cpp | 17 +++++++++++++++++ nixd/tools/nixd-lint/test/lowering-assert.nix | 4 ++++ nixd/tools/nixd-lint/test/lowering-with.nix | 4 ++++ 3 files changed, 25 insertions(+) create mode 100644 nixd/tools/nixd-lint/test/lowering-assert.nix create mode 100644 nixd/tools/nixd-lint/test/lowering-with.nix diff --git a/nixd/lib/Sema/Lowering.cpp b/nixd/lib/Sema/Lowering.cpp index ae37271bb..eacc0bf0d 100644 --- a/nixd/lib/Sema/Lowering.cpp +++ b/nixd/lib/Sema/Lowering.cpp @@ -2,6 +2,7 @@ #include "nixd/Sema/EvalContext.h" #include "nixd/Syntax/Diagnostic.h" #include "nixd/Syntax/Nodes.h" +#include "nixexpr.hh" namespace nixd { @@ -122,6 +123,22 @@ nix::Expr *Lowering::lower(EvalContext &Ctx, nixd::syntax::Node *Root) { auto *Fn = dynamic_cast(Root); return lowerFunction(Ctx, Fn); } + case Node::NK_Assert: { + auto *Assert = dynamic_cast(Root); + auto *Cond = lower(Ctx, Assert->Cond); + auto *Body = lower(Ctx, Assert->Body); + auto *NixAssert = + Ctx.Pool.record(new nix::ExprAssert(Assert->Range.Begin, Cond, Body)); + return NixAssert; + } + case Node::NK_With: { + auto *With = dynamic_cast(Root); + auto *Attrs = lower(Ctx, With->Attrs); + auto *Body = lower(Ctx, With->Body); + auto *NixWith = + Ctx.Pool.record(new nix::ExprWith(With->Range.Begin, Attrs, Body)); + return NixWith; + } } return nullptr; diff --git a/nixd/tools/nixd-lint/test/lowering-assert.nix b/nixd/tools/nixd-lint/test/lowering-assert.nix new file mode 100644 index 000000000..5ea4d7a73 --- /dev/null +++ b/nixd/tools/nixd-lint/test/lowering-assert.nix @@ -0,0 +1,4 @@ +# RUN: nixd-lint %s | FileCheck %s + +# CHECK: function argument duplicated to a function formal +assert 0; a @ { a }: 1 diff --git a/nixd/tools/nixd-lint/test/lowering-with.nix b/nixd/tools/nixd-lint/test/lowering-with.nix new file mode 100644 index 000000000..94418a533 --- /dev/null +++ b/nixd/tools/nixd-lint/test/lowering-with.nix @@ -0,0 +1,4 @@ +# RUN: nixd-lint %s | FileCheck %s + +# CHECK: function argument duplicated to a function formal +with 1; { a } @ a : 1