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

No More Duplicated Vars #665

Merged
merged 4 commits into from
Jan 7, 2023
Merged

No More Duplicated Vars #665

merged 4 commits into from
Jan 7, 2023

Conversation

HoshinoTented
Copy link
Contributor

There are some problems because of the duplicated vars, why don't we just add an assertion and save our time?

@HoshinoTented HoshinoTented added this to the v0.24 milestone Nov 15, 2022
@HoshinoTented
Copy link
Contributor Author

Maybe we should solve all the duplicated vars problems and then merge this...

@ice1000
Copy link
Member

ice1000 commented Nov 16, 2022

I just realized something. You have GlobalConfig and tests will enable it

@ice1000
Copy link
Member

ice1000 commented Nov 16, 2022

I have a lot of other tests/sanity checks that I want to add. I believe we should have a general mechanism for that.

Maybe tracing should be part of this config

@ice1000
Copy link
Member

ice1000 commented Nov 16, 2022

There are already some limited scope checks. There can be more.

@ice1000 ice1000 force-pushed the localCtx-enhance branch 5 times, most recently from 8c6d16e to d3eae0d Compare November 19, 2022 21:58
@ice1000 ice1000 modified the milestones: v0.24, v0.25 Nov 22, 2022
@ice1000 ice1000 force-pushed the localCtx-enhance branch 3 times, most recently from 1699a0b to c7e9bff Compare December 5, 2022 08:35
@ice1000 ice1000 modified the milestones: v0.25, v0.26 Dec 9, 2022
@ice1000 ice1000 modified the milestones: v0.26, v0.27 Dec 18, 2022
@ice1000
Copy link
Member

ice1000 commented Jan 7, 2023

I have fixed some problems caused by duplicated vars. There are more, but I would love to merge these changes and have another PR that enables the assertion back and we'll see what we can do in the future.

What do you think? @HoshinoTented

@ice1000
Copy link
Member

ice1000 commented Jan 7, 2023

bors r+

Tests passed locally.

@codecov
Copy link

codecov bot commented Jan 7, 2023

Codecov Report

Merging #665 (562d076) into main (7c01dac) will increase coverage by 0.01%.
The diff coverage is 54.54%.

@@             Coverage Diff              @@
##               main     #665      +/-   ##
============================================
+ Coverage     82.15%   82.17%   +0.01%     
  Complexity     3383     3383              
============================================
  Files           288      288              
  Lines         10537    10534       -3     
  Branches       1259     1258       -1     
============================================
- Hits           8657     8656       -1     
+ Misses         1168     1166       -2     
  Partials        712      712              
Impacted Files Coverage Δ
...se/src/main/java/org/aya/tyck/env/MapLocalCtx.java 100.00% <ø> (ø)
...se/src/main/java/org/aya/tyck/env/SeqLocalCtx.java 78.57% <ø> (+5.23%) ⬆️
base/src/main/java/org/aya/tyck/env/LocalCtx.java 73.84% <54.54%> (+1.11%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@bors
Copy link
Contributor

bors bot commented Jan 7, 2023

Build succeeded:

@bors bors bot merged commit 29b0943 into main Jan 7, 2023
@bors bors bot deleted the localCtx-enhance branch January 7, 2023 02:26
@ice1000
Copy link
Member

ice1000 commented Jan 7, 2023

What I've done locally:

diff --git a/base/src/main/java/org/aya/tyck/StmtTycker.java b/base/src/main/java/org/aya/tyck/StmtTycker.java
index 4010646a4..8fc19b804 100644
--- a/base/src/main/java/org/aya/tyck/StmtTycker.java
+++ b/base/src/main/java/org/aya/tyck/StmtTycker.java
@@ -108,34 +108,39 @@ public final class StmtTycker extends TracedTycker {
       case TeleDecl.DataDecl decl -> {
         var signature = decl.signature;
         assert signature != null;
-        var body = decl.body.map(clause -> (CtorDef) tyck(clause, tycker));
+        var body = decl.body.map(clause -> traced(clause, tycker, this::checkCtor));
         yield new DataDef(decl.ref, signature.param(), signature.result(), body);
       }
       case TeleDecl.PrimDecl decl -> decl.ref.core;
       case TeleDecl.StructDecl decl -> {
         var signature = decl.signature;
         assert signature != null;
-        var body = decl.fields.map(field -> (FieldDef) tyck(field, tycker));
+        var body = decl.fields.map(field -> traced(field, tycker, (f, e) ->
+          checkField(f, e, false)));
         yield new StructDef(decl.ref, signature.param(), signature.result(), body);
       }
       // Do nothing, data constructors is just a header.
       case TeleDecl.DataCtor ctor -> ctor.ref.core;
-      case TeleDecl.StructField field -> {
-        // TODO[ice]: remove this hack
-        if (field.ref.core != null) yield field.ref.core;
-        var signature = field.signature;
-        assert signature != null; // already handled in the entrance of this method
-        var structRef = field.structRef;
-        var structSig = structRef.concrete.signature;
-        assert structSig != null;
-        loadTele(tycker, structSig);
-        var result = signature.result();
-        var body = field.body.map(e -> tycker.inherit(e, result).wellTyped());
-        yield new FieldDef(structRef, field.ref, structSig.param(), signature.param(), result, body, field.coerce);
-      }
+      case TeleDecl.StructField field -> checkField(field, tycker, true);
     };
   }

