Bug 23162 – cannot use new on a static array type that's aliased
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-06-06T13:52:09Z
Last change time
2022-06-08T16:28:02Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Basile-z
Comments
Comment #0 by b2.temp — 2022-06-06T13:52:09Z
## test case
```
void main(string[] args)
{
alias A = int[1];
new A;
}
```
## output
> /tmp/temp_7F65DA6E8630.d:4:5: Error: cannot create a `int[1]` with `new`
## notes
- should be accepted
- seems to be caused because of the fix for https://issues.dlang.org/show_bug.cgi?id=11581 is applied before the type sema of the new'd stuff
## proposed patch
NewExp sema is a mess. A medium quality fix is to re-run the NewExp sema if a static array type appears after the type sema of the new'd stuff
---
@@ -3483,11 +3483,20 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
exp.type = exp.newtype.typeSemantic(exp.loc, sc);
sc = sc.pop();
}
else
{
+ auto ts1 = exp.newtype.isTypeSArray();
exp.type = exp.newtype.typeSemantic(exp.loc, sc);
+ // got an alias, but fix for 11581 is missed
+ if (!ts1 && exp.type.isTypeSArray())
+ {
+ exp.newtype = exp.type;
+ exp.type = null;
+ visit(exp);
+ return;
+ }
}
if (exp.type.ty == Terror)
return setError();
if (edim)
---
Comment #1 by razvan.nitu1305 — 2022-06-08T13:05:29Z
I think that this bug report is invalid. As can be seen, the grammar for new expression is: https://dlang.org/spec/grammar.html#NewExpression .
So the parser sees `new A` and it thinks that you have the first form of new expression. And I think this is the right behavior. Since A is binded to int[1], the compiler thinks you are newing a type and it is illegal to allocate a static array with the GC. Adding this special case in the compiler is not worth it.