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

Improve the error message on the wrong usage of super in class methods #26473

Open
e-kayrakli opened this issue Jan 6, 2025 · 2 comments
Open

Comments

@e-kayrakli
Copy link
Contributor

e-kayrakli commented Jan 6, 2025

Starting simple:

class C {
  proc init() {
    init this;
    writeln(super.x);
  }
}

var c = new C();

results in error: unresolved call 'RootClass.x'. I don't think we should be exposing RootClass (no, we should). A class that doesn't inherit anything explicitly should not be able to use super. If there's some meaning to RootClass that should be exposed to the user, I still think this error message could be better. #26051 looks thematically related.

The related real-world case might be hairier. What's the meaning of super in a method that's implemented in an inner module? Reading the docs, I think, in the context of modules, super can only be used to find other modules and not variables within them. But my real-world attempt was:

module superTest {
  var x = 10;

  module Inner {
    class C {
      var myX: int;

      proc init() {
        this.myX = super.x; // I wanted this to be `superTest.x` 
      }
    }
  }

  import this.Inner;

  proc main() {
    var c = new Inner.C();
  }
}

But looks like I had to use superTest in the inner module first. I kind of wished super to look also into the parent module, but I understand that it can get complicated with actual inheritance at play. This may be a case for --detailed-errors or a hint in the error message that super can only be used to access superclass(es)'s fields/methods and not module-scope variables.

@bradcray
Copy link
Member

bradcray commented Jan 6, 2025

I don't think we should be exposing RootClass.

We definitely do, and did so intentionally:

Given that, I don't think the error message is particularly bad, but also wouldn't object to improving it further.

What's the meaning of super in a method that's implemented in an inner module?

I believe that, outside of a class/record/method, super can only be used in import statements (and maybe use, though I'm having trouble coming up with a working example)? E.g., see this example.

(this error message could also stand to be improved since it doesn't mention the potential to use super in import/use).

@e-kayrakli
Copy link
Contributor Author

Thanks, I wasn't aware (or not remembering) that discussion. I agree with that rationale. I linked to your comment in the OP.

I still wish for a better error message, though.

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