Skip to content

Commit 6826e2f

Browse files
committed
[error] Reject instantiation of abstract and static classes.
Fix #200
1 parent fa2c3fa commit 6826e2f

File tree

7 files changed

+61
-1
lines changed

7 files changed

+61
-1
lines changed

Sema.fu

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,16 @@ public class FuSema
16571657
if (call.Method.Name == "string")
16581658
return this.Host.Program.System.StringStorageType;
16591659
if (this.Host.Program.TryLookup(call.Method.Name, true) is FuClass! klass2) {
1660+
switch (klass2.CallType) {
1661+
case FuCallType.Static:
1662+
ReportError(expr, "Cannot instantiate static class");
1663+
break;
1664+
case FuCallType.Abstract:
1665+
ReportError(expr, "Cannot instantiate abstract class");
1666+
break;
1667+
default:
1668+
break;
1669+
}
16601670
call.Method.Symbol = klass2; // for vscode goto
16611671
return new FuStorageType { Class = klass2 };
16621672
}

libfut.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6239,6 +6239,16 @@ std::shared_ptr<FuType> FuSema::toBaseType(FuExpr * expr, FuToken ptrModifier, b
62396239
if (call->method->name == "string")
62406240
return this->host->program->system->stringStorageType;
62416241
if (FuClass *klass2 = dynamic_cast<FuClass *>(this->host->program->tryLookup(call->method->name, true).get())) {
6242+
switch (klass2->callType) {
6243+
case FuCallType::static_:
6244+
reportError(expr, "Cannot instantiate static class");
6245+
break;
6246+
case FuCallType::abstract:
6247+
reportError(expr, "Cannot instantiate abstract class");
6248+
break;
6249+
default:
6250+
break;
6251+
}
62426252
call->method->symbol = klass2;
62436253
std::shared_ptr<FuStorageType> futemp0 = std::make_shared<FuStorageType>();
62446254
futemp0->class_ = klass2;

libfut.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6326,6 +6326,16 @@ FuType ToBaseType(FuExpr expr, FuToken ptrModifier, bool nullable)
63266326
if (call.Method.Name == "string")
63276327
return this.Host.Program.System.StringStorageType;
63286328
if (this.Host.Program.TryLookup(call.Method.Name, true) is FuClass klass2) {
6329+
switch (klass2.CallType) {
6330+
case FuCallType.Static:
6331+
ReportError(expr, "Cannot instantiate static class");
6332+
break;
6333+
case FuCallType.Abstract:
6334+
ReportError(expr, "Cannot instantiate abstract class");
6335+
break;
6336+
default:
6337+
break;
6338+
}
63296339
call.Method.Symbol = klass2;
63306340
return new FuStorageType { Class = klass2 };
63316341
}

libfut.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6703,6 +6703,16 @@ export class FuSema
67036703
return this.#host.program.system.stringStorageType;
67046704
let klass2;
67056705
if ((klass2 = this.#host.program.tryLookup(call.method.name, true)) instanceof FuClass) {
6706+
switch (klass2.callType) {
6707+
case FuCallType.STATIC:
6708+
this.#reportError(expr, "Cannot instantiate static class");
6709+
break;
6710+
case FuCallType.ABSTRACT:
6711+
this.#reportError(expr, "Cannot instantiate abstract class");
6712+
break;
6713+
default:
6714+
break;
6715+
}
67066716
call.method.symbol = klass2;
67076717
return Object.assign(new FuStorageType(), { class: klass2 });
67086718
}

test/error/ClassAbstractInstance.fu

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public abstract class Test
2+
{
3+
public static bool Run()
4+
{
5+
Test() o; //ERROR: Cannot instantiate abstract class
6+
Test()[3] a; //ERROR: Cannot instantiate abstract class
7+
Test# p = new Test(); //ERROR: Cannot instantiate abstract class
8+
return true;
9+
}
10+
}

test/error/ClassStaticInstance.fu

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public static class Test
2+
{
3+
public static bool Run()
4+
{
5+
Test() o; //ERROR: Cannot instantiate static class
6+
Test()[3] a; //ERROR: Cannot instantiate static class
7+
Test# p = new Test(); //ERROR: Cannot instantiate static class
8+
return true;
9+
}
10+
}

test/error/OpIndexObject.fu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public static class Test
1+
public class Test
22
{
33
public static bool Run()
44
{

0 commit comments

Comments
 (0)