Bug 2804 – Impure nested functions should be legal inside pure functions[patch included]

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-04-06T06:27:00Z
Last change time
2015-06-09T01:18:03Z
Keywords
patch, rejects-valid
Assigned to
bugzilla
Creator
clugdbug

Attachments

IDFilenameSummaryContent-TypeSize
315purepatch.patchpatch for 2804text/plain1340

Comments

Comment #0 by clugdbug — 2009-04-06T06:27:36Z
Example contributed by bearophile. ------ import std.c.stdio: printf; import std.conv: toInt; pure int double_sqr(int x) { int y, z; void do_sqr() { y *= y; } y = x; do_sqr(); z += y; y = x; do_sqr(); z += y; return z; } void main(string[] args) { int x = args.length == 2 ? toInt(args[1]) : 10; int y = double_sqr(x) + double_sqr(x); printf("4 * x * x = %d\n", y); } -------- pure_test3.d(...): Error: pure function 'double_sqr' cannot call impure function 'do_sqr' pure_test3.d(...): Error: pure function 'double_sqr' cannot call impure function 'do_sqr'
Comment #1 by clugdbug — 2009-04-06T06:30:40Z
Created attachment 315 patch for 2804 This allows pure functions to call their own nested functions (even if such functions aren't marked as pure), but prevents nested functions inside pure functions from (1) calling impure functions and (2) accessing static variables.
Comment #2 by clugdbug — 2009-04-18T01:05:39Z
Fixed DMD2.028