D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 23162 - cannot use new on a static array type that's aliased
Summary: cannot use new on a static array type that's aliased
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2022-06-06 13:52 UTC by basile-z
Modified: 2022-06-08 16:28 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description basile-z 2022-06-06 13:52:09 UTC
## 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 RazvanN 2022-06-08 13:05:29 UTC
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.
Comment 2 basile-z 2022-06-08 16:28:02 UTC
indeed, TIL `new int[1]` is like `new int[](1)`.