+  private @NotNull FieldDef checkField(TeleDecl.StructField field, @NotNull ExprTycker tycker, boolean load) {
+    // TODO[ice]: remove this hack
+    if (field.ref.core != null) return field.ref.core;
+    var structRef = field.structRef;
+    var structSig = structRef.concrete.signature;
+    assert structSig != null;
+    // tyckHeader implies loadTele
+    if (field.signature == null) tyckHeader(field, tycker);
+    else if (load) loadTele(tycker, structSig);
+    var signature = field.signature;
+    assert signature != null;
+    var result = signature.result();
+    var body = field.body.map(e -> tycker.inherit(e, result).wellTyped());
+    return new FieldDef(structRef, field.ref, structSig.param(), signature.param(), result, body, field.coerce);
+  }
+
   // Apply a simple checking strategy for maximal metavar inference.
   public @NotNull FnDef simpleFn(@NotNull ExprTycker tycker, TeleDecl.FnDecl fn) {
     return traced(fn, tycker, this::doSimpleFn);
@@ -223,7 +228,7 @@ public final class StmtTycker extends TracedTycker {
         tycker.solveMetas();
         tycker.ctx = new SeqLocalCtx();
       }
-      case TeleDecl.DataCtor ctor -> checkCtor(tycker, ctor);
+      case TeleDecl.DataCtor ctor -> checkCtor(ctor, tycker);
       case TeleDecl.StructField field -> {
         if (field.signature != null) break;
         var structRef = field.structRef;
@@ -239,9 +244,13 @@ public final class StmtTycker extends TracedTycker {
     tracing(TreeBuilder::reduce);
   }

-  /** Extracted for the extreme complexity. */
-  private void checkCtor(@NotNull ExprTycker tycker, TeleDecl.DataCtor ctor) {
-    if (ctor.ref.core != null) return;
+  /**
+   * Extracted for the extreme complexity.
+   *
+   * @return
+   */
+  private @NotNull CtorDef checkCtor(TeleDecl.DataCtor ctor, @NotNull ExprTycker tycker) {
+    if (ctor.ref.core != null) return ctor.ref.core;
     var dataRef = ctor.dataRef;
     var dataConcrete = dataRef.concrete;
     var dataSig = dataConcrete.signature;
@@ -305,7 +314,9 @@ public final class StmtTycker extends TracedTycker {
       reporter.report(new CubicalError.PathConDominateError(ctor.clauses.sourcePos()));
       elabClauses = PartialTerm.DUMMY_SPLIT;
     }
-    dataConcrete.checkedBody.append(new CtorDef(dataRef, ctor.ref, pat, patternTele, tele, split, dataCall, ctor.coerce));
+    var def = new CtorDef(dataRef, ctor.ref, pat, patternTele, tele, split, dataCall, ctor.coerce);
+    dataConcrete.checkedBody.append(def);
+    return def;
   }

   private static void loadTele(@NotNull ExprTycker tycker, Def.Signature<?> dataSig) {

@ice1000
Copy link
Member

ice1000 commented Jan 7, 2023

It still doesn't work, but it is anyway an attempt to fix record fields being type checked in a messy order. A record field tyck can happen when:

  • Record body is being checked
  • Record field header is being checked before record body being checked
  • Record field body is being checked before header being checked
  • Record field body is being checked after header is already checked in another call to doTyck

...

I don't think it worth fixing because we're going to overhaul records as classes anyway...

Copy link
Member

@ice1000 ice1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants