Skip to content

Commit c1c36cd

Browse files
committed
enhance assignement related error about info about target variable
1 parent 18dfce6 commit c1c36cd

File tree

5 files changed

+111
-66
lines changed

5 files changed

+111
-66
lines changed

expected/plpgsql_check_active-12.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ select * from plpgsql_check_function('test_field_assignment');
442442
warning:42804:5:assignment:target type is different type than source type
443443
Detail: cast "timestamp with time zone" value to "integer" type
444444
Hint: There are no possible explicit coercion between those types, possibly bug!
445-
(3 rows)
445+
Context: at assignment to field "a" of variable "v" declared on line 2
446+
(4 rows)
446447

447448
drop table foo;
448449
create table foo(a int, b int);

expected/plpgsql_check_active.out

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ select f1();
307307
(1 row)
308308

309309
select * from plpgsql_check_function_tb('f1()');
310-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
311-
------------+--------+------------+----------+-----------------------------+--------+------+-------+----------+-------+---------
312-
f1 | 6 | assignment | 42703 | record "r" has no field "c" | | | error | | |
310+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
311+
------------+--------+------------+----------+-----------------------------+--------+------+-------+----------+-------+---------------------------------------------------------------
312+
f1 | 6 | assignment | 42703 | record "r" has no field "c" | | | error | | | at assignment to field "c" of variable "r" declared on line 2
313313
(1 row)
314314

315315
select f1();
@@ -337,9 +337,9 @@ select f1();
337337
(1 row)
338338

339339
select * from plpgsql_check_function_tb('f1()');
340-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
341-
------------+--------+------------+----------+---------------------------+--------+------+-------+----------+--------------+---------
342-
f1 | 5 | assignment | 42703 | column "a" does not exist | | | error | 8 | SELECT a + b |
340+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
341+
------------+--------+------------+----------+---------------------------+--------+------+-------+----------+--------------+--------------------------------------------------
342+
f1 | 5 | assignment | 42703 | column "a" does not exist | | | error | 8 | SELECT a + b | at assignment to variable "r" declared on line 2
343343
(1 row)
344344

345345
select f1();
@@ -365,9 +365,9 @@ select f1();
365365
(1 row)
366366

367367
select * from plpgsql_check_function_tb('f1()');
368-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
369-
------------+--------+------------+----------+---------------------------+--------+------+-------+----------+-------------+---------
370-
f1 | 5 | assignment | 42703 | column "c" does not exist | | | error | 8 | SELECT c+10 |
368+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
369+
------------+--------+------------+----------+---------------------------+--------+------+-------+----------+-------------+-------------------------------------------------------------
370+
f1 | 5 | assignment | 42703 | column "c" does not exist | | | error | 8 | SELECT c+10 | at assignment to element of variable "r" declared on line 2
371371
(1 row)
372372

373373
select f1();
@@ -393,9 +393,9 @@ select f1();
393393
(1 row)
394394

395395
select * from plpgsql_check_function_tb('f1()');
396-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
397-
------------+--------+------------+----------+------------------------------------+--------+------+-------+----------+-------+---------
398-
f1 | 5 | assignment | 42804 | subscripted object is not an array | | | error | | |
396+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
397+
------------+--------+------------+----------+------------------------------------+--------+------+-------+----------+-------+-------------------------------------------------------------
398+
f1 | 5 | assignment | 42804 | subscripted object is not an array | | | error | | | at assignment to element of variable "r" declared on line 2
399399
(1 row)
400400

401401
select f1();
@@ -437,9 +437,9 @@ end;
437437
$$ language plpgsql;
438438
-- should to fail
439439
select * from plpgsql_check_function_tb('f1_trg()','t1');
440-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
441-
------------+--------+------------+----------+-------------------------------+--------+------+-------+----------+-------+---------
442-
f1_trg | 5 | assignment | 42703 | record "new" has no field "c" | | | error | | |
440+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
441+
------------+--------+------------+----------+-------------------------------+--------+------+-------+----------+-------+-----------------------------------------------------------------
442+
f1_trg | 5 | assignment | 42703 | record "new" has no field "c" | | | error | | | at assignment to field "c" of variable "new" declared on line 0
443443
(1 row)
444444

445445
-- should to fail but not crash
@@ -699,10 +699,11 @@ select f1();
699699
(1 row)
700700

701701
select * from plpgsql_check_function('f1()');
702-
plpgsql_check_function
703-
------------------------------------------------------
702+
plpgsql_check_function
703+
------------------------------------------------------------------------
704704
error:42703:6:assignment:record "r" has no field "c"
705-
(1 row)
705+
Context: at assignment to field "c" of variable "r" declared on line 2
706+
(2 rows)
706707

707708
select f1();
708709
f1
@@ -729,12 +730,13 @@ select f1();
729730
(1 row)
730731

