New Issue: Weird error message when returning a promoted 'new' expression

28187, "jabraham17", "Weird error message when returning a promoted 'new' expression", "2025-12-11T00:40:21Z"

The following code should produce an error, but the error you get is pretty inscrutable

record S {
  proc init(x: int) { }
}

record R {
  proc type bar(x: int): int do return x;

  proc type foo(str): S {
    var x = [1, 2, 3];
    return new S(x);
  }
}

proc main() {
  var y = R.foo("123");
  writeln(y);
}
bar.chpl:9: In method 'foo':
bar.chpl:9: error: control reaches end of function that returns a value
  bar.chpl:17: called as type (<type unknown>).foo(str: string)
note: generic instantiations are underlined in the above callstack

The problem is, new S is promoted and will result in an array, so the explicit return type of S is incorrect. But the error message is not related to that.

I wrote a few variants of this that all have bad but slightly different error messages

record S {
  proc init(x: int) { }
}

record R {
  proc type bar(x: int): int do return x;

  proc type foo(): S {
    var x = [1, 2, 3];
    return new S(x);
  }
}

proc main() {
  var y = R.foo();
  writeln(y);
}
bar.chpl:9: In method 'foo':
bar.chpl:9: error: control reaches end of function that returns a value

The simplest reproducer is the following

record S {
  proc init(x: int) { }
}
proc foo(): S {
  var x = [1, 2, 3];
  return new S(x);
}
proc main() {
  var y = foo();
  writeln(y);
}
bar.chpl:5: In function 'foo':
bar.chpl:5: error: control reaches end of function that returns a value

But note that this is specific to returning a record type, the following gets an appropriate error message

proc bar(x: int) do return x;
proc foo(): int {
  var x = [1, 2, 3];
  return bar(x);
}
proc main() {
  var y = foo();
  writeln(y);
}
bar.chpl:6: In function 'foo':
bar.chpl:8: error: cannot initialize return value of type 'int(64)' from a 'promoted expression yielding int(64)'