Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: cannot explicitly specify a generic range return type #26114

Open
jabraham17 opened this issue Oct 21, 2024 · 1 comment
Open

[Bug]: cannot explicitly specify a generic range return type #26114

jabraham17 opened this issue Oct 21, 2024 · 1 comment

Comments

@jabraham17
Copy link
Member

Summary of Problem

Description:

Attempting to specify a generic return type for a range with a non-default type results in an error. However, fully specifying the return type does work. And just removing the explicit return type also works.

I believe this is a quirk of range being generic with defaults, such that range(?) gives you the defaults. I would have expected that range says "the return type is a range with the type defaults" and range(?) says "the return type is a range with unknown type"

Steps to Reproduce

Source Code:

proc foo(): range(?) { // error: initializing a range with strideKind.one from a range with strideKind.positive
  return 1..10 by 2;
}
writeln(foo());


proc foo() { // this is fine and is a workaround
  return 1..10 by 2;
}

proc foo(): range(int, boundKind.both, strideKind.positive) { // this is also fine, being full explicit
  return 1..10 by 2;
}

Compile command:
chpl foo.chpl

Configuration Information

  • Output of chpl --version: 2.2
  • 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 18
@bradcray
Copy link
Member

bradcray commented Oct 21, 2024

I believe this is a quirk of range being generic with defaults, such that range(?) gives you the defaults.

I'd also expect that range(?) should not get you the defaults. In other cases, I believe that's the case, such as:

var r: range(?) = 1..10 by -2;
writeln(r);

proc foo(r: range(?)) {
  writeln(r);
}

foo(r);

which works as expected.

Your issue also seems to be consistent for other fully-defaulted generics:

record R {
  param n = 1;
}

proc foo(): R(?) {
  return new R(n=2);
}
writeln(foo());  // similar error

But interestingly, not for partially-defaulted generics:

record R {
  param n = 1;
  param k;
}

proc foo(): R(?) {
  return new R(n=2, k=33);
}
writeln(foo());  // works

[edited to update comments, which I'd copied without updating]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants