Skip to content

Declaration misplaced for instanceof pattern #2644

@zbynek

Description

@zbynek

The following method:

// import java.util.List;

public int bug(List<List<Object>> matrix) {
	List<Object> row = matrix.get(0);
	int offset = 0;
	while (offset < row.size() && row.get(offset) instanceof String function) {
		if (function.isEmpty()) {
			offset++;
		} else {
			offset += 2;
		}
	}
	if (offset > 0 && row.get(offset - 1) instanceof String function) {
		if (function.isEmpty()) {
			offset--;
		}
	}
	return offset;
}

gets translated into

- (int32_t)bugWithJavaUtilList:(id<JavaUtilList>)matrix {
  id tmp = [((id<JavaUtilList>) nil_chk(row)) getWithInt:offset - 1];
  NSString *function = nil;
  id tmp_1 = [row getWithInt:offset];
  NSString *function_1 = nil;
  id<JavaUtilList> row = JreRetainedLocalValue([((id<JavaUtilList>) nil_chk(matrix)) getWithInt:0]);
  int32_t offset = 0;
  while (offset < [((id<JavaUtilList>) nil_chk(row)) size] && [tmp_1 isKindOfClass:[NSString class]] && (function_1 = (NSString *) tmp_1, true)) {
    if ([((NSString *) nil_chk(function_1)) isEmpty]) {
      offset++;
    }
    else {
      offset += 2;
    }
  }
  if (offset > 0 && [tmp isKindOfClass:[NSString class]] && (function = (NSString *) tmp, true)) {
    if ([((NSString *) nil_chk(function)) isEmpty]) {
      offset--;
    }
  }
  return offset;
}

which is wrong; the variables tmp and tmp_1 need to be declared after offset and row. Compilation of the generated code fails with
error: use of undeclared identifier 'row'.

Reproducible with f49498e

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions