Skip to content

Commit ee3b38e

Browse files
Detect returning a reference to an element of an array temporary. [GH #106]
Follow the array prvalue operand of an array-to-pointer conversion when looking for temporaries, so that "return A{1, 2}[0];" is diagnosed. Co-authored-by: Daveed Vandevoorde <dvandevoorde@nvidia.com>
1 parent 714c29c commit ee3b38e

12 files changed

Lines changed: 128 additions & 18 deletions

File tree

‎src/exprutil.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25255,9 +25255,11 @@ that identifies an object or a prvalue that is a pointer to an object.
2525525255
{
2525625256
if (is_glvalue_node(expr) ||
2525725257
(tblock->follow_class_rvalue_addressing_path &&
25258-
is_class_struct_union_type(expr->type))) {
25258+
is_class_struct_union_type(expr->type)) ||
25259+
(tblock->look_for_temporaries && is_array_type(expr->type))) {
2525925260
/* The expression passed in is a glvalue for an object (or a class
25260-
prvalue object). */
25261+
prvalue object, or, when looking for temporaries, an array
25262+
prvalue). */
2526125263
/* Skip some operation nodes (like value casts) that do not affect the
2526225264
underlying object. */
2526325265
while (is_operation_node(expr)) {
@@ -25302,8 +25304,8 @@ that identifies an object or a prvalue that is a pointer to an object.
2530225304
tblock->terminate = TRUE;
2530325305
} /* if */
2530425306
} else if (tblock->look_for_temporaries && !is_glvalue_node(expr)) {
25305-
/* A class prvalue (e.g., a function call) on the addressing path, for
25306-
example the object expression of "f().x": a temporary is
25307+
/* A class or array prvalue (e.g., a function call) on the addressing
25308+
path, for example the object expression of "f().x": a temporary is
2530725309
materialized for it. */
2530825310
tblock->result = TRUE;
2530925311
tblock->is_temp = TRUE;

‎src/il_walk.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,8 +3409,10 @@ Tips for proper use of the follow_addressing_path mode:
34093409
traverse_expr(operand1, tblock);
34103410
break;
34113411
case eok_array_to_pointer:
3412-
/* Array-to-pointer decay. Watch out for the array prvalue case. */
3413-
if (is_glvalue_node(operand1)) {
3412+
/* Array-to-pointer decay. Watch out for the array prvalue case,
3413+
which is followed only when looking for temporaries (e.g., for
3414+
the "A{1, 2}" in "A{1, 2}[0]"). */
3415+
if (is_glvalue_node(operand1) || tblock->look_for_temporaries) {
34143416
traverse_expr(operand1, tblock);
34153417
} /* if */
34163418
break;

‎src/il_walk.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ typedef struct an_expr_or_stmt_traversal_block {
393393
a_boolean look_for_temporaries;
394394
/* TRUE if a temporary anywhere on the addressing
395395
path (e.g., in either operand of a "?" operator,
396-
or a class prvalue whose member is selected)
397-
should be reported in preference to a local
398-
variable. */
396+
a class prvalue whose member is selected, or an
397+
array prvalue) should be reported in preference
398+
to a local variable. */
399399
#if HANDLE_VIRTUAL_BASES_IN_COMPLETE_CTOR_DTORS
400400
a_variable_ptr
401401
orig_params;

‎src/pending_changes/gh-106-return-ref-to-temporary.txt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ Clang versions before 19, which do the same). For example, with --c++26:
1212

1313
Returning a reference to a member of a temporary, as in g, was previously not
1414
diagnosed at all in the standard (non-GNU, non-Clang) modes; before C++26 it
15-
now gets the same warning as the other cases. Returning a reference to a
16-
local variable is still only a warning.
15+
now gets the same warning as the other cases. The same is true of returning
16+
a reference to an array temporary or to one of its elements (e.g.,
17+
"return A{1, 2};" or "return A{1, 2}[0];" with "using A = int[2];") in all
18+
modes. Returning a reference to a local variable is still only a warning.
1719

1820
As P2748 specifies, this does not apply to the conversion checked by
1921
std::is_convertible (e.g., __is_convertible_to(long, const int&) is still

‎tests/tests/changes/.GH106.rto/default.1.1.txt‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,20 @@ fe_only -DTEST_NUMBER=1 --c++26 Test_name.c
3131
A&& f9() { return A{1, 2}; } // Array temporary.
3232
^
3333

34-
7 errors detected in the compilation of "Test_name.c".
34+
"Test_name.c", line 23: error: returning reference to local temporary
35+
const A& f10() { return A{1, 2}; } // Array temporary.
36+
^
37+
38+
"Test_name.c", line 24: error: returning reference to local temporary
39+
const int& f11() { return A{1, 2}[0]; } // Element of an array temporary.
40+
^
41+
42+
"Test_name.c", line 25: error: returning reference to local temporary
43+
const int& f12(int n) { return A{n, 2}[1]; }
44+
^
45+
46+
"Test_name.c", line 26: error: returning reference to local temporary
47+
const int& f13() { return 0[A{1, 2}]; }
48+
^
49+
50+
11 errors detected in the compilation of "Test_name.c".

‎tests/tests/changes/.GH106.rto/default.2.1.txt‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,24 @@ fe_only -DTEST_NUMBER=2 --c++26 --gnu_version 140000 Test_name.c
3131
A&& f9() { return A{1, 2}; } // Array temporary.
3232
^
3333

34-
"Test_name.c", line 27: warning: returning reference to local temporary
34+
"Test_name.c", line 23: error: returning reference to local temporary
35+
const A& f10() { return A{1, 2}; } // Array temporary.
36+
^
37+
38+
"Test_name.c", line 24: error: returning reference to local temporary
39+
const int& f11() { return A{1, 2}[0]; } // Element of an array temporary.
40+
^
41+
42+
"Test_name.c", line 25: error: returning reference to local temporary
43+
const int& f12(int n) { return A{n, 2}[1]; }
44+
^
45+
46+
"Test_name.c", line 26: error: returning reference to local temporary
47+
const int& f13() { return 0[A{1, 2}]; }
48+
^
49+
50+
"Test_name.c", line 31: warning: returning reference to local temporary
3551
const int *const *const &f6() { return pp; }
3652
^
3753

38-
7 errors detected in the compilation of "Test_name.c".
54+
11 errors detected in the compilation of "Test_name.c".

‎tests/tests/changes/.GH106.rto/default.3.1.txt‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,24 @@ fe_only -DTEST_NUMBER=3 --c++26 --clang_version 190000 Test_name.c
3131
A&& f9() { return A{1, 2}; } // Array temporary.
3232
^
3333

34-
"Test_name.c", line 27: warning: returning reference to local temporary
34+
"Test_name.c", line 23: error: returning reference to local temporary
35+
const A& f10() { return A{1, 2}; } // Array temporary.
36+
^
37+
38+
"Test_name.c", line 24: error: returning reference to local temporary
39+
const int& f11() { return A{1, 2}[0]; } // Element of an array temporary.
40+
^
41+
42+
"Test_name.c", line 25: error: returning reference to local temporary
43+
const int& f12(int n) { return A{n, 2}[1]; }
44+
^
45+
46+
"Test_name.c", line 26: error: returning reference to local temporary
47+
const int& f13() { return 0[A{1, 2}]; }
48+
^
49+
50+
"Test_name.c", line 31: warning: returning reference to local temporary
3551
const int *const *const &f6() { return pp; }
3652
^
3753

38-
7 errors detected in the compilation of "Test_name.c".
54+
11 errors detected in the compilation of "Test_name.c".

‎tests/tests/changes/.GH106_a.rto/default.1.1.txt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ fe_only -DTEST_NUMBER=1 --c++23 Test_name.c
3131
A&& f9() { return A{1, 2}; } // Array temporary.
3232
^
3333

34+
"Test_name.c", line 22: warning: returning reference to local temporary
35+
const A& f10() { return A{1, 2}; } // Array temporary.
36+
^
37+
38+
"Test_name.c", line 23: warning: returning reference to local temporary
39+
const int& f11() { return A{1, 2}[0]; } // Element of an array temporary.
40+
^
41+
42+
"Test_name.c", line 24: warning: returning reference to local temporary
43+
const int& f12(int n) { return A{n, 2}[1]; }
44+
^
45+
46+
"Test_name.c", line 25: warning: returning reference to local temporary
47+
const int& f13() { return 0[A{1, 2}]; }
48+
^
49+

‎tests/tests/changes/.GH106_a.rto/default.2.1.txt‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,23 @@ fe_only -DTEST_NUMBER=2 --c++26 --gnu_version 130000 Test_name.c
3131
A&& f9() { return A{1, 2}; } // Array temporary.
3232
^
3333

34-
"Test_name.c", line 26: warning: returning reference to local temporary
34+
"Test_name.c", line 22: warning: returning reference to local temporary
35+
const A& f10() { return A{1, 2}; } // Array temporary.
36+
^
37+
38+
"Test_name.c", line 23: warning: returning reference to local temporary
39+
const int& f11() { return A{1, 2}[0]; } // Element of an array temporary.
40+
^
41+
42+
"Test_name.c", line 24: warning: returning reference to local temporary
43+
const int& f12(int n) { return A{n, 2}[1]; }
44+
^
45+
46+
"Test_name.c", line 25: warning: returning reference to local temporary
47+
const int& f13() { return 0[A{1, 2}]; }
48+
^
49+
50+
"Test_name.c", line 30: warning: returning reference to local temporary
3551
const int *const *const &f6() { return pp; }
3652
^
3753

‎tests/tests/changes/.GH106_a.rto/default.3.1.txt‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,23 @@ fe_only -DTEST_NUMBER=3 --c++26 --clang_version 180000 Test_name.c
3131
A&& f9() { return A{1, 2}; } // Array temporary.
3232
^
3333

34-
"Test_name.c", line 26: warning: returning reference to local temporary
34+
"Test_name.c", line 22: warning: returning reference to local temporary
35+
const A& f10() { return A{1, 2}; } // Array temporary.
36+
^
37+
38+
"Test_name.c", line 23: warning: returning reference to local temporary
39+
const int& f11() { return A{1, 2}[0]; } // Element of an array temporary.
40+
^
41+
42+
"Test_name.c", line 24: warning: returning reference to local temporary
43+
const int& f12(int n) { return A{n, 2}[1]; }
44+
^
45+
46+
"Test_name.c", line 25: warning: returning reference to local temporary
47+
const int& f13() { return 0[A{1, 2}]; }
48+
^
49+
50+
"Test_name.c", line 30: warning: returning reference to local temporary
3551
const int *const *const &f6() { return pp; }
3652
^
3753

0 commit comments

Comments
 (0)