Skip to content

Commit a2c71a9

Browse files
committed
Ban the shadowing for parameter names too (extends previous commit)
See commit 96c4126
1 parent 96c4126 commit a2c71a9

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

source/cppfront.cpp

+32-14
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,33 @@ class cppfront
31683168
}
31693169

31703170

3171+
//-----------------------------------------------------------------------
3172+
// Within a type scope implementation, disallow declaring a name that
3173+
// is the same as (i.e., shadows) a type scope name... this is a
3174+
// convenient place to check because we have the decls stack
3175+
//
3176+
auto check_shadowing_of_type_scope_names(
3177+
declaration_node const& decl
3178+
)
3179+
-> bool
3180+
{
3181+
if (
3182+
decl.has_name() // this is a named declaration
3183+
&& !decl.parent_is_type() // and the type isn't the direct parent
3184+
&& is_name_declared_in_current_type_scope(*decl.name())
3185+
) // and it shadows a name
3186+
{
3187+
errors.emplace_back(
3188+
decl.position(),
3189+
"a type's implementation may not declare a name that is the same as (i.e., shadows) a type scope name - for example, a type scope function's local variable may not have the same as one of the type's members"
3190+
);
3191+
return false;
3192+
}
3193+
3194+
return true;
3195+
}
3196+
3197+
31713198
//-----------------------------------------------------------------------
31723199
//
31733200
auto emit(
@@ -3181,6 +3208,10 @@ class cppfront
31813208
assert( n.declaration );
31823209
assert( !n.declaration->is_function() );
31833210

3211+
if (!check_shadowing_of_type_scope_names(*n.declaration)) {
3212+
return;
3213+
}
3214+
31843215
if (n.mod == parameter_declaration_node::modifier::implicit)
31853216
{
31863217
if (
@@ -3655,20 +3686,7 @@ class cppfront
36553686
;
36563687
auto is_in_type = n.parent_is_type();
36573688

3658-
// In a type scope function, disallow declaring a name that is
3659-
// the same as (i.e., shadows) a type scope name... this is a
3660-
// convenient place to check because we have the decls stack
3661-
if (
3662-
n.has_name() // this is a named declaration
3663-
&& !n.parent_is_type() // where the type isn't the direct parent
3664-
&& is_name_declared_in_current_type_scope(*n.name()) // and it shadows a name
3665-
&& !emit_constructor_as_assignment // (don't emit the error twice if this is the
3666-
) // second time we're 're handling this function)
3667-
{
3668-
errors.emplace_back(
3669-
n.position(),
3670-
"a type's implementation may not declare a name that is the same as (i.e., shadows) a type scope name - for example, a type scope function's local variable may not have the same as one of the type's members"
3671-
);
3689+
if (!check_shadowing_of_type_scope_names(n)) {
36723690
return;
36733691
}
36743692

0 commit comments

Comments
 (0)