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

[compiler-v2] Avoid infinite recursion showing types with receiver functions in presence of errors #14922

Merged
merged 3 commits into from
Oct 17, 2024

Conversation

brmataptos
Copy link
Contributor

@brmataptos brmataptos commented Oct 10, 2024

Description

When displaying a "receiver function" type in the presence of errors,
a type variable may be shown by showing the receiver function type,
which includes the type variable itself. We break the recursion by
introducing an opt_self_var field in TypeDisplayContext which
is used to recognize the type variable and display "Self" instead
of recursing.

Fixes #14913

How Has This Been Tested?

Added a reduced test based on Igor's finding.

Key Areas to Review

If we allow more recursive types/constraint in the future then we
may want a more general mechanism. I think we should punt for now,
but others may disagree.

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Performance improvement
  • Refactoring
  • Dependency update
  • Documentation update
  • Tests

Which Components or Systems Does This Change Impact?

  • Validator Node
  • Full Node (API, Indexer, etc.)
  • Move/Aptos Virtual Machine
  • Aptos Framework
  • Aptos CLI/SDK
  • Developer Infrastructure
  • Move Compiler
  • Other (specify)

Checklist

  • I have read and followed the CONTRIBUTING doc
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I identified and added all stakeholders and component owners affected by this change as reviewers
  • I tested both happy and unhappy path of the functionality
  • I have made corresponding changes to the documentation

Copy link

trunk-io bot commented Oct 10, 2024

⏱️ 2h 25m total CI duration on this PR
Slowest 15 Jobs Cumulative Duration Recent Runs
execution-performance / single-node-performance 23m 🟩
dispatch_event 17m 🟩
dispatch_event 15m 🟩
dispatch_event 14m 🟩
rust-move-unit-coverage 11m 🟩
rust-move-unit-coverage 10m 🟩
rust-move-unit-coverage 10m 🟩
rust-move-tests 10m 🟩
rust-move-tests 10m 🟩
rust-move-tests 10m 🟩
check-dynamic-deps 7m 🟩🟩🟩
rust-cargo-deny 6m 🟩🟩🟩
general-lints 1m 🟩🟩🟩
semgrep/ci 1m 🟩🟩🟩
file_change_determinator 33s 🟩🟩🟩

settingsfeedbackdocs ⋅ learn more about trunk.io

Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 86.84211% with 5 lines in your changes missing coverage. Please review.

Project coverage is 60.1%. Comparing base (0e4f5df) to head (0f45016).
Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
third_party/move/move-model/src/ty.rs 72.2% 5 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14922   +/-   ##
=======================================
  Coverage    60.1%    60.1%           
=======================================
  Files         856      856           
  Lines      211110   211138   +28     
=======================================
+ Hits       126962   127011   +49     
+ Misses      84148    84127   -21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

}
}

