-
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
An attempt to redo #1226 #1298
Closed
Closed
An attempt to redo #1226 #1298
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d31e8b3
generalize: port some code from #1226
mio-19 2e53426
generalize: store dependencies
ice1000 bc75d24
generalize: implement recursive generalize, fix loads of bugs
ice1000 c5ad6ce
generalize: avoid adding repeated gvars to allowedGeneralizes
ice1000 5bb9251
generalize: correctly implement
ice1000 02b24ca
generalize: cleanup
ice1000 4e22e3e
generalize: integrate `OverGeneralizer`
ice1000 b5170ce
generalize: do not use a set
ice1000 a4aaed1
test: remove duplicated test, port test to integration tests
ice1000 6df35e3
revert: "generalize: integrate `OverGeneralizer`"
ice1000 c40307c
generalize: basic error report
ice1000 27f3a86
generalize: test on error report
ice1000 e4b963e
generalize: more intensive test, fix a long-standing undiscovered bug…
ice1000 9dbce46
generalize: erm I can't get this to work
ice1000 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
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
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
// Copyright (c) 2020-2025 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.prettier.BasePrettier; | ||
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.english("Cyclic dependency detected in variable declarations:"), | ||
Doc.join(Doc.spaced(Doc.symbol("->")), cyclePath.map(BasePrettier::varDoc)) | ||
); | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
base/src/main/java/org/aya/resolve/visitor/OverGeneralizer.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,47 @@ | ||
// Copyright (c) 2020-2025 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.mutable.MutableList; | ||
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.jetbrains.annotations.NotNull; | ||
|
||
/// 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 [#currentPath], that indicates | ||
/// a cyclic dependency, and we report an error. | ||
/// 3. Once a variable is fully processed, it goes into the [#introduceDependency] method; future registrations | ||
/// of the same variable skip repeated traversal using [#contains]. | ||
public abstract class OverGeneralizer { | ||
private final @NotNull Context reporter; | ||
public final @NotNull MutableList<GeneralizedVar> currentPath = MutableList.create(); | ||
|
||
public OverGeneralizer(@NotNull Context reporter) { this.reporter = reporter; } | ||
protected abstract boolean contains(@NotNull GeneralizedVar var); | ||
protected abstract void introduceDependency(@NotNull GeneralizedVar var, @NotNull Expr.Param param); | ||
|
||
public final void introduceDependencies(@NotNull GeneralizedVar var, @NotNull Expr.Param param) { | ||
if (contains(var)) return; | ||
|
||
// If var is already being visited in current DFS path, we found a cycle | ||
if (currentPath.contains(var)) { | ||
// Find cycle start index | ||
var cycleStart = currentPath.indexOf(var); | ||
if (cycleStart < 0) cycleStart = 0; | ||
var cyclePath = currentPath.view().drop(cycleStart).appended(var); | ||
reporter.reportAndThrow(new CyclicDependencyError(var.sourcePos(), var, cyclePath.toImmutableSeq())); | ||
} | ||
|
||
currentPath.append(var); | ||
// Introduce dependencies first | ||
var.owner.dependencies.forEach(this::introduceDependencies); | ||
|
||
// Now introduce the variable itself | ||
introduceDependency(var, param); | ||
currentPath.removeLast(); | ||
} | ||
} |
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
Oops, something went wrong.
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.
This is an important fix though