Skip to content

Mojo Errors Explained (start here)

Got an error from the Mojo compiler and you’re not sure what it wants? You’re in the right place. Mojo checks a lot before your program runs, so you’ll see these messages often, especially in your first weeks. The good news: a small number of messages cover most of what beginners hit, and each one has a fix you can learn once.

Each page below takes one real Mojo 1.2 error message, shows the smallest program that causes it, explains it in plain words, and gives fixes you can run, most likely fix first.

What the compiler says In plain words
use of unknown declaration ‘x’ A name is misspelled, not imported, or used outside the block where it was made
implicit declaration of ‘x’ is not allowed You created a variable without var
expressions must not appear at file scope Your code isn’t inside a function like main()
cannot implicitly convert ‘X’ value to ‘Y’ A value has the wrong type for where you put it, like an Int in a Float64
value passed to ‘x’ cannot be converted from ‘A’ to ‘B’ A function argument (or the right side of *, +) has the wrong type
the SIMD type must be floating point A float literal like 0.5 met an Int
‘X’ value has no attribute ‘Y’ The method or field after the dot doesn’t exist on that type
value of type ‘X’ cannot be implicitly copied A list or struct would be copied; use ^, .copy() or ref
use of uninitialized value ‘x’ A variable has no value yet, or you moved it away with ^
expression must be mutable You changed an argument, self or a loop variable that’s read-only
cannot call function that may raise in a context that cannot raise A call can fail, so you need try or raises
len(String/StringSlice) is not supported Strings have three kinds of length; pick one
type ‘X’ does not conform to trait ‘Writable’ You printed a type that doesn’t know how to print itself
‘X’ does not implement the ‘eq’ method Your type doesn’t support ==, <, + or += yet

Here’s a program with two mistakes in it:

def main():
total = 0
for n in range(4):
total += n
print(totl)
sum.mojo:2:5: error: implicit declaration of 'total' is not allowed; add 'var' to declare a new name
total = 0
^~~~~
var
sum.mojo:5:11: error: use of unknown declaration 'totl'; did you mean 'total'?
print(totl)
^~~~
total
mojo: error: failed to parse the provided Mojo source module

Read it like this:

  1. sum.mojo:2:5 is the file, the line and the column. Line 2, column 5 is where total starts.
  2. error: ... is the message. This is the line people paste into a search engine, and the one the pages above are named after.
  3. The ^~~~~ marks point at the exact code. Sometimes there’s a line under them with a suggested fix: here var for the first error and total for the second.
  4. note: ... lines (not in this example) add context, like where a variable was declared or what a function expects.
  5. The last line, mojo: error: failed to parse the provided Mojo source module, only says the build stopped. Skip it.

Start with the first error. One mistake can cause others further down, and fixing the first one often makes the rest disappear.

Some errors are found in a later step, while Mojo builds the code for a specific function. Those look different:

def main():
var xs: List[Int] = [1, 2, 3]
print(xs[-1])
last.mojo:1:5: error: function instantiation failed
def main():
^
last.mojo:3:13: note: call expansion failed with parameter value(s): (..., ...)
print(xs[-1])
^
Mojo/stdlib/std/collections/list.mojo:1467:9: note: function instantiation failed
Mojo/stdlib/std/collections/list.mojo:1478:9: note: constraint failed: negative indexing is not supported, use e.g. `x[len(x) - 1]`
mojo: error: failed to run the pass manager

The error: line only says that building main failed. The real story is in the notes: the call expansion failed note points at your line, and the last constraint failed note says why. Here, Python’s xs[-1] isn’t allowed; write xs[len(xs) - 1]. When you see function instantiation failed, read the notes from the bottom up. The SIMD floating point page has another example.

The last line also changes: failed to run the pass manager instead of failed to parse. It means the same thing for you: the program wasn’t built.

These are real Mojo 1.2 messages too, each with a short fix and the lesson that explains it.

Message Fix
invalid call to 'area': missing required argument: 'height' Pass every argument the function needs, or give it a default. Function Arguments
constraint failed: negative indexing is not supported Use xs[len(xs) - 1] for the last item. Lists
return expected at end of function with results Some path through the function has no return; add one at the end. Return Values
invalid redefinition of 'x' You wrote var x twice in the same block; drop the second var. Variables
String does not support direct positional indexing like `s[i]` Use s[byte=i], s[codepoint=i] or s[grapheme=i], or loop with for c in s. Strings in Depth
cannot access instance field 'count' directly; did you mean 'self.'? Write self.count inside methods. Methods
global variables are not supported Move the var into a function, or use comptime. file scope error
'raise' requires a surrounding 'try' block or the enclosing function to declare 'raises' Add raises to the function. may raise error
use of invalidated interior reference A ref into a list was used after the list changed (for example after append). Take the reference again, or copy the value. Origins
invalid call to 'repeat': failed to infer parameter 'count' Pass the compile-time parameter in square brackets: repeat[3]("Hi"). Parameters
cannot use a dynamic value in a parameter list Values in [...] must be known when the program is built; use comptime or a normal argument. Parameters
format specifiers are not supported in t-strings {x:.2f} doesn’t work; use round(x, 2) first. String Formatting
'break' is not inside a loop break and continue only work inside for and while. Break & Continue
  • Run often. mojo run file.mojo after every few lines means you meet one error at a time, not ten. See Mojo Get Started.
  • Use the editor extension. The Mojo extension for VS Code shows the same errors as squiggles while you type, with the message on hover. See Mojo Tools.
  • Don’t ignore warnings. A warning: doesn’t stop the build, but messages like 'Int' value is unused; assign to '_' to discard the result often point at a real mistake, such as calling a function and forgetting to use its result.

Many of these errors are bugs Python would only show you at runtime, or never: a misspelled variable, adding a number to text, reading a value that was never set, a missing return. Mojo checks types, names and values before the program runs, so these mistakes can’t reach your users. The price is that you meet them early, and sometimes the messages are long. The pages above are here to make that part quicker.

If you know Python, the official Mojo tips for Python devs is worth reading once: it lists the Python habits behind many of these errors.

  • Read the first error: line first: it has the file, line, column and the message.
  • The ^~~~ marks show the exact spot; the line under them is often the fix.
  • note: lines add context. For function instantiation failed, read the notes from the bottom up.
  • The last line (failed to parse or failed to run the pass manager) only says the build stopped.
  • Fix the first error, run again, repeat.

Next up: use of unknown declaration ‘x’, the error you’ll probably see first.

Quick check

1. An error starts with prices.mojo:12:9:. What do 12 and 9 mean?
2. The output shows three errors. Which should you fix first?
3. The only error: line says function instantiation failed and points at def main(). Where is the real reason?