Skip to content

Commit

Permalink
GROOVY-8254
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 16, 2023
1 parent 596bd72 commit bd781ad
Show file tree
Hide file tree
Showing 26 changed files with 204 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ public void testImportAliasingAndOldReference() {
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
runNegativeTest(sources,
"----------\n" +
"1. ERROR in p\\C.groovy (at line 8)\n" +
"\tpublic static void callitOne(A a) {}\n" +
"\t ^\n" +
Expand All @@ -627,10 +628,10 @@ public void testImportAliasingAndOldReference() {
public void testAliasing_GRE473() {
//@formatter:off
String[] sources = {
"Foo.groovy",
"Main.groovy",
"import java.util.regex.Pattern as JavaPattern\n" +
"class Pattern {JavaPattern javaPattern}\n" +
"def p = new Pattern(javaPattern:~/\\d+/)\n" +
"class Pattern { JavaPattern javaPattern }\n" +
"def p = new Pattern(javaPattern: ~/\\d+/)\n" +
"assert \"123\" ==~ p.javaPattern\n" +
"print 'success '\n" +
"print '['+p.class.name+']['+JavaPattern.class.name+']'\n",
Expand All @@ -641,26 +642,121 @@ public void testAliasing_GRE473() {
}

@Test
public void testAliasing_GRE473_2() {
public void testImportConflict1() {
//@formatter:off
String[] sources = {
"Foo.groovy",
"Main.groovy",
"import java.util.regex.Pattern\n" +
"class Pattern {Pattern javaPattern}\n" +
"def p = new Pattern(javaPattern:~/\\d+/)\n" +
"assert \"123\" ==~ p.javaPattern\n" +
"print 'success'\n",
"class Pattern { }\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 1)\n" +
"\timport java.util.regex.Pattern\n" +
"\t ^^^^^^^^^^^^^^^^^^^^^^^\n" +
"The import java.util.regex.Pattern conflicts with a type defined in the same file\n" +
"----------\n");
}

@Test
public void testImportConflict2() {
//@formatter:off
String[] sources = {
"Main.groovy",
"import java.util.regex.Pattern as Regexp\n" +
"class Regexp { }\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in Foo.groovy (at line 1)\n" +
runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 1)\n" +
"\timport java.util.regex.Pattern as Regexp\n" +
"\t ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
"The import java.util.regex.Pattern as Regexp conflicts with a type defined in the same file\n" +
"----------\n");
}

@Test
public void testImportConflict3() {
//@formatter:off
String[] sources = {
"Pattern.groovy",
"import java.util.regex.Pattern\n" +
"null\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Pattern.groovy (at line 1)\n" +
"\timport java.util.regex.Pattern\n" +
"\t ^^^^^^^^^^^^^^^^^^^^^^^\n" +
"The import java.util.regex.Pattern conflicts with a type defined in the same file\n" +
"----------\n");
}

@Test
public void testImportConflict4() {
//@formatter:off
String[] sources = {
"Main.groovy",
"import groovy.lang.Script as Main\n" +
"null\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 1)\n" +
"\timport groovy.lang.Script as Main\n" +
"\t ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
"The import groovy.lang.Script as Main conflicts with a type defined in the same file\n" +
"----------\n");
}

@Test
public void testImportConflict5() {
//@formatter:off
String[] sources = {
"Main.groovy",
"import java.util.regex.Pattern\n" +
"import java.lang.Object as Pattern\n" +
"Pattern pattern = null\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 2)\n" +
"\timport java.lang.Object as Pattern\n" +
"\t ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
"The import java.lang.Object as Pattern collides with another import statement\n" +
"----------\n");
}

@Test // GROOVY-8254
public void testImportConflict6() {
//@formatter:off
String[] sources = {
"Main.groovy",
"import Foo as Bar\n" +
"class Foo { }\n" +
"class Bar { }\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 1)\n" +
"\timport Foo as Bar\n" +
"\t ^^^^^^^^^^\n" +
"The import Foo as Bar conflicts with a type defined in the same file\n" +
"----------\n");
}

@Test
public void testImportInnerClass1() {
//@formatter:off
Expand Down Expand Up @@ -1067,7 +1163,8 @@ public void testExtraImports_nonMatchingSuffix() {
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
runNegativeTest(sources,
"----------\n" +
"1. ERROR in com\\bar\\Runner.groovy (at line 4)\n" +
"\tType.m()\n" +
"\t^^^^\n" +
Expand Down Expand Up @@ -1106,7 +1203,8 @@ public void testExtraImports_typeDoesNotExist() {
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
runNegativeTest(sources,
"----------\n" +
"1. ERROR in com\\bar\\Runner.groovy (at line 1)\n" +
"\tpackage com.bar\n" +
"\t^\n" +
Expand Down Expand Up @@ -1156,7 +1254,8 @@ public void testExtraImports_packageDoesNotExist() {
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
runNegativeTest(sources,
"----------\n" +
"1. ERROR in com\\bar\\Runner.groovy (at line 4)\n" +
"\tType.m()\n" +
"\t^^^^\n" +
Expand Down Expand Up @@ -1218,7 +1317,8 @@ public void testNonTerminalMissingImport() {
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
runNegativeTest(sources,
"----------\n" +
"1. ERROR in p\\X.groovy (at line 2)\n" +
"\timport a.b.c.D;\n" +
"\t ^^^^^^^\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ protected ClassScope buildClassScope(Scope parent, TypeDeclaration typeDecl) {
protected void checkPublicTypeNameMatchesFilename(TypeDeclaration typeDecl) {
}

@Override
protected Binding findSingleImport(char[][] compoundName, int mask, boolean staticImport) {
if (compoundName.length == 1) {
Binding binding = findType(compoundName[0], environment.defaultPackage, fPackage);
return (binding != null ? binding : new ProblemReferenceBinding(compoundName, null, ProblemReasons.NotFound));
}
return super.findSingleImport(compoundName, mask, staticImport);
}

@Override
protected boolean reportPackageIsNotExpectedPackage(CompilationUnitDeclaration compUnitDecl) {
// check for parser recovery of an incomplete package statement or a script with no package statement before reporting name mismatch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -551,9 +551,9 @@ void faultInImports() {
if (importBinding instanceof PackageBinding) {
problemReporter().cannotImportPackage(importReference);
continue nextImport;
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
if (importedPackage != null) {
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
if (!importedPackage.isValidBinding()) {
problemReporter().importProblem(importReference, importedPackage);
continue nextImport;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1781,7 +1781,7 @@ public void conditionalArgumentsIncompatibleTypes(ConditionalExpression expressi
expression.sourceEnd);
}
public void conflictingImport(ImportReference importRef) {
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handleUntagged(
IProblem.ConflictingImport,
arguments,
Expand Down Expand Up @@ -2072,7 +2072,7 @@ public void duplicateFieldInType(SourceTypeBinding type, FieldDeclaration fieldD
fieldDecl.sourceEnd);
}
public void duplicateImport(ImportReference importRef) {
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handleUntagged(
IProblem.DuplicateImport,
arguments,
Expand Down Expand Up @@ -9391,7 +9391,7 @@ public void unusedImport(ImportReference importRef) {
// GROOVY end
int severity = computeSeverity(IProblem.UnusedImport);
if (severity == ProblemSeverities.Ignore) return;
String[] arguments = new String[] { CharOperation.toString(importRef.tokens) };
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handle(
IProblem.UnusedImport,
arguments,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -551,9 +551,9 @@ void faultInImports() {
if (importBinding instanceof PackageBinding) {
problemReporter().cannotImportPackage(importReference);
continue nextImport;
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
if (importedPackage != null) {
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
if (!importedPackage.isValidBinding()) {
problemReporter().importProblem(importReference, importedPackage);
continue nextImport;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1786,7 +1786,7 @@ public void conditionalArgumentsIncompatibleTypes(ConditionalExpression expressi
expression.sourceEnd);
}
public void conflictingImport(ImportReference importRef) {
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handleUntagged(
IProblem.ConflictingImport,
arguments,
Expand Down Expand Up @@ -2077,7 +2077,7 @@ public void duplicateFieldInType(SourceTypeBinding type, FieldDeclaration fieldD
fieldDecl.sourceEnd);
}
public void duplicateImport(ImportReference importRef) {
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handleUntagged(
IProblem.DuplicateImport,
arguments,
Expand Down Expand Up @@ -9429,7 +9429,7 @@ public void unusedImport(ImportReference importRef) {
// GROOVY end
int severity = computeSeverity(IProblem.UnusedImport);
if (severity == ProblemSeverities.Ignore) return;
String[] arguments = new String[] { CharOperation.toString(importRef.tokens) };
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handle(
IProblem.UnusedImport,
arguments,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -551,9 +551,9 @@ void faultInImports() {
if (importBinding instanceof PackageBinding) {
problemReporter().cannotImportPackage(importReference);
continue nextImport;
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
if (importedPackage != null) {
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
if (!importedPackage.isValidBinding()) {
problemReporter().importProblem(importReference, importedPackage);
continue nextImport;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1786,7 +1786,7 @@ public void conditionalArgumentsIncompatibleTypes(ConditionalExpression expressi
expression.sourceEnd);
}
public void conflictingImport(ImportReference importRef) {
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handleUntagged(
IProblem.ConflictingImport,
arguments,
Expand Down Expand Up @@ -2077,7 +2077,7 @@ public void duplicateFieldInType(SourceTypeBinding type, FieldDeclaration fieldD
fieldDecl.sourceEnd);
}
public void duplicateImport(ImportReference importRef) {
String[] arguments = new String[] {CharOperation.toString(importRef.tokens)};
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handleUntagged(
IProblem.DuplicateImport,
arguments,
Expand Down Expand Up @@ -9429,7 +9429,7 @@ public void unusedImport(ImportReference importRef) {
// GROOVY end
int severity = computeSeverity(IProblem.UnusedImport);
if (severity == ProblemSeverities.Ignore) return;
String[] arguments = new String[] { CharOperation.toString(importRef.tokens) };
String[] arguments = new String[] {importRef.toString()}; // GROOVY edit
this.handle(
IProblem.UnusedImport,
arguments,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -551,9 +551,9 @@ void faultInImports() {
if (importBinding instanceof PackageBinding) {
problemReporter().cannotImportPackage(importReference);
continue nextImport;
} else if (this.environment.useModuleSystem && importBinding instanceof ReferenceBinding) {
} else if (importBinding instanceof ReferenceBinding && this.environment.useModuleSystem) {
PackageBinding importedPackage = ((ReferenceBinding) importBinding).fPackage;
if (importedPackage != null) {
if (importedPackage != null && importedPackage != this.environment.defaultPackage) {
if (!importedPackage.isValidBinding()) {
problemReporter().importProblem(importReference, importedPackage);
continue nextImport;
Expand Down
Loading

0 comments on commit bd781ad

Please sign in to comment.