False errors due to scope condition being set incorrectly when doing Sema lookup
Closed this issue · 1 comments
For example:
namespace foo {
template <typename T>
void bar() {}
}
void baz() {
#ifdef A
int var=0;
for( unsigned i=0; i<10; ++i ) {
if (i)
++var;
}
#endif
}
Produces errors:
scope_error_after_template.cpp:10:9: error: i is not defined in context ~A
if (i)
^
scope_error_after_template.cpp:11:9: error: var is not defined in context ~A
++var;
^
These errors are emitted because the scope condition is sometimes set to True
when it should be A
. This causes Sema to report that the variable's declaration doesn't cover the entire lookup space.
Update: this bug is caused by caching of old Scope
objects. When the parser enters the if
statement, it uses a cached Scope
object which had a condition of True
. Since this condition is not reset, Sema lookup is being done from context True
instead of context A
. This caused Sema to find that the declarations of i
and var
did not cover the entire lookup space.
This issue can be fixed by properly initializing cached Scope
objects after they are retrieved from the cache. The method Scope::Init
can be modified to set the Condition
field of the current Scope
to the Condition
of the parent scope.