[fine] Check the return type of functions

A function returns what it says it does, the check is that the body
returns the right value.
This commit is contained in:
John Doty 2024-01-15 07:46:20 -08:00
parent 257a7e64c2
commit d893002ec2
5 changed files with 70 additions and 14 deletions

View file

@ -1,8 +1,8 @@
fun foo(x: f64) {
fun foo(x: f64) -> f64 {
x + 7
}
fun test() {
fun test() -> f64 {
foo(1)
}
@ -22,6 +22,10 @@ fun test() {
// | TypeExpression
// | Identifier:'"f64"'
// | RightParen:'")"'
// | ReturnType
// | Arrow:'"->"'
// | TypeExpression
// | Identifier:'"f64"'
// | Block
// | LeftBrace:'"{"'
// | ExpressionStatement
@ -38,6 +42,10 @@ fun test() {
// | ParamList
// | LeftParen:'"("'
// | RightParen:'")"'
// | ReturnType
// | Arrow:'"->"'
// | TypeExpression
// | Identifier:'"f64"'
// | Block
// | LeftBrace:'"{"'
// | ExpressionStatement

View file

@ -1,4 +1,4 @@
fun test() {
fun test() -> f64 {
1 * 2 + -3 * 4
}
@ -13,6 +13,10 @@ fun test() {
// | ParamList
// | LeftParen:'"("'
// | RightParen:'")"'
// | ReturnType
// | Arrow:'"->"'
// | TypeExpression
// | Identifier:'"f64"'
// | Block
// | LeftBrace:'"{"'
// | ExpressionStatement

View file

@ -1,4 +1,4 @@
fun test() {
fun test() -> bool {
true and false or false and !true
}
@ -38,6 +38,10 @@ fun test() {
// | ParamList
// | LeftParen:'"("'
// | RightParen:'")"'
// | ReturnType
// | Arrow:'"->"'
// | TypeExpression
// | Identifier:'"bool"'
// | Block
// | LeftBrace:'"{"'
// | ExpressionStatement

View file

@ -1,22 +1,22 @@
fun test() {
fun test() -> f64 {
if true { "discarded"; 23 } else { 45 }
}
// @no-errors
// Here come some type probes!
// (type of the condition)
// @type: 20 bool
// @type: 27 bool
//
// (the discarded expression)
// @type: 27 string
// @type: 34 string
//
// (the "then" clause)
// @type: 40 f64
// @type: 43 f64
// @type: 47 f64
// @type: 50 f64
//
// (the "else" clause)
// @type: 52 f64
// @type: 55 f64
// @type: 59 f64
// @type: 62 f64
//
// @concrete:
// | File
@ -26,6 +26,10 @@ fun test() {
// | ParamList
// | LeftParen:'"("'
// | RightParen:'")"'
// | ReturnType
// | Arrow:'"->"'
// | TypeExpression
// | Identifier:'"f64"'
// | Block
// | LeftBrace:'"{"'
// | IfStatement