Skip to content

Commit 8695756

Browse files
committed
c++, contracts: P2900 3.4.3 Part 2
Do not allow contract result names to shadow function parms.
1 parent ff7cbc9 commit 8695756

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

gcc/cp/contracts.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,33 @@ start_function_contracts (tree decl1)
27692769
if (!handle_contracts_p (decl1))
27702770
return;
27712771

2772+
/* Check that the user did not try to shadow a function parameter with the
2773+
specified postcondition result name. */
2774+
if (flag_contracts_nonattr)
2775+
for (tree ca = DECL_CONTRACTS (decl1); ca; ca = CONTRACT_CHAIN (ca))
2776+
if (POSTCONDITION_P (CONTRACT_STATEMENT (ca)))
2777+
if (tree id = POSTCONDITION_IDENTIFIER (CONTRACT_STATEMENT (ca)))
2778+
{
2779+
if (TREE_CODE (id) == PARM_DECL)
2780+
id = DECL_NAME (id);
2781+
gcc_checking_assert (id && TREE_CODE (id) == IDENTIFIER_NODE);
2782+
tree seen = lookup_name (id);
2783+
if (seen
2784+
&& TREE_CODE (seen) == PARM_DECL
2785+
&& DECL_CONTEXT (seen)
2786+
&& DECL_CONTEXT (seen) == decl1)
2787+
{
2788+
auto_diagnostic_group d;
2789+
error_at (EXPR_LOCATION (CONTRACT_STATEMENT (ca)),
2790+
"contract postcondition result names must not shadow"
2791+
" function parameters");
2792+
inform (DECL_SOURCE_LOCATION (seen), "parameter declared here");
2793+
POSTCONDITION_IDENTIFIER (CONTRACT_STATEMENT (ca))
2794+
= error_mark_node;
2795+
CONTRACT_CONDITION (CONTRACT_STATEMENT (ca)) = error_mark_node;
2796+
}
2797+
}
2798+
27722799
/* For cdtors, we evaluate the contracts check inline. */
27732800
if (!outline_contracts_p (decl1))
27742801
return;
@@ -2778,7 +2805,6 @@ start_function_contracts (tree decl1)
27782805
/* Do we already have declarations generated ? */
27792806
if (!DECL_PRE_FN (decl1) && !DECL_POST_FN (decl1))
27802807
build_contract_function_decls (decl1);
2781-
27822808
}
27832809

27842810
/* If we have a precondition function and it's valid, call it. */

0 commit comments

Comments
 (0)