Skip to content

FEAT: Func implementation must come after all @overload declarations #443

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

iwakitakuma33
Copy link

@iwakitakuma33 iwakitakuma33 commented Jun 7, 2025

@facebook-github-bot
Copy link
Contributor

Hi @iwakitakuma33!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@kinto0
Copy link
Contributor

kinto0 commented Jun 9, 2025

Thank you!!! I'll let @rchen152 review this. Just FYI #430 included both this issue and another (likely more difficult to implement) one

@overload
def setBar(self, value: int, inline: Literal[False]) -> Self: ... # ERROR: Overloaded function must have an implementation

@overload
def setBar(self, value: int, inline: Literal[True]) -> None: ...

# the real problem here is this @overload decorator should not be here
@overload
def setBar(self, value, inline):
    if inline:
        return self
    else:
        return None

@iwakitakuma33
Copy link
Author

@kinto0
Thank you for your reply!
Yes, I will look into issues that seem more difficult problem.

This is my first time contributing to this repository, so I started small.

@yangdanny97
Copy link
Contributor

@iwakitakuma33

Thanks! Could you please add an integration test case in this file?

https://github.com/facebook/pyrefly/blob/fab3bdec8aa597a5e9c10436921d28f617e54f48/pyrefly/lib/test/overload.rs

@iwakitakuma33
Copy link
Author

iwakitakuma33 commented Jun 9, 2025

@yangdanny97
I added test.

I did fix to show error if concrete function has overload.
If the overload function has ... Is it OK .
If the overload function has pass , is it ok that show error??

@iwakitakuma33 iwakitakuma33 force-pushed the iwakitakuma33/imp_before_overload branch 2 times, most recently from a61647f to 5c8b0d5 Compare June 9, 2025 19:33
Copy link
Contributor

@rchen152 rchen152 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! It would be great if the new error messages could be reported instead of the confusing "Overloaded function must have an implementation" messages, rather than both messages being shown. I left in-line comments with suggestions for how to do that.

def.name.range,
ErrorKind::InvalidOverload,
None,
"@overload decorator should not be used on function implementations.".to_owned(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my other comment - this is a good error message, but we should report either this error or "Overloaded function must have an implementation," not both.

If you add a new stub_or_impl field to DecoratedFunction (

pub struct DecoratedFunction {
), then you can move this check to solve_function_binding and do something like:

if !skip_implementation {
    if def.stub_or_impl == FunctionStubOrImpl::Impl {
        // report "@overload decorator should not be used on function implementations."
    else {
        // report "Overloaded function must have an implementation"
    }
}

implementation_range,
ErrorKind::InvalidOverload,
None,
"Function implementation must come after all @overload declarations. ".to_owned(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this error message, but I think it's confusing that this error and "Overloaded function must have an implementation" are both reported; we should report one or the other.

What I'd suggest doing is: when we encounter an @overload-decorated signature with no successors, we loop through the predecessors to see if there's an implementation among them. If there is, then we report this error rather than the "Overloaded function must have an implementation" error. Here's an example of how to loop through predecessors:

fn step_overload_pred(&self, pred: &mut Option<Idx<Key>>) -> Option<Arc<DecoratedFunction>> {
let pred_idx = (*pred)?;
let mut b = self.bindings().get(pred_idx);
while let Binding::Forward(k) = b {
b = self.bindings().get(*k);
}
if let Binding::Function(idx, pred_, _) = b {
let def = self.get_idx(*idx);
if def.metadata.flags.is_overload {
*pred = *pred_;
Some(def)
} else {
None
}
} else {
None
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rchen152
Thank you for your feedback. I have made revisions based on your comments.
I have also added tests.

When I revised the code to prevent the message “Overloaded function needs at least two signatures” from appearing unnecessarily, evaluating get_idx in the successor direction caused lazy evaluation to occur, causing tests other than overload to fail. Therefore, in this PR,I have limited the scope to cases where successor.is_none is true.

@iwakitakuma33 iwakitakuma33 force-pushed the iwakitakuma33/imp_before_overload branch from 23f2f45 to a697a25 Compare June 20, 2025 21:05
@iwakitakuma33 iwakitakuma33 requested a review from rchen152 June 20, 2025 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants