Skip to content

Commit 0d37000

Browse files
committed
Improve error message when aggregate template is used as a type
Note: s.kind is just "struct" for struct templates. Note: When td.onemember is an alias, we don't know if it resolves to a type or not until the template is instantiated.
1 parent 7674cde commit 0d37000

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/dmd/typesem.d

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,12 @@ private extern (C++) final class TypeSemanticVisitor : Visitor
11561156
{
11571157
if (s)
11581158
{
1159-
s.error(loc, "is used as a type");
1159+
auto td = s.isTemplateDeclaration;
1160+
if (td && td.onemember && td.onemember.isAggregateDeclaration)
1161+
mtype.error(loc, "%s template `%s` is used as a type without instantiation",
1162+
s.kind, s.toPrettyChars);
1163+
else
1164+
mtype.error(loc, "%s `%s` is used as a type", s.kind, s.toPrettyChars);
11601165
//assert(0);
11611166
}
11621167
else

test/fail_compilation/notype.d

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct S(int var = 3) {
2+
int a;
3+
}
4+
S s;
5+
6+
alias A() = int;
7+
A a;
8+
9+
enum e() = 5;
10+
e val;
11+
12+
interface I()
13+
{
14+
}
15+
I i;
16+
17+
template t()
18+
{
19+
}
20+
t tv;
21+
22+
/*
23+
TEST_OUTPUT:
24+
---
25+
fail_compilation/notype.d(4): Error: struct template `notype.S(int var = 3)` is used as a type without instantiation
26+
fail_compilation/notype.d(7): Error: template `notype.A()` is used as a type
27+
fail_compilation/notype.d(10): Error: template `notype.e()` is used as a type
28+
fail_compilation/notype.d(15): Error: interface template `notype.I()` is used as a type without instantiation
29+
fail_compilation/notype.d(20): Error: template `notype.t()` is used as a type
30+
---
31+
*/

0 commit comments

Comments
 (0)