External Issue: Inherited class: bug when referencing a vector's element

21073, "Guillaume-Helbecque", "Inherited class: bug when referencing a vector's element", "2022-11-22T10:20:41Z"

https://github.com/chapel-lang/chapel/issues/21073

Summary of Problem

Here is the background: There is (for minimal example) an abstract parent class which implements a default procedure proc plusone(ref n: int): void that modifies the given value by adding 1. There is also an inherited child class, which overrides plusone by adding 2 instead of 1.

Here is the bug: When I pass a vector's element v[id] as reference to child.plusone(v[id]), the value of v[id] is not changed. However, using parent.plusone(v[id]), the value is incremented by 1. Another thing, when using var k = v[id] followed by child.plusone(k), the result is fine and v[id] is incremented by 2.

Proposed workaround: A workaround to this bug is thus to use ref k = v[id] and to pass k.

Steps to Reproduce

Source Code:

class parent {
  proc plusone(ref n: int): void
  {
    n = n + 1;
  }
}

class child : parent {
  override proc plusone(ref n: int): void
  {
    n = n + 2;
  }
}

proc main(): int
{
  var c: parent = new child();
  var v: [1..5] int = 0;

  c.plusone(v[3]);
  writeln("first try: ", v); // returns 0 0 0 0 0 

  ref k: int = v[3];
  c.plusone(k);
  writeln("second try: ", v); // returns 0 0 2 0 0

  return 0;
}

Compile command:
chpl foo.chpl -o foo.o --fast

Execution command:
./foo.o

Configuration Information

  • Output of chpl --version: chpl version 1.28.0
  • Output of $CHPL_HOME/util/printchplenv --anonymize:
CHPL_TARGET_PLATFORM: linux64
CHPL_TARGET_COMPILER: gnu
CHPL_TARGET_ARCH: native *
CHPL_TARGET_CPU: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: qthreads *
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: jemalloc
CHPL_ATOMICS: cstdlib
CHPL_GMP: bundled
CHPL_HWLOC: bundled
CHPL_RE2: bundled
CHPL_LLVM: none *
CHPL_AUX_FILESYS: none
  • Back-end compiler and version, e.g. gcc --version or clang --version: gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0