]> gitweb.factorcode.org Git - factor.git/blob - basis/stack-checker/errors/errors-docs.factor
4b432e733f38cf5083c566c5938b33405b871040
[factor.git] / basis / stack-checker / errors / errors-docs.factor
1 USING: help.markup help.syntax kernel effects sequences
2 sequences.private words combinators ;
3 IN: stack-checker.errors
4
5 HELP: do-not-compile
6 { $error-description "Thrown when inference encounters a macro being applied to a value which is not known to be a literal. Such code needs changes before it can compile and run. See " { $link "inference-combinators" } " and " { $link "inference-escape" } " for details." }
7 { $examples
8     "In this example, " { $link cleave } " is being applied to an array that is constructed on the fly. This is not allowed and fails to compile with a " { $link do-not-compile } " error:"
9     { $code
10         ": cannot-compile-call-example ( x -- y z )"
11         "    [ 1 + ] [ 1 - ] 2array cleave ;"
12     }
13 } ;
14
15 HELP: unknown-macro-input
16 { $error-description "Thrown when inference encounters a combinator or macro being applied to an input parameter of a non-" { $link POSTPONE: inline } " word. The word needs to be declared " { $link POSTPONE: inline } " before its callers can compile and run. See " { $link "inference-combinators" } " and " { $link "inference-escape" } " for details." }
17 { $examples
18     "In this example, the words being defined cannot be called, because they fail to compile with a " { $link unknown-macro-input } " error:"
19     { $code
20         ": bad-example ( quot -- )"
21         "    [ call ] [ call ] bi ;"
22         ""
23         ": usage ( -- )"
24         "    10 [ 2 * ] bad-example . ;"
25     }
26     "One fix is to declare the combinator as inline:"
27     { $code
28         ": good-example ( quot -- )"
29         "    [ call ] [ call ] bi ; inline"
30         ""
31         ": usage ( -- )"
32         "    10 [ 2 * ] good-example . ;"
33     }
34     "Another fix is to use " { $link POSTPONE: call( } ":"
35     { $code
36         ": good-example ( quot -- )"
37         "    [ call( x -- y ) ] [ call( x -- y ) ] bi ;"
38         ""
39         ": usage ( -- )"
40         "    10 [ 2 * ] good-example . ;"
41     }
42 } ;
43
44 HELP: bad-macro-input
45 { $error-description "Thrown when inference encounters a combinator or macro being applied to a value which is not known at compile time. Such code needs changes before it can compile and run. See " { $link "inference-combinators" } " and " { $link "inference-escape" } " for details." }
46 { $examples
47     "In this example, the words being defined cannot be called, because they fail to compile with a " { $link bad-macro-input } " error:"
48     { $code
49         ": bad-example ( quot -- )"
50         "    [ . ] append call ; inline"
51         ""
52         ": usage ( -- )"
53         "    2 2 [ + ] bad-example ;"
54     }
55     "One fix is to use " { $link compose } " instead of " { $link append } ":"
56     { $code
57         ": good-example ( quot -- )"
58         "    [ . ] compose call ; inline"
59         ""
60         ": usage ( -- )"
61         "    2 2 [ + ] good-example ;"
62     }
63 } ;
64
65 HELP: unbalanced-branches-error
66 { $values { "in" "a sequence of integers" } { "out" "a sequence of integers" } }
67 { $description "Throws an " { $link unbalanced-branches-error } "." }
68 { $error-description "Thrown when inference encounters an " { $link if } " or " { $link dispatch } " where the branches do not all exit with the same stack height. See " { $link "inference-branches" } " for details." }
69 { $notes "If this error comes up when inferring the stack effect of a recursive word, check the word's stack effect declaration; it might be wrong." }
70 { $examples
71     { $code
72         ": unbalanced-branches-example ( a b c -- )"
73         "    [ + ] [ dup ] if ;"
74     }
75 } ;
76
77 HELP: too-many->r
78 { $error-description "Thrown if inference notices a quotation pushing elements on the retain stack without popping them at the end." } ;
79
80 HELP: too-many-r>
81 { $error-description "Thrown if inference notices a quotation popping elements from the return stack it did not place there." } ;
82
83 HELP: missing-effect
84 { $error-description "Thrown when inference encounters a word lacking a stack effect declaration. Stack effects of words must be declared, with the exception of words which only push literals on the stack." }
85 { $examples
86     { $code
87         ": missing-effect-example"
88         "    + * ;"
89     }
90 } ;
91
92 HELP: effect-error
93 { $values { "word" word } { "effect" "an instance of " { $link effect } } }
94 { $description "Throws an " { $link effect-error } "." }
95 { $error-description "Thrown when a word's inferred stack effect does not match its declared stack effect." } ;
96
97 HELP: recursive-quotation-error
98 { $error-description "Thrown when a quotation calls itself, directly or indirectly, within the same word. Stack effect inference becomes equivalent to the halting problem if quotation recursion has to be taken into account, hence it is not permitted." }
99 { $examples
100     "Here is an example of quotation recursion:"
101     { $code "[ [ dup call ] dup call ] infer." }
102 } ;
103
104 HELP: undeclared-recursion-error
105 { $error-description "Thrown when an " { $link POSTPONE: inline } " word which is not declared " { $link POSTPONE: recursive } " calls itself, directly or indirectly. The " { $link POSTPONE: recursive } " declaration is mandatory for such words." } ;
106
107 HELP: diverging-recursion-error
108 { $error-description "Thrown when stack effect inference determines that a recursive word might take an arbitrary number of values from the stack." }
109 { $notes "Such words do not have a static stack effect and most likely indicate programmer error." }
110 { $examples
111     { $code
112         ": diverging-recursion-example ( -- )"
113         "    [ diverging-recursion-example f ] when ; inline recursive"
114     }
115 } ;
116
117 HELP: unbalanced-recursion-error
118 { $error-description "Thrown when stack effect inference determines that an inline recursive word has an incorrect stack effect declaration." }
119 { $examples
120     { $code
121         ": unbalanced-recursion-example ( quot: ( -- ? ) -- b )"
122         "    dup call [ unbalanced-recursion-example ] [ drop ] if ;"
123         "    inline recursive"
124     }
125 } ;
126
127 HELP: inconsistent-recursive-call-error
128 { $error-description "Thrown when stack effect inference determines that an inline recursive word calls itself with a different set of quotation parameters than were input." }
129 { $examples
130     { $code
131         ": inconsistent-recursive-call-example ( quot: ( -- ? ) -- b )"
132         "    [ not ] compose inconsistent-recursive-call-example ; inline recursive"
133     }
134 } ;
135
136 ARTICLE: "inference-errors" "Stack checker errors"
137 "These " { $link "inference" } " failure conditions are reported in one of two ways:"
138 { $list
139     { { $link "tools.inference" } " throws them as errors" }
140     { "The " { $link "compiler" } " reports them via " { $link "tools.errors" } }
141 }
142 "Errors thrown when insufficient information is available to calculate the stack effect of a call to a combinator or macro (see " { $link "inference-combinators" } "):"
143 { $subsections
144     do-not-compile
145     unknown-macro-input
146     bad-macro-input
147 }
148 "Error thrown when a word's stack effect declaration does not match the composition of the stack effects of its factors:"
149 { $subsections effect-error }
150 "Error thrown when branches have incompatible stack effects (see " { $link "inference-branches" } "):"
151 { $subsections unbalanced-branches-error }
152 "Inference errors for inline recursive words (see " { $link "inference-recursive-combinators" } "):"
153 { $subsections
154     undeclared-recursion-error
155     diverging-recursion-error
156     unbalanced-recursion-error
157     inconsistent-recursive-call-error
158 }
159 "More obscure errors that are unlikely to arise in ordinary code:"
160 { $subsections
161     recursive-quotation-error
162     too-many->r
163     too-many-r>
164     missing-effect
165 } ;
166
167 ABOUT: "inference-errors"