Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions libnixf/include/nixf/Basic/Nodes/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ namespace nixf {

class ExprSelect : public Expr {
const std::shared_ptr<Expr> E;
const std::shared_ptr<Dot> Do;
const std::shared_ptr<AttrPath> Path;
const std::shared_ptr<Expr> Default;

public:
ExprSelect(LexerCursorRange Range, std::shared_ptr<Expr> E,
std::shared_ptr<AttrPath> Path, std::shared_ptr<Expr> Default)
: Expr(NK_ExprSelect, Range), E(std::move(E)), Path(std::move(Path)),
Default(std::move(Default)) {
std::shared_ptr<Dot> Do, std::shared_ptr<AttrPath> Path,
std::shared_ptr<Expr> Default)
: Expr(NK_ExprSelect, Range), E(std::move(E)), Do(std::move(Do)),
Path(std::move(Path)), Default(std::move(Default)) {
assert(this->E && "E must not be null");
}

Expand All @@ -22,6 +24,8 @@ class ExprSelect : public Expr {
return *E;
}

[[nodiscard]] Dot *dot() const { return Do.get(); }

[[nodiscard]] Expr *defaultExpr() const { return Default.get(); }

[[nodiscard]] AttrPath *path() const { return Path.get(); }
Expand Down
2 changes: 2 additions & 0 deletions libnixf/include/nixf/Sema/VariableLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class VariableLookupAnalysis {

void lookupVar(const ExprVar &Var, const std::shared_ptr<EnvNode> &Env);

void checkBuiltins(const ExprSelect &Sel);

std::shared_ptr<EnvNode> dfsAttrs(const SemaAttrs &SA,
const std::shared_ptr<EnvNode> &Env,
const Node *Syntax,
Expand Down
12 changes: 12 additions & 0 deletions libnixf/src/Basic/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ class Diagnostic(TypedDict):
# Primary Operation (PrimOp) related diagnostics
# We call this "builtin" in messages to match Nix's terminology
# "PrimOp" is specific to Nix/nixf implementation
{
"sname": "sema-primop-unknown",
"cname": "PrimOpUnknown",
"severity": "Error",
"message": "unknown builtin `{}`",
},
{
"sname": "sema-primop-removed-prefix",
"cname": "PrimOpRemovablePrefix",
"severity": "Warning",
"message": "this is a prelude builtin, the `builtins.` prefix can be removed",
},
{
"sname": "sema-primop-needs-prefix",
"cname": "PrimOpNeedsPrefix",
Expand Down
5 changes: 3 additions & 2 deletions libnixf/src/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ std::shared_ptr<Expr> Parser::parseExprSelect() {

// expr_select : expr_simple '.' attrpath
// | expr_simple '.' attrpath 'or' expr_select
auto Do = std::make_shared<Dot>(Tok.range(), Expr.get(), nullptr);
consume(); // .
auto Path = parseAttrPath();
if (!Path) {
Expand All @@ -34,7 +35,7 @@ std::shared_ptr<Expr> Parser::parseExprSelect() {
// expr_select : expr_simple '.' attrpath
return std::make_shared<ExprSelect>(
LexerCursorRange{Begin, LastToken->rCur()}, std::move(Expr),
std::move(Path), /*Default=*/nullptr);
std::move(Do), std::move(Path), /*Default=*/nullptr);
}

// expr_select : expr_simple '.' attrpath 'or' expr_select
Expand All @@ -46,7 +47,7 @@ std::shared_ptr<Expr> Parser::parseExprSelect() {
}
return std::make_shared<ExprSelect>(
LexerCursorRange{Begin, LastToken->rCur()}, std::move(Expr),
std::move(Path), std::move(Default));
std::move(Do), std::move(Path), std::move(Default));
}

std::shared_ptr<Expr> Parser::parseExprApp(int Limit) {
Expand Down
4 changes: 2 additions & 2 deletions libnixf/src/Sema/SemaActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ Sema::desugarInheritExpr(std::shared_ptr<AttrName> Name,
auto Path = std::make_shared<AttrPath>(
Range, std::vector<std::shared_ptr<AttrName>>{std::move(Name)},
std::vector<std::shared_ptr<Dot>>{});
return {std::make_shared<ExprSelect>(Range, std::move(E), std::move(Path),
nullptr),
return {std::make_shared<ExprSelect>(Range, std::move(E), nullptr,
std::move(Path), nullptr),
Attribute::AttributeKind::InheritFrom};
}

Expand Down
52 changes: 52 additions & 0 deletions libnixf/src/Sema/VariableLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,52 @@ void VariableLookupAnalysis::dfs(const ExprWith &With,
}
}

void VariableLookupAnalysis::checkBuiltins(const ExprSelect &Sel) {
if (!Sel.path())
return;

if (Sel.expr().kind() != Node::NK_ExprVar)
return;

const auto &Builtins = static_cast<const ExprVar &>(Sel.expr());
if (Builtins.id().name() != "builtins")
return;

const auto &AP = *Sel.path();

if (AP.names().size() != 1)
return;

AttrName &First = *AP.names()[0];
if (!First.isStatic())
return;

const auto &Name = First.staticName();

switch (lookupGlobalPrimOpInfo(Name)) {
case PrimopLookupResult::Found: {
Diagnostic &D = Diags.emplace_back(Diagnostic::DK_PrimOpRemovablePrefix,
Builtins.range());
Fix &F =
D.fix("remove `builtins.` prefix")
.edit(TextEdit::mkRemoval(Builtins.range())); // remove `builtins`

if (Sel.dot()) {
// remove the dot also.
F.edit(TextEdit::mkRemoval(Sel.dot()->range()));
}
return;
}
case PrimopLookupResult::PrefixedFound:
return;
case PrimopLookupResult::NotFound:
Diagnostic &D = Diags.emplace_back(Diagnostic::DK_PrimOpUnknown,
AP.names()[0]->range());
D << AP.names()[0]->name();
return;
}
}

void VariableLookupAnalysis::dfs(const Node &Root,
const std::shared_ptr<EnvNode> &Env) {
Envs.insert({&Root, Env});
Expand Down Expand Up @@ -354,6 +400,12 @@ void VariableLookupAnalysis::dfs(const Node &Root,
dfs(With, Env);
break;
}
case Node::NK_ExprSelect: {
trivialDispatch(Root, Env);
const auto &Sel = static_cast<const ExprSelect &>(Root);
checkBuiltins(Sel);
break;
}
default:
trivialDispatch(Root, Env);
}
Expand Down
33 changes: 33 additions & 0 deletions libnixf/test/Sema/VariableLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,37 @@ TEST_F(VLATest, Builtins_Needs_Prefix) {
ASSERT_EQ(Diags[0].fixes()[0].edits()[0].newText(), "builtins.");
}

TEST_F(VLATest, Builtins_Removable_Prefix) {
const char *Src = R"(builtins.import)";

std::shared_ptr<Node> AST = parse(Src, Diags);
VariableLookupAnalysis VLA(Diags);
VLA.runOnAST(*AST);

ASSERT_EQ(Diags.size(), 1);

ASSERT_EQ(Diags[0].kind(), Diagnostic::DK_PrimOpRemovablePrefix);
ASSERT_EQ(Diags[0].fixes().size(), 1);
ASSERT_EQ(Diags[0].fixes()[0].edits().size(), 2);
ASSERT_EQ(Diags[0].fixes()[0].edits()[0].oldRange().lCur().offset(), 0);
ASSERT_EQ(Diags[0].fixes()[0].edits()[0].oldRange().rCur().offset(), 8);
ASSERT_EQ(Diags[0].fixes()[0].edits()[0].newText(), "");
ASSERT_EQ(Diags[0].fixes()[0].edits()[1].oldRange().lCur().offset(), 8);
ASSERT_EQ(Diags[0].fixes()[0].edits()[1].oldRange().rCur().offset(), 9);
ASSERT_EQ(Diags[0].fixes()[0].edits()[1].newText(), "");
}

TEST_F(VLATest, Builtins_Unknown) {
const char *Src = R"(builtins.importaaaa)";

std::shared_ptr<Node> AST = parse(Src, Diags);
VariableLookupAnalysis VLA(Diags);
VLA.runOnAST(*AST);

ASSERT_EQ(Diags.size(), 1);

ASSERT_EQ(Diags[0].kind(), Diagnostic::DK_PrimOpUnknown);
ASSERT_EQ(Diags[0].fixes().size(), 0);
}

} // namespace