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

[Patterns] ECJ tolerates erroneous redeclaration of pattern bindings in some cases. #1887

Closed
srikanth-sankaran opened this issue Jan 19, 2024 · 1 comment · Fixed by #1885
Assignees

Comments

@srikanth-sankaran
Copy link
Contributor

The following program should generate 10 errors but elicits only 8. Javac complains at all 10 locations. In all instances javac's diagnostics are more precise about pattern bindings being illegally redeclared while with ecj, sometimes we emit the more bland Duplicate local variable error (which is may be just fine)

Relevant section and text of JLS21 cited inline:

public class X<T> {
	public void foo(T o) {
		/*
		 * 6.3.1 Scope for Pattern Variables in Expressions
		 * 6.3.1.1 Conditional-And Operator &&
		 * 
		 * It is a compile-time error if any of the following conditions hold:
			• A pattern variable is both (i) introduced by a when true and (ii) introduced by
			b when true.
			• A pattern variable is both (i) introduced by a when false and (ii) introduced by
			b when false.
		 */
		boolean b = o instanceof String a && o instanceof Double a;   // Error: correct
		b = !(o instanceof String a) && !(o instanceof Double a);     // <<<----- Error NOT reported by ECJ. Javac complains
		
		/*
		 * 6.3.1.2 Conditional-Or Operator ||
		 * 
		 * It is a compile-time error if any of the following conditions hold:
			• A pattern variable is both (i) introduced by a when true and (ii) introduced by
			b when true.
			• A pattern variable is both (i) introduced by a when false and (ii) introduced by
			b when false.
		 */
		b =  o instanceof String a || o instanceof Double a;      // <<<----- Error NOT reported by ECJ. Javac complains 
		b =  !(o instanceof String a) || !(o instanceof Double a); // Error: correct
		
		/*
		 * 6.3.1.4 Conditional Operator a ? b : c 
		 * 
		 * It is a compile-time error if any of the following conditions hold:
			• A pattern variable is both (i) introduced by a when true and (ii) introduced by
			c when true.
			• A pattern variable is both (i) introduced by a when true and (ii) introduced by
			c when false.
			• A pattern variable is both (i) introduced by a when false and (ii) introduced by
			b when true.
			• A pattern variable is both (i) introduced by a when false and (ii) introduced by
			b when false.
			• A pattern variable is both (i) introduced by b when true and (ii) introduced by
			c when true.
			• A pattern variable is both (i) introduced by b when false and (ii) introduced by
			c when false.
		 */
		
		b = o instanceof String a ? true : o instanceof String a;  // error correctly reported
		b = o instanceof String a ? true : !(o instanceof String a); // error correctly reported
		b = !(o instanceof String a) ? o instanceof String a : true; // error correctly reported
		b = !(o instanceof String a) ? !(o instanceof String a) : true; // error correctly reported
		b = b ? (o instanceof String a) : (o instanceof String a); // error correctly reported
		b = b ? !(o instanceof String a) : !(o instanceof String a); // error correctly reported
		
} 
}

@srikanth-sankaran srikanth-sankaran self-assigned this Jan 19, 2024
@srikanth-sankaran
Copy link
Contributor Author

I will include a fix in #1885 which is substantially reimplementing pattern resolution.

srikanth-sankaran added a commit that referenced this issue Jan 23, 2024
…on (#1885)

* Reimplements pattern resolution and pattern binding management in a much simpler design.

* Fixes [Patterns] Alternate, simpler pattern binding management implementation #1884
* Fixes [Patterns] ECJ tolerates erroneous redeclaration of pattern bindings in some cases.  #1887
robstryker pushed a commit to robstryker/eclipse.jdt.core that referenced this issue Jul 18, 2024
…on (eclipse-jdt#1885)

* Reimplements pattern resolution and pattern binding management in a much simpler design.

* Fixes [Patterns] Alternate, simpler pattern binding management implementation eclipse-jdt#1884
* Fixes [Patterns] ECJ tolerates erroneous redeclaration of pattern bindings in some cases.  eclipse-jdt#1887
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant