D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6300 - Returning a cast(immutable) struct erroneously calls a matching constructor
Summary: Returning a cast(immutable) struct erroneously calls a matching constructor
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-12 12:40 UTC by Sönke Ludwig
Modified: 2015-06-09 05:14 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Sönke Ludwig 2011-07-12 12:40:53 UTC
The following snipped fails to compile. What seems to happen is that the return statement implicitly calls the constructor which in turn returns a mutable S which is not implicitly castable to immutable - thus the error.

Happens since DMD 2.054

---
struct S {
	int[] c;
	this(in S){}
	immutable(S) f() const {
		S s;
		return cast(immutable)s;
		// the following works:
		//immutable(S) sr = cast(immutable)s;
		//return sr;
	}
}
---

dbug2.d(6): Error: cannot implicitly convert expression ((immutable immutable(S) __ctmp3 = 0;
 , __ctmp3).this(s)) of type S to immutable(S)


.. leaving the severity as normal, as there is an easy workaround
Comment 1 Infiltrator 2014-03-19 18:59:32 UTC
The provided code compiles and runs as of v2.065.