Skip to content

[Bug]: logical operators do not work on nilable classes #25382

@jabraham17

Description

@jabraham17

Summary of Problem

Description:
Using logical operators (&& and ||) on a nilable class causes strange resolution error that it cannot match isTrue. Its not clear to me if this is intended to work since we explicitly disallow && and || on integral types.

Making it work is an easy patch, add inline proc isTrue(x:borrowed class?) do return x != nil; to ChapelBase. Making it a better error message is also an easy patch, add proc isTrue(x:borrowed class?) do compilerError("short-circuiting logical operators not supported on classes");

Note there is a similar issue with c_ptr, which gives an error about not being able to promote &&/||. I believe the c_ptr case can be solved similarly.

Is this issue currently blocking your progress?
no, can be worked around with an explict != nil

Steps to Reproduce

Source Code:

class C { }
var x = new unmanaged C?();
var y = new unmanaged C?();
// if x { } // this is fine
if x && y { } // error: unable to resolve call isTrue
if x || y { } // error: unable to resolve call isTrue
use CTypes;
var x: c_ptr(void) = nil;
var y = x;
// if x { } // this is fine
if x && y { } // error: cannot promote short-circuiting &&

Compile command:
chpl foo.chpl

Configuration Information

  • Output of chpl --version: chpl version 2.1.0 pre-release
  • Output of $CHPL_HOME/util/printchplenv --anonymize:
CHPL_TARGET_PLATFORM: darwin
CHPL_TARGET_COMPILER: llvm
CHPL_TARGET_ARCH: arm64
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: system *
CHPL_HWLOC: system *
CHPL_RE2: bundled *
CHPL_LLVM: system *
CHPL_AUX_FILESYS: none
  • Back-end compiler and version, e.g. gcc --version or clang --version: LLVM 15.0.7

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions