Skip to content

Commit

Permalink
improve logic in isCopyConstructable()
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored and thewilsonator committed Oct 7, 2024
1 parent 96f2899 commit 20debb4
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions compiler/src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -907,37 +907,41 @@ private extern(D) bool isCopyConstructorCallable (StructDeclaration argStruct,
*/
OutBuffer buf;
auto callExp = e.isCallExp();
void nocpctor()

bool nocpctor()
{
buf.printf("`struct %s` does not define a copy constructor for `%s` to `%s` copies",
argStruct.toChars(), arg.type.toChars(), tprm.toChars());
}
auto f = callExp.f;
if (!f)
{
nocpctor();
*pMessage = buf.extractChars();
return false;
}
char[] s;
if (!f.isPure && sc.func.setImpure())
s ~= "pure ";
if (!f.isSafe() && !f.isTrusted() && sc.setUnsafe())
s ~= "@safe ";
if (!f.isNogc && sc.func.setGC(arg.loc, null))
s ~= "nogc ";

auto f = callExp.f;
if (!f)
return nocpctor();

if (f.isDisabled() && !f.isGenerated())
{
/* https://issues.dlang.org/show_bug.cgi?id=24301
* Copy constructor is explicitly disabled
*/
buf.printf("`%s` copy constructor cannot be used because it is annotated with `@disable`",
f.type.toChars());
*pMessage = buf.extractChars();
return false;
}
else if (s)

bool bpure = !f.isPure && sc.func.setImpure();
bool bsafe = !f.isSafe() && !f.isTrusted() && sc.setUnsafe();
bool bnogc = !f.isNogc && sc.func.setGC(arg.loc, null);
if (bpure | bsafe | bnogc)
{
s[$-1] = '\0';
buf.printf("`%s` copy constructor cannot be called from a `%s` context", f.type.toChars(), s.ptr);
const nullptr = "".ptr;
buf.printf("`%s` copy constructor cannot be called from a `%s%s%s` context",
f.type.toChars(),
bpure ? "pure " .ptr : nullptr,
bsafe ? "@safe ".ptr : nullptr,
bnogc ? "nogc" .ptr : nullptr);
}
else if (f.isGenerated() && f.isDisabled())
{
Expand All @@ -953,7 +957,7 @@ private extern(D) bool isCopyConstructorCallable (StructDeclaration argStruct,
* i.e: `inout` constructor creates `const` object, not mutable.
* Fallback to using the original generic error before https://issues.dlang.org/show_bug.cgi?id=22202.
*/
nocpctor();
return nocpctor();
}

*pMessage = buf.extractChars();
Expand Down

0 comments on commit 20debb4

Please sign in to comment.