Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libnixf/Sema: differentiate lambda argument with and without formals #561

Merged
merged 1 commit into from
Jul 31, 2024
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
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
Loading