This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 258167

Summary: Incorrect type inferred for assign return value to a new variable
Product: java Reporter: Jan Lahoda <jlahoda>
Component: HintsAssignee: Svata Dedic <sdedic>
Status: RESOLVED FIXED    
Severity: normal CC: matthies
Priority: P3    
Version: 8.1   
Hardware: PC   
OS: Linux   
Issue Type: DEFECT Exception Reporter:
Bug Depends on: 262073    
Bug Blocks:    

Description Jan Lahoda 2016-02-27 18:17:23 UTC
The "assign result value to a new variable" hint sometimes does not produce the correct type for the variable, consider:
---
import java.util.Map;

public class InferTest {
    void test(Map<? extends String, ? extends Number> map) {
        map.entrySet();//assign return value to a new variable here
    }
}
---

The "assign return value to a new variable" will produce:
---
Set<Entry<? extends String, ? extends Number>> entrySet = map.entrySet();
---

Which won't compile:
---
InferTest.java:7: error: incompatible types: Set<Entry<CAP#1,CAP#2>> cannot be converted to Set<Entry<? extends String,? extends Number>>
        Set<Entry<? extends String, ? extends Number>> entrySet = map.entrySet(); //assign return value to a new variable here
  where CAP#1,CAP#2 are fresh type-variables:
    CAP#1 extends String from capture of ? extends String
    CAP#2 extends Number from capture of ? extends Number
1 error
---

Inferring this would be more appropriate:
---
Set<? extends Entry<? extends String, ? extends Number>> entrySet = map.entrySet();
---

This is on:
NetBeans IDE 8.1 RC2 (Build 20151012-f7fbd7b7b85b)

(Reported by Stuart Marks.)
Comment 1 Svata Dedic 2016-05-16 13:48:36 UTC
*** Bug 171272 has been marked as a duplicate of this bug. ***
Comment 2 Svata Dedic 2016-05-16 14:10:49 UTC
*** Bug 170574 has been marked as a duplicate of this bug. ***
Comment 3 Svata Dedic 2016-06-17 11:37:00 UTC
Fixed in jet-main#15fde243019c
Comment 4 Quality Engineering 2016-06-18 03:11:16 UTC
Integrated into 'main-silver', will be available in build *201606180002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/15fde243019c
User: Svata Dedic <sdedic@netbeans.org>
Log: #258167: infer declared variable type from the expression