Skip to content

Commit 13b0745

Browse files
authored
Fix error when struct initializer isn't accepted (dlang#21086)
* Fix error when struct initializer isn't accepted * Rename StructDeclaration.hasRegularCtor parameter to ignoreDisabled checkDisabled detected disabled ctors when it was false, which was confusing! * Update tests
1 parent 3142290 commit 13b0745

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

compiler/src/dmd/aggregate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class StructDeclaration : public AggregateDeclaration
196196

197197
unsigned numArgTypes() const;
198198
Type *argType(unsigned index);
199-
bool hasRegularCtor(bool checkDisabled = false);
199+
bool hasRegularCtor(bool ignoreDisabled = false);
200200
};
201201

202202
class UnionDeclaration final : public StructDeclaration

compiler/src/dmd/dstruct.d

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,12 @@ extern (C++) class StructDeclaration : AggregateDeclaration
399399
* is not disabled.
400400
*
401401
* Params:
402-
* checkDisabled = if the struct has a regular
403-
non-disabled constructor
402+
* ignoreDisabled = true to ignore disabled constructors
404403
* Returns:
405404
* true, if the struct has a regular (optionally,
406405
* not disabled) constructor, false otherwise.
407406
*/
408-
final bool hasRegularCtor(bool checkDisabled = false)
407+
final bool hasRegularCtor(bool ignoreDisabled = false)
409408
{
410409
if (!ctor)
411410
return false;
@@ -415,7 +414,7 @@ extern (C++) class StructDeclaration : AggregateDeclaration
415414
{
416415
if (auto td = s.isTemplateDeclaration())
417416
{
418-
if (checkDisabled && td.onemember)
417+
if (ignoreDisabled && td.onemember)
419418
{
420419
if (auto ctorDecl = td.onemember.isCtorDeclaration())
421420
{
@@ -428,7 +427,7 @@ extern (C++) class StructDeclaration : AggregateDeclaration
428427
}
429428
if (auto ctorDecl = s.isCtorDeclaration())
430429
{
431-
if (!ctorDecl.isCpCtor && (!checkDisabled || !(ctorDecl.storage_class & STC.disable)))
430+
if (!ctorDecl.isCpCtor && (!ignoreDisabled || !(ctorDecl.storage_class & STC.disable)))
432431
{
433432
result = true;
434433
return 1;

compiler/src/dmd/frontend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7441,7 +7441,7 @@ class StructDeclaration : public AggregateDeclaration
74417441
void accept(Visitor* v) override;
74427442
uint32_t numArgTypes() const;
74437443
Type* argType(uint32_t index);
7444-
bool hasRegularCtor(bool checkDisabled = false);
7444+
bool hasRegularCtor(bool ignoreDisabled = false);
74457445
};
74467446

74477447
class UnionDeclaration final : public StructDeclaration

compiler/src/dmd/initsem.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ Initializer initializerSemantic(Initializer init, Scope* sc, ref Type tx, NeedIn
153153
// that is not disabled.
154154
if (sd.hasRegularCtor(true))
155155
{
156-
error(i.loc, "%s `%s` has constructors, cannot use `{ initializers }`, use `%s( initializers )` instead", sd.kind(), sd.toChars(), sd.toChars());
156+
error(i.loc, "Cannot use %s initializer syntax for %s `%s` because it has a constructor",
157+
sd.kind(), sd.kind(), sd.toChars());
158+
errorSupplemental(i.loc, "Use `%s( arguments )` instead of `{ initializers }`",
159+
sd.toChars());
157160
return err();
158161
}
159162
sd.size(i.loc);

compiler/test/fail_compilation/fail21547.d

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
/*
44
TEST_OUTPUT:
55
---
6-
fail_compilation/fail21547.d(32): Error: struct `Bar` has constructors, cannot use `{ initializers }`, use `Bar( initializers )` instead
7-
fail_compilation/fail21547.d(33): Error: struct `Bar1` has constructors, cannot use `{ initializers }`, use `Bar1( initializers )` instead
6+
fail_compilation/fail21547.d(34): Error: Cannot use struct initializer syntax for struct `Bar` because it has a constructor
7+
fail_compilation/fail21547.d(34): Use `Bar( arguments )` instead of `{ initializers }`
8+
fail_compilation/fail21547.d(35): Error: Cannot use struct initializer syntax for struct `Bar1` because it has a constructor
9+
fail_compilation/fail21547.d(35): Use `Bar1( arguments )` instead of `{ initializers }`
810
---
911
*/
1012

compiler/test/fail_compilation/fail336.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
22
TEST_OUTPUT:
33
---
4-
fail_compilation/fail336.d(16): Error: struct `S` has constructors, cannot use `{ initializers }`, use `S( initializers )` instead
4+
fail_compilation/fail336.d(17): Error: Cannot use struct initializer syntax for struct `S` because it has a constructor
5+
fail_compilation/fail336.d(17): Use `S( arguments )` instead of `{ initializers }`
56
---
67
*/
78

0 commit comments

Comments
 (0)