@@ -245,7 +245,7 @@ function removeUnusedVariables(spec) {
245245 // Keep track of all variable names that are referenced somewhere. Note that we
246246 // don't attempt to track specific "ref ids" (e.g. `_some_variable[_subscript]`)
247247 // but instead just track generic variable names (e.g. `_some_variable`). This
248- // ensure that we include all subscripts for a variable, which might mean we
248+ // ensures that we include all subscripts for a variable, which might mean we
249249 // include some subscripts that aren't needed, but it is safer than trying to
250250 // eliminate those and possibly omit something that is needed.
251251 const referencedVarNames = [ ]
@@ -259,7 +259,7 @@ function removeUnusedVariables(spec) {
259259 }
260260
261261 // Add the given variable to the list of referenced variables, and do the same for
262- // some special things (i.e., lookups) that it might reference.a
262+ // some special things (i.e., lookups) that it might reference.
263263 const recordUsedVariable = v => {
264264 // Add the variable to the list of referenced variables
265265 recordUsedVarName ( v . varName )
@@ -291,8 +291,15 @@ function removeUnusedVariables(spec) {
291291 // that it references) as being "used".
292292 const referencedRefIds = [ ]
293293 const recordRefsOfVariable = v => {
294- let refs = v . references . concat ( v . initReferences )
295- for ( const refId of refs ) {
294+ // If this variable is subscripted, we need to record all subscript variants;
295+ // `refIdsWithName` will return those. We also need to record all variables
296+ // that are referenced by this variable, either directly (`v.references`) or
297+ // in an "INITIAL" expression (`v.initReferences`). It's OK if we end up with
298+ // duplicates in this list, because we will examine each reference only once.
299+ let refIds = refIdsWithName ( v . varName )
300+ refIds = refIds . concat ( v . references )
301+ refIds = refIds . concat ( v . initReferences )
302+ for ( const refId of refIds ) {
296303 if ( ! referencedRefIds . includes ( refId ) ) {
297304 referencedRefIds . push ( refId )
298305 const refVar = varWithRefId ( refId )
0 commit comments