Skip to content

Commit

Permalink
Remove top-level SymExprs from the AST (chapel-lang#26321)
Browse files Browse the repository at this point in the history
They aren't used for anything, so deleting them is reasonable.

This fixes chapel-lang#25885. The crux
of that issue is that we incorrectly identify the `R` type as "unused",
since it's generic but not used for any sub-classes and is never
instantiated. I first attempted to fix the problem by adjusting
`isUnusedClass` to respect `SymExprs` still in the tree that still point
to the class. However, subsequent code __deletes all generic type
declarations, because by that point we expect all occurrences to be
concrete__:

```Chapel
new genericType(...); // replaced with a call to concreteType.init
var x: genericType = ...; // replaced with `var x: concreteType`

// the following:
proc foo(type arg) {}
foo(genericType)
// is replaced with:
proc foo() {}
foo();
```

Deleting all generic types seems like a reasonable approach to me; the
standalone variables are a weird exception, in which the type is neither
instantiated nor removed. Moreover, since they don't modify or even
really access the variable, these standalone `SymExprs` can be safely
removed. This PR does just that.

Reviewed by @jabraham17 -- thanks!

## Testing
- [x] paratest
  • Loading branch information
DanilaFe authored Nov 25, 2024
2 parents a5ea505 + 05c6e26 commit d0ac06e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/resolution/cleanups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ static void removeUnusedFunctions() {
}
}

static void removeTopLevelSymExprs() {
for_alive_in_Vec(SymExpr, se, gSymExprs) {
if (se->getStmtExpr() == se) {
se->remove();
}
}
}

static CallExpr* replaceRuntimeTypeGetField(CallExpr* call) {
SymExpr* rt = toSymExpr(call->get(1));

Expand Down Expand Up @@ -1038,6 +1046,8 @@ void pruneResolvedTree() {

removeUnusedFunctions();

removeTopLevelSymExprs();

if (fRemoveUnreachableBlocks) {
deadBlockElimination();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
record R { var arg; }
R;
Empty file.

0 comments on commit d0ac06e

Please sign in to comment.