Skip to content

Commit 75fe50f

Browse files
ImperatornJohan Forsberg
and
Johan Forsberg
authoredMar 19, 2021
Fix issue 21687 - Confusing error message for CTFE (#12287)
Co-authored-by: Johan Forsberg <johan.forsberg@akerstroms.se>
1 parent 939778e commit 75fe50f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed
 

‎src/dmd/initsem.d

+6-1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ extern(C++) Initializer initializerSemantic(Initializer init, Scope* sc, Type t,
382382
return new ErrorInitializer();
383383
}
384384
uint olderrors = global.errors;
385+
/* Save the expression before ctfe
386+
* Otherwise the error message would contain for example "&[0][0]" instead of "new int"
387+
* Regression: https://issues.dlang.org/show_bug.cgi?id=21687
388+
*/
389+
Expression currExp = i.exp;
385390
if (needInterpret)
386391
{
387392
// If the result will be implicitly cast, move the cast into CTFE
@@ -421,7 +426,7 @@ extern(C++) Initializer initializerSemantic(Initializer init, Scope* sc, Type t,
421426
// Make sure all pointers are constants
422427
if (needInterpret && hasNonConstPointers(i.exp))
423428
{
424-
i.exp.error("cannot use non-constant CTFE pointer in an initializer `%s`", i.exp.toChars());
429+
i.exp.error("cannot use non-constant CTFE pointer in an initializer `%s`", currExp.toChars());
425430
return new ErrorInitializer();
426431
}
427432
Type tb = t.toBasetype();

‎test/fail_compilation/test15989.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ TEST_OUTPUT:
33
---
44
fail_compilation/test15989.d(39): Error: variable `test15989.main.ctRegex` : Unable to initialize enum with class or pointer to struct. Use static const variable instead.
55
fail_compilation/test15989.d(48): Error: variable `test15989.test.c` : Unable to initialize enum with class or pointer to struct. Use static const variable instead.
6-
fail_compilation/test15989.d(49): Error: cannot use non-constant CTFE pointer in an initializer `&[3][0]`
6+
fail_compilation/test15989.d(49): Error: cannot use non-constant CTFE pointer in an initializer `new int(3)`
77
---
88
*/
99

0 commit comments

Comments
 (0)
Please sign in to comment.