Skip to content

Commit

Permalink
feat(types): add check if overridden functions have super function
Browse files Browse the repository at this point in the history
Fixes #1022
  • Loading branch information
i582 committed Jan 13, 2025
1 parent ed30e4c commit f997e13
Show file tree
Hide file tree
Showing 20 changed files with 5,515 additions and 15 deletions.
5,325 changes: 5,325 additions & 0 deletions src/types/__snapshots__/resolveDescriptors.spec.ts.snap

Large diffs are not rendered by default.

57 changes: 42 additions & 15 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import {
AstAsmFunctionDef,
AstConstantDecl,
AstConstantDef,
AstFieldDecl,
AstContractInit,
AstExpression,
AstFieldDecl,
AstFunctionDecl,
AstFunctionDef,
AstId,
AstMapType,
AstNativeFunctionDecl,
AstNode,
AstType,
idText,
AstId,
AstTypeId,
eqNames,
AstFunctionDef,
FactoryAst,
idText,
isSelfId,
isSlice,
AstFunctionDecl,
AstConstantDecl,
AstExpression,
AstMapType,
AstTypeId,
AstAsmFunctionDef,
FactoryAst,
} from "../grammar/ast";
import { traverse } from "../grammar/iterators";
import {
idTextErr,
throwCompilationError,
throwInternalCompilerError,
} from "../errors";
import { CompilerContext, Store, createContextStore } from "../context";
import { CompilerContext, createContextStore, Store } from "../context";
import {
ConstantDescription,
FieldDescription,
FunctionParameter,
FunctionDescription,
InitParameter,
FunctionParameter,
InitDescription,
InitParameter,
printTypeRef,
ReceiverSelector,
receiverSelectorName,
Expand All @@ -46,9 +46,9 @@ import { crc16 } from "../utils/crc16";
import { isSubsetOf } from "../utils/isSubsetOf";
import { evalConstantExpression } from "../constEval";
import {
resolveABIType,
intMapKeyFormats,
intMapValFormats,
resolveABIType,
} from "./resolveABITypeRef";
import { enabledExternals } from "../config/features";
import { isRuntimeType } from "./isRuntimeType";
Expand Down Expand Up @@ -1562,6 +1562,33 @@ export function resolveDescriptors(ctx: CompilerContext, Ast: FactoryAst) {
//

function copyTraits(contractOrTrait: TypeDescription) {
const inheritOnlyBaseTrait = contractOrTrait.traits.length === 1;

// Check that "override" functions have a super function
for (const funInContractOrTrait of contractOrTrait.functions.values()) {
if (!funInContractOrTrait.isOverride) {
continue;
}

const foundOverriddenFunction = contractOrTrait.traits.some(
(inheritedTrait) => {
return (
inheritedTrait.functions.get(
funInContractOrTrait.name,
) !== undefined
);
},
);

if (!foundOverriddenFunction) {
const msg = inheritOnlyBaseTrait
? `Function "${funInContractOrTrait.name}" overrides nothing, remove "override" modifier or inherit any traits with this function`
: `Function "${funInContractOrTrait.name}" overrides nothing, remove "override" modifier`;

throwCompilationError(msg, funInContractOrTrait.ast.loc);
}
}

for (const inheritedTrait of contractOrTrait.traits) {
// Copy functions
for (const traitFunction of inheritedTrait.functions.values()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait BaseTrait {
fun bar() {}
}

trait Test {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait BaseTrait {}
trait T {
fun bar() {}
}

trait T2 {
fun baz() {}
}

contract Test with T, T2 {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait BaseTrait {}

trait Test {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait BaseTrait {}
trait T {}

trait Test with T {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait BaseTrait {
fun bar() {}
}

contract Test {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait BaseTrait {}
trait T {
fun bar() {}
}

trait T2 {
fun baz() {}
}

trait Test with T, T2 {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait BaseTrait {}

contract Test {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait BaseTrait {}
trait T {}

contract Test with T {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait BaseTrait {
fun foo() {}
}

trait Test {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
trait BaseTrait {
fun foo() {}
}
trait T {
fun bar() {}
}

trait T2 {
fun baz() {}
}

contract Test with T, T2 {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
trait BaseTrait {}
trait T {}

trait T2 {
fun foo() {}
}

contract Test with T, T2 {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait BaseTrait {}

trait Test {
fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trait BaseTrait {}
trait T {
fun foo() {}
}

trait Test with T {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait BaseTrait {
fun foo() {}
}

contract Test {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
trait BaseTrait {
fun foo() {}
}
trait T {
fun bar() {}
}

trait T2 {
fun baz() {}
}

trait Test with T, T2 {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
trait BaseTrait {}
trait T {}

trait T2 {
fun foo() {}
}

trait Test with T, T2 {
override fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait BaseTrait {}

contract Test {
fun foo() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trait BaseTrait {}
trait T {
fun foo() {}
}

contract Test with T {
override fun foo() {}
}

0 comments on commit f997e13

Please sign in to comment.