From a9a07857ded62793e80bdfd5d098f31e2315c6d0 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 28 Nov 2011 17:54:50 +0400 Subject: [PATCH] sometimes it not permitted to redeclare variable in nested scope (KT-527) --- .../jet/lang/diagnostics/Errors.java | 5 +++- .../diagnostics/RedeclarationDiagnostic.java | 14 ++++++----- .../RedeclarationDiagnosticFactory.java | 25 +++++++++++++++---- .../ControlStructureTypingVisitor.java | 13 ++++++++++ .../ExpressionTypingVisitorForStatements.java | 8 ++++++ .../ShadowParameterInFunctionBody.jet | 5 ++++ .../ShadowParameterInNestedBlockInFor.jet | 7 ++++++ .../shadowing/ShadowPropertyInClosure.jet | 3 +++ .../quick/shadowing/ShadowPropertyInFor.jet | 11 ++++++++ .../shadowing/ShadowPropertyInFunction.jet | 10 ++++++++ .../quick/shadowing/ShadowVariableInFor.jet | 6 +++++ .../shadowing/ShadowVariableInNestedBlock.jet | 7 ++++++ .../ShadowVariableInNestedClosure.jet | 5 ++++ .../ShadowVariableInNestedClosureParam.jet | 5 ++++ 14 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowParameterInFunctionBody.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowParameterInNestedBlockInFor.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInClosure.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFor.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFunction.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInFor.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedBlock.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosure.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosureParam.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index 2ea2a103180..fc8ce9013f2 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -44,7 +44,10 @@ public interface Errors { } }; UnresolvedReferenceDiagnosticFactory UNRESOLVED_REFERENCE = new UnresolvedReferenceDiagnosticFactory("Unresolved reference"); - RedeclarationDiagnosticFactory REDECLARATION = RedeclarationDiagnosticFactory.INSTANCE; + + RedeclarationDiagnosticFactory REDECLARATION = RedeclarationDiagnosticFactory.REDECLARATION; + RedeclarationDiagnosticFactory NAME_SHADOWING = RedeclarationDiagnosticFactory.NAME_SHADOWING; + PsiElementOnlyDiagnosticFactory2 TYPE_MISMATCH = PsiElementOnlyDiagnosticFactory2.create(ERROR, "Type mismatch: inferred type is {1} but {0} was expected"); ParameterizedDiagnosticFactory1> INCOMPATIBLE_MODIFIERS = new ParameterizedDiagnosticFactory1>(ERROR, "Incompatible modifiers: ''{0}''") { @Override diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/RedeclarationDiagnostic.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/RedeclarationDiagnostic.java index 3a8c2f02733..648c8bfe5d2 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/RedeclarationDiagnostic.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/RedeclarationDiagnostic.java @@ -15,8 +15,8 @@ import static org.jetbrains.jet.lang.diagnostics.Severity.ERROR; public interface RedeclarationDiagnostic extends DiagnosticWithPsiElement { public class SimpleRedeclarationDiagnostic extends DiagnosticWithPsiElementImpl implements RedeclarationDiagnostic { - public SimpleRedeclarationDiagnostic(@NotNull PsiElement psiElement, @NotNull String name) { - super(RedeclarationDiagnosticFactory.INSTANCE, ERROR, "Redeclaration: " + name, psiElement); + public SimpleRedeclarationDiagnostic(@NotNull PsiElement psiElement, @NotNull String name, RedeclarationDiagnosticFactory factory) { + super(factory, factory.severity, factory.makeMessage(name), psiElement); } } @@ -24,11 +24,13 @@ public interface RedeclarationDiagnostic extends DiagnosticWithPsiElementp = 2 + return p +} diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowParameterInNestedBlockInFor.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowParameterInNestedBlockInFor.jet new file mode 100644 index 00000000000..e3e91fbe857 --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowParameterInNestedBlockInFor.jet @@ -0,0 +1,7 @@ +fun f(i: Int) { + for (j in 1..100) { + { + var i = 12 + } + } +} diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInClosure.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInClosure.jet new file mode 100644 index 00000000000..c785e3f5fc7 --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInClosure.jet @@ -0,0 +1,3 @@ +val i = 17 + +val f = { (): Int => var i = 17; i } diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFor.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFor.jet new file mode 100644 index 00000000000..e0d4766e89f --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFor.jet @@ -0,0 +1,11 @@ +class RedefinePropertyInFor() { + + var i = 1 + + fun ff() { + for (i in 0..10) { + } + } + +} + diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFunction.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFunction.jet new file mode 100644 index 00000000000..cacf415e65e --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowPropertyInFunction.jet @@ -0,0 +1,10 @@ +class RedefinePropertyInFunction() { + + var i = 17 + + fun f(): Int { + var i = 18 + return i + } + +} diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInFor.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInFor.jet new file mode 100644 index 00000000000..b62b6d0ea30 --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInFor.jet @@ -0,0 +1,6 @@ +fun ff(): Int { + var i = 1 + for (i in 0..10) { + } + return i +} diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedBlock.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedBlock.jet new file mode 100644 index 00000000000..2fd755bdc1c --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedBlock.jet @@ -0,0 +1,7 @@ +fun ff(): Int { + var i = 1 + { + val i = 2 + } + return i +} diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosure.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosure.jet new file mode 100644 index 00000000000..d9dd9110893 --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosure.jet @@ -0,0 +1,5 @@ +fun f(): Int { + var i = 17 + { (): Unit => var i = 18 } + return i +} diff --git a/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosureParam.jet b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosureParam.jet new file mode 100644 index 00000000000..6ba5fd4c919 --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/shadowing/ShadowVariableInNestedClosureParam.jet @@ -0,0 +1,5 @@ +fun ff(): Int { + var i = 1 + { (i: Int) => i } + return i +}