Skip to content

Commit

Permalink
Ban the shadowing for parameter names too (extends previous commit)
Browse files Browse the repository at this point in the history
See commit 96c4126
  • Loading branch information
hsutter committed Mar 12, 2023
1 parent 96c4126 commit a2c71a9
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3168,6 +3168,33 @@ class cppfront
}


//-----------------------------------------------------------------------
// Within a type scope implementation, disallow declaring a name that
// is the same as (i.e., shadows) a type scope name... this is a
// convenient place to check because we have the decls stack
//
auto check_shadowing_of_type_scope_names(
declaration_node const& decl
)
-> bool
{
if (
decl.has_name() // this is a named declaration
&& !decl.parent_is_type() // and the type isn't the direct parent
&& is_name_declared_in_current_type_scope(*decl.name())
) // and it shadows a name
{
errors.emplace_back(
decl.position(),
"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"
);
return false;
}

return true;
}


//-----------------------------------------------------------------------
//
auto emit(
Expand All @@ -3181,6 +3208,10 @@ class cppfront
assert( n.declaration );
assert( !n.declaration->is_function() );

if (!check_shadowing_of_type_scope_names(*n.declaration)) {
return;
}

if (n.mod == parameter_declaration_node::modifier::implicit)
{
if (
Expand Down Expand Up @@ -3655,20 +3686,7 @@ class cppfront
;
auto is_in_type = n.parent_is_type();

// In a type scope function, disallow declaring a name that is
// the same as (i.e., shadows) a type scope name... this is a
// convenient place to check because we have the decls stack
if (
n.has_name() // this is a named declaration
&& !n.parent_is_type() // where the type isn't the direct parent
&& is_name_declared_in_current_type_scope(*n.name()) // and it shadows a name
&& !emit_constructor_as_assignment // (don't emit the error twice if this is the
) // second time we're 're handling this function)
{
errors.emplace_back(
n.position(),
"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"
);
if (!check_shadowing_of_type_scope_names(n)) {
return;
}

Expand Down

0 comments on commit a2c71a9

Please sign in to comment.