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][type checker] Make arithmetic operators generic with constraints #9792

Merged
merged 3 commits into from
Sep 1, 2023

Conversation

wrwg
Copy link
Contributor

@wrwg wrwg commented Aug 27, 2023

Description

Until now, we had represented arithmetic operators via overloads. For instance, we had fun _+_(u8,u8):u8, fun _+_(u16,u16):u16 and so on for all integer types. The problem with this approach is that it does not work for constants, as described in #9330. Move allows to write 1+2 and this expression behaves from the typing perspective like 3, which has a type which is automatically adapted to the usage context.

In this PR, arithmetic ops are represented not longer as overloads but as generic functions with constraints. For this, the already existing SomeNumber(..) type constraint is used. Basically:

fun _+_<A>(x: A, y: A): A where A is SomeNumber({u8,u16,u32,..,u256});
fun _-_<A>(...)...

This has the following effects:

  • Expressions over constants inherit their generic result type (that is, 1 + 2 does not fix the operator to a specific integer type. Instead the context may do this.)
  • Error messages get better. Before we just printed non-matching overloads, now we see more precise error messages.

Technically, this needed some refinements of the type constraint system, which brings us even closer to a type checker supporting traits/type classes. Also, some error reporting has been fixed which was before only working in very specific contexts.

Test Plan

Updates baselines files, adds a new test for constant expression type inference

@wrwg wrwg marked this pull request as draft August 27, 2023 06:17
@wrwg wrwg force-pushed the wg-ints branch 3 times, most recently from d9b0fb9 to 3678009 Compare August 28, 2023 04:21
@wrwg wrwg marked this pull request as ready for review August 28, 2023 04:27
@wrwg wrwg requested a review from vineethk August 28, 2023 17:05
Copy link
Contributor

@brmataptos brmataptos left a comment

Choose a reason for hiding this comment

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

Looks pretty nice.

@wrwg
Copy link
Contributor Author

wrwg commented Aug 31, 2023

I attempted to fix some of the orientation errors (where it appears 'expected' is wrong wr.t. 'found'), and in course of this removed some heuristics which appeared to be broken. Those heuristics have been added in the last review round on pressure of reviewers. Please note: error messages are not perfect and this is not the objective at this point. Specifically, in a contra-variance scenario (when assigning values), the expected vs found might be now a bit confusing again, but at least I hope it is correct.

@brmataptos
Copy link
Contributor

I filed a bug to follow up on the clarity of some of these error messages.

Copy link
Contributor

@brmataptos brmataptos left a comment

Choose a reason for hiding this comment

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

I'm ok with this for now, although I filed 2 followup issues.

Copy link
Contributor

@rahxephon89 rahxephon89 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@wrwg wrwg force-pushed the wg-ints branch 2 times, most recently from be9262c to 1594409 Compare August 31, 2023 20:27
@wrwg wrwg enabled auto-merge (squash) August 31, 2023 23:25
wrwg added 3 commits August 31, 2023 16:28
…nstraints

Until now, we had represented arithmetic operators via overloads. For instance, we had `fun _+_(u8,u8):u8, fun _+_(u16,u16):u16` and so on for all integer types. The problem with this approach is that it does not work for constants, as described in #9330. Move allows to write `1+2` and this expression gets its type inferred from the context.

In this PR, arithmetic ops are represented not longer as overloads but as generic functions with constraints. Basically:

```
fun _+_<A>(x: A, y: A): A where A:u8|u16|u32|..|u256;
fun _-_<A>(...)...
```

This has the following effects:

- Expressions over constants inherit their generic result type (that is, `1 + 2` does not fix the operator to a specific integer type. Instead the context may do this.)
- Error messages get better. The messages where we just printed non-matching overloads were not very good, no we see more precise error messages.

Technically, this needed some refinements of the type constraint system. Also, some error reporting has been fixed which was before only working in very specific contexts.
…or messages, removing some older broken heuristics and trying to fix the problem of orientation of 'expected' vs 'found' at the core. Some context annotation 'from assignment context' not tries to clarify the error message if the typing error is for an lvalue, where the expected type is a _lower bound_ instead of an upper bound, which may leads to confusion for end users. The messages are now more systematic but perhaps a bit harder to understand, yet this is not trivial to fix and we have to iterate on this.
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2023

✅ Forge suite compat success on aptos-node-v1.6.2 ==> 13620be1d42d7cd94f9d32e8249e216b6b433fae

Compatibility test results for aptos-node-v1.6.2 ==> 13620be1d42d7cd94f9d32e8249e216b6b433fae (PR)
1. Check liveness of validators at old version: aptos-node-v1.6.2
compatibility::simple-validator-upgrade::liveness-check : committed: 4524 txn/s, latency: 6701 ms, (p50: 6900 ms, p90: 9000 ms, p99: 11100 ms), latency samples: 180980
2. Upgrading first Validator to new version: 13620be1d42d7cd94f9d32e8249e216b6b433fae
compatibility::simple-validator-upgrade::single-validator-upgrade : committed: 1773 txn/s, latency: 15986 ms, (p50: 18900 ms, p90: 22000 ms, p99: 22500 ms), latency samples: 92220
3. Upgrading rest of first batch to new version: 13620be1d42d7cd94f9d32e8249e216b6b433fae
compatibility::simple-validator-upgrade::half-validator-upgrade : committed: 1770 txn/s, latency: 16328 ms, (p50: 18700 ms, p90: 22300 ms, p99: 22600 ms), latency samples: 92060
4. upgrading second batch to new version: 13620be1d42d7cd94f9d32e8249e216b6b433fae
compatibility::simple-validator-upgrade::rest-validator-upgrade : committed: 3358 txn/s, latency: 9371 ms, (p50: 10500 ms, p90: 12900 ms, p99: 13600 ms), latency samples: 134340
5. check swarm health
Compatibility test for aptos-node-v1.6.2 ==> 13620be1d42d7cd94f9d32e8249e216b6b433fae passed
Test Ok

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2023

✅ Forge suite realistic_env_max_load success on 13620be1d42d7cd94f9d32e8249e216b6b433fae

two traffics test: inner traffic : committed: 6702 txn/s, latency: 5853 ms, (p50: 5700 ms, p90: 7500 ms, p99: 11100 ms), latency samples: 2895620
two traffics test : committed: 100 txn/s, latency: 3197 ms, (p50: 3100 ms, p90: 3800 ms, p99: 5300 ms), latency samples: 1780
Latency breakdown for phase 0: ["QsBatchToPos: max: 0.226, avg: 0.213", "QsPosToProposal: max: 0.156, avg: 0.142", "ConsensusProposalToOrdered: max: 0.617, avg: 0.592", "ConsensusOrderedToCommit: max: 0.539, avg: 0.519", "ConsensusProposalToCommit: max: 1.141, avg: 1.111"]
Max round gap was 1 [limit 4] at version 1486377. Max no progress secs was 3.71948 [limit 10] at version 1486377.
Test Ok

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2023

✅ Forge suite framework_upgrade success on aptos-node-v1.5.1 ==> 13620be1d42d7cd94f9d32e8249e216b6b433fae

Compatibility test results for aptos-node-v1.5.1 ==> 13620be1d42d7cd94f9d32e8249e216b6b433fae (PR)
Upgrade the nodes to version: 13620be1d42d7cd94f9d32e8249e216b6b433fae
framework_upgrade::framework-upgrade::full-framework-upgrade : committed: 4849 txn/s, latency: 6707 ms, (p50: 6900 ms, p90: 9900 ms, p99: 10800 ms), latency samples: 174580
5. check swarm health
Compatibility test for aptos-node-v1.5.1 ==> 13620be1d42d7cd94f9d32e8249e216b6b433fae passed
Test Ok

@wrwg wrwg merged commit 6a1cded into main Sep 1, 2023
@wrwg wrwg deleted the wg-ints branch September 1, 2023 02:58
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.

3 participants