-
Notifications
You must be signed in to change notification settings - Fork 18
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
Support dependent types in variable
#1226
Open
mio-19
wants to merge
10
commits into
main
Choose a base branch
from
variable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−8
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6fb8076
resolve: add introduceDependencies for variables
mio-19 661c3d5
resolve: add a test
mio-19 239cb61
resolve: fix
mio-19 053e8af
resolve: more test for dependent types in variable
mio-19 a7154d6
resolve: implement VariableDependencyCollector
mio-19 995a858
resolve: update references collection logic
mio-19 b5ee1e0
resolve: more tests
mio-19 97cf4a7
resolve: update references collection cycle detect logic
mio-19 fb113b6
resolve: test passed for variable rules
mio-19 c76c7da
resolve: save dependencies in GeneralizedVar; print cycle
mio-19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
base/src/main/java/org/aya/resolve/error/CyclicDependencyError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang. | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file. | ||
package org.aya.resolve.error; | ||
|
||
import org.aya.pretty.doc.Doc; | ||
import org.aya.util.prettier.PrettierOptions; | ||
import org.aya.util.reporter.Problem; | ||
import org.aya.syntax.ref.GeneralizedVar; | ||
import org.aya.util.error.SourcePos; | ||
import org.jetbrains.annotations.NotNull; | ||
import kala.collection.immutable.ImmutableSeq; | ||
|
||
public record CyclicDependencyError( | ||
@NotNull SourcePos sourcePos, | ||
@NotNull GeneralizedVar var, | ||
@NotNull ImmutableSeq<GeneralizedVar> cyclePath | ||
) implements Problem { | ||
@Override public @NotNull Severity level() { return Severity.ERROR; } | ||
@Override public @NotNull Stage stage() { return Stage.RESOLVE; } | ||
@Override public @NotNull Doc describe(@NotNull PrettierOptions options) { | ||
return Doc.vcat( | ||
Doc.plain("Cyclic dependency detected in variable declarations:"), | ||
Doc.plain(String.join(" -> ", cyclePath.view() | ||
.map(GeneralizedVar::name) | ||
.toImmutableSeq())) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
base/src/main/java/org/aya/resolve/visitor/VariableDependencyCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang. | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file. | ||
package org.aya.resolve.visitor; | ||
|
||
import kala.collection.immutable.ImmutableSeq; | ||
import kala.collection.mutable.MutableList; | ||
import kala.collection.mutable.MutableSet; | ||
import org.aya.util.error.PosedUnaryOperator; | ||
import org.aya.resolve.context.Context; | ||
import org.aya.resolve.error.CyclicDependencyError; | ||
import org.aya.syntax.concrete.Expr; | ||
import org.aya.syntax.ref.GeneralizedVar; | ||
import org.aya.util.error.SourcePos; | ||
import org.aya.util.reporter.Reporter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Collects dependency information for generalized variables using DFS on their types. | ||
* | ||
* 1. A variable's type may reference other generalized variables; we record those as dependencies. | ||
* 2. If we revisit a variable already on the DFS stack ("visiting" set), that indicates | ||
* a cyclic dependency, and we report an error. | ||
* 3. Once a variable is fully processed, it goes into the "visited" set; future registrations | ||
* of the same variable skip repeated traversal. | ||
* | ||
* Pitfalls & Notes: | ||
* - A single variable (e.g. “A”) should be registered once, to avoid duplication. | ||
* - Attempting to re-scan or re-introduce “A” in another variable’s context can cause | ||
* confusion or potential cycles. So we do all dependency scans here, at declaration time. | ||
* - Any reference to a variable out of scope is handled as an error in the resolver | ||
* if it’s not in the allowedGeneralizes map. | ||
*/ | ||
public final class VariableDependencyCollector { | ||
private final Reporter reporter; | ||
private final MutableSet<GeneralizedVar> visiting = MutableSet.create(); | ||
private final MutableSet<GeneralizedVar> visited = MutableSet.create(); | ||
private final MutableList<GeneralizedVar> currentPath = MutableList.create(); | ||
|
||
public VariableDependencyCollector(Reporter reporter) { | ||
this.reporter = reporter; | ||
} | ||
|
||
public void registerVariable(GeneralizedVar var) { | ||
if (visited.contains(var)) return; | ||
|
||
// If var is already being visited in current DFS path, we found a cycle | ||
if (!visiting.add(var)) { | ||
// Find cycle start index | ||
var cycleStart = currentPath.indexOf(var); | ||
var cyclePath = currentPath.view().drop(cycleStart).appended(var); | ||
reporter.report(new CyclicDependencyError(var.sourcePos(), var, cyclePath.toImmutableSeq())); | ||
throw new Context.ResolvingInterruptedException(); | ||
} | ||
|
||
currentPath.append(var); | ||
var deps = collectReferences(var); | ||
var.setDependencies(deps); | ||
|
||
// Recursively register dependencies | ||
for (var dep : deps) { | ||
registerVariable(dep); | ||
} | ||
|
||
currentPath.removeLast(); | ||
visiting.remove(var); | ||
visited.add(var); | ||
} | ||
|
||
public ImmutableSeq<GeneralizedVar> getDependencies(GeneralizedVar var) { | ||
return var.getDependencies(); | ||
} | ||
|
||
private ImmutableSeq<GeneralizedVar> collectReferences(GeneralizedVar var) { | ||
var type = var.owner.type; | ||
var collector = new StaticGeneralizedVarCollector(); | ||
type.descent(collector); | ||
return collector.getCollected(); | ||
} | ||
|
||
private static class StaticGeneralizedVarCollector implements PosedUnaryOperator<Expr> { | ||
private final MutableList<GeneralizedVar> collected = MutableList.create(); | ||
|
||
@Override | ||
public @NotNull Expr apply(@NotNull SourcePos pos, @NotNull Expr expr) { | ||
if (expr instanceof Expr.Ref ref && ref.var() instanceof GeneralizedVar gvar) { | ||
collected.append(gvar); | ||
} | ||
return expr.descent(this); | ||
} | ||
|
||
public ImmutableSeq<GeneralizedVar> getCollected() { | ||
return collected.toImmutableSeq(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,3 +318,6 @@ That looks right! | |
LocalShadowSuppress: | ||
That looks right! | ||
|
||
CyclicDependency: | ||
That looks right! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
@Nullable
dependencies with anull
initial value can be better because we shouldn't use them before resolving.