Draconyx

Blog

The same mistakes, in Rust and in Draconyx

Six everyday mistakes, each made once in Rust and once in Draconyx, with what each compiler says about them.

diagnosticstooling

Every programmer makes the same few mistakes every day: a name typed wrong, a value of the wrong type, a case left out. What matters is what the compiler says next, and how quickly it gets you, or the model writing the code, back on track.

Here are six such mistakes, each written once in Rust and once in Draconyx. First (on the left, on a wide screen) is Rust's compiler, rustc 1.98.0, exactly as it prints in a terminal, run with the command shown. Then (on the right) is drx, the Draconyx toolchain.

drx reports each problem as one block. A bar in the problem's colour runs down its left side, and a badge says what kind it is: an error, a warning, a note or a suggestion. Then come the line, with the spot marked; what is wrong, in a sentence that uses your own names; why; and what to do. The reference code comes last, in grey, for whoever wants more. One line at the end counts the problems and says whether the package was built.

1. A misspelled name

A parameter called quantity, used as quantitty.

Rust

/// Works out what `quantity` items at `price` cost.
pub fn total(price: u32, quantity: u32) -> u32 {
    price * quantitty
}

rustc says:

$ rustc --edition 2024 --crate-type lib shop.rs

error[E0425]: cannot find value `quantitty` in this scope
 --> shop.rs:3:13
  |
3 |     price * quantitty
  |             ^^^^^^^^^
  |
help: a local variable with a similar name exists
  |
3 -     price * quantitty
3 +     price * quantity
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0425`.

Draconyx

module shop

""" Works out what `quantity` items at `price` cost.

    * price: the price of one item
    * quantity: how many items are bought
"""
total(price: Int, quantity: Int) -> Int:
    price * quantitty

drx says:

$ drx check shop

 ERROR   src/shop.ax, line 9

    price * quantitty
            ^^^^^^^^^

quantitty is not a name this function knows.

total has the parameters price and quantity, and nothing
else in reach is called quantitty.

Did you mean quantity, or quantize from std.tensor?

(AX0234. For more, run: drx explain AX0234)

1 error in 1 file. The package was not built.

Both compilers find the name and suggest quantity; drx also names a function of that spelling in the standard library.

2. A value of the wrong type

A price written as text where a number is expected.

Rust

/// Works out what `quantity` items at `price` cost.
pub fn total(price: u32, quantity: u32) -> u32 {
    price * quantity
}

/// Works out the cost of three items at 12.
pub fn checkout() -> u32 {
    total("12", 3)
}

rustc says:

$ rustc --edition 2024 --crate-type lib shop.rs

error[E0308]: mismatched types
 --> shop.rs:8:11
  |
8 |     total("12", 3)
  |     ----- ^^^^ expected `u32`, found `&str`
  |     |
  |     arguments to this function are incorrect
  |
note: function defined here
 --> shop.rs:2:8
  |
2 | pub fn total(price: u32, quantity: u32) -> u32 {
  |        ^^^^^ ----------

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

Draconyx

module shop

""" Works out what `quantity` items at `price` cost.

    * price: the price of one item
    * quantity: how many items are bought
"""
total(price: Int, quantity: Int) -> Int:
    price * quantity

""" Works out the cost of three items at 12. """
checkout() -> Int:
    total(price = "12", quantity = 3)

drx says:

$ drx check shop

 ERROR   src/shop.ax, line 13

    total(price = "12", quantity = 3)
                  ^^^^

price takes an Int, and "12" is a String.

Text is never turned into a number on its own, because text
may hold anything.

To fix it, write the number:

    total(price = 12, quantity = 3)

(AX1001. For more, run: drx explain AX1001)

1 error in 1 file. The package was not built.

Both stop at the text. rustc marks the parameter in the definition, price: u32; drx names it in a sentence and shows the call to write.

3. A kind left out

A receipt for a cart that holds a book, a gift card and a bookmark, with a line written only for books and gift cards.

Rust

/// Something the shop sells.
pub enum Item {
    Book { name: String, price: u32 },
    GiftCard { name: String, price: u32, recipient: String },
    Bookmark { name: String, price: u32 },
}

/// Writes an item's line on the receipt.
pub fn line(item: &Item) -> String {
    match item {
        Item::Book { name, price } => format!("{name}: {price}"),
        Item::GiftCard { name, price, recipient } => format!("{name} for {recipient}: {price}"),
    }
}

/// Writes the receipt's lines for every item in `cart`.
pub fn receipt(cart: &[Item]) -> Vec<String> {
    let mut lines = Vec::new();
    for item in cart {
        lines.push(line(item));
    }
    lines
}

/// Writes the receipt for a cart of a book, a gift card and a bookmark.
pub fn checkout() -> Vec<String> {
    receipt(&[
        Item::Book { name: "Dune".into(), price: 12 },
        Item::GiftCard { name: "Gift card".into(), price: 25, recipient: "Ada".into() },
        Item::Bookmark { name: "Bookmark".into(), price: 2 },
    ])
}

rustc says:

$ rustc --edition 2024 --crate-type lib shop.rs

error[E0004]: non-exhaustive patterns: `&Item::Bookmark { .. }` not covered
  --> shop.rs:10:11
   |
10 |     match item {
   |           ^^^^ pattern `&Item::Bookmark { .. }` not covered
   |
note: `Item` defined here
  --> shop.rs:2:10
   |
 2 | pub enum Item {
   |          ^^^^
...
 5 |     Bookmark { name: String, price: u32 },
   |     -------- not covered
   = note: the matched value is of type `&Item`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
12 ~         Item::GiftCard { name, price, recipient } => format!("{name} for {recipient}: {price}"),
13 ~         &Item::Bookmark { .. } => todo!(),
   |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0004`.

