3801 – CTFE: this.arr[i] cannot be evaluated at compile time for structs

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3801 - CTFE: this.arr[i] cannot be evaluated at compile time for structs
Summary: CTFE: this.arr[i] cannot be evaluated at compile time for structs
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 blocker
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
Reported: 2010年02月14日 07:45 UTC by Sönke Ludwig
Modified: 2015年06月09日 01:27 UTC (History)
1 user (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Sönke Ludwig 2010年02月14日 07:45:33 UTC
A static array contained inside a struct is not accessible during CTFE. Because of this, it seems impossible (since some versions) to define compile-time constants of such structures (for example tuple vectors or matrices).
---
struct S {
 int arr[1];
 this(int x){
 arr[0] = x; // (this.arr[0u]) = x cannot be evaluated at compile time
 }
}
immutable S s_constant = S(1); // Error: cannot evaluate __ctmp1.this(1) at compile time
---
Comment 1 Don 2010年02月26日 07:37:38 UTC
a.b[i]=c; isn't implemented in CTFE yet, but a.b=c; is, so I'm downgrading from blocker. (It's still high priority, though).
Workaround:
struct S {
 int arr[1];
 this(int x){
 int[1] z = x;
 arr = z;
 }
}
Comment 2 Sönke Ludwig 2010年02月27日 03:44:47 UTC
Unfortunately, the workaround also errors out with (although I did not check a current svn build):
test.d(6): Error: this.arr[] = cast(const(int[]))x cannot be evaluated at compile time
But this gave me finally another idea, which seems to work:
struct test {
 int[1] f;
 this(int x){
 int[] dst = f;
 dst[0] = x;
 }
}
[Now off to some code porting and finally trying out the recent features... ;-)]
Comment 3 Sönke Ludwig 2010年03月18日 23:46:33 UTC
Unfortunately, the workaround also does not work (compiles but generates incorrect code): http://d.puremagic.com/issues/show_bug.cgi?id=3984.
Comment 4 Don 2010年03月24日 12:06:16 UTC
Yeah, there doesn't seem to be any workaround. Changing it back to blocker.


AltStyle によって変換されたページ (->オリジナル) /