Skip to content

Commit bd781ad

Browse files
committed
GROOVY-8254
1 parent 596bd72 commit bd781ad

File tree

26 files changed

+204
-95
lines changed

26 files changed

+204
-95
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/ImportsTests.java

+116-16
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ public void testImportAliasingAndOldReference() {
615615
};
616616
//@formatter:on
617617

618-
runNegativeTest(sources, "----------\n" +
618+
runNegativeTest(sources,
619+
"----------\n" +
619620
"1. ERROR in p\\C.groovy (at line 8)\n" +
620621
"\tpublic static void callitOne(A a) {}\n" +
621622
"\t ^\n" +
@@ -627,10 +628,10 @@ public void testImportAliasingAndOldReference() {
627628
public void testAliasing_GRE473() {
628629
//@formatter:off
629630
String[] sources = {
630-
"Foo.groovy",
631+
"Main.groovy",
631632
"import java.util.regex.Pattern as JavaPattern\n" +
632-
"class Pattern {JavaPattern javaPattern}\n" +
633-
"def p = new Pattern(javaPattern:~/\\d+/)\n" +
633+
"class Pattern { JavaPattern javaPattern }\n" +
634+
"def p = new Pattern(javaPattern: ~/\\d+/)\n" +
634635
"assert \"123\" ==~ p.javaPattern\n" +
635636
"print 'success '\n" +
636637
"print '['+p.class.name+']['+JavaPattern.class.name+']'\n",
@@ -641,26 +642,121 @@ public void testAliasing_GRE473() {
641642
}
642643

643644
@Test
644-
public void testAliasing_GRE473_2() {
645+
public void testImportConflict1() {
645646
//@formatter:off
646647
String[] sources = {
647-
"Foo.groovy",
648+
"Main.groovy",
648649
"import java.util.regex.Pattern\n" +
649-
"class Pattern {Pattern javaPattern}\n" +
650-
"def p = new Pattern(javaPattern:~/\\d+/)\n" +
651-
"assert \"123\" ==~ p.javaPattern\n" +
652-
"print 'success'\n",
650+
"class Pattern { }\n",
651+
};
652+
//@formatter:on
653+
654+
runNegativeTest(sources,
655+
"----------\n" +
656+
"1. ERROR in Main.groovy (at line 1)\n" +
657+
"\timport java.util.regex.Pattern\n" +
658+
"\t ^^^^^^^^^^^^^^^^^^^^^^^\n" +
659+
"The import java.util.regex.Pattern conflicts with a type defined in the same file\n" +
660+
"----------\n");
661+
}
662+
663+
@Test
664+
public void testImportConflict2() {
665+
//@formatter:off
666+
String[] sources = {
667+
"Main.groovy",
668+
"import java.util.regex.Pattern as Regexp\n" +
669+
"class Regexp { }\n",
653670
};
654671
//@formatter:on
655672

656-
runNegativeTest(sources, "----------\n" +
657-
"1. ERROR in Foo.groovy (at line 1)\n" +
673+
runNegativeTest(sources,
674+
"----------\n" +
675+
"1. ERROR in Main.groovy (at line 1)\n" +
676+
"\timport java.util.regex.Pattern as Regexp\n" +
677+
"\t ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
678+
"The import java.util.regex.Pattern as Regexp conflicts with a type defined in the same file\n" +
679+
"----------\n");
680+
}
681+
682+
@Test
683+
public void testImportConflict3() {
684+
//@formatter:off
685+
String[] sources = {
686+
"Pattern.groovy",
687+
"import java.util.regex.Pattern\n" +
688+
"null\n",
689+
};
690+
//@formatter:on
691+
692+
runNegativeTest(sources,
693+
"----------\n" +
694+
"1. ERROR in Pattern.groovy (at line 1)\n" +
658695
"\timport java.util.regex.Pattern\n" +
659696
"\t ^^^^^^^^^^^^^^^^^^^^^^^\n" +
660697
"The import java.util.regex.Pattern conflicts with a type defined in the same file\n" +
661698
"----------\n");
662699
}
663700

701+
@Test
702+
public void testImportConflict4() {
703+
//@formatter:off
704+
String[] sources = {
705+
"Main.groovy",
706+
"import groovy.lang.Script as Main\n" +
707+
"null\n",
708+
};
709+
//@formatter:on
710+
711+
runNegativeTest(sources,
712+
"----------\n" +
713+
"1. ERROR in Main.groovy (at line 1)\n" +
714+
"\timport groovy.lang.Script as Main\n" +
715+
"\t ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
716+
"The import groovy.lang.Script as Main conflicts with a type defined in the same file\n" +
717+
"----------\n");
718+
}
719+
720+
@Test
721+
public void testImportConflict5() {
722+
//@formatter:off
723+
String[] sources = {
724+
"Main.groovy",
725+
"import java.util.regex.Pattern\n" +
726+
"import java.lang.Object as Pattern\n" +
727+
"Pattern pattern = null\n",
728+
};
729+
//@formatter:on
730+
731+
runNegativeTest(sources,
732+
"----------\n" +
733+
"1. ERROR in Main.groovy (at line 2)\n" +
734+
"\timport java.lang.Object as Pattern\n" +
735+
"\t ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
736+
"The import java.lang.Object as Pattern collides with another import statement\n" +
737+
"----------\n");
738+
}
739+
740+
@Test // GROOVY-8254
741+
public void testImportConflict6() {
742+
//@formatter:off
743+
String[] sources = {
744+
"Main.groovy",
745+
"import Foo as Bar\n" +
746+
"class Foo { }\n" +
747+
"class Bar { }\n",
748+
};
749+
//@formatter:on
750+
751+
runNegativeTest(sources,
752+
"----------\n" +
753+
"1. ERROR in Main.groovy (at line 1)\n" +
754+
"\timport Foo as Bar\n" +
755+
"\t ^^^^^^^^^^\n" +
756+
"The import Foo as Bar conflicts with a type defined in the same file\n" +
757+
"----------\n");
758+
}
759+
664760
@Test
665761
public void testImportInnerClass1() {
666762
//@formatter:off
@@ -1067,7 +1163,8 @@ public void testExtraImports_nonMatchingSuffix() {
10671163
};
10681164
//@formatter:on
10691165

1070-
runNegativeTest(sources, "----------\n" +
1166+
runNegativeTest(sources,
1167+
"----------\n" +
10711168
"1. ERROR in com\\bar\\Runner.groovy (at line 4)\n" +
10721169
"\tType.m()\n" +
10731170
"\t^^^^\n" +
@@ -1106,7 +1203,8 @@ public void testExtraImports_typeDoesNotExist() {
11061203
};
11071204
//@formatter:on
11081205

1109-
runNegativeTest(sources, "----------\n" +
1206+
runNegativeTest(sources,
1207+
"----------\n" +
11101208
"1. ERROR in com\\bar\\Runner.groovy (at line 1)\n" +
11111209
"\tpackage com.bar\n" +
11121210
"\t^\n" +
@@ -1156,7 +1254,8 @@ public void testExtraImports_packageDoesNotExist() {
11561254
};
11571255
//@formatter:on
11581256

1159-
runNegativeTest(sources, "----------\n" +
1257+
runNegativeTest(sources,
1258+
"----------\n" +
11601259
"1. ERROR in com\\bar\\Runner.groovy (at line 4)\n" +
11611260
"\tType.m()\n" +
11621261
"\t^^^^\n" +
@@ -1218,7 +1317,8 @@ public void testNonTerminalMissingImport() {
12181317
};
12191318
//@formatter:on
12201319

1221-
runNegativeTest(sources, "----------\n" +
1320+
runNegativeTest(sources,
1321+
"----------\n" +
12221322
"1. ERROR in p\\X.groovy (at line 2)\n" +
12231323
"\timport a.b.c.D;\n" +
12241324
"\t ^^^^^^^\n" +

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitScope.java

+9
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ protected ClassScope buildClassScope(Scope parent, TypeDeclaration typeDecl) {
123123
protected void checkPublicTypeNameMatchesFilename(TypeDeclaration typeDecl) {
124124
}
125125

126+
@Override
127+
protected Binding findSingleImport(char[][] compoundName, int mask, boolean staticImport) {
128+
if (compoundName.length == 1) {
129+
Binding binding = findType(compoundName[0], environment.defaultPackage, fPackage);
130+
return (binding != null ? binding : new ProblemReferenceBinding(compoundName, null, ProblemReasons.NotFound));
131+
}
132+
return super.findSingleImport(compoundName, mask, staticImport);
133+
}
134+
126135
@Override
127136
protected boolean reportPackageIsNotExpectedPackage(CompilationUnitDeclaration compUnitDecl) {
128137
// check for parser recovery of an incomplete package statement or a script with no package statement before reporting name mismatch

jdt-patch/e419/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GROOVY PATCHED
22
/*******************************************************************************
3-
* Copyright (c) 2000, 2020 IBM Corporation and others.
3+
* Copyright (c) 2000, 2023 IBM Corporation and others.
44
*
55
* This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License 2.0
@@ -551,9 +551,9 @@ void faultInImports() {
551551
if (importBinding instanceof PackageBinding) {
552552
problemReporter().cannotImportPackage(importReference);
553553
continue nextImport;
554-
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
554+
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
555555
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
556-
if (importedPackage != null) {
556+
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
557557
if (!importedPackage.isValidBinding()) {
558558
problemReporter().importProblem(importReference, importedPackage);
559559
continue nextImport;

jdt-patch/e419/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GROOVY PATCHED
22
/*******************************************************************************
3-
* Copyright (c) 2000, 2020 IBM Corporation and others.
3+
* Copyright (c) 2000, 2023 IBM Corporation and others.
44
*
55
* This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License 2.0
@@ -1781,7 +1781,7 @@ public void conditionalArgumentsIncompatibleTypes(ConditionalExpression expressi
17811781
expression.sourceEnd);
17821782
}
17831783
public void conflictingImport(ImportReference importRef) {
1784-
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
1784+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
17851785
this.handleUntagged(
17861786
IProblem.ConflictingImport,
17871787
arguments,
@@ -2072,7 +2072,7 @@ public void duplicateFieldInType(SourceTypeBinding type, FieldDeclaration fieldD
20722072
fieldDecl.sourceEnd);
20732073
}
20742074
public void duplicateImport(ImportReference importRef) {
2075-
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
2075+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
20762076
this.handleUntagged(
20772077
IProblem.DuplicateImport,
20782078
arguments,
@@ -9391,7 +9391,7 @@ public void unusedImport(ImportReference importRef) {
93919391
// GROOVY end
93929392
int severity = computeSeverity(IProblem.UnusedImport);
93939393
if (severity == ProblemSeverities.Ignore) return;
9394-
String[] arguments = new String[] { CharOperation.toString(importRef.tokens) };
9394+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
93959395
this.handle(
93969396
IProblem.UnusedImport,
93979397
arguments,

jdt-patch/e420/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GROOVY PATCHED
22
/*******************************************************************************
3-
* Copyright (c) 2000, 2020 IBM Corporation and others.
3+
* Copyright (c) 2000, 2023 IBM Corporation and others.
44
*
55
* This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License 2.0
@@ -551,9 +551,9 @@ void faultInImports() {
551551
if (importBinding instanceof PackageBinding) {
552552
problemReporter().cannotImportPackage(importReference);
553553
continue nextImport;
554-
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
554+
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
555555
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
556-
if (importedPackage != null) {
556+
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
557557
if (!importedPackage.isValidBinding()) {
558558
problemReporter().importProblem(importReference, importedPackage);
559559
continue nextImport;

jdt-patch/e420/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GROOVY PATCHED
22
/*******************************************************************************
3-
* Copyright (c) 2000, 2021 IBM Corporation and others.
3+
* Copyright (c) 2000, 2023 IBM Corporation and others.
44
*
55
* This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License 2.0
@@ -1786,7 +1786,7 @@ public void conditionalArgumentsIncompatibleTypes(ConditionalExpression expressi
17861786
expression.sourceEnd);
17871787
}
17881788
public void conflictingImport(ImportReference importRef) {
1789-
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
1789+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
17901790
this.handleUntagged(
17911791
IProblem.ConflictingImport,
17921792
arguments,
@@ -2077,7 +2077,7 @@ public void duplicateFieldInType(SourceTypeBinding type, FieldDeclaration fieldD
20772077
fieldDecl.sourceEnd);
20782078
}
20792079
public void duplicateImport(ImportReference importRef) {
2080-
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
2080+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
20812081
this.handleUntagged(
20822082
IProblem.DuplicateImport,
20832083
arguments,
@@ -9429,7 +9429,7 @@ public void unusedImport(ImportReference importRef) {
94299429
// GROOVY end
94309430
int severity = computeSeverity(IProblem.UnusedImport);
94319431
if (severity == ProblemSeverities.Ignore) return;
9432-
String[] arguments = new String[] { CharOperation.toString(importRef.tokens) };
9432+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
94339433
this.handle(
94349434
IProblem.UnusedImport,
94359435
arguments,

jdt-patch/e421/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GROOVY PATCHED
22
/*******************************************************************************
3-
* Copyright (c) 2000, 2020 IBM Corporation and others.
3+
* Copyright (c) 2000, 2023 IBM Corporation and others.
44
*
55
* This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License 2.0
@@ -551,9 +551,9 @@ void faultInImports() {
551551
if (importBinding instanceof PackageBinding) {
552552
problemReporter().cannotImportPackage(importReference);
553553
continue nextImport;
554-
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
554+
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
555555
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
556-
if (importedPackage != null) {
556+
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
557557
if (!importedPackage.isValidBinding()) {
558558
problemReporter().importProblem(importReference, importedPackage);
559559
continue nextImport;

jdt-patch/e421/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GROOVY PATCHED
22
/*******************************************************************************
3-
* Copyright (c) 2000, 2021 IBM Corporation and others.
3+
* Copyright (c) 2000, 2023 IBM Corporation and others.
44
*
55
* This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License 2.0
@@ -1786,7 +1786,7 @@ public void conditionalArgumentsIncompatibleTypes(ConditionalExpression expressi
17861786
expression.sourceEnd);
17871787
}
17881788
public void conflictingImport(ImportReference importRef) {
1789-
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
1789+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
17901790
this.handleUntagged(
17911791
IProblem.ConflictingImport,
17921792
arguments,
@@ -2077,7 +2077,7 @@ public void duplicateFieldInType(SourceTypeBinding type, FieldDeclaration fieldD
20772077
fieldDecl.sourceEnd);
20782078
}
20792079
public void duplicateImport(ImportReference importRef) {
2080-
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
2080+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
20812081
this.handleUntagged(
20822082
IProblem.DuplicateImport,
20832083
arguments,
@@ -9429,7 +9429,7 @@ public void unusedImport(ImportReference importRef) {
94299429
// GROOVY end
94309430
int severity = computeSeverity(IProblem.UnusedImport);
94319431
if (severity == ProblemSeverities.Ignore) return;
9432-
String[] arguments = new String[] { CharOperation.toString(importRef.tokens) };
9432+
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
94339433
this.handle(
94349434
IProblem.UnusedImport,
94359435
arguments,

jdt-patch/e422/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GROOVY PATCHED
22
/*******************************************************************************
3-
* Copyright (c) 2000, 2020 IBM Corporation and others.
3+
* Copyright (c) 2000, 2023 IBM Corporation and others.
44
*
55
* This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License 2.0
@@ -551,9 +551,9 @@ void faultInImports() {
551551
if (importBinding instanceof PackageBinding) {
552552
problemReporter().cannotImportPackage(importReference);
553553
continue nextImport;
554-
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
554+
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
555555
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
556-
if (importedPackage != null) {
556+
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
557557
if (!importedPackage.isValidBinding()) {
558558
problemReporter().importProblem(importReference, importedPackage);
559559
continue nextImport;

0 commit comments

Comments
 (0)