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

ANN: annotate wrong return type for empty functions #5552

Merged
merged 1 commit into from Jan 18, 2021

Conversation

Kobzol
Copy link
Member

@Kobzol Kobzol commented Jun 11, 2020

Fixes: #5166

Copy link
Member

@mchernyavsky mchernyavsky left a comment

Choose a reason for hiding this comment

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

Maybe it's better to annotate the function's closing bracket? (just like in Kotlin)

image

@Kobzol
Copy link
Member Author

Kobzol commented Jul 31, 2020

If we want to do this: #4833 then it would be consistent to mark the return type (although it might be missing in case of unit type, hmm).. Otherwise I'll change it to annotate the closing brace.

@mchernyavsky mchernyavsky requested a review from Undin July 31, 2020 17:16
@Stzx

This comment has been minimized.

@Kobzol
Copy link
Member Author

Kobzol commented Jul 31, 2020

@Stzx I'm sorry, I don't understand, what do you mean?

@Stzx

This comment has been minimized.

@Kobzol
Copy link
Member Author

Kobzol commented Dec 31, 2020

@mchernyavsky What error type marking should be used? Only the return type, whole function, closing brace?

@mchernyavsky
Copy link
Member

@mchernyavsky What error type marking should be used? Only the return type, whole function, closing brace?

I prefer the closing brace. @Undin @vlad20012 What do you think?

@vlad20012
Copy link
Member

vlad20012 commented Jan 14, 2021

This is the reason why the PR is not merged yet? Oh...
I would not say that this is important. We can change it later. Let's make it somehow. cargo check already highlight a return type, so we won't make things worse if we do that too.
Btw #4833 can also be implemented by } brace highlighting

@Kobzol
Copy link
Member Author

Kobzol commented Jan 14, 2021

This is the reason why the PR is not merged yet? Oh...

Yup 😅

The closing brace is probably more general, since we cannot mark the return type if it's missing. Should I change it to the brace?

@vlad20012
Copy link
Member

Should I change it to the brace?

Oh 😅
Okay, let's chenge

@Kobzol Kobzol force-pushed the ann-empty-function-return-type branch from d5aaa53 to b1282c8 Compare January 16, 2021 18:23
@Kobzol
Copy link
Member Author

Kobzol commented Jan 16, 2021

I changed it to the closing brace for empty functions. The ChangeReturnTypeFix is not currently offered for them. It would require non-trivial changes to it to make it work, and I don't think that you usually want to change the return type in situations when the function is empty. If you want the fix to work, let me know.

To solve #4833 with brace highlighting, it would have to only highlight the brace in the case of a trailing expr (and highlight return expressions for return statements, as it does now), which might be a bit confusing.

Copy link
Member

@vlad20012 vlad20012 left a comment

Choose a reason for hiding this comment

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

bors r+

bors bot added a commit that referenced this pull request Jan 17, 2021
5552: ANN: annotate wrong return type for empty functions r=vlad20012 a=Kobzol

Fixes: #5166

Co-authored-by: Jakub Beránek <berykubik@gmail.com>
@vlad20012
Copy link
Member

Hmm, I think I found a false-positive case:

trait FooBar {}
impl FooBar for () {}

fn foobar() -> impl FooBar {

}

bors r-

@bors
Copy link
Contributor

bors bot commented Jan 17, 2021

Canceled.

@Kobzol
Copy link
Member Author

Kobzol commented Jan 17, 2021

Oh, that's a very... interesting case :D Well, to support cases such as this one, I wonder if maybe the error annotation for empty functions should be moved into TypeInferenceWalker? Or should I just change the current implementation to use something like TypeInference::combineTypes to see if the result type is compatible with ()?

@vlad20012
Copy link
Member

TypeInferenceWalker looks more robust solution, but impl Trait is poorly supported even there. Let's just ignore impl Traits at all for now

@Kobzol Kobzol force-pushed the ann-empty-function-return-type branch from b1282c8 to a7f3868 Compare January 17, 2021 13:39
@vlad20012
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 18, 2021

Build succeeded:

@bors bors bot merged commit 0c577aa into intellij-rust:master Jan 18, 2021
To test automation moved this from In Progress to Test Jan 18, 2021
@github-actions github-actions bot added this to the v140 milestone Jan 18, 2021
@Kobzol Kobzol deleted the ann-empty-function-return-type branch January 18, 2021 08:56
@lancelote lancelote moved this from Test to Done in To test Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
To test
  
Done
Development

Successfully merging this pull request may close these issues.

No "mismatched types" [E0308] error on empty function
4 participants