Compare commits

...
4 Commits
14 changed files with 353 additions and 13 deletions

No files matched your search

+4 -1
View File
@@ -9,7 +9,10 @@ History note:
## Unreleased ## Unreleased
- No unreleased entries yet. - Reject member functions named after their enclosing class and diagnose unsupported `this(...)` constructor delegation at compile time.
- Preserve declared, inferred, and generic return types from static method calls.
- Initialize singleton static members and dispatch their calls through class scope, including trailing-lambda calls.
- Update the language reference and OOP guide with constructor naming rules and typed static factory examples.
## 1.5.6 (2026-05-16) ## 1.5.6 (2026-05-16)
+5 -1
View File
@@ -17,11 +17,15 @@
# #
# #
# Pass snapshot overrides through explicitly, e.g. -Plyng.version=1.5.11-SNAPSHOT.
GRADLE_ARGS=()
upload_only=false upload_only=false
target=vps # default: new server; use --old for d.lynglang.com target=vps # default: new server; use --old for d.lynglang.com
for arg in "$@"; do for arg in "$@"; do
if [[ "$arg" == "-u" || "$arg" == "--upload-only" ]]; then if [[ "$arg" == "-u" || "$arg" == "--upload-only" ]]; then
upload_only=true upload_only=true
elif [[ "$arg" == -P* ]]; then
GRADLE_ARGS+=("$arg")
elif [[ "$arg" == "--old" ]]; then elif [[ "$arg" == "--old" ]]; then
target=com target=com
fi fi
@@ -125,7 +129,7 @@ if [[ "$upload_only" == false ]]; then
refreshTextmateZip refreshTextmateZip
updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link" updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link"
./gradlew site:jsBrowserDistribution ./gradlew site:jsBrowserDistribution "${GRADLE_ARGS[@]}"
BUILD_RC=$? BUILD_RC=$?
# Always restore original doc if backup exists # Always restore original doc if backup exists
+2 -1
View File
@@ -113,9 +113,10 @@ process_file() {
# - [text](file.md) -> [text](#file.md) # - [text](file.md) -> [text](#file.md)
# - [text](dir/file.md) -> [text](#dir_file.md) # - [text](dir/file.md) -> [text](#dir_file.md)
# - [text](file.md#anchor) -> [text](#anchor) # - [text](file.md#anchor) -> [text](#anchor)
# - Fix image links: [alt](../images/...) -> [alt](images/...) if needed, but none found yet. # - Resolve site-root image URLs against the local site resources for embedding.
cat "$full_path" | \ cat "$full_path" | \
perl -pe 's{(!\[[^\]]*\]\()/}{$1site/src/jsMain/resources/}g' | \
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md\)/"[$1](#" . ($2 =~ s|\/|_|gr) . ".md)"/ge' | \ perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md\)/"[$1](#" . ($2 =~ s|\/|_|gr) . ".md)"/ge' | \
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md#([^)]+)\)/[$1](#$3)/g' >> "$MERGED_MD" perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md#([^)]+)\)/[$1](#$3)/g' >> "$MERGED_MD"
+25
View File
@@ -42,6 +42,12 @@ a _constructor_ that requires two parameters for fields. So when creating it wit
Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the
example above. example above.
A member function cannot have the same name as its enclosing class, including a
static member function. Constructors are declared in the class header; use the
class name to create another instance. `this(...)` and `this@Type(...)` do not
declare or delegate to secondary constructors. Local functions may shadow class
names according to the normal lexical scope rules.
## Singleton Objects ## Singleton Objects
Singleton objects are declared using the `object` keyword. An `object` declaration defines both a class and a single instance of that class at the same time. This is perfect for stateless utilities, global configuration, or shared delegates. Singleton objects are declared using the `object` keyword. An `object` declaration defines both a class and a single instance of that class at the same time. This is perfect for stateless utilities, global configuration, or shared delegates.
@@ -1042,6 +1048,25 @@ Static fields can be accessed from static methods via the class qualifier:
assertEquals("bar", Test.getData() ) assertEquals("bar", Test.getData() )
>>> void >>> void
Static calls preserve their declared or inferred return types, so their results
can be used directly in chained expressions or assigned without a type annotation.
Generic static factories also preserve the type inferred from their arguments:
class StaticBox<T>(val value: T) {
static fun of<T>(value: T): StaticBox<T> = StaticBox(value)
}
val box = StaticBox.of("hello")
assertEquals(5, box.value.length)
>>> void
Named singleton objects also support static methods:
object StaticGreeting {
static fun text(): String = "hello"
}
assertEquals(5, StaticGreeting.text().length)
>>> void
# Extension members # Extension members
Sometimes an existing type or named singleton object is missing some particular functionality that can be _added to it_ without rewriting its inner logic and without using its private state. In this case, _extension members_ can be used. Sometimes an existing type or named singleton object is missing some particular functionality that can be _added to it_ without rewriting its inner logic and without using its private state. In this case, _extension members_ can be used.
+3
View File
@@ -126,6 +126,7 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- extension functions: `fun Type.name(...) { ... }`. - extension functions: `fun Type.name(...) { ... }`.
- context-aware extension functions: `context(Tag) fun String.unaryPlus() { this@Tag.addText(this) }`. - context-aware extension functions: `context(Tag) fun String.unaryPlus() { this@Tag.addText(this) }`.
- named singleton `object` declarations can be extension receivers too: `fun Config.describe(...) { ... }`, `val Config.tag get() = ...`. - named singleton `object` declarations can be extension receivers too: `fun Config.describe(...) { ... }`, `val Config.tag get() = ...`.
- Static class/object methods preserve declared or inferable return types at call sites, including chained member access and generic return types inferred from arguments. For example, `static fun of<T>(v: T): Box<T> = Box(v)` lets `Box.of("abc").value.length` retain the `String` member type.
- static extension functions are callable on the type object: `static fun List<T>.fill(...)` -> `List.fill(...)`. - static extension functions are callable on the type object: `static fun List<T>.fill(...)` -> `List.fill(...)`.
- delegated callable: `fun f(...) by delegate`. - delegated callable: `fun f(...) by delegate`.
- Type aliases: - Type aliases:
@@ -133,6 +134,8 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- generic: `type Box<T> = List<T>` - generic: `type Box<T> = List<T>`
- aliases are expanded structurally. - aliases are expanded structurally.
- Classes/objects/enums/interfaces: - Classes/objects/enums/interfaces:
- Constructors use the class name. A direct member function (including `static`) cannot have the same name as its enclosing class; local functions may still shadow names normally.
- `this(...)` and `this@Type(...)` are not secondary-constructor delegation syntax; call the class by name to construct another instance.
- `interface` is parsed as abstract class synonym. - `interface` is parsed as abstract class synonym.
- `object` supports named singleton and anonymous object expression forms. - `object` supports named singleton and anonymous object expression forms.
- enums support lifted entries: `enum E* { A, B }`. - enums support lifted entries: `enum E* { A, B }`.
+74
View File
@@ -0,0 +1,74 @@
### Ticket 1 — Name resolution: a member function named like its class silently shadows the primary constructor inside the class body
**Summary.** `class` bodies accept a member function whose name equals the class name. That member then wins name resolution for **all unqualified calls inside the class body**, including `init` blocks, method bodies and nested lambdas. The primary constructor becomes unreachable from inside the class, with no escape hatch. Compiles cleanly, breaks at runtime.
**Repro**
```lyng
class C1(x) {
fun C1(y): String = "member " + y.toString()
init { println(C1(9)) } // -> "member C1(9)", NOT the constructor
}
class C2(x) {
fun C2(y, z): String = y.toString() + z.toString()
fun probe(): String = C2(7, 8) // member chosen although ctor takes 1 arg
}
class Fact(x) {
fun Fact(y): Fact = Fact(y * 2) // compiles; unqualified Fact(...) = itself
}
Fact(1).Fact(3) // java.lang.StackOverflowError
```
Local functions shadow too:
```lyng
class M3(x) {
fun probe(y): String {
fun M3(z): String = "local " + z.toString()
return M3(y) // local member wins, ctor unreachable
}
}
```
No workaround found:
```lyng
class Q1(x) {
fun Q1(y): String = "member"
fun a(): String = Q1.Q1(3) // IllegalArgumentException: expected 1 arguments, got 0
fun b(): String = (Q1(5) as Q1).Q1(6) // ClassCastException: Cannot cast String to Q1
}
class G1(x) { fun g(): G1 = this(x + 1) } // Exception: operation is not implemented
```
Related symptom: adding `Type::class` in a class that also has a same-name member turns the **declaration** itself into a hard error pointing at the wrong place:
```lyng
class D16(x) {
fun D16(y): String = "member " + y.toString() // Error: expected 1 arguments, got 0
fun lit(): String = D16::class.toString()
}
```
Control cases compile fine: same-name member alone ✔, `::class` alone ✔, the combination ✘.
**Expected.** Either (a) reject a member whose name collides with the class name at compile time (preferred — it is never a constructor in Lyng, there are no secondary constructors), or (b) keep constructor calls resolvable for the primary arity and fall back to members only for non-matching arity, plus a working `this(...)`/`this@Type(...)` delegation. In any case `Type::class` must not be affected by a same-name member, and diagnostics must point at the actual construct.
**Suggested tests.** `fun SameName(...)` declared in a class with the same name → expected compile error; self-recursive factory → must not reach runtime stack overflow; `Type::class` inside such a class → must compile; `this(...)` → either implement or emit a clear compile-time error instead of `operation is not implemented`.
## Resolution
Valid: direct instance/static member functions named after their enclosing class
are now rejected at the function name. This also rejects the collision before
any `::class` expression can produce a misleading downstream diagnostic.
The supported control is `Type::class` in a class without the illegal member;
the suggested test asking the illegal declaration to compile contradicts the
report's preferred resolution.
Direct `this(...)` / `this@Type(...)` calls on the enclosing class instance now
report unsupported constructor delegation during compilation. Use `Type(...)`
to construct another instance; no secondary constructors were introduced.
Local functions shadowing class names are ordinary lexical shadowing, not a bug,
and remain supported. Regression coverage: `ClassNameMemberRegressionTest`.
Before the fix, 2 of its 4 tests failed; the constructor/class-literal and local
shadowing controls passed.
Validation: all 4 focused JVM regression tests passed, followed by the full
`:lynglib:jvmTest` suite (benchmarks excluded by the repository default).
+65
View File
@@ -0,0 +1,65 @@
### Ticket 2 — Static member calls: declared return type is not propagated, receiver becomes `Object`
**Summary.** The **invocation** of a `static` member (class or `object` singleton) loses its declared return type at the call site; the result is treated as a general receiver, so every type-specific member access fails to compile. The runtime value is correct — this is purely a static-typing defect. Constructor calls, instance-method calls and top-level function calls all preserve the declared type correctly, so the inconsistency is specific to static members.
**Repro**
```lyng
class S(x) {
static fun make(y): S = S(y) // return type is explicitly declared
fun hi(): String = "hi"
}
val a: S = S.make(1) // OK — annotation rescues it
println(a.hi())
val b = S.make(2) // b is S == true at runtime
println(b.hi()) // Error: member access requires compile-time receiver type: hi
println(S.make(1).hi())// Error: same, chained call
println((S.make(3) as S).hi()) // OK — explicit cast rescues it
fun need(v: S) = v.hi(); need(S.make(4)) // OK — typed argument position rescues it
```
Not limited to self-returning factories — any declared return type is dropped:
```lyng
class K(x) { static fun text(y): String = "t"; static fun num(y): Int = y }
val s = K.text(1); println(s.length) // Error: ... compile-time receiver type: length
val n = K.num(1); println(n + 1) // same class of failure
object O { static fun text(y): String = "o" }
val o = O.text(1); println(o.length) // Error: ... on O.text result
class B(x) { static fun noRet(y) = B(y) } // untyped static: also general
class Box<T>(val value: T) { static fun of<T>(v: T): Box<T> = Box(v) }
val bx = Box.of(42); println(bx.value) // Error; val bx2: Box<Int> = Box.of(43) works
```
Controls that **do** work correctly: `val d = K8(9); d.hi()` (constructor), `val c = S(3).again(4); c.hi()` (instance method), `val t = topLevelMake(1); t.x` (top-level function).
**Expected.** `StaticClass.member(args)` must produce the declared return type as the static compile-time receiver type, exactly like instance and top-level calls. Only genuinely untyped statics (`fun` without a return type and uninferrable bodies) should fall back to `Object`/`Obj`.
**Suggested tests.** Matrix over `{constructor | instance method | top-level fun | static fun} × {declared ret type | inferred | omitted}` asserting that `.length`, arithmetic, `is T`-then-member, chained calls and generic instantiation behave identically for statics as for the other three; plus `object` singletons and generic statics.
## Resolution
Valid: static method signatures were excluded from the compiler's member type
metadata, both before body compilation and when inferred returns were recorded.
They now use the same callable signature and generic substitution path as
instance methods. Explicit annotations, inferred expression returns, primitive
returns, chained calls, and inferred/explicit generic arguments are covered by
`StaticReturnTypeRegressionTest`.
The singleton example additionally exposed runtime bugs once typing was
corrected: object static initializers were discarded, and a static call on an
object was sent to instance-method dispatch. Object declarations now preserve
and execute their static initialization statements before constructing the
singleton instance.
The compiler now retains its class-scope call decision for bytecode generation;
singleton static calls use the object's class and the existing class-scope
access opcode. Instance methods retain their dispatch path. Tests also cover
static calls with a trailing lambda.
Before the fix, all 5 original regression cases failed with missing compile-time
receiver types. The initial type-only correction passed 4, with the singleton
case then failing at runtime; dispatch is corrected in this same report.
Validation: all 6 focused JVM regression tests passed, followed by all 1,040
`:lynglib:jvmTest` tests (benchmarks excluded by the repository default).
`git diff --check` also passed.
+5
View File
@@ -0,0 +1,5 @@
# Invalid reports
Place reports here when reproduction and the language contract show that they
are not bugs. Append the reason and verification evidence to each moved report.
Valid reports with an invalid subclaim stay with their resolution notes.
@@ -88,6 +88,9 @@ internal suspend fun executeClassDecl(
classScope.addConst("object", newClass) classScope.addConst("object", newClass)
spec.bodyInit?.let { executeBytecodeWithSeed(classScope, it, "object body init") } spec.bodyInit?.let { executeBytecodeWithSeed(classScope, it, "object body init") }
for (statement in spec.initScope) {
executeBytecodeWithSeed(classScope, statement, "object static init")
}
val instance = newClass.callOn(scope.createChildScope(Arguments.EMPTY)) val instance = newClass.callOn(scope.createChildScope(Arguments.EMPTY))
if (spec.declaredName != null) { if (spec.declaredName != null) {
@@ -3121,7 +3121,7 @@ class Compiler(
val field = FieldRef(left, next.value, isOptional) val field = FieldRef(left, next.value, isOptional)
operand = CallRef(field, args, tailBlock, isOptional, memberCallTypeArgs) operand = CallRef(field, args, tailBlock, isOptional, memberCallTypeArgs)
} else { } else {
operand = MethodCallRef(left, next.value, args, tailBlock, isOptional, memberCallTypeArgs) operand = MethodCallRef(left, next.value, args, tailBlock, isOptional, memberCallTypeArgs, classScopeCall = true)
} }
} else { } else {
// instance method call // instance method call
@@ -3238,14 +3238,14 @@ class Compiler(
resolutionSink?.referenceReflection(name.value, next.pos) resolutionSink?.referenceReflection(name.value, next.pos)
} }
} }
MethodCallRef(left, next.value, args, true, isOptional) MethodCallRef(left, next.value, args, true, isOptional, classScopeCall = shouldTreatAsClassScopeCall(left, next.value))
} else { } else {
val unionCall = buildUnionMethodCall(left, next.value, next.pos, isOptional, args, true) val unionCall = buildUnionMethodCall(left, next.value, next.pos, isOptional, args, true)
if (unionCall != null) { if (unionCall != null) {
unionCall unionCall
} else { } else {
enforceReceiverTypeForMember(left, next.value, next.pos) enforceReceiverTypeForMember(left, next.value, next.pos)
MethodCallRef(left, next.value, args, true, isOptional) MethodCallRef(left, next.value, args, true, isOptional, classScopeCall = shouldTreatAsClassScopeCall(left, next.value))
} }
} }
is QualifiedThisRef -> is QualifiedThisRef ->
@@ -3265,7 +3265,7 @@ class Compiler(
unionCall unionCall
} else { } else {
enforceReceiverTypeForMember(left, next.value, next.pos) enforceReceiverTypeForMember(left, next.value, next.pos)
MethodCallRef(left, next.value, args, true, isOptional) MethodCallRef(left, next.value, args, true, isOptional, classScopeCall = shouldTreatAsClassScopeCall(left, next.value))
} }
} }
} }
@@ -7458,6 +7458,14 @@ class Compiler(
isOptional: Boolean, isOptional: Boolean,
explicitTypeArgs: List<TypeDecl>? = null explicitTypeArgs: List<TypeDecl>? = null
): ObjRef { ): ObjRef {
val thisTypeName = when (left) {
is LocalVarRef -> if (left.name == "this") currentImplicitThisTypeName() else null
is QualifiedThisRef -> left.typeName
else -> null
}
if (thisTypeName != null && codeContexts.any { it is CodeContext.ClassBody && it.name == thisTypeName }) {
throw ScriptError(cc.currentPos(), "constructor delegation through this is not supported; call the class by name")
}
var detectedBlockArgument = blockArgument var detectedBlockArgument = blockArgument
val expectedReceiver = tailBlockReceiverType(left) val expectedReceiver = tailBlockReceiverType(left)
val expectedTailCallable = expectedCallableArgumentType(left, 0) val expectedTailCallable = expectedCallableArgumentType(left, 0)
@@ -8678,6 +8686,7 @@ class Compiler(
val initScope = popInitScope() val initScope = popInitScope()
val wrappedBodyInit = bodyInit?.let { wrapClassBodyBytecode(it, "object@$className") } val wrappedBodyInit = bodyInit?.let { wrapClassBodyBytecode(it, "object@$className") }
val wrappedInitScope = initScope.map { wrapClassBodyBytecode(it, "objectInit@$className") }
val spec = ClassDeclSpec( val spec = ClassDeclSpec(
declaredName = declaredName, declaredName = declaredName,
@@ -8693,7 +8702,7 @@ class Compiler(
constructorArgs = null, constructorArgs = null,
constructorFieldIds = null, constructorFieldIds = null,
bodyInit = wrappedBodyInit, bodyInit = wrappedBodyInit,
initScope = emptyList() initScope = wrappedInitScope
) )
return ClassDeclStatement(spec) return ClassDeclStatement(spec)
} }
@@ -9984,6 +9993,10 @@ class Compiler(
name = t.value name = t.value
nameStartPos = t.pos nameStartPos = t.pos
} }
if (parentContext is CodeContext.ClassBody && extTypeName == null &&
name == parentContext.name.substringAfterLast('.')) {
throw ScriptError(nameStartPos, "member function cannot have the same name as its enclosing class: $name")
}
val extensionWrapperName = extTypeName?.let { extensionCallableName(it, name) } val extensionWrapperName = extTypeName?.let { extensionCallableName(it, name) }
val classCtx = codeContexts.asReversed().firstOrNull { it is CodeContext.ClassBody } as? CodeContext.ClassBody val classCtx = codeContexts.asReversed().firstOrNull { it is CodeContext.ClassBody } as? CodeContext.ClassBody
var memberMethodId = if (extTypeName == null) classCtx?.memberMethodIds?.get(name) else null var memberMethodId = if (extTypeName == null) classCtx?.memberMethodIds?.get(name) else null
@@ -10059,7 +10072,7 @@ class Compiler(
pendingTypeParamStack.removeLast() pendingTypeParamStack.removeLast()
} }
if (parentContext is CodeContext.ClassBody && !isStatic && extTypeName == null) { if (parentContext is CodeContext.ClassBody && extTypeName == null) {
classMemberTypeDeclByName.getOrPut(parentContext.name) { mutableMapOf() }[name] = TypeDecl.Function( classMemberTypeDeclByName.getOrPut(parentContext.name) { mutableMapOf() }[name] = TypeDecl.Function(
receiver = receiverTypeDecl, receiver = receiverTypeDecl,
contextReceivers = contextReceiverTypeDecls, contextReceivers = contextReceiverTypeDecls,
@@ -10233,7 +10246,7 @@ class Compiler(
val inferredReturnDecl = returnTypeDecl ?: inferReturnTypeDeclFromStatement(rawFnStatements) val inferredReturnDecl = returnTypeDecl ?: inferReturnTypeDeclFromStatement(rawFnStatements)
val inferredReturnClass = inferredReturnDecl?.let { resolveTypeDeclObjClass(it) } val inferredReturnClass = inferredReturnDecl?.let { resolveTypeDeclObjClass(it) }
?: inferReturnClassFromStatement(rawFnStatements) ?: inferReturnClassFromStatement(rawFnStatements)
if (parentContext is CodeContext.ClassBody && !isStatic && extTypeName == null) { if (parentContext is CodeContext.ClassBody && extTypeName == null) {
val ownerClassName = parentContext.name val ownerClassName = parentContext.name
run { run {
val memberTypeDecl = TypeDecl.Function( val memberTypeDecl = TypeDecl.Function(
@@ -4876,11 +4876,16 @@ class BytecodeCompiler(
val receiverClass = resolveReceiverClass(ref.receiver) ?: ObjDynamic.type val receiverClass = resolveReceiverClass(ref.receiver) ?: ObjDynamic.type
val receiver = compileRefWithFallback(ref.receiver, null, refPosOrCurrent(ref.receiver)) ?: return null val receiver = compileRefWithFallback(ref.receiver, null, refPosOrCurrent(ref.receiver)) ?: return null
val dst = allocSlot() val dst = allocSlot()
if (shouldUseClassScopeReceiver(ref.receiver, receiver, receiverClass)) { val classReceiver = shouldUseClassScopeReceiver(ref.receiver, receiver, receiverClass)
if (ref.classScopeCall || classReceiver) {
// Singleton objects carry their static members on the instance's class.
val classSlot = if (classReceiver) receiver.slot else allocSlot().also {
builder.emit(Opcode.GET_OBJ_CLASS, receiver.slot, it)
}
val nameId = builder.addConst(BytecodeConst.StringVal(ref.name)) val nameId = builder.addConst(BytecodeConst.StringVal(ref.name))
val memberSlot = allocSlot() val memberSlot = allocSlot()
if (!ref.isOptional) { if (!ref.isOptional) {
builder.emit(Opcode.GET_CLASS_SCOPE, receiver.slot, nameId, memberSlot) builder.emit(Opcode.GET_CLASS_SCOPE, classSlot, nameId, memberSlot)
} else { } else {
val nullSlot = allocSlot() val nullSlot = allocSlot()
builder.emit(Opcode.CONST_NULL, nullSlot) builder.emit(Opcode.CONST_NULL, nullSlot)
@@ -4892,7 +4897,7 @@ class BytecodeCompiler(
Opcode.JMP_IF_TRUE, Opcode.JMP_IF_TRUE,
listOf(CmdBuilder.Operand.IntVal(cmpSlot), CmdBuilder.Operand.LabelRef(nullLabel)) listOf(CmdBuilder.Operand.IntVal(cmpSlot), CmdBuilder.Operand.LabelRef(nullLabel))
) )
builder.emit(Opcode.GET_CLASS_SCOPE, receiver.slot, nameId, memberSlot) builder.emit(Opcode.GET_CLASS_SCOPE, classSlot, nameId, memberSlot)
builder.emit(Opcode.JMP, listOf(CmdBuilder.Operand.LabelRef(endLabel))) builder.emit(Opcode.JMP, listOf(CmdBuilder.Operand.LabelRef(endLabel)))
builder.mark(nullLabel) builder.mark(nullLabel)
builder.emit(Opcode.CONST_NULL, memberSlot) builder.emit(Opcode.CONST_NULL, memberSlot)
@@ -484,6 +484,7 @@ class MethodCallRef(
internal val tailBlock: Boolean, internal val tailBlock: Boolean,
internal val isOptional: Boolean, internal val isOptional: Boolean,
internal val explicitTypeArgs: List<TypeDecl>? = null, internal val explicitTypeArgs: List<TypeDecl>? = null,
internal val classScopeCall: Boolean = false,
) : ObjRef { ) : ObjRef {
// 4-entry PIC for method invocations (guarded by PerfFlags.METHOD_PIC) // 4-entry PIC for method invocations (guarded by PerfFlags.METHOD_PIC)
private var mKey1: Long = 0L; private var mVer1: Int = -1; private var mInvoker1: (suspend (Obj, Scope, Arguments) -> Obj)? = null private var mKey1: Long = 0L; private var mVer1: Int = -1; private var mInvoker1: (suspend (Obj, Scope, Arguments) -> Obj)? = null
@@ -0,0 +1,62 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.ScriptError
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class ClassNameMemberRegressionTest {
@Test
fun rejectsClassNamedMembersAtTheirDeclaration() = runTest {
for (member in listOf(
"fun Same(y): String = y.toString()",
"fun Same(y, z): String = y.toString() + z.toString()",
"fun Same(y): Same = Same(y * 2)",
"static fun Same(y): Same = Same(y)",
"fun Same(y): String = \"member\"; fun literal() = Same::class.toString()"
)) {
val error = assertFailsWith<ScriptError> {
eval("class Same(x) {\n $member\n}")
}
assertContains(error.errorMessage, "member function cannot have the same name as its enclosing class")
assertEquals(1, error.pos.line)
}
}
@Test
fun thisIsNotConstructorDelegation() = runTest {
for (receiver in listOf("this", "this@Factory")) {
val error = assertFailsWith<ScriptError> {
eval("class Factory(x) { fun again(): Factory = $receiver(x + 1) }")
}
assertContains(error.errorMessage, "constructor delegation through this is not supported")
}
}
@Test
fun constructorAndClassLiteralRemainAvailable() = runTest {
assertEquals("3", eval("""
class Factory(val x: Int) {
fun again(y: Int): Factory = Factory(y)
fun literal(): String = Factory::class.toString()
}
val f = Factory(1)
f.literal()
f.again(3).x.toString()
""".trimIndent()).toString())
}
@Test
fun localFunctionsCanStillShadowClassNames() = runTest {
assertEquals("local 7", eval("""
class Local(x) {
fun probe(y): String {
fun Local(z): String = "local " + z.toString()
return Local(y)
}
}
Local(1).probe(7)
""".trimIndent()).toString())
}
}
@@ -0,0 +1,76 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.assertEquals
class StaticReturnTypeRegressionTest {
@Test
fun declaredFactoryReturnSupportsInferredVariablesAndChains() = runTest {
assertEquals("hi hi hi", eval("""
class Factory(x) {
static fun make(y): Factory = Factory(y)
fun hi(): String = "hi"
}
val a: Factory = Factory.make(1)
val b = Factory.make(2)
a.hi() + " " + b.hi() + " " + Factory.make(3).hi()
""".trimIndent()).toString())
}
@Test
fun declaredScalarReturnsSupportMembersAndArithmetic() = runTest {
assertEquals("4", eval("""
class Scalars {
static fun text(y): String = "abc"
static fun num(y: Int): Int = y
}
val s = Scalars.text(1)
val n = Scalars.num(1)
s.length + n
""".trimIndent()).toString())
}
@Test
fun singletonStaticReturnSupportsMembers() = runTest {
assertEquals("3", eval("""
object Singleton { static fun text(): String = "abc" }
val text = Singleton.text()
text.length
""".trimIndent()).toString())
}
@Test
fun singletonStaticTailBlockAndInstanceMethodsUseTheirOwnDispatch() = runTest {
assertEquals("6", eval("""
object Singleton {
static fun text(block: ()->String): String = block()
fun instanceText(): String = "abc"
}
Singleton.text { "abc" }.length + Singleton.instanceText().length
""".trimIndent()).toString())
}
@Test
fun inferredStaticReturnMatchesInstanceAndTopLevelFunctions() = runTest {
assertEquals("9", eval("""
class Factory {
static fun text() = "abc"
fun instanceText() = "abc"
}
fun topText() = "abc"
Factory.text().length + Factory().instanceText().length + topText().length
""".trimIndent()).toString())
}
@Test
fun genericStaticReturnSubstitutesArgumentType() = runTest {
assertEquals("7", eval("""
class Box<T>(val value: T) {
static fun of<T>(v: T): Box<T> = Box(v)
}
val box = Box.of("abc")
val explicit = Box.of<String>("abcd")
box.value.length + explicit.value.length
""".trimIndent()).toString())
}
}