Draconyx

module shop

""" A book the shop sells. """
type Book:
    name: String
    price: Int

""" A gift card bought for someone. """
type GiftCard:
    name: String
    price: Int
    recipient: String

""" A bookmark the shop sells. """
type Bookmark:
    name: String
    price: Int

""" Writes a book's line on the receipt.

    * book: the book to write
"""
line(book: Book) -> String:
    "{book.name}: {book.price}"

""" Writes a gift card's line on the receipt.

    * card: the gift card to write
"""
line(card: GiftCard) -> String:
    "{card.name} for {card.recipient}: {card.price}"

""" Writes the receipt's lines for every item in `cart`.

    * cart: the items bought, of any kind
"""
receipt(cart: List<any T>) -> List<String>:
    lines: List<String> = []

    for item in cart:
        push(list = borrow lines, item = line(item))

    lines

""" Writes the receipt for a cart of a book, a gift card and a bookmark. """
checkout() -> List<String>:
    receipt([Book(name = "Dune", price = 12), GiftCard(name = "Gift card", price = 25, recipient = "Ada"),
             Bookmark(name = "Bookmark", price = 2)])

drx says:

$ drx check shop

 ERROR   src/shop.ax, line 48

    Bookmark(name = "Bookmark", price = 2)])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This Bookmark goes into a cart of any T, and no line is
written for Bookmark.

receipt calls line on every item of the cart, on line 41,
so each kind that goes in needs a line of its own.

To fix it, write a line for Bookmark:

    """ Writes a bookmark's line on the receipt.

        * bookmark: the bookmark to write
    """
    line(bookmark: Bookmark) -> String:
        "{bookmark.name}: {bookmark.price}"

(AX0420. For more, run: drx explain AX0420)

1 error in 1 file. The package was not built.

Both refuse the program before it runs, and both name the bookmark. In Rust the kinds are one enum and the cases are the arms of one match; rustc points at the match, names Item::Bookmark, and offers an arm with todo!() to fill in. In Draconyx each kind is a type of its own with its own line, joined where they meet by any T; drx points at the bookmark where it goes into the cart, names the line it has none of, and shows the line to write.

4. A value used after it was given away

A greeting sent twice, when sending takes it.

Rust

/// Sends `message` and counts the bytes that went out.
pub fn send(message: String) -> usize {
    message.len()
}

/// Sends the same greeting twice.
pub fn notify() -> usize {
    let greeting = String::from("Hello");
    let first = send(greeting);
    let second = send(greeting);
    first + second
}

rustc says:

$ rustc --edition 2024 --crate-type lib mail.rs

