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

Bug 475018

Summary: [1.8][compile] Misleading error message when parenthesis is forgotten somewhere around a lambda expression
Product: [Eclipse Project] JDT Reporter: Lukas Eder <lukas.eder>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: stephan.herrmann
Version: 4.5   
Target Milestone: ---   
Hardware: PC   
OS: Windows NT   
Whiteboard: stalebug

Description Lukas Eder CLA 2015-08-15 04:20:20 EDT
Consider the following code:

--------------------------------------------------------------------
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


class Person {
    int id;
    String name;

    void x() {
        List<Person> people = Collections.emptyList();
        Map<Integer, List<String>> namesPerId =
        people.stream()
              .collect(Collectors.groupingBy(
                  p -> p.id,
                  Collectors.mapping(p -> p.name, Collectors.toList()
              ));
              
        System.out.println("test");
    }
}

--------------------------------------------------------------------

The call to Collectors.mapping() is missing a closing parenthesis. This, however is not reported by the Eclipse compiler, which reports a very generic error on "p -> p.name":

Syntax error on token(s), misplaced construct(s)

Note that when leaving out the "System.out.println("test") call ...

--------------------------------------------------------------------
    void x() {
        List<Person> people = Collections.emptyList();
        Map<Integer, List<String>> namesPerId =
        people.stream()
              .collect(Collectors.groupingBy(
                  p -> p.id,
                  Collectors.mapping(p -> p.name, Collectors.toList()
              ));
    }
--------------------------------------------------------------------

... the compiler now reports two errors. One on "p.name" (1), and one on the missing parenthesis (2):

1) Syntax error on tokens, LambdaBody expected instead
2) Syntax error, insert ")" to complete Expression
Comment 1 Eclipse Genie CLA 2020-02-04 07:39:44 EST
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.
Comment 2 Stephan Herrmann CLA 2020-02-04 08:19:48 EST
I agree that the generic error message is not great, but unfortunately detecting this situation in order to report a better error message would be a inappropriately huge project.

Background: reporting of syntax errors in ecj is driven by sophisticated machinery searching for the closest distance between the actual input and the Java grammar. For ecj that grammar is given as an LALR(1) grammar suitable for the parser generator jikespg. Unfortunately, it is no longer possible to describe Java syntax using an LALR(1) grammar. Ergo, ecj had to accumulate tons of workarounds to accommodate each new grammar weirdness. Each of these workarounds may work fine for correct code, but inevitably conflicts with the syntax recovery and error diagnostic engines.

It is not realistic to significantly improve this situation short of re-implementing the compiler.