Skip to content

Commit

Permalink
whoopsie
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed Mar 26, 2024
1 parent dffe1f3 commit c863a49
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 86 deletions.
19 changes: 13 additions & 6 deletions reference/abort-and-assert.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,22 @@ <h2 id="abort"><a class="header" href="#abort"><code>abort</code></a></h2>
condition cannot be met.</p>
<p>In this example, the function will pop two items off of the vector, but will abort early if the
vector does not have two items</p>
<pre><code class="language-move=">use std::vector;
<!-- // Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
module ref::abort_and_assert {
} -->
<pre><code class="language-move">use std::vector;

fun pop_twice&lt;T&gt;(v: &amp;mut vector&lt;T&gt;): (T, T) {
if (vector::length(v) &lt; 2) abort 42;
(vector::pop_back(v), vector::pop_back(v))
}
</code></pre>
<p>This is even more useful deep inside a control-flow construct. For example, this function checks
that all numbers in the vector are less than the specified <code>bound</code>. And aborts otherwise</p>
<pre><code class="language-move=">use std::vector;
<pre><code class="language-move">use std::vector;
fun check_vec(v: &amp;vector&lt;u64&gt;, bound: u64) {
let i = 0;
let n = vector::length(v);
Expand All @@ -235,14 +242,14 @@ <h3 id="assert"><a class="header" href="#assert"><code>assert</code></a></h3>
</code></pre>
<p><code>assert</code> is more commonly used than just <code>abort</code> by itself. The <code>abort</code> examples above can be
rewritten using <code>assert</code></p>
<pre><code class="language-move=">use std::vector;
<pre><code class="language-move">use std::vector;
fun pop_twice&lt;T&gt;(v: &amp;mut vector&lt;T&gt;): (T, T) {
assert!(vector::length(v) &gt;= 2, 42); // Now uses 'assert'
(vector::pop_back(v), vector::pop_back(v))
}
</code></pre>
<p>and</p>
<pre><code class="language-move=">use std::vector;
<pre><code class="language-move">use std::vector;
fun check_vec(v: &amp;vector&lt;u64&gt;, bound: u64) {
let i = 0;
let n = vector::length(v);
Expand Down Expand Up @@ -272,7 +279,7 @@ <h3 id="abort-codes-in-the-move-vm"><a class="header" href="#abort-codes-in-the-
<li>The abort code.</li>
</ul>
<p>For example</p>
<pre><code class="language-move=">module 0x2::example {
<pre><code class="language-move">module 0x2::example {
public fun aborts() {
abort 42
}
Expand All @@ -288,7 +295,7 @@ <h3 id="abort-codes-in-the-move-vm"><a class="header" href="#abort-codes-in-the-
would produce an error that indicated the module <code>0x2::example</code> and the code <code>42</code>.</p>
<p>This can be useful for having multiple aborts being grouped together inside a module.</p>
<p>In this example, the module has two separate error codes used in multiple functions</p>
<pre><code class="language-move=">module 0x42::example {
<pre><code class="language-move">module 0x42::example {

use std::vector;

Expand Down
2 changes: 1 addition & 1 deletion reference/control-flow/loops.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ <h2 id="while-loops"><a class="header" href="#while-loops"><code>while</code> Lo
}
</code></pre>
<p>Infinite <code>while</code> loops are also allowed:</p>
<pre><code class="language-move=">fun foo() {
<pre><code class="language-move">fun foo() {
while (true) { }
}
</code></pre>
Expand Down
10 changes: 5 additions & 5 deletions reference/friends.html
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ <h3 id="friend-declaration-rules"><a class="header" href="#friend-declaration-ru
<ul>
<li>
<p>A module cannot declare itself as a friend.</p>
<pre><code class="language-move=">module 0x42::m { friend Self; // ERROR! }
<pre><code class="language-move">module 0x42::m { friend Self; // ERROR! }
// ^^^^ Cannot declare the module itself as a friend

module 0x43::m { friend 0x43::M; // ERROR! }
Expand All @@ -238,13 +238,13 @@ <h3 id="friend-declaration-rules"><a class="header" href="#friend-declaration-ru
</li>
<li>
<p>Friend modules must be known by the compiler</p>
<pre><code class="language-move=">module 0x42::m { friend 0x42::nonexistent; // ERROR! }
<pre><code class="language-move">module 0x42::m { friend 0x42::nonexistent; // ERROR! }
// ^^^^^^^^^^^^^^^^^ Unbound module '0x42::nonexistent'
</code></pre>
</li>
<li>
<p>Friend modules must be within the same account address.</p>
<pre><code class="language-move=">module 0x42::m {}
<pre><code class="language-move">module 0x42::m {}

module 0x42::n { friend 0x42::m; // ERROR! }
// ^^^^^^^ Cannot declare modules out of the current address as a friend
Expand All @@ -257,7 +257,7 @@ <h3 id="friend-declaration-rules"><a class="header" href="#friend-declaration-ru
dependency upon the current module to the friend module (because the purpose is for the friend to
call functions in the current module). If that friend module is already used, either directly or
transitively, a cycle of dependencies would be created.</p>
<pre><code class="language-move=">module 0x2::a {
<pre><code class="language-move">module 0x2::a {
use 0x2::c;
friend 0x2::b;

Expand All @@ -278,7 +278,7 @@ <h3 id="friend-declaration-rules"><a class="header" href="#friend-declaration-ru
</li>
<li>
<p>The friend list for a module cannot contain duplicates.</p>
<pre><code class="language-move=">module 0x42::a {}
<pre><code class="language-move">module 0x42::a {}

module 0x42::m {
use 0x42::a as aliased_a;
Expand Down
6 changes: 3 additions & 3 deletions reference/index-syntax.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ <h2 id="overview-and-summary"><a class="header" href="#overview-and-summary">Ove
<p>To start, consider a <code>Matrix</code> type that uses a vector of vectors to represent its values. You can
write a small library using <code>index</code> syntax annotations on the <code>borrow</code> and <code>borrow_mut</code> functions as
follows:</p>
<pre><code>module matrix {
<pre><code class="language-move">module matrix {

public struct Matrix&lt;T&gt; { v: vector&lt;vector&lt;T&gt;&gt; }

Expand All @@ -216,7 +216,7 @@ <h2 id="overview-and-summary"><a class="header" href="#overview-and-summary">Ove
}
</code></pre>
<p>Now anyone using this <code>Matrix</code> type has access to index syntax for it:</p>
<pre><code>let v0 = vector&lt;u64&gt;[1, 0, 0];
<pre><code class="language-move">let v0 = vector&lt;u64&gt;[1, 0, 0];
let v1 = vector&lt;u64&gt;[0, 1, 0];
let v2 = vector&lt;u64&gt;[0, 0, 1];
let v = vector&lt;vector&lt;u64&gt;&gt;[v0, v1, v2];
Expand Down Expand Up @@ -274,7 +274,7 @@ <h3 id="index-functions-take-flexible-arguments"><a class="header" href="#index-
Move places no restrictions on the values your index syntax method takes as parameters. This allows
you to implement intricate programmatic behavior when defining index syntax, such as a data
structure that takes a default value if the index is out of bounds:</p>
<pre><code>#[syntax(index)]
<pre><code class="language-move">#[syntax(index)]
public fun borrow_or_set&lt;Key: copy, Value: drop&gt;(
input: &amp;mut MTable&lt;Key, Value&gt;,
key: &amp;Key,
Expand Down
38 changes: 19 additions & 19 deletions reference/packages.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ <h1 id="packages"><a class="header" href="#packages">Packages</a></h1>
<h2 id="package-layout-and-manifest-syntax"><a class="header" href="#package-layout-and-manifest-syntax">Package Layout and Manifest Syntax</a></h2>
<p>A Move package source directory contains a <code>Move.toml</code> package manifest file, a
generated <code>Move.lock</code> file, and a set of subdirectories:</p>
<pre><code>a_move_package
<pre><code class="language-plaintext">a_move_package
├── Move.toml (required)
├── Move.lock (generated)
├── sources (required)
Expand Down Expand Up @@ -234,7 +234,7 @@ <h2 id="package-layout-and-manifest-syntax"><a class="header" href="#package-lay
<h3 id="movetoml"><a class="header" href="#movetoml">Move.toml</a></h3>
<p>The Move package manifest is defined within the <code>Move.toml</code> file and has the following syntax.
Optional fields are marked with <code>*</code>, <code>+</code> denotes one or more elements:</p>
<pre><code>[package]
<pre><code class="language-ini">[package]
name = &lt;string&gt;
edition* = &lt;string&gt; # e.g., "2024.alpha" to use the Move 2024 edition,
# currently in alpha. Will default to the latest stable edition if not specified.
Expand All @@ -244,7 +244,7 @@ <h3 id="movetoml"><a class="header" href="#movetoml">Move.toml</a></h3>
# Additional fields may be added to this section by external tools. E.g., on Sui the following sections are added:
published-at* = "&lt;hex-address&gt;" # The address that the package is published at. Should be set after the first publication.

[dependencies] # (Optional section) Paths to dependencies
[dependencies] # (Optional section) Paths to dependencies
# One or more lines declaring dependencies in the following format

# ##### Local Dependencies #####
Expand All @@ -254,18 +254,18 @@ <h3 id="movetoml"><a class="header" href="#movetoml">Move.toml</a></h3>
# override you can use `override = true`
# Override = { local = "../conflicting/version", override = true }
# To instantiate address values in a dependency, use `addr_subst`
&lt;string&gt; = {
&lt;string&gt; = {
local = &lt;string&gt;,
override* = &lt;bool&gt;,
addr_subst* = { (&lt;string&gt; = (&lt;string&gt; | "&lt;hex_address&gt;"))+ }
addr_subst* = { (&lt;string&gt; = (&lt;string&gt; | "&lt;hex_address&gt;"))+ }
}

# ##### Git Dependencies #####
# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision must be supplied, it can be a branch, a tag, or a commit hash.
# If no `subdir` is specified, the root of the repository is used.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }
&lt;string&gt; = {
&lt;string&gt; = {
git = &lt;URL ending in .git&gt;,
subdir=&lt;path to dir containing Move.toml inside git repo&gt;,
rev=&lt;git commit hash&gt;,
Expand All @@ -286,12 +286,12 @@ <h3 id="movetoml"><a class="header" href="#movetoml">Move.toml</a></h3>
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can e.g., introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }
&lt;string&gt; = {
&lt;string&gt; = {
local = &lt;string&gt;,
override* = &lt;bool&gt;,
addr_subst* = { (&lt;string&gt; = (&lt;string&gt; | "&lt;hex_address&gt;"))+ }
addr_subst* = { (&lt;string&gt; = (&lt;string&gt; | "&lt;hex_address&gt;"))+ }
}
&lt;string&gt; = {
&lt;string&gt; = {
git = &lt;URL ending in .git&gt;,
subdir=&lt;path to dir containing Move.toml inside git repo&gt;,
rev=&lt;git commit hash&gt;,
Expand All @@ -305,12 +305,12 @@ <h3 id="movetoml"><a class="header" href="#movetoml">Move.toml</a></h3>
&lt;addr_name&gt; = "&lt;hex_address&gt;" # e.g., alice = "0xB0B"
</code></pre>
<p>An example of a minimal package manifest:</p>
<pre><code>[package]
<pre><code class="language-ini">[package]
name = "AName"
</code></pre>
<p>An example of a more standard package manifest that also includes the Move standard library and
instantiates the named address <code>std</code> from the <code>LocalDep</code> package with the address value <code>0x1</code>:</p>
<pre><code>[package]
<pre><code class="language-ini">[package]
name = "AName"
license = "Apache 2.0"

Expand Down Expand Up @@ -363,7 +363,7 @@ <h3 id="the-move-section"><a class="header" href="#the-move-section">The <code>[
present, this will be an empty string.</li>
<li>The list of dependencies.</li>
</ul>
<pre><code>[move]
<pre><code class="language-ini">[move]
version = &lt;string&gt; # Lock file version, used for backwards compatibility checking.
manifest_digest = &lt;hash&gt; # Sha3-256 hash of the Move.toml file that was used to generate this lock file.
deps_digest = &lt;hash&gt; # Sha3-256 hash of the Move.lock file of all dependencies. If no depencies are present, this will be an empty string.
Expand All @@ -376,7 +376,7 @@ <h3 id="the-movepackage-sections"><a class="header" href="#the-movepackage-secti
build fails. If all dependencies resolve, the <code>Move.lock</code> file contains the
locations (local and remote) of all of the package's transitive dependencies.
These will be stored in the <code>Move.lock</code> file in the following format:</p>
<pre><code>...
<pre><code class="language-ini"># ...

[[move.package]]
name = "A"
Expand All @@ -390,7 +390,7 @@ <h3 id="the-movetoolchain-version-section"><a class="header" href="#the-movetool
<p>As mentioned above, additional fields may be added to the lock file by external
tools. For example, the Sui package manager adds toolchain version information
to the lock file that can then be used for on-chain source verification:</p>
<pre><code>...
<pre><code class="language-ini"># ...

[move.toolchain-version]
compiler-version = &lt;string&gt; # The version of the Move compiler used to build the package, e.g. "1.21.0"
Expand All @@ -415,7 +415,7 @@ <h3 id="declaring-named-addresses"><a class="header" href="#declaring-named-addr
</code></pre>
<p>We could in <code>example_pkg/Move.toml</code> declare the named address <code>named_addr</code> in two different ways.
The first:</p>
<pre><code>[package]
<pre><code class="language-ini">[package]
name = "example_pkg"
...
[addresses]
Expand All @@ -427,7 +427,7 @@ <h3 id="declaring-named-addresses"><a class="header" href="#declaring-named-addr
package <code>example_pkg</code> by the named address <code>named_addr</code>, and the package can then be instantiated
later on by an importing package.</p>
<p><code>named_addr</code> can also be declared as:</p>
<pre><code>[package]
<pre><code class="language-ini">[package]
name = "example_pkg"
...
[addresses]
Expand Down Expand Up @@ -465,7 +465,7 @@ <h2 id="scope-and-renaming-of-named-addresses"><a class="header" href="#scope-an
package that brings them into scope is imported.</p>
<p>Renaming a named address when importing can be done as follows in our <code>P</code>, <code>P1</code>, and <code>P2</code> example
above:</p>
<pre><code>[package]
<pre><code class="language-ini">[package]
name = "P"
...
[dependencies]
Expand All @@ -492,7 +492,7 @@ <h3 id="instantiating-named-addresses"><a class="header" href="#instantiating-na
addresses. Additionally, only the <code>[dev-addresses]</code> in the root package are included in <code>dev</code> mode.
For example a root package with the following manifest would not compile outside of <code>dev</code> mode since
<code>named_addr</code> would be uninstantiated:</p>
<pre><code>[package]
<pre><code class="language-ini">[package]
name = "example_pkg"
...
[addresses]
Expand All @@ -511,7 +511,7 @@ <h3 id="artifacts"><a class="header" href="#artifacts">Artifacts</a></h3>
This will create a <code>build</code>
directory containing build-related artifacts (such as bytecode, source maps, and
documentation). The general layout of the <code>build</code> directory is as follows:</p>
<pre><code>a_move_package
<pre><code class="language-plaintext">a_move_package
├── BuildInfo.yaml
├── bytecode_modules
│   ├── dependencies
Expand Down
6 changes: 3 additions & 3 deletions reference/primitive-types/references.html
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ <h2 id="reading-and-writing-through-references"><a class="header" href="#reading
<p>In order for a reference to be read, the underlying type must have the
<a href="../abilities.html"><code>copy</code> ability</a> as reading the reference creates a new copy of the value. This
rule prevents the copying of assets:</p>
<pre><code class="language-move=">fun copy_coin_via_ref_bad(c: Coin) {
<pre><code class="language-move">fun copy_coin_via_ref_bad(c: Coin) {
let c_ref = &amp;c;
let counterfeit: Coin = *c_ref; // not allowed!
pay(c);
Expand All @@ -256,7 +256,7 @@ <h2 id="freeze-inference"><a class="header" href="#freeze-inference"><code>freez
</code></pre>
<p>This works because the under the hood, the compiler inserts <code>freeze</code> instructions where they are
needed. Here are a few more examples of <code>freeze</code> inference in action:</p>
<pre><code class="language-move=">fun takes_immut_returns_immut(x: &amp;u64): &amp;u64 { x }
<pre><code class="language-move">fun takes_immut_returns_immut(x: &amp;u64): &amp;u64 { x }

// freeze inference on return value
fun takes_mut_returns_immut(x: &amp;mut u64): &amp;u64 { x }
Expand Down Expand Up @@ -285,7 +285,7 @@ <h3 id="subtyping"><a class="header" href="#subtyping">Subtyping</a></h3>
above, this means that anywhere for any expression where a <code>&amp;T</code> value is used, a <code>&amp;mut T</code> value can
also be used. This terminology is used in error messages to concisely indicate that a <code>&amp;mut T</code> was
needed where a <code>&amp;T</code> was supplied. For example</p>
<pre><code class="language-move=">module a::example {
<pre><code class="language-move">module a::example {
fun read_and_assign(store: &amp;mut u64, new_value: &amp;u64) {
*store = *new_value
}
Expand Down
Loading

0 comments on commit c863a49

Please sign in to comment.