[Issue 10751] New: Propagate some value ranges from contracts
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Aug 3 04:29:00 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10751
Summary: Propagate some value ranges from contracts
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2013-08-03 04:28:59 PDT ---
I suggest to take a bit more seriously D contract programming, to increase a
little its usefulness.
So I think the following code should compile:
char foo(immutable uint digit)
in {
assert(digit < 10);
} body {
return digit + '0'; // Error.
}
uint bar()
out(result) {
assert(result < 100);
} body {
return 99;
}
void main() {
ubyte x = bar(); // Error.
}
DMD 2.064alpha gives:
test.d(5): Error: cannot implicitly convert expression (digit + 48u) of type
uint to char
test.d(14): Error: cannot implicitly convert expression (bar()) of type uint to
ubyte
In the first case 'digit' is immutable and the pre-condition asserts it can't
be bigger than 9, so the result should be cast-able to char implicitly.
In the second case the post-condition tells that the result of 'bar' is an uint
that fits in the range of an ubyte.
(Probably if D contracts become a bit more useful their usage will increase a
little. Every useless cast removed from the code reduces the chance of bugs.)
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list