New Issue: Cannot define '=' operator as a method if type has an explicit initializer

17156, "lydia-duncan", "Cannot define '=' operator as a method if type has an explicit initializer", "2021-02-12T17:14:46Z"

Summary of Problem

When we try to compile the following code, we get an error:

assignmentOpMethod.chpl:4: In initializer:
assignmentOpMethod.chpl:6: error: cannot call a method on a record before this.complete()
assignmentOpMethod.chpl:6: error: field "x" used before it is initialized

It should not be an error - the assignment call is perfectly appropriate, as it is on the field type, not the containing record type. However, it seems that if we are in a method and making a call to an operator that could be a method on that type, we will insert a method token and this argument corresponding to the type into the call.

I had seen this sort of behavior earlier when resolving the contents of an operator method but had worked around it in that case (e.g. + on type Foo had a call to + when the arguments were both ints). This shouldn't impact regular method calls as those will always either already have a this or should match to the method over standalone functions with the same name.

I think what I need to do is to prevent the insertion of those arguments at scope resolution when the match found has the operator flag.

Steps to Reproduce

Source Code:

record Foo {
  var x: int;

  proc init(x: int) {
    writeln("In user init");
    this.x = x;
  }
}
operator Foo.=(val: Foo) {
  return new Foo(val.x);
}
proc main() {
  var x: Foo = new Foo(7);
  var y: Foo = new Foo(3);
  x = y;
  writeln(x);
}

Compile command:
chpl foo.chpl

Associated Future Test(s):
test/functions/operatorOverloads/operatorMethods/assignmentOpMethod.chpl #TBD

Configuration Information

  • Output of chpl --version: chapel 1.24.0 (pre-release)