D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7046 - CTFE: append null does nothing
Summary: CTFE: append null does nothing
Status: RESOLVED DUPLICATE of issue 6077
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-01 18:07 UTC by Adam D. Ruppe
Modified: 2011-12-14 08:35 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adam D. Ruppe 2011-12-01 18:07:38 UTC
string[] test() {
	string[] ret;

	ret ~= null;
	ret ~= null;

	assert(ret.length == 2); // fails
	return ret;
}

void main() {
	enum ctfe = test();
}

===
test90.d(7): Error: assert(ret.length == 2u) failed
test90.d(12):        called from here: a()
Comment 1 Don 2011-12-02 02:57:40 UTC
This works on git HEAD. Probably a duplicate of a recently fixed bug.
Comment 2 Steven Schveighoffer 2011-12-02 04:30:34 UTC
Are we sure this isn't expected?  I mean a null could mean a null string[], not a null string.

string[] ret;
string[] a = null;

ret ~= a;
ret ~= a;

neither of these lines should do anything to ret, since they are empty arrays of the same type.

I almost think the code in question should fail to compile for being too ambiguous.
Comment 3 Don 2011-12-02 04:44:28 UTC
See bug 2006.
Comment 4 bearophile_hugs 2011-12-02 04:48:35 UTC
(In reply to comment #2)

> I almost think the code in question should fail to compile for being too
> ambiguous.

Right.
Comment 5 Steven Schveighoffer 2011-12-02 04:54:29 UTC
(In reply to comment #3)
> See bug 2006.

So is this a dupe?  I'm concerned there was a change in git that makes this "work."  Moving from one ambiguous interpretation to another doesn't sound like progress.

Do we have some definitive answer on what *should* happen?
Comment 6 Adam D. Ruppe 2011-12-02 07:47:17 UTC
(In reply to comment #2)
> I mean a null could mean a null string[], not a null string.

I oversimplified the test - in the original code it came from,
it was more like

string a = null;
ret ~= a;

which gets the same result in 2.056

I haven't tried git though. I'm not sure how to use that yet!

> I almost think the code in question should fail to compile for being too
> ambiguous.

With null itself... maybe. In the case of string[], ~= null could
have two meanings, but one of them is pretty obviously a no-op, so it's
probably not what you meant.

If a variable happens to be null, maybe doing nothing is what you wanted,
but in that case, the variable has an exact type declared so it's not
ambiguous anymore.
Comment 7 Adam D. Ruppe 2011-12-02 07:53:36 UTC
BTW, I labeled this as CTFE because in 2.056, running the same function at runtime, the assert passes.

I think this worked in CTFE a couple releases ago too, and since it works in the git version again, it was probably just a temporary regression in the interpreter.
Comment 8 Don 2011-12-14 08:35:17 UTC
This compiled on DMD2.053, but generated wrong code. It failed to compile on 2.054 to 2.056. Fixed 2.057 -- dup of bug 6077

*** This issue has been marked as a duplicate of issue 6077 ***