Skip to content

Commit

Permalink
Fix Issue 22054 - Referencing a fwd-declared field results in many er…
Browse files Browse the repository at this point in the history
…ror messages
  • Loading branch information
RazvanN7 committed Jul 5, 2021
1 parent 8423098 commit ef3ff05
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/dmd/dclass.d
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ extern (C++) class ClassDeclaration : AggregateDeclaration
if (!members || !symtab) // opaque or addMember is not yet done
{
// .stringof is always defined (but may be hidden by some other symbol)
if (ident != Id.stringof && !(flags & IgnoreErrors))
if (ident != Id.stringof && !(flags & IgnoreErrors) && semanticRun < PASS.semanticdone)
error("is forward referenced when looking for `%s`", ident.toChars());
//*(char*)0=0;
return null;
Expand Down
10 changes: 9 additions & 1 deletion src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2387,8 +2387,12 @@ Expression getProperty(Type t, Scope* scope_, const ref Loc loc, Identifier iden
error(loc, "no property `%s` for type `%s`", ident.toChars(), mt.toPrettyChars(true));
if (auto dsym = mt.toDsymbol(scope_))
if (auto sym = dsym.isAggregateDeclaration())
{
if (auto fd = search_function(sym, Id.opDispatch))
errorSupplemental(loc, "potentially malformed `opDispatch`. Use an explicit instantiation to get a better error message");
else if (!sym.members)
errorSupplemental(loc, "`%s` `%s` is forward declared and does not have a body.", sym.kind, mt.toPrettyChars(true));
}
}
}
e = ErrorExp.get();
Expand Down Expand Up @@ -3730,7 +3734,11 @@ Expression dotExp(Type mt, Scope* sc, Expression e, Identifier ident, int flag)
assert(mt.ty == Tstruct || mt.ty == Tclass);
auto sym = mt.toDsymbol(sc).isAggregateDeclaration();
assert(sym);
if (ident != Id.__sizeof &&
if (// https://issues.dlang.org/show_bug.cgi?id=22054
// if a class or struct does not have a body
// there is no point in searching for its members
sym.members &&
ident != Id.__sizeof &&
ident != Id.__xalignof &&
ident != Id._init &&
ident != Id._mangleof &&
Expand Down
16 changes: 16 additions & 0 deletions test/fail_compilation/fail22054.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://issues.dlang.org/show_bug.cgi?id=22054

/*
TEST_OUTPUT:
---
fail_compilation/test22054.d(15): Error: no property `what` for type `test22054.exception`
fail_compilation/test22054.d(15): `class` `test22054.exception` is forward declared and does not have a body.
---
*/

class exception;

void main ()
{
assert(exception.what() == "Hello");
}

0 comments on commit ef3ff05

Please sign in to comment.