Skip to content

Commit

Permalink
libnixf/Sema: differentiate lambda argument with and without formals
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksanaa committed Jul 31, 2024
1 parent 4c306e7 commit d2a05f3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions libnixf/include/nixf/Sema/VariableLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Definition {
/// \brief From lambda formal, e.g. { a }: a + 1
DS_LambdaFormal,

/// \brief From lambda arg with formal, e.g. { foo, bar }@a: a + 1
DS_LambdaArgWithFormal,

/// \brief From recursive attribute set. e.g. rec { }
DS_Rec,

Expand Down
11 changes: 8 additions & 3 deletions libnixf/src/Sema/VariableLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ void VariableLookupAnalysis::dfs(const ExprLambda &Lambda,
// foo: body
// ^~~<------- add function argument.
if (Arg.id()) {
// Function arg cannot duplicate to it's formal.
// If it this unluckily happens, we would like to skip this definition.
if (!Arg.formals() || !Arg.formals()->dedup().contains(Arg.id()->name()))
if (!Arg.formals()) {
ToDef.insert_or_assign(Arg.id(), DBuilder.add(Arg.id()->name(), Arg.id(),
Definition::DS_LambdaArg));
// Function arg cannot duplicate to it's formal.
// If it this unluckily happens, we would like to skip this definition.
} else if (!Arg.formals()->dedup().contains(Arg.id()->name())) {
ToDef.insert_or_assign(Arg.id(),
DBuilder.add(Arg.id()->name(), Arg.id(),
Definition::DS_LambdaArgWithFormal));
}
}

// { foo, bar, ... } : body
Expand Down
13 changes: 13 additions & 0 deletions libnixf/test/Sema/VariableLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ TEST_F(VLATest, LivenessDupSymbol) {
ASSERT_EQ(Diags[0].tags().size(), 0);
}

TEST_F(VLATest, LivenessArgWithFormal) {
std::shared_ptr<Node> AST = parse("{ foo }@bar: foo", Diags);
VariableLookupAnalysis VLA(Diags);
VLA.runOnAST(*AST);

ASSERT_EQ(Diags.size(), 1);

ASSERT_EQ(Diags[0].kind(), Diagnostic::DK_DefinitionNotUsed);
ASSERT_EQ(Diags[0].range().lCur().column(), 8);
ASSERT_EQ(Diags[0].tags().size(), 1);
ASSERT_EQ(Diags[0].tags()[0], DiagnosticTag::Faded);
}

TEST_F(VLATest, ToDefAttrs) {
std::shared_ptr<Node> AST = parse("rec { x = 1; y = x; z = x; }", Diags);
VariableLookupAnalysis VLA(Diags);
Expand Down

0 comments on commit d2a05f3

Please sign in to comment.