External Issue: [Bug]: generic with try-catch handling

28436, "jmag722", "[Bug]: generic with try-catch handling", "2026-02-23T13:17:50Z"

Summary of Problem

While working on #17277, I attempted to design assertThrows to function using a manage statement, similar to pytest.raises. I was unable to compile it

See initial discussion on Chapel Discourse

Could be related to #28148, #24882

Description:

Unable to compile a context manager class that had a generic variable with a try-catch statement.

Per @jabraham17's testing, it's related to having a try-catch with a generic

The workaround in the meantime is to have a conventional assertThrows(function, errType, ...args?) for UnitTest.

Is this issue currently blocking your progress?
No, but I think this is a better design for asserting if an error is thrown.

Steps to Reproduce

Source Code:

original code that had this issue:

module Foo {

  class AssertionError: Error {  // thanks UnitTest
    proc init(msg: string = "") {
      super.init(msg);
    }
  }

  class RaiseErrorManager: contextManager {
    type errType;

    proc enterContext() {}

    proc exitContext(in err: owned Error?) throws {
      try {
        if err then throw err;
        throw new owned AssertionError("manage stmt didn't throw any error!");
      }
      catch e: errType {  // anticipated error
        return;
      }
      catch e {  // unanticipated error
        throw e;
      }
    }
  }

  class SomeError : Error {
    proc init() {}
  }


  proc main() {

    proc foo() throws {
      throw new owned SomeError();
    }

    manage new owned RaiseErrorManager(SomeError) {
      foo();
    }
  }
}

more minimalistic examples found by @jabraham17 that zero in on the issue (not context-manager related):

record R {
  type T;

  proc doit() {
    try! {
      throw new T();
    } catch e: T {
      writeln("caught error of type ", T:string);
    }
  }
}
var r = new R(Error);
r.doit();

OR

proc foo(type T) {
  try! {
    throw new T();
  } catch e: T {
    writeln("caught error of type ", T:string);
  }
}
foo(Error);

OR

class R {
  type T;

  proc doit() {
    type locT = T;
    try! {
      throw new locT();
    } catch e: locT { // error: mismatched number of arguments in call to 'chpl_error'
      writeln("caught error of type ", T:string);
    }
  }
}
var r = new R(Error);
r.doit();

Compile command:
mason build

Execution command:
Not there yet

Associated Future Test(s):

Configuration Information

  • Output of chpl --version:
chpl version 2.8.0 pre-release (35de7651ed9)
  built with LLVM version 20.1.8
  available LLVM targets: xcore, x86-64, x86, wasm64, wasm32, ve, systemz, spirv, spirv64, spirv32, sparcel, sparcv9, sparc, riscv64, riscv32, ppc64le, ppc64, ppc32le, ppc32, nvptx64, nvptx, msp430, mips64el, mips64, mipsel, mips, loongarch64, loongarch32, lanai, hexagon, bpfeb, bpfel, bpf, avr, thumbeb, thumb, armeb, arm, amdgcn, r600, aarch64_32, aarch64_be, aarch64, arm64_32, arm64
Copyright 2020-2026 Hewlett Packard Enterprise Development LP
Copyright 2004-2019 Cray Inc.
(See LICENSE file for more details)
  • Output of $CHPL_HOME/util/printchplenv --anonymize:
CHPL_TARGET_PLATFORM: linux64
CHPL_TARGET_COMPILER: llvm
CHPL_TARGET_ARCH: x86_64
CHPL_TARGET_CPU: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: qthreads
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: bundled
CHPL_TARGET_MEM: jemalloc
CHPL_ATOMICS: cstdlib
CHPL_GMP: bundled
CHPL_HWLOC: bundled
CHPL_RE2: bundled
CHPL_LLVM: system +
CHPL_AUX_FILESYS: none
  • Back-end compiler and version, e.g. gcc --version or clang --version:
clang version 20.1.8
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: .../opt/spack/opt/spack/linux-skylake/llvm-20.1.8-mhehzzjyy6fsnij4lutzbjgox7k2dahc/bin
Configuration file: .../opt/spack/opt/spack/linux-skylake/llvm-20.1.8-mhehzzjyy6fsnij4lutzbjgox7k2dahc/bin/clang.cfg