error[E0382]: use of moved value: `greeting`
  --> mail.rs:10:23
   |
 8 |     let greeting = String::from("Hello");
   |         -------- move occurs because `greeting` has type `String`, which does not implement the `Copy` trait
 9 |     let first = send(greeting);
   |                      -------- value moved here
10 |     let second = send(greeting);
   |                       ^^^^^^^^ value used here after move
   |
note: consider changing this parameter type in function `send` to borrow instead if owning the value isn't necessary
  --> mail.rs:2:22
   |
 2 | pub fn send(message: String) -> usize {
   |        ----          ^^^^^^ this parameter takes ownership of the value
   |        |
   |        in this function
help: consider cloning the value if the performance cost is acceptable
   |
 9 |     let first = send(greeting.clone());
   |                              ++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.

Draconyx

module mail

""" Sends `message` and counts the bytes that went out.

    * message: the text to send
"""
send(take message: String) -> Int:
    len_bytes(message)

""" Sends the same greeting twice. """
notify() -> Int:
    greeting = "Hello"
    first = send(take greeting)
    second = send(take greeting)
    first + second

drx says:

$ drx check mail

 ERROR   src/mail.ax, line 14

    second = send(take greeting)
                  ^^^^^^^^^^^^^

greeting was given to send on line 13, so it cannot be
used again here.

send takes its message with take: after the first call,
send owns greeting and this function no longer has it.

If send only reads the message, drop take in three places:
here, on line 13, and in the signature of send:

    send(message: String) -> Int:

(AX0312. For more, run: drx explain AX0312)

1 error in 1 file. The package was not built.

In Rust, giving a value away looks the same as passing a number, which is copied: send(greeting). rustc finds the move and labels it, on line 9. In Draconyx the call itself says take, so the code shows the line that gives the value away, not only the compiler's message.

5. A missing argument

A call of total that forgets the quantity.

Rust

/// Works out what `quantity` items at `price` cost.
pub fn total(price: u32, quantity: u32) -> u32 {
    price * quantity
}

/// Works out the cost of one item at 12.
pub fn checkout() -> u32 {
    total(12)
}

rustc says:

$ rustc --edition 2024 --crate-type lib shop.rs

error[E0061]: this function takes 2 arguments but 1 argument was supplied
 --> shop.rs:8:5
  |
8 |     total(12)
  |     ^^^^^---- argument #2 of type `u32` is missing
  |
note: function defined here
 --> shop.rs:2:8
  |
2 | pub fn total(price: u32, quantity: u32) -> u32 {
  |        ^^^^^             -------------
help: provide the argument
  |
8 |     total(12, /* u32 */)
  |             +++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0061`.

Draconyx

module shop

""" Works out what `quantity` items at `price` cost.

    * price: the price of one item
    * quantity: how many items are bought
"""
total(price: Int, quantity: Int) -> Int:
    price * quantity

""" Works out the cost of one item at 12. """
checkout() -> Int:
    total(price = 12)

drx says:

$ drx check shop

 ERROR   src/shop.ax, line 13

    total(price = 12)
    ^^^^^

This call of total gives price but no quantity.

total is declared as total(price: Int, quantity: Int), and
each of its parameters needs a value.

To fix it, write:

    total(price = 12, quantity = 1)

(AX0249. For more, run: drx explain AX0249)

1 error in 1 file. The package was not built.

rustc labels the missing argument by its position, argument #2, underlines quantity: u32 in the definition and offers total(12, /* u32 */). Draconyx calls name their arguments, so drx names it, quantity, in the sentence itself.

6. One that drx fix repairs for you

A local name in camelCase, the habit of JavaScript and Java.

Rust

/// Adds a 20% tax to `price`.
pub fn with_tax(price: u32) -> u32 {
    let totalPrice = price + price / 5;
    totalPrice
}

rustc says:

$ rustc --edition 2024 --crate-type lib shop.rs

warning: variable `totalPrice` should have a snake case name
 --> shop.rs:3:9
  |
3 |     let totalPrice = price + price / 5;
  |         ^^^^^^^^^^ help: convert the identifier to snake case: `total_price`
  |
  = note: `#[warn(non_snake_case)]` (part of `#[warn(nonstandard_style)]`) on by default

warning: 1 warning emitted

Draconyx

module shop

""" Adds a 20% tax to `price`.

    * price: the price before tax
"""
with_tax(price: Int) -> Int:
    totalPrice = price + price / 5
    totalPrice

drx says:

$ drx check shop

 ERROR   src/shop.ax, line 8

    totalPrice = price + price / 5
    ^^^^^^^^^^

totalPrice is a local name, and local names are written in
lower_snake case.

The case tells values from types: a value is written
total_price, a type TotalPrice.

Rename it total_price, here and on line 9.

drx fix makes this change for you.

(AX1201. For more, run: drx explain AX1201)

1 error in 1 file. The package was not built.

rustc builds the code and warns. Its suggestion is marked as one that may be wrong, so cargo fix leaves the name as it is. drx refuses the name, and drx fix renames it on both lines:

module shop

""" Adds a 20% tax to `price`.

    * price: the price before tax
"""
with_tax(price: Int) -> Int:
    total_price = price + price / 5
    total_price

For agents: the same report as JSON

Both compilers can print their reports as JSON, for editors, tools and AI agents. Here is the misspelled name from the first example, once from each, with line breaks added. rustc prints one object per diagnostic, plus two short ones for its closing lines; this is the first. drx prints one line per diagnostic.

Rust

$ rustc --edition 2024 --crate-type lib --error-format=json shop.rs

rustc says:

{
  "$message_type": "diagnostic",
  "message": "cannot find value `quantitty` in this scope",
  "code": {
    "code": "E0425",
    "explanation": "An unresolved name was used.\n\nErroneous code examples:\n\n```compile_fail,E0425\nsomething_that_doesnt_exist::foo;\n// error: unresolved name `something_that_doesnt_exist::foo`\n\n// or:\n\ntrait Foo {\n    fn bar() {\n        Self; // error: unresolved name `Self`\n    }\n}\n\n// or:\n\nlet x = unknown_variable;  // error: unresolved name `unknown_variable`\n```\n\nPlease verify that the name wasn't misspelled and ensure that the\nidentifier being referred to is valid for the given situation. Example:\n\n```\nenum something_that_does_exist {\n    Foo,\n}\n```\n\nOr:\n\n```\nmod something_that_does_exist {\n    pub static foo : i32 = 0i32;\n}\n\nsomething_that_does_exist::foo; // ok!\n```\n\nOr:\n\n```\nlet unknown_variable = 12u32;\nlet x = unknown_variable; // ok!\n```\n\nIf the item is not defined in the current module, it must be imported using a\n`use` statement, like so:\n\n```\n# mod foo { pub fn bar() {} }\n# fn main() {\nuse foo::bar;\nbar();\n# }\n```\n\nIf the item you are importing is not defined in some super-module of the\ncurrent module, then it must also be declared as public (e.g., `pub fn`).\n"
  },
  "level": "error",
  "spans": [
    {
      "file_name": "shop.rs",
      "byte_start": 114,
      "byte_end": 123,
      "line_start": 3,
      "line_end": 3,
      "column_start": 13,
      "column_end": 22,
      "is_primary": true,
      "text": [
        {
          "text": "    price * quantitty",
          "highlight_start": 13,
          "highlight_end": 22
        }
      ],
      "label": null,
      "suggested_replacement": null,
      "suggestion_applicability": null,
      "expansion": null
    }
  ],
  "children": [
    {
      "message": "a local variable with a similar name exists",
      "code": null,
      "level": "help",
      "spans": [
        {
          "file_name": "shop.rs",
          "byte_start": 114,
          "byte_end": 123,
          "line_start": 3,
          "line_end": 3,
          "column_start": 13,
          "column_end": 22,
          "is_primary": true,
          "text": [
            {
              "text": "    price * quantitty",
              "highlight_start": 13,
              "highlight_end": 22
            }
          ],
          "label": null,
          "suggested_replacement": "quantity",
          "suggestion_applicability": "MaybeIncorrect",
          "expansion": null
        }
      ],
      "children": [],
      "rendered": null
    }
  ],
  "rendered": "error[E0425]: cannot find value `quantitty` in this scope\n --> shop.rs:3:13\n  |\n3 |     price * quantitty\n  |             ^^^^^^^^^\n  |\nhelp: a local variable with a similar name exists\n  |\n3 -     price * quantitty\n3 +     price * quantity\n  |\n\n"
}

Draconyx

$ drx check shop --json

drx says:

{
  "code": "AX0234",
  "severity": "error",
  "message": "`quantitty` is not a name this function knows.",
  "primary_span": {
    "file": "src/shop.ax",
    "start": [9, 13],
    "end": [9, 22],
    "bytes": [201, 210]
  },
  "secondary_spans": [],
  "explanation": "`total` has the parameters `price` and `quantity`, and nothing else in reach is called `quantitty`.",
  "suggested_edits": [
    {
      "span": {
        "file": "src/shop.ax",
        "start": [9, 13],
        "end": [9, 22],
        "bytes": [201, 210]
      },
      "replacement": "quantity",
      "applicability": "maybe",
      "description": "replace `quantitty` with `quantity`",
      "consequence": "`quantity` names the local `quantity`"
    },
    {
      "span": {
        "file": "src/shop.ax",
        "start": [9, 13],
        "end": [9, 22],
        "bytes": [201, 210]
      },
      "replacement": "quantize",
      "also": [
        {
          "span": {
            "file": "src/shop.ax",
            "start": [3, 1],
            "end": [3, 1],
            "bytes": [13, 13]
          },
          "replacement": "use std.tensor.quantize\n"
        }
      ],
      "applicability": "maybe",
      "description": "replace `quantitty` with `quantize` and add `use std.tensor.quantize`",
      "consequence": "`quantize` names the std `std.tensor.quantize`"
    }
  ],
  "help": [
    "Did you mean `quantity`, or `quantize` from `std.tensor`?"
  ]
}

rustc's object carries the message and the code, and in code.explanation the long explanation that rustc --explain E0425 prints, as Markdown, the same for every mistake with that code; the marked spans; each help as a child, with its replacement and how safe it is to apply (MaybeIncorrect here); and in rendered the report as the terminal shows it, without colour.

drx's carries the same three sentences as its terminal report: what is wrong in message, why in explanation, what to do in help. Each suggested edit says what it changes and what the new name will refer to, and whether drx fix may make it alone (always) or someone has to choose (maybe); here there are two candidates, so it is maybe. The sentences keep their backticks, so a program can tell code from words. drx's explanation is the reason for this mistake, in its own names; the long explanation, with examples, is not in the JSON: drx explain prints it.

When you want more: drx explain

rustc --explain E0382 prints a longer explanation of a Rust error code. drx explain does the same for a Draconyx one, in the same layout as the report, with an example of the mistake and its fix:

$ drx explain AX0312

 ERROR AX0312   A value used after it was given away

A value has one owner at a time. Passing it with take gives it to the
function called, and the name that held it can no longer be read.

For example:

    """ Sends `message` and counts the bytes that went out.

        * message: the text to send
    """
    send(take message: String) -> Int:
        len_bytes(message)

    """ Sends the same greeting twice. """
    notify() -> Int:
        greeting = "Hello"
        first = send(take greeting)
        second = send(take greeting)
                      ^^^^^^^^^^^^^
        first + second

To fix it, pass the value bare when the function only reads it, and drop
take from its parameter too:

    """ Sends `message` and counts the bytes that went out.

        * message: the text to send
    """
    send(message: String) -> Int:
        len_bytes(message)

    """ Sends the same greeting twice. """
    notify() -> Int:
        greeting = "Hello"
        first = send(greeting)
        second = send(greeting)
        first + second

If the function changes the value, borrow it at both ends instead.

(Spec: section 7.4, Ownership Is Linear: Use-After-Move.)

The mistakes are the same in both languages, and both compilers find them, in your own names, most often with a fix. What changes is how it is said: plain sentences, with the reason for this mistake right beside it.

All posts · Atom feed

Stay tuned

The dragon is hatching. Be first to know when Draconyx launches, and hear about every new feature the moment it lands. Updates only, straight from the source. One click to leave.

We send a confirmation link first. Nothing arrives until you click it.

Get in early

Want in early? For design discussion, collaboration, or to train or evaluate models on Draconyx, email us.