diff options
| author | Jesper S Moller | 2013-02-12 23:54:09 +0000 |
|---|---|---|
| committer | ssankaran | 2013-02-12 23:54:09 +0000 |
| commit | 7d431a6663c03691d383a66bc16841bb87e3308d (patch) | |
| tree | 58f97e7c62e45029c8c3669f426974b0ea0c830d | |
| parent | 4e993a8d5cbdc7815c7050ad8787e1c566312d6a (diff) | |
| download | eclipse.jdt.core-7d431a6663c03691d383a66bc16841bb87e3308d.tar.gz eclipse.jdt.core-7d431a6663c03691d383a66bc16841bb87e3308d.tar.xz eclipse.jdt.core-7d431a6663c03691d383a66bc16841bb87e3308d.zip | |
Fixed Bug 384687 - [1.8] Wildcard type arguments should be rejected for
lambda and reference expressions
3 files changed, 107 insertions, 43 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GrammarCoverageTests308.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GrammarCoverageTests308.java index dd45a5be00..f8e6f37f0d 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GrammarCoverageTests308.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GrammarCoverageTests308.java @@ -1502,8 +1502,12 @@ public class GrammarCoverageTests308 extends AbstractRegressionTest { " i = @Marker W<@Marker Integer>::<@Marker String> new;\n" + " ^^^^^^\n" + "Marker cannot be resolved to a type\n" + - "----------\n" - ); + "----------\n" + + "5. ERROR in X.java (at line 12)\n" + + " i = @Marker W<@Marker Integer>::<@Marker String> new;\n" + + " ^^^^^^\n" + + "Marker cannot be resolved to a type\n" + + "----------\n"); } // CastExpression ::= PushLPAREN Name PushRPAREN InsideCastExpressionLL1 UnaryExpressionNotPlusMinus // CastExpression ::= PushLPAREN Name Dims PushRPAREN InsideCastExpression UnaryExpressionNotPlusMinus diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java index 101ec94267..f90c2b5934 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java @@ -14,6 +14,7 @@ * Jesper S Moller - Contributions for * bug 382701 - [1.8][compiler] Implement semantic analysis of Lambda expressions & Reference expression * bug 382721 - [1.8][compiler] Effectively final variables needs special treatment + * Bug 384687 - [1.8] Wildcard type arguments should be rejected for lambda and reference expressions *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -168,6 +169,13 @@ public void test006() { this.runNegativeTest( new String[] { "X.java", + "interface One{}\n" + + "interface Two{}\n" + + "interface Three{}\n" + + "interface Four{}\n" + + "interface Five{}\n" + + "interface Blah{}\n" + + "interface Outer<T1,T2>{interface Inner<T3,T4>{interface Leaf{ <T> void method(); } } }\n" + "interface IX{\n" + " public void foo();\n" + "}\n" + @@ -177,42 +185,22 @@ public void test006() { "}\n", }, "----------\n" + - "1. ERROR in X.java (at line 5)\n" + - " IX i = Outer<One, Two>.Inner<Three, Four>.Deeper<Five, Six<String>>.Leaf::<Blah, Blah>method;\n" + - " ^^^^^\n" + - "Outer cannot be resolved to a type\n" + - "----------\n" + - "2. ERROR in X.java (at line 5)\n" + - " IX i = Outer<One, Two>.Inner<Three, Four>.Deeper<Five, Six<String>>.Leaf::<Blah, Blah>method;\n" + - " ^^^\n" + - "One cannot be resolved to a type\n" + - "----------\n" + - "3. ERROR in X.java (at line 5)\n" + - " IX i = Outer<One, Two>.Inner<Three, Four>.Deeper<Five, Six<String>>.Leaf::<Blah, Blah>method;\n" + - " ^^^\n" + - "Two cannot be resolved to a type\n" + - "----------\n" + - "4. ERROR in X.java (at line 5)\n" + - " IX i = Outer<One, Two>.Inner<Three, Four>.Deeper<Five, Six<String>>.Leaf::<Blah, Blah>method;\n" + - " ^^^^^\n" + - "Three cannot be resolved to a type\n" + - "----------\n" + - "5. ERROR in X.java (at line 5)\n" + + "1. ERROR in X.java (at line 12)\n" + " IX i = Outer<One, Two>.Inner<Three, Four>.Deeper<Five, Six<String>>.Leaf::<Blah, Blah>method;\n" + - " ^^^^\n" + - "Four cannot be resolved to a type\n" + + " ^^^^^^^^^^^^^^^^^^^^^\n" + + "The member type Outer<One,Two>.Inner cannot be qualified with a parameterized type, since it is static. Remove arguments from qualifying type Outer<One,Two>\n" + "----------\n" + - "6. ERROR in X.java (at line 5)\n" + + "2. ERROR in X.java (at line 12)\n" + " IX i = Outer<One, Two>.Inner<Three, Four>.Deeper<Five, Six<String>>.Leaf::<Blah, Blah>method;\n" + - " ^^^^\n" + - "Five cannot be resolved to a type\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Outer.Inner.Deeper cannot be resolved to a type\n" + "----------\n" + - "7. ERROR in X.java (at line 5)\n" + + "3. ERROR in X.java (at line 12)\n" + " IX i = Outer<One, Two>.Inner<Three, Four>.Deeper<Five, Six<String>>.Leaf::<Blah, Blah>method;\n" + " ^^^\n" + "Six cannot be resolved to a type\n" + "----------\n" + - "8. ERROR in X.java (at line 6)\n" + + "4. ERROR in X.java (at line 13)\n" + " int x\n" + " ^\n" + "Syntax error, insert \";\" to complete FieldDeclaration\n" + @@ -3214,6 +3202,60 @@ public void test096() { "Duplicate local variable x2\n" + "----------\n"); } +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=384687 [1.8] Wildcard type arguments should be rejected for lambda and reference expressions +public void test097() { + this.runNegativeTest( + new String[] { + "X.java", + "class Action<K> {\r\n" + + " static <T1> int fooMethod(Object x) { return 0; }\r\n" + + "}\r\n" + + "interface I {\r\n" + + " int foo(Object x);\r\n" + + "}\r\n" + + "public class X {\r\n" + + " public static void main(String[] args) {\r\n" + + " I functional = Action::<?>fooMethod;\r\n" + + " }\r\n" + + "}"}, + "----------\n" + + "1. WARNING in X.java (at line 9)\n" + + " I functional = Action::<?>fooMethod;\n" + + " ^^^^^^\n" + + "Action is a raw type. References to generic type Action<K> should be parameterized\n" + + "----------\n" + + "2. ERROR in X.java (at line 9)\n" + + " I functional = Action::<?>fooMethod;\n" + + " ^\n" + + "Wildcard is not allowed at this location\n" + + "----------\n"); +} + +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=384687 [1.8] Wildcard type arguments should be rejected for lambda and reference expressions +public void test098() { + this.runNegativeTest( + new String[] { + "X.java", + "class Action<K> {\r\n" + + " int foo(Object x, Object y, Object z) { return 0; }\r\n" + + "}\r\n" + + "interface I {\r\n" + + " void foo(Object x);\r\n" + + "}\r\n" + + "public class X {\r\n" + + " public static void main(String[] args) {\r\n" + + " Action<Object> exp = new Action<Object>();\r\n" + + " int x,y,z;\r\n" + + " I len6 = foo->exp.<?>method(x, y, z);\r\n" + + " }\r\n" + + "}"}, + "----------\n" + + "1. ERROR in X.java (at line 11)\n" + + " I len6 = foo->exp.<?>method(x, y, z);\n" + + " ^\n" + + "Wildcard is not allowed at this location\n" + + "----------\n"); +} public static Class testClass() { return NegativeLambdaExpressionsTest.class; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java index 7d74761e56..1e64c32b43 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,11 +13,13 @@ * IBM Corporation - initial API and implementation * Jesper S Moller - Contributions for * bug 382701 - [1.8][compiler] Implement semantic analysis of Lambda expressions & Reference expression + * Bug 384687 - [1.8] Wildcard type arguments should be rejected for lambda and reference expressions *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; import org.eclipse.jdt.internal.compiler.ASTVisitor; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.lookup.BlockScope; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; @@ -27,13 +29,14 @@ public class ReferenceExpression extends FunctionalExpression { protected TypeReference type; protected Expression primary; - protected TypeReference [] typeParameters; + protected TypeReference [] typeArguments; protected SingleNameReference method; // == null ? "::new" : "::method" + private TypeBinding[] genericTypeArguments; public ReferenceExpression(NameReference name, TypeReference[] typeArguments, int sourceEnd) { this.name = name; - this.typeParameters = typeArguments; + this.typeArguments = typeArguments; this.method = null; this.sourceStart = name.sourceStart; this.sourceEnd = sourceEnd; @@ -41,7 +44,7 @@ public class ReferenceExpression extends FunctionalExpression { public ReferenceExpression(NameReference name, TypeReference[] typeArguments, SingleNameReference method) { this.name = name; - this.typeParameters = typeArguments; + this.typeArguments = typeArguments; this.method = method; this.sourceStart = name.sourceStart; this.sourceEnd = method.sourceEnd; @@ -49,7 +52,7 @@ public class ReferenceExpression extends FunctionalExpression { public ReferenceExpression(Expression primary, TypeReference [] typeArguments, SingleNameReference method) { this.primary = primary; - this.typeParameters = typeArguments; + this.typeArguments = typeArguments; this.method = method; this.sourceStart = primary.sourceStart; this.sourceEnd = method.sourceEnd; @@ -57,7 +60,7 @@ public class ReferenceExpression extends FunctionalExpression { public ReferenceExpression(TypeReference type, TypeReference[] typeArguments, SingleNameReference method) { this.type = type; - this.typeParameters = typeArguments; + this.typeArguments = typeArguments; this.method = method; this.sourceStart = type.sourceStart; this.sourceEnd = method.sourceEnd; @@ -65,7 +68,7 @@ public class ReferenceExpression extends FunctionalExpression { public ReferenceExpression(TypeReference type, TypeReference[] typeArguments, int sourceEnd) { this.type = type; - this.typeParameters = typeArguments; + this.typeArguments = typeArguments; this.method = null; this.sourceStart = type.sourceStart; this.sourceEnd = sourceEnd; @@ -80,6 +83,21 @@ public class ReferenceExpression extends FunctionalExpression { } else if (this.type != null) { this.type.resolveType(blockScope); } + if (this.typeArguments != null) { + int length = this.typeArguments.length; + boolean argHasError = blockScope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_5; + this.genericTypeArguments = new TypeBinding[length]; + for (int i = 0; i < length; i++) { + TypeReference typeReference = this.typeArguments[i]; + if ((this.genericTypeArguments[i] = typeReference.resolveType(blockScope, true /* check bounds*/)) == null) { + argHasError = true; + } + if (argHasError && typeReference instanceof Wildcard) { + blockScope.problemReporter().illegalUsageOfWildcard(typeReference); + } + } + } + return this.resolvedType; } @@ -93,14 +111,14 @@ public class ReferenceExpression extends FunctionalExpression { this.primary.print(0, output); } output.append("::"); //$NON-NLS-1$ - if (this.typeParameters != null) { + if (this.typeArguments != null) { output.append('<'); - int max = this.typeParameters.length - 1; + int max = this.typeArguments.length - 1; for (int j = 0; j < max; j++) { - this.typeParameters[j].print(0, output); + this.typeArguments[j].print(0, output); output.append(", ");//$NON-NLS-1$ } - this.typeParameters[max].print(0, output); + this.typeArguments[max].print(0, output); output.append('>'); } if (this.method == null) { @@ -129,9 +147,9 @@ public class ReferenceExpression extends FunctionalExpression { if (this.primary != null) this.primary.traverse(visitor, blockScope); - int length = this.typeParameters == null ? 0 : this.typeParameters.length; + int length = this.typeArguments == null ? 0 : this.typeArguments.length; for (int i = 0; i < length; i++) { - this.typeParameters[i].traverse(visitor, blockScope); + this.typeArguments[i].traverse(visitor, blockScope); } if (this.method != null) |