731732
select * from plpgsql_check_function('f1()');
732-
plpgsql_check_function
733-
----------------------------------------------------
733+
plpgsql_check_function
734+
-----------------------------------------------------------
734735
error:42703:5:assignment:column "a" does not exist
735736
Query: SELECT a + b
736737
-- ^
737-
(3 rows)
738+
Context: at assignment to variable "r" declared on line 2
739+
(4 rows)
738740

739741
select f1();
740742
f1
@@ -759,12 +761,13 @@ select f1();
759761
(1 row)
760762

761763
select * from plpgsql_check_function('f1()');
762-
plpgsql_check_function
763-
----------------------------------------------------
764+
plpgsql_check_function
765+
----------------------------------------------------------------------
764766
error:42703:5:assignment:column "c" does not exist
765767
Query: SELECT c+10
766768
-- ^
767-
(3 rows)
769+
Context: at assignment to element of variable "r" declared on line 2
770+
(4 rows)
768771

769772
select f1();
770773
f1
@@ -789,10 +792,11 @@ select f1();
789792
(1 row)
790793

791794
select * from plpgsql_check_function('f1()');
792-
plpgsql_check_function
793-
-------------------------------------------------------------
795+
plpgsql_check_function
796+
----------------------------------------------------------------------
794797
error:42804:5:assignment:subscripted object is not an array
795-
(1 row)
798+
Context: at assignment to element of variable "r" declared on line 2
799+
(2 rows)
796800

797801
select f1();
798802
f1
@@ -834,10 +838,11 @@ end;
834838
$$ language plpgsql;
835839
-- should to fail
836840
select * from plpgsql_check_function('f1_trg()','t1');
837-
plpgsql_check_function
838-
--------------------------------------------------------
841+
plpgsql_check_function
842+
--------------------------------------------------------------------------
839843
error:42703:5:assignment:record "new" has no field "c"
840-
(1 row)
844+
Context: at assignment to field "c" of variable "new" declared on line 0
845+
(2 rows)
841846

842847
-- should to fail but not crash
843848
insert into t1 values(6,30);
@@ -1213,12 +1218,15 @@ select * from plpgsql_check_function('f1()', performance_warnings := true);
12131218
warning:42804:7:assignment:target type is different type than source type
12141219
Detail: cast "text" value to "integer" type
12151220
Hint: The input expression type does not have an assignment cast to the target type.
1221+
Context: at assignment to variable "intval" declared on line 3
12161222
warning:42804:8:assignment:target type is different type than source type
12171223
Detail: cast "text" value to "integer" type
12181224
Hint: The input expression type does not have an assignment cast to the target type.
1225+
Context: at assignment to variable "intval" declared on line 3
12191226
warning:42804:9:assignment:target type is different type than source type
12201227
Detail: cast "date" value to "integer" type
12211228
Hint: There are no possible explicit coercion between those types, possibly bug!
1229+
Context: at assignment to variable "intval" declared on line 3
12221230
warning:42804:12:SQL statement:target type is different type than source type
12231231
Detail: cast "text" value to "integer" type
12241232
Hint: The input expression type does not have an assignment cast to the target type.
@@ -1228,7 +1236,7 @@ select * from plpgsql_check_function('f1()', performance_warnings := true);
12281236
warning extra:00000:3:DECLARE:never read variable "intval"
12291237
performance:00000:routine is marked as VOLATILE, should be STABLE
12301238
Hint: When you fix this issue, please, recheck other functions that uses this function.
1231-
(18 rows)
1239+
(21 rows)
12321240

12331241
drop function f1();
12341242
create or replace function f1()
@@ -1806,10 +1814,10 @@ begin
18061814
end;
18071815
$$ language plpgsql;
18081816
select * from plpgsql_check_function_tb('fx()', performance_warnings := true);
1809-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1810-
------------+--------+--------------------+----------+-------------------------------------------------+----------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+---------
1817+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1818+
------------+--------+--------------------+----------+-------------------------------------------------+----------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+--------------------------------------------------
18111819
fx | 6 | FOREACH over array | 42804 | target type is different type than source type | cast "integer" value to "numeric" type | Hidden casting can be a performance issue. | performance | | |
1812-
fx | 8 | assignment | 42804 | target type is different type than source type | cast "numeric" value to "integer" type | Hidden casting can be a performance issue. | performance | | |
1820+
fx | 8 | assignment | 42804 | target type is different type than source type | cast "numeric" value to "integer" type | Hidden casting can be a performance issue. | performance | | | at assignment to variable "s" declared on line 3
18131821
fx | | | 00000 | routine is marked as VOLATILE, should be STABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
18141822
(3 rows)
18151823

