From 6cec6a90780f6215a017ced5383d048f3fa29657 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Wed, 12 Dec 2012 10:48:13 +0400 Subject: [PATCH] IDEA-96860 Improper error reporting with type parameter --- .../plugins/groovy/GroovyBundle.properties | 3 ++- .../groovy/annotator/GroovyAnnotator.java | 24 +++++++++---------- .../plugins/groovy/LightGroovyTestCase.groovy | 11 +++++++++ .../GroovyHighlightingTest.groovy | 23 ++++++++++++++++-- .../VeryLongDfaWithComplexGenerics.groovy | 4 ++-- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties index 844827b8cd85..e56f14b9f4fb 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties @@ -338,4 +338,5 @@ java.style.for.each.statement.requires.a.type.declaration=Java-style for-each st enums.may.not.have.extends.clause=Enums may not have 'extends' clause super.cannot.be.used.in.static.context='super' cannot be used in static context qualified.0.is.allowed.only.in.nested.or.inner.classes=Qualified {0} is allowed only in nested/inner classes -groovy.file.0=Groovy file ''{0}'' \ No newline at end of file +groovy.file.0=Groovy file ''{0}'' +type.argument.list.is.not.allowed.here=Type argument list is not allowed here \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java index 20d01e2bff60..3a54d61cf4d7 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java @@ -149,19 +149,16 @@ public class GroovyAnnotator extends GroovyElementVisitor implements Annotator { @Override public void visitTypeArgumentList(GrTypeArgumentList typeArgumentList) { PsiElement parent = typeArgumentList.getParent(); - final PsiElement resolved; - if (parent instanceof GrReferenceElement) { - resolved = ((GrReferenceElement)parent).resolve(); - } - else { - resolved = null; - } + if (!(parent instanceof GrReferenceElement)) return; + + final GroovyResolveResult resolveResult = ((GrReferenceElement)parent).advancedResolve(); + final PsiElement resolved = resolveResult.getElement(); + final PsiSubstitutor substitutor = resolveResult.getSubstitutor(); if (resolved == null) return; if (!(resolved instanceof PsiTypeParameterListOwner)) { - //myHolder.createErrorAnnotation(typeArgumentList, GroovyBundle.message("type.argument.list.is.no.a")) - //todo correct error description + myHolder.createWarningAnnotation(typeArgumentList, GroovyBundle.message("type.argument.list.is.not.allowed.here")); return; } @@ -173,8 +170,8 @@ public class GroovyAnnotator extends GroovyElementVisitor implements Annotator { final GrTypeElement[] arguments = typeArgumentList.getTypeArgumentElements(); if (arguments.length != parameters.length) { - myHolder.createErrorAnnotation(typeArgumentList, - GroovyBundle.message("wrong.number.of.type.arguments", arguments.length, parameters.length)); + myHolder.createWarningAnnotation(typeArgumentList, + GroovyBundle.message("wrong.number.of.type.arguments", arguments.length, parameters.length)); return; } @@ -183,8 +180,9 @@ public class GroovyAnnotator extends GroovyElementVisitor implements Annotator { final PsiClassType[] superTypes = parameter.getExtendsListTypes(); final PsiType argType = arguments[i].getType(); for (PsiClassType superType : superTypes) { - if (!superType.isAssignableFrom(argType)) { - myHolder.createErrorAnnotation(arguments[i], GroovyBundle + final PsiType substitutedSuper = substitutor.substitute(superType); + if (substitutedSuper != null && !substitutedSuper.isAssignableFrom(argType)) { + myHolder.createWarningAnnotation(arguments[i], GroovyBundle .message("type.argument.0.is.not.in.its.bound.should.extend.1", argType.getCanonicalText(), superType.getCanonicalText())); break; } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy index 70a55b2cf674..fbb2719df6de 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy @@ -75,6 +75,17 @@ package java.math; public class BigDecimal extends Number implements Comparable { } +''') + } + + protected void addHashSet() { + myFixture.addClass('''\ +package java.util; + +public class HashSet + extends AbstractSet + implements Set, Cloneable, java.io.Serializable +{} ''') } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy index f5e86e5b2cc9..a7fcc5677c3e 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy @@ -408,9 +408,9 @@ private def handleImplicitBind(arg) { public void testIncorrectTypeArguments() { myFixture.configureByText('_.groovy', '''\ class C {} -C<Double> c +C<Double> c C c2 -C c3 +C c3 ''') myFixture.testHighlighting(true, false, true) } @@ -953,6 +953,25 @@ print new Foo() { String toString() {'abc'} } +''') + } + + void testGenerics() { + addHashSet() + testHighlighting(''' + +class NodeInfo{} + +interface NodeEvent {} + +interface TrackerEventsListener> { + void onEvents(Collection events) +} + +class AgentInfo extends NodeInfo {} + +print new HashSet>>() //correct +print new HashSetNodeEvent>>() //incorrect ''') } } \ No newline at end of file diff --git a/plugins/groovy/testdata/highlighting/VeryLongDfaWithComplexGenerics.groovy b/plugins/groovy/testdata/highlighting/VeryLongDfaWithComplexGenerics.groovy index 952de9bdc403..f01763796029 100644 --- a/plugins/groovy/testdata/highlighting/VeryLongDfaWithComplexGenerics.groovy +++ b/plugins/groovy/testdata/highlighting/VeryLongDfaWithComplexGenerics.groovy @@ -28,10 +28,10 @@ class TroubleCase { } public interface Foo {} -public class Bar implements Cloneable, ZooCoo, Woo> {} +public class Bar implements Cloneable, Zoo {} public interface Zoo, FM extends Hoo> {} public interface Hoo {} -public class Baz implements Cloneable, ZooToo,Yoo> {} +public class Baz implements Cloneable, Zoo {} public class Goo {} public class Too implements Hoo {} public class Coo implements Serializable, Cloneable, Hoo {}