From b28384fed1ce638e8b00b72dba974e9fd7e55eb4 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 28 Oct 2026 13:36:16 +0000 Subject: feat(overload): support default return types Co-Authored-By: Claude Opus 5.5 --- tests/i/default-returns/case.json | 1 + tests/i/default-returns/expected.txt | 12 ++++++ tests/i/default-returns/notes.md | 7 ++++ tests/i/default-returns/program.cps | 54 ++++++++++++++++++++++++++ tests/iii/default-return-errors/case.json | 10 +++++ tests/iii/default-return-errors/program.cps | 33 ++++++++++++++++ tests/iii/default-return-template/case.json | 5 +++ tests/iii/default-return-template/program.cps | 5 +++ tests/iii/multifile-default-return/case.json | 6 +++ tests/iii/multifile-default-return/first.cps | 3 ++ tests/iii/multifile-default-return/program.cps | 3 ++ tests/iii/multifile-default-return/second.cps | 3 ++ 12 files changed, 142 insertions(+) create mode 100644 tests/i/default-returns/case.json create mode 100644 tests/i/default-returns/expected.txt create mode 100644 tests/i/default-returns/notes.md create mode 100644 tests/i/default-returns/program.cps create mode 100644 tests/iii/default-return-errors/case.json create mode 100644 tests/iii/default-return-errors/program.cps create mode 100644 tests/iii/default-return-template/case.json create mode 100644 tests/iii/default-return-template/program.cps create mode 100644 tests/iii/multifile-default-return/case.json create mode 100644 tests/iii/multifile-default-return/first.cps create mode 100644 tests/iii/multifile-default-return/program.cps create mode 100644 tests/iii/multifile-default-return/second.cps (limited to 'tests') diff --git a/tests/i/default-returns/case.json b/tests/i/default-returns/case.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/i/default-returns/case.json @@ -0,0 +1 @@ +{} diff --git a/tests/i/default-returns/expected.txt b/tests/i/default-returns/expected.txt new file mode 100644 index 0000000..3813090 --- /dev/null +++ b/tests/i/default-returns/expected.txt @@ -0,0 +1,12 @@ +2.5 +4 +3 +3 +2 +6.5 +7 +8.5 +9 +10.5 +11 +12 diff --git a/tests/i/default-returns/notes.md b/tests/i/default-returns/notes.md new file mode 100644 index 0000000..973986f --- /dev/null +++ b/tests/i/default-returns/notes.md @@ -0,0 +1,7 @@ +# Default returns + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: DEFAULT and return-type overloading are project extensions. + +Trace: Foo(INTEGER) defaults to REAL 2.5, while Foo(REAL) defaults to INTEGER 4. INTEGER assignment and cast select 3; a C_INT cast falls back to the default 2.5 then converts to 2. Get defaults to 6.5, member addition to 8.5, and global negation to 10.5; INTEGER assignments select 7, 9 and 11. The nested Foo call defaults to REAL, selecting Consume(REAL), which returns 12. diff --git a/tests/i/default-returns/program.cps b/tests/i/default-returns/program.cps new file mode 100644 index 0000000..a6a0694 --- /dev/null +++ b/tests/i/default-returns/program.cps @@ -0,0 +1,54 @@ +OVERLOAD FUNCTION Foo(X : INTEGER) RETURNS DEFAULT REAL + RETURN 2.5 +ENDFUNCTION +OVERLOAD FUNCTION Foo(X : INTEGER) RETURNS INTEGER + RETURN 3 +ENDFUNCTION +OVERLOAD FUNCTION Foo(X : REAL) RETURNS DEFAULT INTEGER + RETURN 4 +ENDFUNCTION +OVERLOAD FUNCTION Foo(X : REAL) RETURNS REAL + RETURN 5.5 +ENDFUNCTION +CLASS Box + Value : INTEGER + OVERLOAD FUNCTION Get() RETURNS DEFAULT REAL + RETURN 6.5 + ENDFUNCTION + OVERLOAD FUNCTION Get() RETURNS INTEGER + RETURN 7 + ENDFUNCTION + OVERLOAD FUNCTION OPERATOR+(Other : Box) RETURNS DEFAULT REAL + RETURN 8.5 + ENDFUNCTION + OVERLOAD FUNCTION OPERATOR+(Other : Box) RETURNS INTEGER + RETURN 9 + ENDFUNCTION +ENDCLASS +OVERLOAD FUNCTION OPERATOR-(X : Box) RETURNS DEFAULT REAL + RETURN 10.5 +ENDFUNCTION +OVERLOAD FUNCTION OPERATOR-(X : Box) RETURNS INTEGER + RETURN 11 +ENDFUNCTION +OVERLOAD FUNCTION Consume(X : REAL) RETURNS INTEGER + RETURN 12 +ENDFUNCTION +DECLARE B : Box +DECLARE N : INTEGER +OUTPUT Foo(1) +OUTPUT Foo(1.0) +N <- Foo(1) +OUTPUT N +OUTPUT (INTEGER)Foo(1) +OUTPUT (C_INT)Foo(1) +OUTPUT B.Get() +N <- B.Get() +OUTPUT N +OUTPUT B + B +N <- B + B +OUTPUT N +OUTPUT -B +N <- -B +OUTPUT N +OUTPUT Consume(Foo(1)) diff --git a/tests/iii/default-return-errors/case.json b/tests/iii/default-return-errors/case.json new file mode 100644 index 0000000..654bca5 --- /dev/null +++ b/tests/iii/default-return-errors/case.json @@ -0,0 +1,10 @@ +{ + "errors": [ + {"reason": "Duplicate DEFAULT return", "line": 4}, + {"reason": "DEFAULT requires a non-VOID OVERLOAD FUNCTION", "line": 7}, + {"reason": "DEFAULT requires a non-VOID OVERLOAD FUNCTION", "line": 10}, + {"reason": "No matching overload for Typed\\(\\) returning BOOLEAN", "line": 25}, + {"reason": "Ambiguous call to Ambiguous", "line": 26}, + {"reason": "Ambiguous call to Clash", "line": 33} + ] +} diff --git a/tests/iii/default-return-errors/program.cps b/tests/iii/default-return-errors/program.cps new file mode 100644 index 0000000..cd30994 --- /dev/null +++ b/tests/iii/default-return-errors/program.cps @@ -0,0 +1,33 @@ +OVERLOAD FUNCTION Duplicate(X : INTEGER) RETURNS DEFAULT INTEGER + RETURN 1 +ENDFUNCTION +OVERLOAD FUNCTION duplicate(X : INTEGER) RETURNS DEFAULT REAL + RETURN 2.0 +ENDFUNCTION +FUNCTION Plain() RETURNS DEFAULT INTEGER + RETURN 1 +ENDFUNCTION +OVERLOAD FUNCTION Empty() RETURNS DEFAULT VOID +ENDFUNCTION +OVERLOAD FUNCTION Ambiguous() RETURNS INTEGER + RETURN 1 +ENDFUNCTION +OVERLOAD FUNCTION Ambiguous() RETURNS REAL + RETURN 2.0 +ENDFUNCTION +OVERLOAD FUNCTION Typed() RETURNS DEFAULT INTEGER + RETURN 1 +ENDFUNCTION +OVERLOAD FUNCTION Typed() RETURNS REAL + RETURN 2.0 +ENDFUNCTION +DECLARE Flag : BOOLEAN +Flag <- Typed() +OUTPUT Ambiguous() +OVERLOAD FUNCTION Clash(A : STRING, B : CHAR) RETURNS DEFAULT INTEGER + RETURN 1 +ENDFUNCTION +OVERLOAD FUNCTION Clash(A : CHAR, B : STRING) RETURNS REAL + RETURN 2.0 +ENDFUNCTION +OUTPUT Clash("a", "b") diff --git a/tests/iii/default-return-template/case.json b/tests/iii/default-return-template/case.json new file mode 100644 index 0000000..6e43a53 --- /dev/null +++ b/tests/iii/default-return-template/case.json @@ -0,0 +1,5 @@ +{ + "errors": [ + {"reason": "DEFAULT return types are not supported in templates", "line": 2} + ] +} diff --git a/tests/iii/default-return-template/program.cps b/tests/iii/default-return-template/program.cps new file mode 100644 index 0000000..8f71900 --- /dev/null +++ b/tests/iii/default-return-template/program.cps @@ -0,0 +1,5 @@ +TEMPLATE +OVERLOAD FUNCTION Pick(X : T) RETURNS DEFAULT INTEGER + RETURN 1 +ENDFUNCTION +OUTPUT 1 diff --git a/tests/iii/multifile-default-return/case.json b/tests/iii/multifile-default-return/case.json new file mode 100644 index 0000000..5679a0f --- /dev/null +++ b/tests/iii/multifile-default-return/case.json @@ -0,0 +1,6 @@ +{ + "sources": ["program.cps", "first.cps", "second.cps"], + "errors": [ + {"reason": "Duplicate DEFAULT return", "file": "second.cps", "line": 1} + ] +} diff --git a/tests/iii/multifile-default-return/first.cps b/tests/iii/multifile-default-return/first.cps new file mode 100644 index 0000000..a376dda --- /dev/null +++ b/tests/iii/multifile-default-return/first.cps @@ -0,0 +1,3 @@ +OVERLOAD FUNCTION Pick(X : INTEGER) RETURNS DEFAULT INTEGER + RETURN 1 +ENDFUNCTION diff --git a/tests/iii/multifile-default-return/program.cps b/tests/iii/multifile-default-return/program.cps new file mode 100644 index 0000000..15feac7 --- /dev/null +++ b/tests/iii/multifile-default-return/program.cps @@ -0,0 +1,3 @@ +INCLUDE "first.cps" +INCLUDE "second.cps" +OUTPUT 1 diff --git a/tests/iii/multifile-default-return/second.cps b/tests/iii/multifile-default-return/second.cps new file mode 100644 index 0000000..70b4667 --- /dev/null +++ b/tests/iii/multifile-default-return/second.cps @@ -0,0 +1,3 @@ +OVERLOAD FUNCTION Pick(X : INTEGER) RETURNS DEFAULT REAL + RETURN 2.0 +ENDFUNCTION -- cgit v1.3.1-4-g156e