]> gitweb.factorcode.org Git - factor.git/blob - basis/stack-checker/errors/errors-docs.factor
merge project-euler.factor
[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: literal-expected
16 { $error-description "Thrown when inference encounters a combinator or macro being applied to a value which is not known to be a literal, or constructed in a manner which can be analyzed statically. Such code needs changes before it 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 literal-expected } " 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: unbalanced-branches-error
45 { $values { "in" "a sequence of integers" } { "out" "a sequence of integers" } }
46 { $description "Throws an " { $link unbalanced-branches-error } "." }
47 { $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." }
48 { $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." }
49 { $examples
50     { $code
51         ": unbalanced-branches-example ( a b c -- )"
52         "    [ + ] [ dup ] if ;"
53     }
54 } ;
55
56 HELP: too-many->r
57 { $error-description "Thrown if inference notices a quotation pushing elements on the retain stack without popping them at the end." } ;
58
59 HELP: too-many-r>
60 { $error-description "Thrown if inference notices a quotation popping elements from the return stack it did not place there." } ;
61
62 HELP: missing-effect
63 { $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." }
64 { $examples
65     { $code
66         ": missing-effect-example"
67         "    + * ;"
68     }
69 } ;
70
71 HELP: effect-error
72 { $values { "word" word } { "effect" "an instance of " { $link effect } } }
73 { $description "Throws an " { $link effect-error } "." }
74 { $error-description "Thrown when a word's inferred stack effect does not match its declared stack effect." } ;
75
76 HELP: recursive-quotation-error
77 { $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." }
78 { $examples
79     "Here is an example of quotation recursion:"
80     { $code "[ [ dup call ] dup call ] infer." }
81 } ;
82
83 HELP: undeclared-recursion-error
84 { $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." } ;
85
86 HELP: diverging-recursion-error
87 { $error-description "Thrown when stack effect inference determines that a recursive word might take an arbitrary number of values from the stack." }
88 { $notes "Such words do not have a static stack effect and most likely indicate programmer error." }
89 { $examples
90     { $code
91         ": diverging-recursion-example ( -- )"
92         "    [ diverging-recursion-example f ] when ; inline recursive"
93     }
94 } ;
95
96 HELP: unbalanced-recursion-error
97 { $error-description "Thrown when stack effect inference determines that an inline recursive word has an incorrect stack effect declaration." }
98 { $examples
99     { $code
100         ": unbalanced-recursion-example ( quot: ( -- ? ) -- b )"
101         "    dup call [ unbalanced-recursion-example ] [ drop ] if ;"
102         "    inline recursive"
103     }
104 } ;
105
106 HELP: inconsistent-recursive-call-error
107 { $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." }
108 { $examples
109     { $code
110         ": inconsistent-recursive-call-example ( quot: ( -- ? ) -- b )"
111         "    [ not ] compose inconsistent-recursive-call-example ; inline recursive"
112     }
113 } ;
114
115 ARTICLE: "inference-errors" "Stack checker errors"
116 "These " { $link "inference" } " failure conditions are reported in one of two ways:"
117 { $list
118     { { $link "tools.inference" } " throws them as errors" }
119     { "The " { $link "compiler" } " reports them via " { $link "tools.errors" } }
120 }
121 "Errors thrown when insufficient information is available to calculate the stack effect of a call to a combinator or macro (see " { $link "inference-combinators" } "):"
122 { $subsections
123     do-not-compile
124     literal-expected
125 }
126 "Error thrown when a word's stack effect declaration does not match the composition of the stack effects of its factors:"
127 { $subsections effect-error }
128 "Error thrown when branches have incompatible stack effects (see " { $link "inference-branches" } "):"
129 { $subsections unbalanced-branches-error }
130 "Inference errors for inline recursive words (see " { $link "inference-recursive-combinators" } "):"
131 { $subsections
132     undeclared-recursion-error
133     diverging-recursion-error
134     unbalanced-recursion-error
135     inconsistent-recursive-call-error
136 }
137 "More obscure errors that are unlikely to arise in ordinary code:"
138 { $subsections
139     recursive-quotation-error
140     too-many->r
141     too-many-r>
142     missing-effect
143 } ;
144
145 ABOUT: "inference-errors"