From 20debb42d89cbea20460402b8e7dfd3ae6b10c9c Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Fri, 4 Oct 2024 17:12:50 -0700 Subject: [PATCH] improve logic in isCopyConstructable() --- compiler/src/dmd/typesem.d | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/compiler/src/dmd/typesem.d b/compiler/src/dmd/typesem.d index d951abbf4357..c6306021c8cf 100644 --- a/compiler/src/dmd/typesem.d +++ b/compiler/src/dmd/typesem.d @@ -907,25 +907,19 @@ 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 @@ -933,11 +927,21 @@ private extern(D) bool isCopyConstructorCallable (StructDeclaration argStruct, */ 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()) { @@ -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();