Skip to content

Commit

Permalink
[error] base in static context.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Jun 27, 2024
1 parent 41fcbb3 commit fa5cf71
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 20 deletions.
7 changes: 5 additions & 2 deletions Sema.fu
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,13 @@ public class FuSema
FuScope scope;
bool isBase = left is FuSymbolReference baseSymbol && baseSymbol.Symbol.Id == FuId.BasePtr;
if (isBase) {
if (this.CurrentMethod == null || !(this.CurrentMethod.Parent.Parent is FuClass baseClass))
if (this.CurrentMethod == null)
return PoisonError(left, "'base' invalid outside methods");
if (this.CurrentMethod.IsStatic())
return PoisonError(left, "'base' invalid in static context");
if (!(this.CurrentMethod.Parent.Parent is FuClass baseClass))
return PoisonError(left, "No base class");
scope = baseClass;
// TODO: static?
}
else if (left is FuSymbolReference leftSymbol && leftSymbol.Symbol is FuScope obj)
scope = obj;
Expand Down
6 changes: 5 additions & 1 deletion libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4655,8 +4655,12 @@ std::shared_ptr<FuExpr> FuSema::visitSymbolReference(std::shared_ptr<FuSymbolRef
const FuSymbolReference * baseSymbol;
bool isBase = (baseSymbol = dynamic_cast<const FuSymbolReference *>(left.get())) && baseSymbol->symbol->id == FuId::basePtr;
if (isBase) {
if (this->currentMethod == nullptr)
return poisonError(left.get(), "'base' invalid outside methods");
if (this->currentMethod->isStatic())
return poisonError(left.get(), "'base' invalid in static context");
const FuClass * baseClass;
if (this->currentMethod == nullptr || !(baseClass = dynamic_cast<const FuClass *>(this->currentMethod->parent->parent)))
if (!(baseClass = dynamic_cast<const FuClass *>(this->currentMethod->parent->parent)))
return poisonError(left.get(), "No base class");
scope = baseClass;
}
Expand Down
6 changes: 5 additions & 1 deletion libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4911,7 +4911,11 @@ FuExpr VisitSymbolReference(FuSymbolReference expr)
FuScope scope;
bool isBase = left is FuSymbolReference baseSymbol && baseSymbol.Symbol.Id == FuId.BasePtr;
if (isBase) {
if (this.CurrentMethod == null || !(this.CurrentMethod.Parent.Parent is FuClass baseClass))
if (this.CurrentMethod == null)
return PoisonError(left, "'base' invalid outside methods");
if (this.CurrentMethod.IsStatic())
return PoisonError(left, "'base' invalid in static context");
if (!(this.CurrentMethod.Parent.Parent is FuClass baseClass))
return PoisonError(left, "No base class");
scope = baseClass;
}
Expand Down
6 changes: 5 additions & 1 deletion libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -5147,8 +5147,12 @@ export class FuSema
let baseSymbol;
let isBase = (baseSymbol = left) instanceof FuSymbolReference && baseSymbol.symbol.id == FuId.BASE_PTR;
if (isBase) {
if (this.#currentMethod == null)
return this.#poisonError(left, "'base' invalid outside methods");
if (this.#currentMethod.isStatic())
return this.#poisonError(left, "'base' invalid in static context");
let baseClass;
if (this.#currentMethod == null || !((baseClass = this.#currentMethod.parent.parent) instanceof FuClass))
if (!((baseClass = this.#currentMethod.parent.parent) instanceof FuClass))
return this.#poisonError(left, "No base class");
scope = baseClass;
}
Expand Down
2 changes: 1 addition & 1 deletion test/error/ConstValueBase.fu
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public static class Test
{
const int Foo = base.Bar(); //ERROR: No base class
const int Foo = base.Bar(); //ERROR: 'base' invalid outside methods

public static bool Run()
{
Expand Down
9 changes: 4 additions & 5 deletions test/error/MethodBaseNoBase.fu
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
public static class Test
public class Test
{
public static bool Run()
{
return base.Foo(); //ERROR: No base class
}
bool Foo() => base.Bar(); //ERROR: No base class

public static bool Run() => true;
}
7 changes: 3 additions & 4 deletions test/error/MethodBaseNotFound.fu
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ public class Base

public class Test : Base
{
public static bool Run()
{
return base.Foo(); //ERROR: 'Foo' not found
}
bool Foo() => base.Bar(); //ERROR: 'Bar' not found

public static bool Run() => true;
}
9 changes: 4 additions & 5 deletions test/error/MethodBaseNotMethod.fu
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
public class Base
{
internal int Foo;
internal int Bar;
}

public class Test : Base
{
public static bool Run()
{
return base.Foo(); //ERROR: Expected a method
}
bool Foo() => base.Bar(); //ERROR: Expected a method

public static bool Run() => true;
}
12 changes: 12 additions & 0 deletions test/error/MethodBaseStatic.fu
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public abstract class Base
{
protected bool Foo() => true;
}

public class Test : Base
{
public static bool Run()
{
return base.Foo(); //ERROR: 'base' invalid in static context
}
}

0 comments on commit fa5cf71

Please sign in to comment.