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

Show deprecation on arithmetic word operations #2339

Merged
merged 57 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
0e0b447
Value.Numerics: Refactoring to prepare word deprecation
nomeata Feb 9, 2021
42b046c
Forgotten
nomeata Feb 9, 2021
79a92ab
Keep mo:prim compatible
nomeata Feb 9, 2021
a0c9fd7
Update test output
nomeata Feb 9, 2021
095b32d
Fix of_string for words
nomeata Feb 9, 2021
c36a8d4
Add bit-operations to nat and int, and wrapping operations to all
nomeata Feb 9, 2021
8ff5032
Fix wrong name for Nat64Wrap primop
nomeata Feb 9, 2021
7cd401f
Update src/mo_values/operator.ml
nomeata Feb 9, 2021
68af7c7
Forgot to actually support them operators
nomeata Feb 9, 2021
b040f02
Empty line be gone
nomeata Feb 9, 2021
806ba92
Update src/codegen/compile.ml
nomeata Feb 9, 2021
9514c35
Use right PowOp for Words
nomeata Feb 9, 2021
ce49293
Do not even begin to support +>> on IntN and NatN
nomeata Feb 9, 2021
36fb907
Shorter prefix: Just w
nomeata Feb 9, 2021
2e9e8a2
Fixup shorter prefix
nomeata Feb 9, 2021
60b8293
Update src/mo_values/numerics.ml
nomeata Feb 9, 2021
178da47
Merge remote-tracking branch 'origin/joachim/value-refactor' into joa…
nomeata Feb 9, 2021
f90a256
Simplify num_conv_wrap_prim
nomeata Feb 9, 2021
37f4399
Merge branch 'joachim/value-refactor' into joachim/new-bit-ops
nomeata Feb 9, 2021
2d8e324
Typos
nomeata Feb 9, 2021
1a3e6e1
Merge branch 'joachim/value-refactor' into joachim/new-bit-ops
nomeata Feb 9, 2021
c211c5d
Update src/lowering/desugar.ml
nomeata Feb 10, 2021
3a0ab52
Update src/lowering/desugar.ml
nomeata Feb 10, 2021
3e5959a
Merge branch 'master' of github.com:dfinity/motoko into joachim/value…
nomeata Feb 10, 2021
159a47b
Merge branch 'joachim/value-refactor' into joachim/new-bit-ops
nomeata Feb 10, 2021
a35e51c
Post-squash merge of origin/master
nomeata Feb 10, 2021
4e74f44
Changelog
nomeata Feb 10, 2021
ac3c889
Cherry-pick users’s guide
nomeata Feb 10, 2021
735c0f4
Deleted too much
nomeata Feb 10, 2021
c84c04f
Unrelated typo
nomeata Feb 10, 2021
dd6ef23
Already provide charToNat32/nat32ToChar in prim
nomeata Feb 10, 2021
53254a2
Merge branch 'master' of github.com:dfinity/motoko into joachim/new-b…
nomeata Feb 10, 2021
6fd6d70
Merge branch 'master' into joachim/new-bit-ops
nomeata Feb 10, 2021
19ab1b1
Merge branch 'master' into joachim/new-bit-ops
nomeata Feb 12, 2021
99d4086
Show deprecation on arithmetic word operations
nomeata Feb 12, 2021
2aa9b73
Merge branch 'joachim/new-bit-ops' into joachim/warn-on-word-ops
nomeata Feb 12, 2021
baa7b0b
Update test output
nomeata Feb 12, 2021
2bfd78c
Use wrapping ops on Word in numeric-ops.mo
nomeata Feb 12, 2021
ad13a79
More test suite updates
nomeata Feb 12, 2021
220bbbd
User’s guide: Migration comment (and changelog)
nomeata Feb 12, 2021
bfdf241
More test suite updates
nomeata Feb 12, 2021
e347a66
Apply suggestions from code review
nomeata Feb 12, 2021
52b005a
Update Changelog.md
nomeata Feb 12, 2021
ebfca8d
Also give deprecation warning in checking mode
nomeata Feb 12, 2021
c9f4eab
Typo in error_reporting.ml
nomeata Feb 12, 2021
76c5ef7
More copy’n’pasta (and bad testing coverage…)
nomeata Feb 12, 2021
07259ae
Merge branch 'joachim/new-bit-ops' into joachim/warn-on-word-ops
nomeata Feb 12, 2021
693a117
Update doc/overview-slides.md
nomeata Feb 12, 2021
b462eb3
More test suite updates
nomeata Feb 12, 2021
73512d6
Update perf tests
nomeata Feb 12, 2021
e5c1f01
Update doc/modules/language-guide/pages/basic-concepts.adoc
nomeata Feb 12, 2021
7e1a2ef
Update Changelog.md
nomeata Feb 12, 2021
ed4ed69
Apply suggestions from code review
nomeata Feb 12, 2021
4b50977
Merge branch 'master' into joachim/new-bit-ops
mergify[bot] Feb 12, 2021
b0f83a1
Merge branch 'joachim/new-bit-ops' into joachim/warn-on-word-ops
nomeata Feb 12, 2021
7a3c39d
Remove more code fences (```)
nomeata Feb 12, 2021
1a73d0e
Post-squash merge of origin/master
nomeata Feb 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
= Motoko compiler changelog

* `mo-doc` now generates cross-references for types in signatures in
both the Html as well as the Asciidoc output. So a signature like
`fromIter : I.Iter<Nat> -> List.List<Nat>` will now let you click on
`I.Iter` or `List.List` and take you to their definitions.
* Wrapping arithmetic and bit-wise operations on `NatN` and `IntN`

The conventional arithmetic operators on `NatN` and `IntN` trap on overflow.
If wrapping semantics is desired, the operators `+%`, `-%`, `*%` and `**%`
can be used.

Likewise, the bit fidddling operators (`&`, `|`, `^`, `<<`, `>>`, `<<>`,
`<>>` etc.) are now also available on `NatN` and `IntN`. The right shift
operator (`>>`) is a unsigned right shift on `NatN` and a signed right shift
on `IntN`; the `+>>` operator is _not_ available on these types.

The motivation for this change is to eventually deprecate and remove the
`WordN` types.

* For values `x` of type `Blob`, an iterator over the elements of the blob
`x.vals()` is introduced. It works like `x.bytes()`, but returns the elements
as type `Nat8`.

* `mo-doc` now generates cross-references for types in signatures in
both the Html as well as the Asciidoc output. So a signature like
`fromIter : I.Iter<Nat> -> List.List<Nat>` will now let you click on
`I.Iter` or `List.List` and take you to their definitions.

* Bugfix: Certain ill-typed object literals are now prevented by the type
checker.

Expand Down
8 changes: 8 additions & 0 deletions doc/modules/language-guide/examples/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
'/'
'%'
'**'
'+%'
'-%'
'*%'
'**%'
'&'
'|'
'^'
Expand Down Expand Up @@ -117,6 +121,10 @@
'/='
'%='
'**-'
'+%='
'-%='
'*%='
'**%='
'&='
'|='
'^='
Expand Down
6 changes: 2 additions & 4 deletions doc/modules/language-guide/pages/basic-concepts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,13 @@ As a broader language overview, however, we briefly summarize the other value fo
- Text values --- strings of unicode characters.
- Words --- fixed-width numbers, _without_ overflow checks, and _with_ explicit wrap-around semantics.

*Numbers.* By default, integers and natural numbers are _unbounded_ and do not overflow.
*Numbers.* By default, integers and natural numbers are _unbounded_ and do not overflow.
Instead, they use representations that grow to accommodate any finite number.

For practical reasons, {proglang} also includes _bounded_ types for integers and natural numbers, distinct from the default versions.
Each bounded variant has a fixed width (one of `8`, `16`, `32`, `64`) and each carries the potential for "`overflow`". If and when this event occurs, it is an error and causes the
<<overview-traps,program to trap>>.
There are no unchecked, uncaught overflows in {proglang}, except in well-defined situations, for specific (`Word`-based) types.

Word types permit bitwise operations that are unsupported by the other number types.
There are no unchecked, uncaught overflows in {proglang}, except in well-defined situations, for explicitly _wrapping_ operations and specific (`Word`-based) types.
The language provides primitive built-ins to convert between these various number representations.

The link:language-manual{outfilesuffix}[language quick reference] contains a complete list of link:language-manual{outfilesuffix}#primitive-types[primitive types].
Expand Down
58 changes: 48 additions & 10 deletions doc/modules/language-guide/pages/language-manual.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ To simplify the presentation of available operators, operators and primitive typ

| A | Arithmetic | arithmetic operations
| L | Logical | logical/Boolean operations
| B | Bitwise | bitwise operations
| B | Bitwise | bitwise and wrapping operations
| O | Ordered | comparison
| T | Text | concatenation
|===
Expand Down Expand Up @@ -269,7 +269,7 @@ Equality and inequality are structural and based on the observable content of th
|===

[[syntax-ops-bitwise]]
=== Bitwise binary operators
=== Bitwise and wrapping binary operators

|===
| `<binop>` | Category |
Expand All @@ -279,9 +279,14 @@ Equality and inequality are structural and based on the observable content of th
| `^` | B | exclusive or
| `<<` | B | shift left
| `␣>>` | B | shift right *(must be preceded by whitespace)*
| `+>>` | B | signed shift right
| `+>>` | B | signed shift right (only on `Word`-types)
| `<<>` | B | rotate left
| `<>>` | B | rotate right

| `+%` | A | addition (wrap-on-overflow)
| `-%` | A | subtraction (wrap-on-overflow)
| `*%` | A | multiplication (wrap-on-overflow)
| `**%`| A | exponentiation (wrap-on-overflow)
|===

[[syntax-ops-string]]
Expand Down Expand Up @@ -311,9 +316,13 @@ Equality and inequality are structural and based on the observable content of th
| `^=` | B | in place exclusive or
| `<\<=` | B | in place shift left
| `>>=` | B | in place shift right
| `+>>=` | B | in place signed shift right
| `+>>=` | B | in place signed shift right (only on `Word`-types)
| `<<>=` | B | in place rotate left
| `<>>=` | B | in place rotate right
| `+%=` | B | in place add (wrap-on-overflow)
| `-%=` | B | in place subtract (wrap-on-overflow)
| `*%=` | B | in place multiply (wrap-on-overflow)
| `**%=` | B | in place exponentiation (wrap-on-overflow)
| `#=` | T | in place concatenation
|===

Expand All @@ -330,18 +339,18 @@ Tokens on the same line have equal precedence with the indicated associativity.

| LOWEST | none | `if _ _` (no `else`), `loop _` (no `while`)
|(higher)| none | `else`, `while`
|(higher)| right | `:=`, `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `#=`, `&=`, `\|=`, `^=`, `<\<=`, `>>-`, `<<>=`, `<>>=`
|(higher)| right | `:=`, `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `#=`, `&=`, `\|=`, `^=`, `<\<=`, `>>=`, `<<>=`, `<>>=`, `+%=`, `-%=`, `*%=`, `**%=`,`
|(higher)| left | `:`
|(higher)| left | `or`
|(higher)| left | `and`
|(higher)| none | `==`, `!=`, `<`, `>`, `\<=`, `>`, `>=`
|(higher)| left | `+`, `-`, `#`
|(higher)| left | `*`, `/`, `%`
|(higher)| left | `+`, `-`, `#`, `+%`, `-%`
|(higher)| left | `*`, `/`, `%`, `*%`
|(higher)| left | `\|`
|(higher)| left | `+&+`
|(higher)| left | `+^+`
|(higher)| none | `<<`, `>>`, `<<>`, `<>>`
| HIGHEST | left | `+**+`
| HIGHEST | left | `+**+`, `+**%+`
|===


Expand Down Expand Up @@ -684,19 +693,48 @@ In particular, every value of type `Nat` is also a value of type `Int`, without

The types `Int8`, `Int16`, `Int32` and `Int64` represent
signed integers with respectively 8, 16, 32 and 64 bit precision.
All have categories A (Arithmetic) and O (Ordered).
All have categories A (Arithmetic), B (Bitwise) and O (Ordered).

Operations that may under- or overflow the representation are checked and trap on error.

The operations `+%`, `-%`, `*%` and `**%` provide access to the raw wrap-on-overflow operations.

As bitwise types, these types support bitwise operations *and* `(&)`,
*or* `(|)` and *exclusive-or* `(^)`. Further, they can be rotated
left `(<<>)`, right `(<>>)`, and shifted left `(<<)`, right `(>>)`.
The right-shift preserves the two's-complement sign.
All shift and rotate amounts are considered modulo the word's width *n*.

Bounded integer types are not in subtype relationship with each other or with
other arithmetic types, and their literals need type annotation, e.g.
`(-42 : Int16)`.

The corresponding module in the base library provides conversion functions:
Conversion to `Int`, checked and wrapping conversions from `Int` and wrapping
conversion to the bounded natural type of the same size.


[[bounded-naturals]]
=== Bounded naturals `Nat8`, `Nat16`, `Nat32` and `Nat64`

The types `Nat8`, `Nat16`, `Nat32` and `Nat64` represent
unsigned integers with respectively 8, 16, 32 and 64 bit precision.
All have categories A (Arithmetic) and O (Ordered).
All have categories A (Arithmetic), B (Bitwise) and O (Ordered).

Operations that may under- or overflow the representation are checked and trap on error.

The operations `+%`, `-%`, `*%` and `**%` provide access to the raw wrap-on-overflow operations.

As bitwise types, these types support bitwise operations *and* `(&)`,
*or* `(|)` and *exclusive-or* `(^)`. Further, they can be rotated
left `(<<>)`, right `(<>>)`, and shifted left `(<<)`, right `(>>)`.
The right-shift is logical.
All shift and rotate amounts are considered modulo the word's width *n*.

The corresponding module in the base library provides conversion functions:
Conversion to `Int`, checked and wrapping conversions from `Int` and wrapping
conversion to the bounded natural type of the same size.

[[word-types]]
=== Word types

Expand Down
4 changes: 2 additions & 2 deletions doc/modules/language-guide/pages/modules-and-imports.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ This distinction can affect how some data structures are typed.

For the imported canister actor, types are derived from the Candid file—the _project-name_.did file—for the canister rather than from {proglang} itself.

The translation from {proglang} actor type to Candid service type is mostly, but not entirely, one-to-one, and there are some distinct {proglang} types that map to the same Candid type. For example, the {proglang} `Nat8` and `Word8` types both exported as Candid type `nat8`, but `nat8` is canonically imported as {proglang} `Nat8`, not `Word8`.
The translation from {proglang} actor type to Candid service type is mostly, but not entirely, one-to-one, and there are some distinct {proglang} types that map to the same Candid type. For example, the {proglang} `Nat32` and `Char` types both exported as Candid type `nat32`, but `nat32` is canonically imported as {proglang} `Nat32`, not `Char`.

The type of an imported canister function, therefore, might differ from the type of the original {proglang} code that implements it.
For example, if the {proglang} function had type `+shared Word8 -> async Word64+` in the implementation, its exported Candid type would be `+(Nat8) -> Nat64+` but the {proglang} type imported from this Candid type will actually be the correct—but perhaps unexpected—type `+shared Nat8 -> async Nat64+`.
For example, if the {proglang} function had type `+shared Nat32 -> async Char+` in the implementation, its exported Candid type would be `+(nat32) -> (nat32)+` but the {proglang} type imported from this Candid type will actually be the correct—but perhaps unexpected—type `+shared Nat32 -> async Nat32+`.

These type differences are the result of the Candid-to-{proglang} composition layer inherent to the canister abstraction.

Expand Down
17 changes: 4 additions & 13 deletions doc/overview-slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,11 @@ Literals: `13`, `0xf4`, `1_000_000`

## Bounded numbers (trapping)

`Nat8`, `Nat16`, `Nat32`, `Nat64`,
`Nat8`, `Nat16`, `Nat32`, `Nat64`,
`Int8`, `Int16`, `Int32`, `Int64`

Trap on over- and underflow.

Needs type annotations (somewhere)

Literals: `13`, `0xf4`, `-20`, `1_000_000`

## Bounded numbers (wrapping)

`Word8`, `Word16`, `Word32`, `Word64`

Wrap-around on over/under-flow. Use for bit-fiddling.
Trap on over- and underflow. Wrap-on-trap and bit-fiddling operations available.

Needs type annotations (somewhere)

Expand Down Expand Up @@ -531,10 +522,10 @@ let ? name = d.find(1);
### Language prelude

* connects internal primitives with surface syntax (types, operations)
* conversions like `intToWord32`
* conversions like `intToNat32`
* side-effecting operations `debugPrintInt`
(tie into execution environment)
* utilities like `hashInt`, `clzWord32`
* utilities like `hashInt`, `clzNat32`


# Sample App
Expand Down
Loading