SCAML_ERRORS.md 5.07 KB
Newer Older
Jun Furuse's avatar
Jun Furuse committed
1 2
# SCaml error codes

Jun Furuse's avatar
doc  
Jun Furuse committed
3 4 5 6 7 8
SCaml is a restricted version of OCaml.  In addition to the ordinary OCaml
parsing and typing errors (consult OCaml documents for these OCaml errors), SCaml
may reject your code with its own errors.

SCaml errors are prefixed with `[ESCaml<error code>]`.  Do *not* consult OCaml documents for these SCaml specific errors.  Here are the list of SCaml errors.

Jun Furuse's avatar
Jun Furuse committed
9 10 11 12
## 000 Unsupported OCaml feature

SCaml does not support that OCaml language feature.

Jun Furuse's avatar
doc  
Jun Furuse committed
13 14 15 16 17
### Reason

SCaml team has good reasons not to support it:  it could be too hard to give a proper compilation to Michelson.  Or, it would not be a very essential language feature.

### Workaround
Jun Furuse's avatar
Jun Furuse committed
18 19 20 21 22

The best way to avoid this error is not to use that language feature.

## 100 Unsupported type

Jun Furuse's avatar
doc  
Jun Furuse committed
23 24 25
A type expression explicitly written, of an expression, or of a pattern is unsupported or invalid in SCaml.

### Workaround
Jun Furuse's avatar
Jun Furuse committed
26

Jun Furuse's avatar
doc  
Jun Furuse committed
27
Do not use that type construction.
Jun Furuse's avatar
Jun Furuse committed
28 29 30

## 200 Constant error

Jun Furuse's avatar
doc  
Jun Furuse committed
31 32 33
Invalid use of smart contract related data constructors which can only take constants.

### Reason
Jun Furuse's avatar
Jun Furuse committed
34

Jun Furuse's avatar
doc  
Jun Furuse committed
35
Smart contract related data constructors such as `Int`, `Address`, `Set` etc can only take constant expressions.  Some of them can only take some specific form of data: for example, `Timestamp` can only take a constant string in the RFC3339 time representation.
Jun Furuse's avatar
Jun Furuse committed
36 37 38 39 40 41 42 43 44 45

## 300 Entry point

Something wrong has been found in the form of entry points.  Possibilities:

* There exists no entry point.
* Problem found in one of `[@entry]` attributes.

## 310 Entry point typing error

Jun Furuse's avatar
doc  
Jun Furuse committed
46 47 48 49 50
One of the entry points failed to type to make a program valid as a smart contract.

### Reason

Several conditions must hold:
Jun Furuse's avatar
Jun Furuse committed
51 52 53 54 55 56 57 58 59 60 61

* The entry points in a contract must have the form:
  `(param, storage) SCaml.entry` for some type `param` and `storage`,
  which is equal to `param -> storage -> SCaml.operations * storage`.
* If there are multiple entry points, they must share the same storage type.
* Some type constructors cannot appear in `param` and `storage` types.
    * `param` cannot contain `SCaml.operation`.
	* `storage` cannot contain `SCaml.operation` and `SCaml.contract`.

## 400 Free variable

Jun Furuse's avatar
doc  
Jun Furuse committed
62
This is the most non intuitive SCaml restriciton.
Jun Furuse's avatar
Jun Furuse committed
63 64 65

SCaml's functions do not allow free variable occurrences of non packable types (note: older Michelson documents refer them as "unstorable") in their bodies:

Jun Furuse's avatar
doc  
Jun Furuse committed
66
* Non packable types are `SCaml.big_map`, `SCaml.operation`, `SCaml.contract`, and types containing these types.
Jun Furuse's avatar
Jun Furuse committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
* The free variable occurrences of a function are variables defined outside of the function body.  For example, `fun x -> (x, y, let z = 1 in z)`, `y` is the sole free variable occurrence of the function; `x` is bound by the function abstractrion and `z` is locally defined.

This may sound strange but is forced by the same restriction of Michelson's function closures.

The restriction is *not* applied to the entry point arguments; they are handled without Michelson closures.

### Uncurried primitives

This restriction is troublesome especially when you use higher order functions
like `List.fold_left` with non packable types.  Here is an *invalid* expression example which tries to send tokens to each member of a contract list:

```
List.fold_left
	(fun ops -> fun c -> Operation.transfer_tokens () (Tz 1.0) c :: ops)
	[] contracts
```

This is rejected by SCaml, since `ops` freely occurring in the inner function
`fun c -> ..`.  This is troublesome since this kind of code is quite typical
in smart contract programming.

To work around this specific problem, SCaml provides another version of list folding `List.fold_left'`:

```
List.fold_left'
	(fun (ops, c) -> Operation.transfer_tokens () (Tz 1.0) c :: ops)
	[] contracts
```

Here, the two function abstractions in the previous example are "uncurried" i.e. squashed into one which takes a tuple `(ops, c)`, thus making the occurrence of `ops` is no longer free.  SCaml provides this kind of uncurried foldings for `list`, `set`, and `map`.

### General workaround

The principle is to avoid free occurrences of non packable variables.  There are several ways:

* Use `SCaml.key_hash` or `SCaml.address` instead of `SCaml.contract` if possible.
* Use uncurried functions.

## 500 Self

## 510 Self typing error

Jun Furuse's avatar
doc  
Jun Furuse committed
109 110 111
## 600 Invalid contract creation

Error around `Contract.create_*`.  They have the following restrictions:
Jun Furuse's avatar
Jun Furuse committed
112

Jun Furuse's avatar
doc  
Jun Furuse committed
113 114
* They must be fully applied.
* Code text and file name of the contract source must be in constant.
Jun Furuse's avatar
Jun Furuse committed
115

Jun Furuse's avatar
doc  
Jun Furuse committed
116 117 118 119

## 700 Pattern match error

SCaml introduces some additional restrictions to pattern matching:
Jun Furuse's avatar
Jun Furuse committed
120 121 122 123 124 125

* SCaml rejects non exhaustive pattern matches.
* Conversion mode does not support complex patterns.

## 800 Primitive

Jun Furuse's avatar
doc  
Jun Furuse committed
126 127 128 129
An illegal use of a SCaml primitive is found:

* Comparison primitives only take comparable types.
* Packing primitives only take packable types.
Jun Furuse's avatar
Jun Furuse committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

## 900 Flags

Compiler flags conflict each other.

## 910 Attribute error

The problem happened around SCaml's attributes.

## 999 Internal error

This error strongly suggests that there is a bug of SCaml compiler
rather than in your code.

Please do not waste your time to analyze the cause of the error in your code,
but report it at SCaml issues: https://gitlab.com/dailambda/scaml/issues