public fun borrow<K, V>(self: &OrderedMao<K, V>, key: &K): &V {
Copy link

Choose a reason for hiding this comment

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

There appears to be a typo in the type name OrderedMao. This should be corrected to OrderedMap to match the struct definition and maintain consistency throughout the code.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the bot correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, in this case it's in a test directory, so I intended it to be incorrect, and it should not be corrected. Their bot should notice a directory named compiler.*test.* and consider the possibility that the code is test input code for a compiler.

39 │ public fun borrow<K, V>(self: &OrderedMao<K, V>, key: &K): &V {
│ ^^^^^^^^^^

error: unable to infer instantiation of type `fun self.iter_borrow(Self,&*error*):&V` (consider providing type arguments or annotating the type)
Copy link
Contributor

Choose a reason for hiding this comment

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

The type fun self.iter_borrow(Self,&*error*):&V mentioned here is not particularly helpful to the user. Maybe we could display something better when *error* is involved?

Could be addressed in a separate PR though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The *error* shown here is the type K in the parameter type. What we probably should do is to not print this error message, since we already see an*error* type and we presumably print another error message when generating that type.

@wrwg wrwg changed the title Avoid infinite recursion showing types with receiver functions in presence of errors [compiler-v2] Avoid infinite recursion showing types with receiver functions in presence of errors Oct 10, 2024
@@ -3264,6 +3264,8 @@
pub used_modules: BTreeSet<ModuleId>,
/// Whether to use `m::T` for representing types, for stable output in docgen
pub use_module_qualification: bool,
/// Var type that should appear as Self in display
pub opt_self_var: Option<u32>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this. Self does not play a special rule, and a variable specifically shouldn't be mapped to anything as long as it is not assigned. This does not seem to be the right fix. What is the problem? Is it a cyclic substitution? Then this is a bug around a cycle check missing in type unification.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a comment to the issue to carefully explain the problem, which is that

  • To display a variable type constrained by just a SomeReceiverFunction constraint, we display the signature of the function, whose first parameter type is the variable type itself, so we try to display it...

Self does play a special role, in that we may have a type that is unconstrained other than by being the target of a receiver function call. This happens only in the case of an error, but that's why we are displaying the type.

Possible solutions include:

  1. Catching the error earlier and avoiding any attempts to print out the SomeReceverFunction constraint.
  2. Always displaying the first parameter type of SomeReceiverFunction as Self, ignoring the actual type.
    • This seems like it could lose info in some other cases, where the first type is displayable without this loop
  3. Adding a hack to show Self in this particular case.
  4. Generalize this hack to be able to print recursive types if they happen to show up: Add new substitution for the variable, and if the substitution is used, then display an explicitly recursive type such as exists v. fun self.method(v, ...)

I was able to get rid of one occurrence of the error earlier, but one remained, so I went to solution #3. I alluded to the possible desire to generalize to solution #4 in the PR summary, but I think it's overkill for the current situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to use .. instead of Self, and also to allow multiple variables to be stored when displaying via constraints to catch potential recursions. I also check the error type name for *error* in the one case where it is showing up, and avoid showing the error at all in this case, since it is guaranteed to be redundant. This has the unfortunate side-effect of leaving us without any test case to show recursion happening, but it was tested before this change.

@brmataptos brmataptos requested review from wrwg and vineethk October 10, 2024 20:47
Copy link
Contributor

@wrwg wrwg left a comment

Choose a reason for hiding this comment

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

Looks great!

…s. check for `*error*` in type for instantiation type error message, avoid showing an error message which is actually redundant
@brmataptos brmataptos enabled auto-merge (squash) October 17, 2024 17:35

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

✅ Forge suite compat success on b29f09f57e898d8d211c8bc3e303f6e50bba2266 ==> 0f45016afc1ffad726f0d635f7868899b5f79059

Compatibility test results for b29f09f57e898d8d211c8bc3e303f6e50bba2266 ==> 0f45016afc1ffad726f0d635f7868899b5f79059 (PR)
1. Check liveness of validators at old version: b29f09f57e898d8d211c8bc3e303f6e50bba2266
compatibility::simple-validator-upgrade::liveness-check : committed: 9269.93 txn/s, latency: 2811.33 ms, (p50: 2100 ms, p70: 2300, p90: 2800 ms, p99: 34400 ms), latency samples: 432500
2. Upgrading first Validator to new version: 0f45016afc1ffad726f0d635f7868899b5f79059
compatibility::simple-validator-upgrade::single-validator-upgrading : committed: 6670.64 txn/s, latency: 4040.42 ms, (p50: 4500 ms, p70: 5100, p90: 5400 ms, p99: 5600 ms), latency samples: 120960
compatibility::simple-validator-upgrade::single-validator-upgrade : committed: 7045.59 txn/s, latency: 4464.40 ms, (p50: 4500 ms, p70: 4600, p90: 7000 ms, p99: 7300 ms), latency samples: 235040
3. Upgrading rest of first batch to new version: 0f45016afc1ffad726f0d635f7868899b5f79059
compatibility::simple-validator-upgrade::half-validator-upgrading : committed: 6368.58 txn/s, latency: 4344.92 ms, (p50: 4700 ms, p70: 4900, p90: 5900 ms, p99: 6200 ms), latency samples: 124400
compatibility::simple-validator-upgrade::half-validator-upgrade : committed: 6641.22 txn/s, latency: 4807.99 ms, (p50: 4900 ms, p70: 5400, p90: 7100 ms, p99: 7400 ms), latency samples: 224000
4. upgrading second batch to new version: 0f45016afc1ffad726f0d635f7868899b5f79059
compatibility::simple-validator-upgrade::rest-validator-upgrading : committed: 9108.98 txn/s, latency: 2891.83 ms, (p50: 2800 ms, p70: 3500, p90: 4100 ms, p99: 4800 ms), latency samples: 166800
compatibility::simple-validator-upgrade::rest-validator-upgrade : committed: 8789.20 txn/s, latency: 3382.77 ms, (p50: 2800 ms, p70: 3700, p90: 6300 ms, p99: 8600 ms), latency samples: 304680
5. check swarm health
Compatibility test for b29f09f57e898d8d211c8bc3e303f6e50bba2266 ==> 0f45016afc1ffad726f0d635f7868899b5f79059 passed
Test Ok

Copy link
Contributor

✅ Forge suite realistic_env_max_load success on 0f45016afc1ffad726f0d635f7868899b5f79059

two traffics test: inner traffic : committed: 11334.96 txn/s, submitted: 11337.22 txn/s, expired: 2.26 txn/s, latency: 3494.02 ms, (p50: 3000 ms, p70: 3300, p90: 4500 ms, p99: 12900 ms), latency samples: 4309780
two traffics test : committed: 99.97 txn/s, latency: 3101.86 ms, (p50: 2400 ms, p70: 2600, p90: 5800 ms, p99: 13700 ms), latency samples: 1800
Latency breakdown for phase 0: ["QsBatchToPos: max: 0.242, avg: 0.225", "QsPosToProposal: max: 0.895, avg: 0.667", "ConsensusProposalToOrdered: max: 0.338, avg: 0.335", "ConsensusOrderedToCommit: max: 0.564, avg: 0.532", "ConsensusProposalToCommit: max: 0.898, avg: 0.866"]
Max non-epoch-change gap was: 0 rounds at version 0 (avg 0.00) [limit 4], 2.03s no progress at version 2090798 (avg 0.24s) [limit 15].
Max epoch-change gap was: 0 rounds at version 0 (avg 0.00) [limit 4], 7.22s no progress at version 2090796 (avg 7.22s) [limit 15].
Test Ok

Copy link
Contributor

✅ Forge suite framework_upgrade success on b29f09f57e898d8d211c8bc3e303f6e50bba2266 ==> 0f45016afc1ffad726f0d635f7868899b5f79059

Compatibility test results for b29f09f57e898d8d211c8bc3e303f6e50bba2266 ==> 0f45016afc1ffad726f0d635f7868899b5f79059 (PR)
Upgrade the nodes to version: 0f45016afc1ffad726f0d635f7868899b5f79059
framework_upgrade::framework-upgrade::full-framework-upgrade : committed: 1253.36 txn/s, submitted: 1257.74 txn/s, failed submission: 4.38 txn/s, expired: 4.38 txn/s, latency: 2492.69 ms, (p50: 2300 ms, p70: 2400, p90: 4200 ms, p99: 5700 ms), latency samples: 114520
framework_upgrade::framework-upgrade::full-framework-upgrade : committed: 1255.11 txn/s, submitted: 1256.95 txn/s, failed submission: 1.84 txn/s, expired: 1.84 txn/s, latency: 2514.91 ms, (p50: 2100 ms, p70: 2400, p90: 4500 ms, p99: 5800 ms), latency samples: 109060
5. check swarm health
Compatibility test for b29f09f57e898d8d211c8bc3e303f6e50bba2266 ==> 0f45016afc1ffad726f0d635f7868899b5f79059 passed
Upgrade the remaining nodes to version: 0f45016afc1ffad726f0d635f7868899b5f79059
framework_upgrade::framework-upgrade::full-framework-upgrade : committed: 919.33 txn/s, submitted: 921.08 txn/s, failed submission: 1.75 txn/s, expired: 1.75 txn/s, latency: 3288.68 ms, (p50: 2400 ms, p70: 3900, p90: 6600 ms, p99: 8400 ms), latency samples: 84060
Test Ok

@brmataptos brmataptos merged commit 4ea40fe into main Oct 17, 2024
85 of 93 checks passed
@brmataptos brmataptos deleted the brm-issue-14913 branch October 17, 2024 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug][Compiler] compiling overflows stack
5 participants