diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index e1baa4887e..9e332986a4 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -5677,7 +5677,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor symtab = sds.symtab; } assert(symtab); - Identifier id = Identifier.generateIdWithLoc(s, exp.loc); + Identifier id = Identifier.generateIdWithLoc(s, exp.loc, cast(string) toDString(sc.parent.toPrettyChars())); exp.fd.ident = id; if (exp.td) exp.td.ident = id; diff --git a/compiler/src/dmd/identifier.d b/compiler/src/dmd/identifier.d index 6fd0d3ad5e..74be1beb29 100644 --- a/compiler/src/dmd/identifier.d +++ b/compiler/src/dmd/identifier.d @@ -211,11 +211,14 @@ nothrow: * Params: * prefix = first part of the identifier name. * loc = source location to use in the identifier name. + * parent = (optional) extra part to be used in uniqueness check, + * if (prefix1, loc1) == (prefix2, loc2), but + * parent1 != parent2, no new name will be generated. * Returns: * Identifier (inside Identifier.idPool) with deterministic name based * on the source location. */ - extern (D) static Identifier generateIdWithLoc(string prefix, const ref Loc loc) + extern (D) static Identifier generateIdWithLoc(string prefix, const ref Loc loc, string parent = "") { // generate `_L_C` OutBuffer idBuf; @@ -234,14 +237,20 @@ nothrow: * https://issues.dlang.org/show_bug.cgi?id=18880 * https://issues.dlang.org/show_bug.cgi?id=18868 * https://issues.dlang.org/show_bug.cgi?id=19058 + * + * It is a bit trickier for lambdas/dgliterals: we want them to be unique per + * module/mixin + function/template instantiation context. So we use extra parent + * argument for that when dealing with lambdas. We could have added it to prefix + * directly, but that would unnecessary lengthen symbols names. See issue: + * https://issues.dlang.org/show_bug.cgi?id=23722 */ - static struct Key { Loc loc; string prefix; } + static struct Key { Loc loc; string prefix; string parent; } __gshared uint[Key] counters; static if (__traits(compiles, counters.update(Key.init, () => 0u, (ref uint a) => 0u))) { // 2.082+ - counters.update(Key(loc, prefix), + counters.update(Key(loc, prefix, parent), () => 1u, // insertion (ref uint counter) // update { @@ -253,7 +262,7 @@ nothrow: } else { - const key = Key(loc, prefix); + const key = Key(loc, prefix, parent); if (auto pCounter = key in counters) { idBuf.writestring("_"); diff --git a/compiler/test/runnable/imports/test23722_2b.d b/compiler/test/runnable/imports/test23722_2b.d new file mode 100644 index 0000000000..b0e54a936f --- /dev/null +++ b/compiler/test/runnable/imports/test23722_2b.d @@ -0,0 +1,13 @@ +module imports.test23722_2b; + +struct T(alias fun) { } + +struct S(int i) { + auto t = T!(x => i)(); +} + +string g() { + S!0 s0; + S!1 s1; + return s1.t.init.mangleof; +} diff --git a/compiler/test/runnable/test23722_2.d b/compiler/test/runnable/test23722_2.d new file mode 100644 index 0000000000..cce4629933 --- /dev/null +++ b/compiler/test/runnable/test23722_2.d @@ -0,0 +1,10 @@ +// COMPILE_SEPARATELY: +// EXTRA_SOURCES: imports/test23722_2b.d +// https://issues.dlang.org/show_bug.cgi?id=23722 +// Lambdas are mangled incorrectly when using multiple compilation units, resulting in incorrect code +import imports.test23722_2b; + +void main() { + S!1 s1; + assert(s1.t.init.mangleof == g); +}