Bug 245007 - [compiler] Should not completely ignore anonymous type with missing super type
Summary: [compiler] Should not completely ignore anonymous type with missing super type
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 3.6 M5   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-08-22 17:53 EDT by Stephan Herrmann CLA
Modified: 2010-01-25 14:40 EST (History)
3 users (show)

See Also:


Attachments
proposed patch (820 bytes, patch)
2008-08-22 17:53 EDT, Stephan Herrmann CLA
no flags Details | Diff
Plausible source + test patch (8.91 KB, patch)
2010-01-13 06:51 EST, Srikanth Sankaran CLA
no flags Details | Diff
Revised source + test patch (12.76 KB, patch)
2010-01-18 06:17 EST, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Herrmann CLA 2008-08-22 17:53:44 EDT
Created attachment 110722 [details]
proposed patch

Consider the following class:

public class ClassWithAnon {
    int container() {
        new Missing() {
            void bar(int i) {}
        }.bar(false);
        
        return new Missing2() {};
    }
}

Currently, the compiler correctly reports:

----------
1. ERROR in ClassWithAnon.java (at line 3)
	new Missing() {
	    ^^^^^^^
Missing cannot be resolved to a type
----------
2. ERROR in ClassWithAnon.java (at line 7)
	return new Missing2() {};
	           ^^^^^^^^
Missing2 cannot be resolved to a type
----------
2 problems (2 errors)

However, by only deleting(!) two source lines in QualifiedAllocationExpression
it can be made to report two more errors, which, too, are interesting:

----------
1. ERROR in ClassWithAnon.java (at line 3)
	new Missing() {
	    ^^^^^^^
Missing cannot be resolved to a type
----------
2. ERROR in ClassWithAnon.java (at line 5)
	}.bar(false);
	  ^^^
The method bar(int) in the type new Missing(){} is not applicable for the arguments (boolean)
----------
3. ERROR in ClassWithAnon.java (at line 7)
	return new Missing2() {};
	       ^^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from new Missing2(){} to int
----------
4. ERROR in ClassWithAnon.java (at line 7)
	return new Missing2() {};
	           ^^^^^^^^
Missing2 cannot be resolved to a type
----------
4 problems (4 errors)

I'll attach a proposed micro-patch. With all the recently gained resilience I
see no danger in proceeding one step further. Am I missing any thing?
Comment 1 Stephan Herrmann CLA 2008-08-22 18:02:13 EDT
I just now realize that the patch will also bring full analysis (resolve) to 
the *body* of those anonymous classes, which currently are skipped completely.

Hm, so much more functionality by just *deleting* two lines ;-)
(I *must* be missing something).
Comment 2 Stephan Herrmann CLA 2008-08-23 07:04:54 EDT
OK, a few tests change with my patch in place:
  ASTConverterTest.test0312, 
  ASTConverterAST3Test.test0312:
    (declaring node is actually found)
    linking to http://dev.eclipse.org/bugs/show_bug.cgi?id=11638
  ASTConverterTestAST3_2.test0652:
    (4 more errors reported)    
  ASTConverterBugsTest.testBug218824a,
  ASTConverterBugsTestJLS3.testBug218824a:
    (one more error reported)
  ProblemTypeAndMethodTest.test059
    (one more error reported)
  ExtractMethodTest.test016
    (assumes statement cannot be selected, assumption no longer holds)

All additional diagnostics look right to me. 
The negative extract method test might become just obsolete.

To me, the test changes seem to indicate that the patch will make a
difference to some people: more valid errors reported, more AST resolved.
Comment 3 Stephan Herrmann CLA 2008-09-16 13:34:31 EDT
I have the proposed patch in our production code.
While updating JDT/Core and tests to 3.4.1 I had to adapt
one more test:
org.eclipse.jdt.ui.tests.quickfix.UnresolvedTypesQuickFixTest#testParameterizedType2
now detects two problems instead of one, new one is about incompatible
assignment from missing type to known type.

Just let me know if you'd like to see a patch with the changes
I had to make to tests.
Comment 4 Philipe Mulet CLA 2009-07-16 07:31:06 EDT
Need to be careful with reporting too many secondary errors in presence of missing types. In order to validate the idea, I would like to hear about a real world testcase. Like take an existing codebase, and discard some types (or remove binaries from the build path) and run a build; check the new errors to see how much value they provide.

Like in a case like:

new Listener() {
  void foo() {... }
}.notify(...)

