Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 427377

Summary: [compiler] Wrong error message in initializer: "Void methods cannot return a value"
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: Jay Arthanareeswaran <jarthana>
Status: CLOSED WONTFIX QA Contact:
Severity: minor    
Priority: P3 CC: manoj.palat, srikanth_sankaran
Version: 4.4   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard: stalebug

Description Markus Keller CLA 2014-02-04 10:03:30 EST
public class X {
    int foo() { }
    {
        return 0; // Compile error: Void methods cannot return a value
    }
}


It took me a while to understand that the problem is not the return type of method foo().

The class actually has two members:
1.) method foo(), which misses a return statement
2.) an initializer, which should not contain a return statement.

In an initializer, the error message shouldn't talk about "Void methods". I would expect error "162 = Cannot return from within an initializer" here.

And I wouldn't expect the "This method must return a result of type int" error on foo() to be suppressed when the error in the initializer shows up.
Comment 1 Markus Keller CLA 2014-02-04 10:03:56 EST
Happens in master and in BETA_JAVA8.
Comment 2 Srikanth Sankaran CLA 2014-09-18 00:58:54 EDT
(In reply to Markus Keller from comment #0)
> public class X {
>     int foo() { }
>     {
>         return 0; // Compile error: Void methods cannot return a value
>     }
> }
> 
> 
> It took me a while to understand that the problem is not the return type of
> method foo().
> 
> The class actually has two members:
> 1.) method foo(), which misses a return statement
> 2.) an initializer, which should not contain a return statement.
> 
> In an initializer, the error message shouldn't talk about "Void methods". I
> would expect error "162 = Cannot return from within an initializer" here.

This message matches what we emit for constructors. Constructors are void
methods, aren't they ;-) Initializer blocks are simply constructor fragments
spilled out of line in code and shared.

> And I wouldn't expect the "This method must return a result of type int"
> error on foo() to be suppressed when the error in the initializer shows up.

This is by design for good or bad. When there are errors in resolution phase,
we don't proceed to flow analysis.
Comment 3 Markus Keller CLA 2014-09-19 10:35:37 EDT
JLS8 doesn't define the term "void method", but uses it only for methods that return void (and not for constructors). I understand your reasoning, but I think it's too far-fetched.

To avoid a separate problem ID, the message could be changed to:
"Cannot return a value from void method, constructor, or initializer"
Comment 4 Eclipse Genie CLA 2020-05-14 16:17:36 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.