$rangeChecks: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | The [[FPC]]’s [[local compiler directives|local compiler directive]] **`{$rangeChecks}`** enables or disable range checks of ordinal data type expressions, the default being `{$rangeChecks off}`. The short form `{$R+}` and `{$R-}` is recognized too. | |
| 2 | 2 | ||
| 3 | + | ## Contents | |
| 4 | + | ||
| 5 | + | * 1 range checks | |
| 6 | + | * 1.1 behavior | |
| 7 | + | * 1.2 eligibility | |
| 8 | + | * 1.3 placement | |
| 9 | + | * 2 caveats | |
| 10 | + | * 3 application | |
| 11 | + | ||
| 3 | 12 | ## range checks | |
| 4 | 13 | ||
| 5 | - | ||
| 14 | + | A range check ensures a value is within the domain of the destination operand. As soon as an operand needs to be “squeezed” into a smaller range, the expression or statement is eligible to a range check. | |
| 6 | 15 | ||
| 7 | 16 | ### behavior | |
| 8 | 17 | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 18 | + | If a range check fails, the [[runtime error]] 201 “Range check error” is generated. Thus the code sample | |
| 17 | 19 | ``` | |
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 20 | + | {$rangeChecks on} | |
| 21 | + | x := y; | |
| 22 | + | ``` | |
| 25 | 23 | ||
| 26 | - | ||
| 24 | + | is _semantically_ equivalent to | |
| 25 | + | ``` | |
| 26 | + | {$rangeChecks off} | |
| 27 | + | x := y; | |
| 28 | + | if (x < low(x)) or (x > high(x)) then | |
| 29 | + | begin | |
| 30 | + | runError(201); | |
| 31 | + | end; | |
| 32 | + | ``` | |
| 27 | 33 | ||
| 28 | - | ||
| 34 | + | (The reported memory address at which the error occurred will differ and the compiler may produce optimized code.) | |
| 29 | 35 | ||
| 30 | 36 | ### eligibility | |
| 31 | 37 | ||
| 32 | - | ||
| 38 | + | Range checks can be performed | |
| 33 | 39 | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 40 | + | * in [[Becomes|assignments]] (`:=`), and more specifically | |
| 41 | + | * when passing actual parameters, or | |
| 42 | + | * indicating [[Array|array]] indices, but also | |
| 43 | + | * when doing [[Typecast|typecasts]]. | |
| 38 | 44 | ||
| 39 | - | ||
| 45 | + | For assignments involving constant expressions a range check is performed already at [[compile-time error|compile-time]], but is either a warning or error depending on the current setting. | |
| 40 | 46 | ||
| 41 | 47 | ### placement | |
| 42 | 48 | ||
| 43 | - | ||
| 49 | + | The FPC allows enabling and disabling range checks on a per-statement basis, while Delphi allows changing the setting only a per-routine level. | |
| 44 | 50 | ||
| 45 | 51 | ## caveats | |
| 46 | 52 | ||
| 47 | - | ||
| 48 | - | ||
| 53 | + | * It is important to remember that in [[Pascal]] _arithmetic_ [[expression|expressions]] are always evaluated on a computer’s “native” (signed) integer data type. If the destination operand of an arithmetic expression is, for instance a `ALUSInt`, this will never trigger a runtime error. | |
| 54 | + | * If range checks are meant to be performed when passing parameters, range checks must be enabled _at_ the call site. | |
| 49 | 55 | ||
| 50 | 56 | ## application | |
| 51 | 57 | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 58 | + | Range checks can incur a significant performance penalty. Therefore, the FPC disables range checks by default. However, at least _during_ development and prior releases of software, range checks should be enabled to find “obvious” programming mistakes. As a consequence of this practice, if a code fragment is _meant to_ exceed the permissible range, it should be documented like this: | |
| 63 | 59 | ``` | |
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 60 | + | {$push} | |
| 61 | + | {$rangeChecks off} | |
| 62 | + | x := y; | |
| 63 | + | {$pop} | |
| 64 | + | ``` | |
| 68 | 65 | ||
| 69 | - | ||
| 66 | + | Use `{$ifOpt}` if code needs to be different on the current range-check-setting: | |
| 67 | + | ``` | |
| 68 | + | {$ifOpt R-} // or R+ | |
| 69 | + | … | |
| 70 | + | {$endIf} | |
| 71 | + | ``` | |
| 70 | 72 | ||
| 71 | 73 | --- | |
| 72 | - | ||
| 73 | - | ||
| 74 | + | *From the Free Pascal wiki, page "$rangeChecks", as the Internet Archive kept it on 2024-09-19. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
