D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2933 - Cannot return const/immutable with contracts (out/invariant) enabled
Summary: Cannot return const/immutable with contracts (out/invariant) enabled
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 All
: P2 normal
Assignee: Walter Bright
URL:
Keywords: patch, rejects-valid
: 2799 3048 (view as issue list)
Depends on:
Blocks:
 
Reported: 2009-05-04 01:26 UTC by Shin Fujishiro
Modified: 2015-06-09 01:26 UTC (History)
4 users (show)

See Also:


Attachments
This patch should fix the problem. (914 bytes, patch)
2009-05-10 15:17 UTC, Shin Fujishiro
Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description Shin Fujishiro 2009-05-04 01:26:09 UTC
If a method of a class returns a const or immutable value, and
the class has class invariant, then the compile fails.  It also
fails if a method returning a const/immutable has an out contract.

The following code:
--------------------
class Foo
{
    invariant() {}
    const(int) foo()
    {
        return 0;
    }
    immutable(int) bar()
    out(abc) { }
    body
    {
        return 0;
    }
}
--------------------
causes this compile error:
--------------------
test.d(4): Error: variable test.Foo.foo.__result cannot modify const
test.d(9): Error: variable test.Foo.bar.abc cannot modify mutable
--------------------
Comment 1 Shin Fujishiro 2009-05-10 15:17:32 UTC
Created attachment 356 [details]
This patch should fix the problem.
Comment 2 anonymous4 2009-06-04 01:07:00 UTC
*** Issue 3048 has been marked as a duplicate of this issue. ***
Comment 3 anonymous4 2009-06-10 02:35:19 UTC
*** Issue 2799 has been marked as a duplicate of this issue. ***
Comment 4 Don 2010-05-14 12:29:43 UTC
On svn 484(DMD 2.046), this patch doesn't fix the test case. It's possible that the patch will work once bug 3667 is fixed, but I haven't tested that.
Comment 5 Don 2010-06-05 03:47:28 UTC
Fixed when bug 3667 was fixed, in svn 490 (DMD 2.047).