@@ -1912,9 +1920,9 @@ begin
19121920
end;
19131921
$$ language plpgsql;
19141922
select * from plpgsql_check_function_tb('fx()', performance_warnings := true, fatal_errors := false);
1915-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1916-
------------+--------+------------+----------+----------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+---------------+----------+-------+---------
1917-
fx | 9 | assignment | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | |
1923+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1924+
------------+--------+------------+----------+----------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+---------------+----------+-------+--------------------------------------------------------------
1925+
fx | 9 | assignment | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at assignment to element of variable "sa" declared on line 4
19181926
fx | 4 | DECLARE | 00000 | never read variable "sa" | | | warning extra | | |
19191927
fx | | | 00000 | routine is marked as VOLATILE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
19201928
(3 rows)
@@ -1935,11 +1943,12 @@ select * from plpgsql_check_function('fx()', performance_warnings := true);
19351943
plpgsql_check_function
19361944
-----------------------------------------------------------------------------------------
19371945
error:42804:7:assignment:cannot cast composite value to a scalar type
1946+
Context: at assignment to variable "_txt" declared on line 3
19381947
warning extra:00000:2:DECLARE:never read variable "_tt"
19391948
warning extra:00000:3:DECLARE:never read variable "_txt"
19401949
performance:00000:routine is marked as VOLATILE, should be IMMUTABLE
19411950
Hint: When you fix this issue, please, recheck other functions that uses this function.
1942-
(5 rows)
1951+
(6 rows)
19431952

19441953
drop function fx();
19451954
create or replace function fx()
@@ -2200,9 +2209,10 @@ select * from plpgsql_check_function('fx()', performance_warnings := true, fatal
22002209
warning:42804:4:assignment:target type is different type than source type
22012210
Detail: cast "integer" value to "date" type
22022211
Hint: There are no possible explicit coercion between those types, possibly bug!
2212+
Context: at assignment to variable "d" declared on line 2
22032213
performance:00000:routine is marked as VOLATILE, should be STABLE
22042214
Hint: When you fix this issue, please, recheck other functions that uses this function.
2205-
(5 rows)
2215+
(6 rows)
22062216

22072217
drop function fx();
22082218
create table tab_1(i int);
@@ -3540,6 +3550,7 @@ select * from plpgsql_check_function('test_crash', fatal_errors=>true);
35403550
Query: SELECT buggyfunc(10)
35413551
-- ^
35423552
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
3543-
(4 rows)
3553+
Context: during statement block local variable "ec" initialization on line 3
3554+
(5 rows)
35443555

35453556
drop function test_crash();

expected/plpgsql_check_passive.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ end;
8585
$$ language plpgsql;
8686
select f1();
8787
ERROR: record "r" has no field "c"
88-
CONTEXT: PL/pgSQL function f1() line 6 at assignment
88+
CONTEXT: PL/pgSQL function f1() line 6 at assignment to field "c" of variable "r" declared on line 2
8989
drop function f1();
9090
drop function g1();
9191
create function f1()
@@ -103,7 +103,7 @@ ERROR: column "a" does not exist
103103
LINE 1: SELECT a + b
104104
^
105105
QUERY: SELECT a + b
106-
CONTEXT: PL/pgSQL function f1() line 5 at assignment
106+
CONTEXT: PL/pgSQL function f1() line 5 at assignment to variable "r" declared on line 2
107107
drop function f1();
108108
create or replace function f1()
109109
returns void as $$
@@ -119,7 +119,7 @@ ERROR: column "c" does not exist
119119
LINE 1: SELECT c+10
120120
^
121121
QUERY: SELECT c+10
122-
CONTEXT: PL/pgSQL function f1() line 5 at assignment
122+
CONTEXT: PL/pgSQL function f1() line 5 at assignment to element of variable "r" declared on line 2
123123
drop function f1();
124124
create or replace function f1()
125125
returns void as $$
@@ -132,7 +132,7 @@ end;
132132
$$ language plpgsql;
133133
select f1();
134134
ERROR: subscripted object is not an array
135-
CONTEXT: PL/pgSQL function f1() line 5 at assignment
135+
CONTEXT: PL/pgSQL function f1() line 5 at assignment to element of variable "r" declared on line 2
136136
drop function f1();
137137
create or replace function f1()
138138
returns void as $$

src/report.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ datum_is_explicit(PLpgSQL_checkstate *cstate, int dno)
115115
}
116116
}
117117

118-
119118
/*
120119
* returns true, when datum or some child is used
121120
*/
@@ -190,8 +189,6 @@ datum_is_used(PLpgSQL_checkstate *cstate, int dno, bool write)
190189
return false;
191190
}
192191

193-
194-
195192
/*
196193
* Reports all unused variables explicitly DECLAREd by the user. Ignores
197194
* special variables created by PL/PgSQL.

0 commit comments

Comments
 (0)