Skip to content

Commit b0e7164

Browse files
committed
test-solution
1 parent c07fdc5 commit b0e7164

File tree

8 files changed

+59
-7
lines changed

8 files changed

+59
-7
lines changed

compiler/src/dmd/astbase.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3867,6 +3867,7 @@ struct ASTBase
38673867
// .next is the return type
38683868

38693869
ParameterList parameterList; // function parameters
3870+
Loc paramCloseLoc; // location of closing ')' of parameter list, for diagnostics
38703871

38713872
private enum FunctionFlag : uint
38723873
{
@@ -3945,6 +3946,7 @@ struct ASTBase
39453946
t.isInOutQual = isInOutQual;
39463947
t.trust = trust;
39473948
t.fargs = fargs;
3949+
t.paramCloseLoc = paramCloseLoc;
39483950
return t;
39493951
}
39503952

compiler/src/dmd/funcsem.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,10 +2116,11 @@ FuncDeclaration resolveFuncCall(Loc loc, Scope* sc, Dsymbol s,
21162116
.error(loc, "%smethod `%s` is not callable using a %sobject",
21172117
funcBuf.peekChars(), fd.toPrettyChars(), thisBuf.peekChars());
21182118

2119+
const qualLoc = tf.paramCloseLoc != Loc.initial ? tf.paramCloseLoc : fd.loc;
21192120
if (mismatches.isNotShared)
2120-
.errorSupplemental(fd.loc, "Consider adding `shared` here");
2121+
.errorSupplemental(qualLoc, "Consider adding `shared` here");
21212122
else if (mismatches.isMutable)
2122-
.errorSupplemental(fd.loc, "Consider adding `const` or `inout` here");
2123+
.errorSupplemental(qualLoc, "Consider adding `const` or `inout` here");
21232124
return null;
21242125
}
21252126

compiler/src/dmd/mtype.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,7 @@ extern (C++) final class TypeFunction : TypeNext
13431343
import dmd.common.bitfields : generateBitFields;
13441344
mixin(generateBitFields!(BitFields, ushort));
13451345

1346+
Loc paramCloseLoc; // location of closing ')' of parameter list, for diagnostics
13461347
LINK linkage; // calling convention
13471348
TRUST trust; // level of trust
13481349
PURE purity = PURE.impure;
@@ -1425,6 +1426,7 @@ extern (C++) final class TypeFunction : TypeNext
14251426
t.trust = trust;
14261427
t.inferenceArguments = inferenceArguments;
14271428
t.isCtor = isCtor;
1429+
t.paramCloseLoc = paramCloseLoc;
14281430
return t;
14291431
}
14301432

compiler/src/dmd/mtype.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ class TypeFunction final : public TypeNext
462462
// .next is the return type
463463

464464
ParameterList parameterList; // function parameters
465+
Loc paramCloseLoc; // location of closing ')' of parameter list, for diagnostics
465466
uint16_t bitFields;
466467
LINK linkage; // calling convention
467468
TRUST trust; // level of trust

compiler/src/dmd/parse.d

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4262,14 +4262,16 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
42624262
}
42634263

42644264
auto parameterList = parseParameterList(null);
4265+
const paramCloseLoc = prevloc; // location of closing ')'
42654266

42664267
/* Parse const/immutable/shared/inout/nothrow/pure/return postfix
42674268
*/
42684269
// merge prefix storage classes
42694270
STC stc = parsePostfix(storageClass, pudas);
42704271

4271-
AST.Type tf = new AST.TypeFunction(parameterList, t, linkage, stc);
4272-
tf = AST.addSTC(tf, stc);
4272+
auto tfunc = new AST.TypeFunction(parameterList, t, linkage, stc);
4273+
tfunc.paramCloseLoc = paramCloseLoc;
4274+
AST.Type tf = AST.addSTC(tfunc, stc);
42734275
if (pdisable)
42744276
*pdisable = stc & STC.disable ? true : false;
42754277

compiler/src/dmd/statementsem.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,9 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
828828
!fd.type.mod ? "mutable" : fd.type.modToChars(),
829829
fd.toPrettyChars(),
830830
fs.aggr.type.toChars());
831-
errorSupplemental(fd.loc, "Consider adding a method type qualifier here");
831+
const fdtf = fd.type.isTypeFunction();
832+
const qualLoc = fdtf && fdtf.paramCloseLoc != Loc.initial ? fdtf.paramCloseLoc : fd.loc;
833+
errorSupplemental(qualLoc, "Consider adding a method type qualifier here");
832834
return setError();
833835
}
834836
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://issues.dlang.org/show_bug.cgi?id=22125
2+
3+
/*
4+
REQUIRED_ARGS: -verrors=context
5+
TEST_OUTPUT:
6+
---
7+
fail_compilation/issue22125.d(37): Error: mutable method `issue22125.S.doStuff` is not callable using a `const` object
8+
s.doStuff();
9+
^
10+
fail_compilation/issue22125.d(30): Consider adding `const` or `inout` here
11+
void doStuff() { }
12+
^
13+
fail_compilation/issue22125.d(38): Error: mutable method `issue22125.S.doStuffWithArgs` is not callable using a `const` object
14+
s.doStuffWithArgs(1);
15+
^
16+
fail_compilation/issue22125.d(31): Consider adding `const` or `inout` here
17+
void doStuffWithArgs(int a) { }
18+
^
19+
fail_compilation/issue22125.d(40): Error: non-shared method `issue22125.S.doStuff` is not callable using a `shared` object
20+
ss.doStuff();
21+
^
22+
fail_compilation/issue22125.d(30): Consider adding `shared` here
23+
void doStuff() { }
24+
^
25+
---
26+
*/
27+
28+
struct S
29+
{
30+
void doStuff() { }
31+
void doStuffWithArgs(int a) { }
32+
}
33+
34+
void test()
35+
{
36+
const S s;
37+
s.doStuff();
38+
s.doStuffWithArgs(1);
39+
shared S ss;
40+
ss.doStuff();
41+
}
42+

compiler/test/fail_compilation/test24353.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ fail_compilation/test24353.d(37): Error: mutable method `test24353.S.opApply` is
99
^
1010
fail_compilation/test24353.d(28): Consider adding a method type qualifier here
1111
int opApply(int delegate(int) dg)
12-
^
12+
^
1313
fail_compilation/test24353.d(40): Error: shared const method `test24353.S2.opApply` is not callable using a `const(S2)` foreach aggregate
1414
foreach (i, e; s2) {} // mod error
1515
^
1616
fail_compilation/test24353.d(47): Consider adding a method type qualifier here
1717
int opApply(int delegate(int, int) dg) const shared;
18-
^
18+
^
1919
fail_compilation/test24353.d(42): Error: cannot uniquely infer `foreach` argument types
2020
foreach (i, e; const S3()) {} // cannot infer
2121
^

0 commit comments

Comments
 (0)