Skip to content

Commit

Permalink
libnixf: diagnose empty inherit (nix-community#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Apr 24, 2024
1 parent 044d726 commit c58edbf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
16 changes: 15 additions & 1 deletion libnixf/src/Parse/ParseAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,25 @@ std::shared_ptr<Inherit> Parser::parseInherit() {
}
break;
}
if (ExpectResult ER = expect(tok_semi_colon); ER.ok())
ExpectResult ER = expect(tok_semi_colon);
if (ER.ok())
consume();
else
ER.diag().note(Note::NK_ToMachThis, TokInherit.range())
<< std::string(tok::spelling(tok_kw_inherit));

// If attrnames are emtpy, this is an emtpy "inherit";
if (AttrNames.empty()) {
Diagnostic &D =
Diags.emplace_back(Diagnostic::DK_EmptyInherit, TokInherit.range());
D.tag(DiagnosticTag::Faded);
Fix &F = D.fix("remove `inherit` keyword");
F.edit(TextEdit::mkRemoval(TokInherit.range()));
if (ER.ok()) {
// Remove ";" also.
F.edit(TextEdit::mkRemoval(ER.tok().range()));
}
}
return std::make_shared<Inherit>(
LexerCursorRange{TokInherit.lCur(), LastToken->rCur()},
std::move(AttrNames), std::move(Expr));
Expand Down
34 changes: 32 additions & 2 deletions libnixf/test/Parse/ParseAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ TEST(Parser, AttrsBindingInherit) {
auto AST = nixf::parse(Src, Diags);

ASSERT_TRUE(AST);
ASSERT_EQ(Diags.size(), 5);
ASSERT_EQ(Diags.size(), 6);

// Check the bindings.
const auto &B = static_cast<ExprAttrs *>(AST.get())->binds()->bindings();
Expand Down Expand Up @@ -421,6 +421,36 @@ TEST(Parser, SyncInherit3) {
ASSERT_EQ(N.args()[0], "(");
}

TEST(Parser, InheritEmpty) {
auto Src = R"({ inherit; })"sv;

std::vector<Diagnostic> Diags;
auto AST = nixf::parse(Src, Diags);

ASSERT_TRUE(AST);

ASSERT_EQ(Diags.size(), 1);
auto &D = Diags[0];
ASSERT_TRUE(D.range().lCur().isAt(0, 2, 2));
ASSERT_TRUE(D.range().rCur().isAt(0, 9, 9));

// Check the fix.
const Fix &F = Diags[0].fixes()[0];

ASSERT_EQ(F.edits().size(), 2);
ASSERT_TRUE(F.edits()[0].isRemoval());
ASSERT_EQ(F.edits()[0].oldRange().lCur().line(), 0);
ASSERT_EQ(F.edits()[0].oldRange().lCur().column(), 2);
ASSERT_EQ(F.edits()[0].oldRange().rCur().line(), 0);
ASSERT_EQ(F.edits()[0].oldRange().rCur().column(), 9);

// Second, remove the semicolon.
ASSERT_EQ(F.edits()[1].oldRange().lCur().line(), 0);
ASSERT_EQ(F.edits()[1].oldRange().lCur().column(), 9);
ASSERT_EQ(F.edits()[1].oldRange().rCur().line(), 0);
ASSERT_EQ(F.edits()[1].oldRange().rCur().column(), 10);
}

TEST(Parser, InheritMissingSemi) {
auto Src = R"(
{
Expand All @@ -433,7 +463,7 @@ TEST(Parser, InheritMissingSemi) {

ASSERT_TRUE(AST);

ASSERT_EQ(Diags.size(), 1);
ASSERT_EQ(Diags.size(), 2);
auto &D = Diags[0];
ASSERT_TRUE(D.range().lCur().isAt(2, 9, 12));
ASSERT_TRUE(D.range().rCur().isAt(2, 9, 12));
Expand Down

0 comments on commit c58edbf

Please sign in to comment.