Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(statementsem): correctly resolve opDispatch in WithStatement #20671

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions compiler/src/dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -3183,19 +3183,19 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
else
{
Type texp = ws.exp.type;
Type t = texp.toBasetype();
if (!texp)
{
error(ws.loc, "Expression has no type.");
return setError();
}

Expression olde = ws.exp;
if (t.ty == Tpointer)
Type t = texp.toBasetype();
if (!t)
{
ws.exp = new PtrExp(ws.loc, ws.exp);
ws.exp = ws.exp.expressionSemantic(sc);
texp = ws.exp.type;
t = texp.toBasetype();
error(ws.loc, "Unable to resolve base type.");
return setError();
}

assert(t);
t = t.toBasetype();
if (t.isClassHandle())
{
_init = new ExpInitializer(ws.loc, ws.exp);
Expand Down Expand Up @@ -3234,8 +3234,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
ws.wthis.storage_class |= STC.temp;
ws.wthis.dsymbolSemantic(sc);
sym = new WithScopeSymbol(ws);
// Need to set the scope to make use of resolveAliasThis
sym.setScope(sc);
sym.setScope(sc); // Enable resolveAliasThis
sym.parent = sc.scopesym;
sym.endlinnum = ws.endloc.linnum;
}
Expand All @@ -3248,7 +3247,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
}
else
{
error(ws.loc, "`with` expression types must be enums or aggregates or pointers to them, not `%s`", olde.type.toChars());
error(ws.loc, "`with` expression types must be enums, structs, or classes, not `%s`", t.toChars());
return setError();
}
}
Expand Down
38 changes: 38 additions & 0 deletions compiler/test/fail_compilation/test20274.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
TEST_OUTPUT:
---
fail_compilation/test20274.d(28): Error: function expected before `()`, not `(*__withSym).opDispatch()` of type `void`
fail_compilation/test20274.d(35): Error: undefined identifier `foo`
---
*/

// https://issues.dlang.org/show_bug.cgi?id=20274

struct A {
void opDispatch(string name, A...)(A a) {
}
}

struct B {
void opDispatch(string name, T)(T a) {
}
}

void main()
{
A a;
a.foo(2); // ok

with (a)
{
foo(2); // fails
}

B b;
b.foo(3); // ok

with(b) {
foo(3); // fails
}

}
Loading