Skip to content

Commit e024774

Browse files
committed
fix(statementsem): correctly resolve opDispatch in WithStatement
Signed-off-by: royalpinto007 <[email protected]>
1 parent 6523a70 commit e024774

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

compiler/src/dmd/statementsem.d

+11-12
Original file line numberDiff line numberDiff line change
@@ -3183,19 +3183,19 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
31833183
else
31843184
{
31853185
Type texp = ws.exp.type;
3186-
Type t = texp.toBasetype();
3186+
if (!texp)
3187+
{
3188+
error(ws.loc, "Expression has no type.");
3189+
return setError();
3190+
}
31873191

3188-
Expression olde = ws.exp;
3189-
if (t.ty == Tpointer)
3192+
Type t = texp.toBasetype();
3193+
if (!t)
31903194
{
3191-
ws.exp = new PtrExp(ws.loc, ws.exp);
3192-
ws.exp = ws.exp.expressionSemantic(sc);
3193-
texp = ws.exp.type;
3194-
t = texp.toBasetype();
3195+
error(ws.loc, "Unable to resolve base type.");
3196+
return setError();
31953197
}
31963198

3197-
assert(t);
3198-
t = t.toBasetype();
31993199
if (t.isClassHandle())
32003200
{
32013201
_init = new ExpInitializer(ws.loc, ws.exp);
@@ -3234,8 +3234,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
32343234
ws.wthis.storage_class |= STC.temp;
32353235
ws.wthis.dsymbolSemantic(sc);
32363236
sym = new WithScopeSymbol(ws);
3237-
// Need to set the scope to make use of resolveAliasThis
3238-
sym.setScope(sc);
3237+
sym.setScope(sc); // Enable resolveAliasThis
32393238
sym.parent = sc.scopesym;
32403239
sym.endlinnum = ws.endloc.linnum;
32413240
}
@@ -3248,7 +3247,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
32483247
}
32493248
else
32503249
{
3251-
error(ws.loc, "`with` expression types must be enums or aggregates or pointers to them, not `%s`", olde.type.toChars());
3250+
error(ws.loc, "`with` expression types must be enums, structs, or classes, not `%s`", t.toChars());
32523251
return setError();
32533252
}
32543253
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation/test20274.d(28): Error: function expected before `()`, not `(*__withSym).opDispatch()` of type `void`
5+
fail_compilation/test20274.d(35): Error: undefined identifier `foo`
6+
---
7+
*/
8+
9+
// https://issues.dlang.org/show_bug.cgi?id=20274
10+
11+
struct A {
12+
void opDispatch(string name, A...)(A a) {
13+
}
14+
}
15+
16+
struct B {
17+
void opDispatch(string name, T)(T a) {
18+
}
19+
}
20+
21+
void main()
22+
{
23+
A a;
24+
a.foo(2); // ok
25+
26+
with (a)
27+
{
28+
foo(2); // fails
29+
}
30+
31+
B b;
32+
b.foo(3); // ok
33+
34+
with(b) {
35+
foo(3); // fails
36+
}
37+
38+
}

0 commit comments

Comments
 (0)