How valuable is it going to claim #notify(...) cannot be bound, until you can resolve Listener ?

Comment 5 Stephan Herrmann CLA 2009-07-17 12:02:24 EDT
(In reply to comment #4)

Not a real world example but a simple comparison:

public class Test1 extends Missing {
    void buggy(Absent a) {
        notThere.dontEventSearch(unavailable);
    }
}

----------
1. ERROR in Test1.java (at line 1)
        public class Test1 extends Missing {
                                   ^^^^^^^
Missing cannot be resolved to a type
----------
2. ERROR in Test1.java (at line 2)
        void buggy(Absent a) {
                   ^^^^^^
Absent cannot be resolved to a type
----------
3. ERROR in Test1.java (at line 3)
        notThere.dontEventSearch(unavailable);
        ^^^^^^^^
notThere cannot be resolved
----------
4. ERROR in Test1.java (at line 3)
        notThere.dontEventSearch(unavailable);
                                 ^^^^^^^^^^^
unavailable cannot be resolved
----------
4 problems (4 errors)



GOOD.



public class Test2 {
    Object anonymous() {
       return new Missing() {
            void buggy(Absent a) {
                notThere.dontEventSearch(unavailable);
            }
       };
    }
}

----------
1. ERROR in Test2.java (at line 3)
        return new Missing() {
                   ^^^^^^^
Missing cannot be resolved to a type
----------
1 problem (1 error)



Hmmm, why this difference?



Reporting too many secondary errors is already avoided by 
checking HierarchyHasProblems further down, which I'd consider
the most consistent solution.
Comment 6 Stephan Herrmann CLA 2009-12-05 15:26:32 EST
Adjusting title to better reflect the discussion.
Comment 7 Stephan Herrmann CLA 2010-01-12 16:49:40 EST
I thought this issue would be a quick-and-easy one:

Normally, missing supertype does not prevent analysis of the type
declaration.

For anonymous types it does. 

Effects: 
 - No errors inside are detected.
 - Code inside is not selectable (cf. ExtractMethodTest.test016)

Change is tiny, changes in test results looked OK to me.

Any comments?
Comment 8 Olivier Thomann CLA 2010-01-12 20:58:18 EST
Srikanth, please see what we can do in this case. I think with comment 5 Stephan has a case.
Comment 9 Srikanth Sankaran CLA 2010-01-13 06:51:57 EST
Created attachment 155967 [details]
Plausible source + test patch

Cumulative source + test patch (only JDT/Core tests)
Comment 10 Srikanth Sankaran CLA 2010-01-13 06:54:01 EST
I don't feel strongly one way or other about this issue.
Olivier, what is your opinion on this ?
Comment 11 Olivier Thomann CLA 2010-01-13 08:46:53 EST
I would say that errors detected inside an anonymous type should not be much different from the error reported against a top level type when the super type is missing.
So I would be inclined to go towards reporting more secondary errors. If we could report errors inside the anonymous type without reporting errors as mentioned in comment 4, this would be ideal.
Comment 12 Srikanth Sankaran CLA 2010-01-18 06:17:06 EST
Created attachment 156371 [details]
Revised source + test patch 

This patch avoids reporting secondary problems when
the super type of anonymous classes is missing.
Comment 13 Srikanth Sankaran CLA 2010-01-18 08:25:05 EST
Released in HEAD for 3.6M5.

Markus, a heads up that some JDT/UI tests may need
adjusting/remastering. FYI, Thanks.
Comment 14 Stephan Herrmann CLA 2010-01-18 10:11:10 EST
(In reply to comment #12)
> Created an attachment (id=156371) [details]
> Revised source + test patch 
> 
> This patch avoids reporting secondary problems when
> the super type of anonymous classes is missing.

I basically agree.

Just two comments:
 + why restrict the new conditional to anonymous types?
   Are secondary errors any more valuable when the type is not
   anonymous?
 + wouldn't TagBits.HierarchyHasProblems be a more systematic
   indicator that secondary errors are useless?
   (haven't tried it, just guessing)

no big deal, though.
Comment 15 Markus Keller CLA 2010-01-18 11:06:29 EST
(In reply to comment #13)
> Markus, a heads up that some JDT/UI tests may need adjusting/remastering.

I've adjusted ExtractMethodTest.test016 and UnresolvedTypesQuickFixTest#testParameterizedType2 in HEAD.
Comment 16 Olivier Thomann CLA 2010-01-25 14:40:05 EST
Verified for 3.6M5 using I20100125-0800