Skip to content

Commit

Permalink
Test and handle similar shadowing formal case
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Rift <[email protected]>
  • Loading branch information
riftEmber committed Sep 19, 2024
1 parent 0cfd280 commit 377ec06
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
5 changes: 0 additions & 5 deletions frontend/include/chpl/parsing/parsing-queries.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,6 @@ bool idIsPrivateDecl(Context* context, ID id);
*/
bool idIsFunction(Context* context, ID id);

/**
Returns true if the ID is a variable.
*/
bool idIsVariable(Context* context, ID id);

/**
Returns true if the ID is marked 'extern'.
*/
Expand Down
5 changes: 0 additions & 5 deletions frontend/lib/parsing/parsing-queries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,11 +1315,6 @@ bool idIsFunction(Context* context, ID id) {
return asttags::isFunction(tag);
}

bool idIsVariable(Context* context, ID id) {
AstTag tag = idToTag(context, id);
return asttags::isVariable(tag);
}

static bool
checkLinkage(Context* context, ID id, uast::Decl::Linkage linkage) {
if (id.isEmpty()) return false;
Expand Down
3 changes: 2 additions & 1 deletion frontend/lib/resolution/Resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3003,7 +3003,8 @@ void Resolver::resolveIdentifier(const Identifier* ident) {
if (resolvingCalledIdent && ids.numIds() > 1) {
bool onlyVars = true;
for (auto idIt = ids.begin(); idIt != ids.end(); ++idIt) {
if (!parsing::idIsVariable(context, idIt.curIdAndFlags().id())) {
if (!parsing::idToAst(context, idIt.curIdAndFlags().id())
->isVarLikeDecl()) {
onlyVars = false;
break;
}
Expand Down
64 changes: 47 additions & 17 deletions frontend/test/resolution/testMethodCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,29 +767,59 @@ static void test15() {
}

static void test16() {
// Test resolving 'this' call on automatic variable that shadows field
Context ctx;
Context* context = &ctx;
ErrorGuard guard(context);
// Test resolving 'this' call on variable that shadows field

std::string program = R"""(
class Foo {
var tup : 2*int;
{
// For automatic variable
Context ctx;
Context* context = &ctx;
ErrorGuard guard(context);

proc init() {}
std::string program = R"""(
class Foo {
var tup : 2*int;
proc doSomething() {
const tup = this.tup;
return tup(0);
proc init() {}
proc doSomething() {
const tup = this.tup;
return tup(0);
}
}
}
var f = new Foo();
var x = f.doSomething();
)""";
var f = new Foo();
var x = f.doSomething();
)""";

auto vars = resolveTypesOfVariables(context, program, { "x" });
assert(guard.realizeErrors() == 0);
auto vars = resolveTypesOfVariables(context, program, { "x" });
assert(guard.realizeErrors() == 0);
}

{
// For formal
Context ctx;
Context* context = &ctx;
ErrorGuard guard(context);

std::string program = R"""(
class Foo {
var tup : 2*int;
proc init() {}
proc doSomething(tup) {
return tup(0);
}
}
var f = new Foo();
var anotherTup : 2*int;
var x = f.doSomething(anotherTup);
)""";

auto vars = resolveTypesOfVariables(context, program, { "x" });
assert(guard.realizeErrors() == 0);
}
}

int main() {
Expand Down

0 comments on commit 377ec06

Please sign in to comment.