PL/pgSQL: Simplify RETURN checking for procedures
authorPeter Eisentraut <[email protected]>
Sun, 4 Mar 2018 15:35:23 +0000 (10:35 -0500)
committerPeter Eisentraut <[email protected]>
Sun, 4 Mar 2018 15:35:23 +0000 (10:35 -0500)
Check at compile time that RETURN in a procedure does not specify a
parameter, rather than at run time.

src/pl/plpgsql/src/expected/plpgsql_call.out
src/pl/plpgsql/src/pl_exec.c
src/pl/plpgsql/src/pl_gram.y
src/pl/plpgsql/src/sql/plpgsql_call.sql

index e2442c603cd1f925919dcb2b0909d1209c4741f2..2f3adcd8d8211e2b9ee010d9e55fb9a5febccc4c 100644 (file)
@@ -17,9 +17,9 @@ BEGIN
     RETURN 5;
 END;
 $$;
-CALL test_proc2();
-ERROR:  cannot return a value from a procedure
-CONTEXT:  PL/pgSQL function test_proc2() while casting return value to function's return type
+ERROR:  RETURN cannot have a parameter in a procedure
+LINE 5:     RETURN 5;
+                   ^
 CREATE TABLE test1 (a int);
 CREATE PROCEDURE test_proc3(x int)
 LANGUAGE plpgsql
@@ -54,7 +54,6 @@ SELECT * FROM test1;
 (2 rows)
 
 DROP PROCEDURE test_proc1;
-DROP PROCEDURE test_proc2;
 DROP PROCEDURE test_proc3;
 DROP PROCEDURE test_proc4;
 DROP TABLE test1;
index 297aa3e47328662a3bb6142a3cc81df1a5648722..489484f184c4b57a0b2860efd083b57bc5e08c02 100644 (file)
@@ -617,11 +617,6 @@ plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo,
        }
        else if (!estate.retisnull)
        {
-               if (func->fn_prokind == PROKIND_PROCEDURE)
-                       ereport(ERROR,
-                                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                        errmsg("cannot return a value from a procedure")));
-
                /*
                 * Cast result value to function's declared result type, and copy it
                 * out to the upper executor memory context.  We must treat tuple
index 697ead0570e0fad76b9d80877adcb82c00eaa350..9fcf2424daeeda1241130b9e191248e9f59936d9 100644 (file)
@@ -3138,14 +3138,21 @@ make_return_stmt(int location)
                                         parser_errposition(yylloc)));
                new->retvarno = plpgsql_curr_compile->out_param_varno;
        }
-       else if (plpgsql_curr_compile->fn_rettype == VOIDOID &&
-                        plpgsql_curr_compile->fn_prokind != PROKIND_PROCEDURE)
+       else if (plpgsql_curr_compile->fn_rettype == VOIDOID)
        {
                if (yylex() != ';')
-                       ereport(ERROR,
-                                       (errcode(ERRCODE_DATATYPE_MISMATCH),
-                                        errmsg("RETURN cannot have a parameter in function returning void"),
-                                        parser_errposition(yylloc)));
+               {
+                       if (plpgsql_curr_compile->fn_prokind == PROKIND_PROCEDURE)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("RETURN cannot have a parameter in a procedure"),
+                                                parser_errposition(yylloc)));
+                       else
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_DATATYPE_MISMATCH),
+                                                errmsg("RETURN cannot have a parameter in function returning void"),
+                                                parser_errposition(yylloc)));
+               }
        }
        else
        {
index 321ed43af8eab7d82a2b985d591480ef853f2043..e580e5fea0728dd398812aede7896dc06c986f67 100644 (file)
@@ -22,8 +22,6 @@ BEGIN
 END;
 $$;
 
-CALL test_proc2();
-
 
 CREATE TABLE test1 (a int);
 
@@ -58,7 +56,6 @@ SELECT * FROM test1;
 
 
 DROP PROCEDURE test_proc1;
-DROP PROCEDURE test_proc2;
 DROP PROCEDURE test_proc3;
 DROP PROCEDURE test_proc4;