$rangeChecks: what changed
Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 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. | |
| 11 | 2 | ||
| 12 | 3 | ## range checks | |
| 13 | 4 | ||
| 14 | - | ||
| 5 | + | 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. | |
| 15 | 6 | ||
| 16 | 7 | ### behavior | |
| 17 | 8 | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 9 | + | If a range check fails, the [[runtime error]] 201 “Range check error” is generated. Thus the code sample | |
| 23 | 10 | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 11 | + | ```delphi> | |
| 12 | + | {$rangeChecks on} | |
| 13 | + | x := y; | |
| 14 | + | </syntaxhighlight> | |
| 15 | + | is ''semantically'' equivalent to | |
| 16 | + | <syntaxhighlight lang= | |
| 32 | 17 | ``` | |
| 33 | 18 | ||
| 34 | - | ||
| 19 | + | ` {$rangeChecks off}`\ | |
| 20 | + | ` x := y;`\ | |
| 21 | + | ` if (x < low(x)) or (x > high(x)) then`\ | |
| 22 | + | ` begin`\ | |
| 23 | + | ` runError(201);`\ | |
| 24 | + | ` end;` | |
| 35 | 25 | ||
| 26 | + | (The reported memory address at which the error occurred will differ and the compiler may produce optimized code.) | |
| 27 | + | ||
| 36 | 28 | ### eligibility | |
| 37 | 29 | ||
| 38 | - | ||
| 30 | + | Range checks can be performed | |
| 39 | 31 | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 32 | + | - in [[Becomes|assignments]] (`:=`), and more specifically | |
| 33 | + | - when passing actual parameters, or | |
| 34 | + | - indicating [[Array|array]] indices, but also | |
| 35 | + | - when doing [[Typecast|typecasts]]. | |
| 44 | 36 | ||
| 45 | - | ||
| 37 | + | 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. | |
| 46 | 38 | ||
| 47 | 39 | ### placement | |
| 48 | 40 | ||
| 49 | - | ||
| 41 | + | The FPC allows enabling and disabling range checks on a per-statement basis, while Delphi allows changing the setting only a per-routine level. | |
| 50 | 42 | ||
| 51 | 43 | ## caveats | |
| 52 | 44 | ||
| 53 | - | ||
| 54 | - | ||
| 45 | + | - 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. | |
| 46 | + | - If range checks are meant to be performed when passing parameters, range checks must be enabled *at* the call site. | |
| 55 | 47 | ||
| 56 | 48 | ## application | |
| 57 | 49 | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 50 | + | 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: | |
| 65 | 51 | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 52 | + | ```delphi> | |
| 53 | + | {$push} | |
| 54 | + | {$rangeChecks off} | |
| 55 | + | x := y; | |
| 56 | + | {$pop} | |
| 57 | + | </syntaxhighlight> | |
| 58 | + | ||
| 59 | + | Use <code>{$ifOpt}</code> if code needs to be different on the current range-check-setting: | |
| 60 | + | <syntaxhighlight lang= | |
| 71 | 61 | ``` | |
| 72 | 62 | ||
| 63 | + | ` {$ifOpt R-} // or R+`\ | |
| 64 | + | ` …`\ | |
| 65 | + | ` {$endIf}` | |
| 66 | + | ||
| 73 | 67 | --- | |
| 74 | - | ||
| 68 | + | *From the Free Pascal wiki, page "$rangeChecks", as it stood on 2022-03-05 after a change by Alextp, out of the wiki's dump of 2024-05-26. 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).* | |
| 69 | + | Category: FPC |
