diff --git a/compiler/src/dmd/dsymbolsem.d b/compiler/src/dmd/dsymbolsem.d index 173532af397..65aab8a6ded 100644 --- a/compiler/src/dmd/dsymbolsem.d +++ b/compiler/src/dmd/dsymbolsem.d @@ -2432,6 +2432,24 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor sc.stc &= ~STC.static_; // not a static constructor funcDeclarationSemantic(sc, ctd); + // Check short constructor: this() => expr; + if (ctd.fbody) + { + if (auto s = ctd.fbody.isExpStatement()) + { + if (s.exp) + { + auto ce = s.exp.isCallExp(); + // check this/super before semantic + if (!ce || (!ce.e1.isThisExp() && !ce.e1.isSuperExp())) + { + s.exp = s.exp.expressionSemantic(sc); + if (s.exp.type.ty != Tvoid) + error(s.loc, "can only return void expression, `this` call or `super` call from constructor"); + } + } + } + } sc.pop(); diff --git a/compiler/test/compilable/shortened_methods.d b/compiler/test/compilable/shortened_methods.d index 81d6da8734e..76a4c8a55db 100644 --- a/compiler/test/compilable/shortened_methods.d +++ b/compiler/test/compilable/shortened_methods.d @@ -15,7 +15,7 @@ class A { bool isNull() => this is null; this() {} - this(int x) => _x = x; + this(int x) { _x = x; } this(float y) => this(cast(int) y); } diff --git a/compiler/test/fail_compilation/short_fn.d b/compiler/test/fail_compilation/short_fn.d new file mode 100644 index 00000000000..04f3e3fffbf --- /dev/null +++ b/compiler/test/fail_compilation/short_fn.d @@ -0,0 +1,15 @@ +/* +TEST_OUTPUT: +--- +fail_compilation/short_fn.d(13): Error: can only return void expression, `this` call or `super` call from constructor +fail_compilation/short_fn.d(14): Error: can only return void expression, `this` call or `super` call from constructor +--- +*/ + +struct Number +{ + int x; + + this(int x) => this.x = x; + this(byte x) => Number(); +}