D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3006 - ICE(e2ir.c, tocsym.c) template module using array operation
Summary: ICE(e2ir.c, tocsym.c) template module using array operation
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: Walter Bright
URL:
Keywords: ice-on-valid-code, patch
Depends on:
Blocks:
 
Reported: 2009-05-17 22:55 UTC by ZY Zhou
Modified: 2014-04-18 09:12 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description ZY Zhou 2009-05-17 22:55:04 UTC
dmd testmodule.d test.d -oftest
Internal error: e2ir.c 632

testmodule.d:
-----------------------------------
module testmodule;
template foo(T)
{
    void foo()
    {
        T[3] a,b,c;
        a[] = b[] + c[];
    }
}
-----------------------------------

test.d:
-----------------------------------
import testmodule;
void main()
{
    foo!ulong();
        // long, double, real, cfloat etc. have this bug
        // int, short, float etc. work fine
}
-----------------------------------
Comment 1 Don 2009-05-19 01:18:11 UTC
Fails also on D1 with a bizarre error message. Where the heck did variable 'p' come from?

dmd bug2.d bug.d  (order is important).
================
Error: variable p forward referenced
Error: variable p forward referenced
linkage = 0
Assertion failure: '0' on line 262 in file 'tocsym.c'

abnormal program termination
===========

bug.d
------
import bug2;
void main()
{
    foo!(long)();
}
------
bug2.d
------
void foo(T)() {
   long[] a;
   a[] = -a[];
}
Comment 2 Don 2009-06-03 16:07:40 UTC
Root cause: semantic3 never gets run on the function which 
is created, if it's compiled in a module which doesn't instantiate the
template it's in. To fix this, we can run its semantic3 immediately, since we're already in semantic3 for this module.
BTW: It also seems to me that the back-end should have
assert(FuncDeclaration->semanticRun==4) before running toObj(), to ensure
semantic3 has been run (it would be a better place to ICE).
BTW: FuncDeclaration::toObj() is declared but never defined or used.

PATCH: Add this code to arrayop.c, BinExp::arrayOp, line 393.

	    sc->linkage = LINKc;
	    fd->semantic(sc);
+		  fd->semantic2(sc);
+		  fd->semantic3(sc);
	    sc->pop();

// TEST CASE 1. Compilation order is important.
dmd bugx.d bug.d
------
bug.d
------
import bugx;
void main(){
    foo!(long)();
}
------
bugx.d
------
void foo(T)() {
   long[] a;
   a[] = -a[];
}
// TEST CASE 2: replace bugx with:
void foo(T)() {
   T[] a;
   a[] = -a[];
}
Comment 3 HOSOKAWA Kenchi 2009-08-20 13:06:22 UTC
(In reply to comment #2)

Much thanks for your analysis, Don.
I met almost the same error today.

// bugx.d
void foo() {
	real[3] s, a, b;
	s[] = a[] + b[];
}

// bug.d
void bar() {
	foo; // p2 Internal error: e2ir.c 632 @ DMD 2.031
}

This ICE occurs without template in this case.
Compiling bugx.d cause no error, calling foo from OTHER module cause ICE.

Besides, compiler switchs affect:
( -release && !-unittest ) cause this ICE.

Anyway the root cause is obviously the same.
Comment 4 Walter Bright 2009-10-13 13:45:08 UTC
Fixed dmd 1.049 and 2.034