From 74f013adabcedab636b8338632bf2b2598162c91 Mon Sep 17 00:00:00 2001 From: Origami404 Date: Sun, 23 Jun 2024 20:46:51 +0800 Subject: [PATCH] libnixf: disable escaping `with` warning for builtin (#528) Giving an "escaping with" warning to builtin variables like `true`, `false` or `null` is not very suitable: ![image](https://github.com/nix-community/nixd/assets/29816865/daeb04ad-37a6-4f36-bf0f-81397e9a5725) --- libnixf/src/Sema/VariableLookup.cpp | 2 +- libnixf/test/Sema/VariableLookup.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libnixf/src/Sema/VariableLookup.cpp b/libnixf/src/Sema/VariableLookup.cpp index 02365d9f8..0f28cf700 100644 --- a/libnixf/src/Sema/VariableLookup.cpp +++ b/libnixf/src/Sema/VariableLookup.cpp @@ -81,7 +81,7 @@ void VariableLookupAnalysis::lookupVar(const ExprVar &Var, Def->usedBy(Var); Results.insert({&Var, LookupResult{LookupResultKind::Defined, Def}}); - if (EnclosedWith) { + if (EnclosedWith && !Def->isBuiltin()) { // Escaping from "with" to outer scope. // https://github.com/NixOS/nix/issues/490 diff --git a/libnixf/test/Sema/VariableLookup.cpp b/libnixf/test/Sema/VariableLookup.cpp index d2b84f13d..e0c420bff 100644 --- a/libnixf/test/Sema/VariableLookup.cpp +++ b/libnixf/test/Sema/VariableLookup.cpp @@ -270,6 +270,14 @@ TEST_F(VLATest, EscapingWith) { ASSERT_EQ(D.notes()[1].range().rCur().offset(), 7); } +TEST_F(VLATest, EscapingWithButBuiltin) { + std::shared_ptr AST = parse("with { a = 1; }; [ a true false null ]", Diags); + VariableLookupAnalysis VLA(Diags); + VLA.runOnAST(*AST); + + ASSERT_EQ(Diags.size(), 0); +} + TEST_F(VLATest, InheritRec) { // Make sure inheirt (expr), the expression is binded to "NewEnv". std::shared_ptr AST =