Skip to content

Commit

Permalink
Merge pull request #20806 from riftEmber/use-name-conflict
Browse files Browse the repository at this point in the history
Check conflicting imported symbol names in Dyno scope-resolver

Updates the Dyno scope-resolver to search for a name in all use/imports, not just the first one that matches, so that name conflicts between imported symbols can be detected.

Resolves Cray/chapel-private#3903.

[reviewed by @DanilaFe]

Testing:
- [x] checking against tests listed in Cray/chapel-private#3903
- [x] primers with --dyno 
- [x] paratest
  • Loading branch information
riftEmber authored Oct 10, 2022
2 parents eb8b367 + de9b766 commit e88f789
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
14 changes: 8 additions & 6 deletions compiler/dyno/lib/resolution/scope-queries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ static bool doLookupInImports(Context* context,

if (r != nullptr) {
// check to see if it's mentioned in names/renames
bool found = false;
for (const VisibilitySymbols& is: r->visibilityClauses()) {
// if we should not continue transitively through private use/includes,
// and this is private, skip it
Expand All @@ -365,18 +366,19 @@ static bool doLookupInImports(Context* context,
}

// find it in that scope
bool found = doLookupInScope(context, symScope, nullptr, resolving,
from, newConfig,
checkedScopes, result);
if (found && onlyInnermost)
return true;
found |= doLookupInScope(context, symScope, nullptr, resolving, from,
newConfig, checkedScopes, result);
}

if (named && is.kind() == VisibilitySymbols::SYMBOL_ONLY) {
result.push_back(BorrowedIdsWithName(is.scope()->id()));
return true;
found = true;
}
}

if (found && onlyInnermost) {
return true;
}
}

if (scope->autoUsesModules() && !skipPrivateVisibilities) {
Expand Down
33 changes: 33 additions & 0 deletions compiler/dyno/test/resolution/testScopeResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,38 @@ static void test9() {
assert(reC.toId().isEmpty());
}

// test name conflicts can occur with uses
static void test10() {
printf("test10\n");
Context ctx;
Context* context = &ctx;

auto path = UniqueString::get(context, "input.chpl");
std::string contents = R""""(
module A {
var x = 2;
}
module B {
var x = 4;
}
module Z {
use A, B;
var a = x;
}
)"""";
setFileText(context, path, contents);

const ModuleVec& vec = parseToplevel(context, path);

const Variable* x = findVariable(vec, "x");
assert(x);
const Variable* a = findVariable(vec, "a");
assert(a);

const ResolvedExpression& reA = scopeResolveIt(context, a->initExpression());
assert(reA.toId().isEmpty());
}

int main() {
test1();
test2();
Expand All @@ -438,6 +470,7 @@ int main() {
test7();
test8();
test9();
test10();

return 0;
}

0 comments on commit e88f789

Please sign in to comment.