From 8bdfafd8c10eddf8503089f89049e3277cfeffbe Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 7 Dec 2011 16:24:49 +0400 Subject: [PATCH 1/4] Test for KT-557 Wrong type inference near sure extension function --- .../testData/diagnostics/tests/regressions/kt557.jet | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/regressions/kt557.jet diff --git a/compiler/testData/diagnostics/tests/regressions/kt557.jet b/compiler/testData/diagnostics/tests/regressions/kt557.jet new file mode 100644 index 00000000000..546965a6064 --- /dev/null +++ b/compiler/testData/diagnostics/tests/regressions/kt557.jet @@ -0,0 +1,9 @@ +// KT-557 Wrong type inference near sure extension function + +fun Array.length() : Int { + return 0; +} + +fun test(array : Array?) { + array?.sure>().length() +} \ No newline at end of file From 40937e438bbbb7887a7e9c1ae46531a9ed2e3c7b Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 7 Dec 2011 17:26:28 +0400 Subject: [PATCH 2/4] KT-762 Wrong highlighting in imports (No errors reported on unresolved imports) --- .../jet/lang/resolve/ImportsResolver.java | 108 ++++++++++++++++++ .../lang/resolve/TypeHierarchyResolver.java | 86 ++------------ .../diagnostics/tests/regressions/kt762.jet | 4 + 3 files changed, 119 insertions(+), 79 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/ImportsResolver.java create mode 100644 compiler/testData/diagnostics/tests/regressions/kt762.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ImportsResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ImportsResolver.java new file mode 100644 index 00000000000..a96ce5d3f4a --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ImportsResolver.java @@ -0,0 +1,108 @@ +package org.jetbrains.jet.lang.resolve; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; +import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.scopes.JetScope; +import org.jetbrains.jet.lang.resolve.scopes.WritableScope; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices; + +import java.util.List; + +import static org.jetbrains.jet.lang.diagnostics.Errors.UNRESOLVED_REFERENCE; +import static org.jetbrains.jet.lang.diagnostics.Errors.UNSUPPORTED; + +/** + * @author abreslav + */ +public class ImportsResolver { + private final TopDownAnalysisContext context; + + public ImportsResolver(@NotNull TopDownAnalysisContext context) { + this.context = context; + } + + public void processTypeImports() { + for (JetNamespace jetNamespace : context.getNamespaceDescriptors().keySet()) { + processImports(jetNamespace, context.getNamespaceScopes().get(jetNamespace), context.getDeclaringScopes().get(jetNamespace)); + } + } + + private void processImports(@NotNull JetNamespace namespace, @NotNull WritableScope namespaceScope, @NotNull JetScope outerScope) { + context.getConfiguration().addDefaultImports(context.getTrace(), namespaceScope); + + List importDirectives = namespace.getImportDirectives(); + for (JetImportDirective importDirective : importDirectives) { + if (importDirective.isAbsoluteInRootNamespace()) { + context.getTrace().report(UNSUPPORTED.on(importDirective, "TypeHierarchyResolver")); // TODO + continue; + } + if (importDirective.isAllUnder()) { + JetExpression importedReference = importDirective.getImportedReference(); + if (importedReference != null) { + ExpressionTypingServices typeInferrerServices = context.getSemanticServices().getTypeInferrerServices(context.getTrace()); + JetType type = typeInferrerServices.getTypeWithNamespaces(namespaceScope, importedReference); + if (type != null) { + namespaceScope.importScope(type.getMemberScope()); + } + } + } + else { + ClassifierDescriptor classifierDescriptor = null; + NamespaceDescriptor namespaceDescriptor = null; + JetSimpleNameExpression referenceExpression = null; + + JetExpression importedReference = importDirective.getImportedReference(); + if (importedReference instanceof JetDotQualifiedExpression) { + JetDotQualifiedExpression reference = (JetDotQualifiedExpression) importedReference; + JetType type = context.getSemanticServices().getTypeInferrerServices(context.getTrace()).getTypeWithNamespaces(namespaceScope, reference.getReceiverExpression()); + JetExpression selectorExpression = reference.getSelectorExpression(); + if (selectorExpression != null) { + referenceExpression = (JetSimpleNameExpression) selectorExpression; + String referencedName = referenceExpression.getReferencedName(); + if (type != null && referencedName != null) { + classifierDescriptor = type.getMemberScope().getClassifier(referencedName); + namespaceDescriptor = type.getMemberScope().getNamespace(referencedName); + } + } + } + else { + assert importedReference instanceof JetSimpleNameExpression; + referenceExpression = (JetSimpleNameExpression) importedReference; + + String referencedName = referenceExpression.getReferencedName(); + if (referencedName != null) { + classifierDescriptor = outerScope.getClassifier(referencedName); + namespaceDescriptor = outerScope.getNamespace(referencedName); + + if (classifierDescriptor == null && namespaceDescriptor == null) { + context.getTrace().report(UNRESOLVED_REFERENCE.on(referenceExpression)); + } + } + } + + String aliasName = importDirective.getAliasName(); + if (aliasName == null) { + aliasName = referenceExpression != null ? referenceExpression.getReferencedName() : null; + } + if (classifierDescriptor != null) { + context.getTrace().record(BindingContext.REFERENCE_TARGET, referenceExpression, classifierDescriptor); + + if (aliasName != null) { + namespaceScope.importClassifierAlias(aliasName, classifierDescriptor); + } + } + if (namespaceDescriptor != null) { + if (classifierDescriptor == null) { + context.getTrace().record(BindingContext.REFERENCE_TARGET, referenceExpression, namespaceDescriptor); + } + if (aliasName != null) { + namespaceScope.importNamespaceAlias(aliasName, namespaceDescriptor); + } + } + } + } + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java index 6a694ed7d99..435fed20e44 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java @@ -15,7 +15,10 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.WritableScope; import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl; import org.jetbrains.jet.lang.resolve.scopes.WriteThroughScope; -import org.jetbrains.jet.lang.types.*; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.TypeConstructor; +import org.jetbrains.jet.lang.types.TypeProjection; +import org.jetbrains.jet.lang.types.TypeUtils; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices; import org.jetbrains.jet.lexer.JetTokens; @@ -30,17 +33,19 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.*; */ public class TypeHierarchyResolver { private final TopDownAnalysisContext context; + private final ImportsResolver importsResolver; private LinkedList topologicalOrder; public TypeHierarchyResolver(TopDownAnalysisContext context) { this.context = context; + this.importsResolver = new ImportsResolver(context); } public void process(@NotNull JetScope outerScope, @NotNull NamespaceLike owner, @NotNull Collection declarations) { collectNamespacesAndClassifiers(outerScope, outerScope, owner, declarations); // namespaceScopes, classes - processTypeImports(); + importsResolver.processTypeImports(); createTypeConstructors(); // create type constructors for classes and generic parameters, supertypes are not filled in resolveTypesInClassHeaders(); // Generic bounds and types in supertype lists (no expressions or constructor resolution) @@ -75,7 +80,6 @@ public class TypeHierarchyResolver { WriteThroughScope namespaceScope = new WriteThroughScope(outerScope, namespaceDescriptor.getMemberScope(), new TraceBasedRedeclarationHandler(context.getTrace())); namespaceScope.changeLockLevel(WritableScope.LockLevel.BOTH); - context.getConfiguration().addDefaultImports(context.getTrace(), namespaceScope); context.getNamespaceScopes().put(namespace, namespaceScope); context.getDeclaringScopes().put(namespace, outerScope); @@ -233,82 +237,6 @@ public class TypeHierarchyResolver { return ClassKind.CLASS; } - private void processTypeImports() { - for (JetNamespace jetNamespace : context.getNamespaceDescriptors().keySet()) { - processImports(jetNamespace, context.getNamespaceScopes().get(jetNamespace), context.getDeclaringScopes().get(jetNamespace)); - } - } - - private void processImports(@NotNull JetNamespace namespace, @NotNull WritableScope namespaceScope, @NotNull JetScope outerScope) { - List importDirectives = namespace.getImportDirectives(); - for (JetImportDirective importDirective : importDirectives) { - if (importDirective.isAbsoluteInRootNamespace()) { - context.getTrace().report(UNSUPPORTED.on(importDirective, "TypeHierarchyResolver")); // TODO - continue; - } - if (importDirective.isAllUnder()) { - JetExpression importedReference = importDirective.getImportedReference(); - if (importedReference != null) { - ExpressionTypingServices typeInferrerServices = context.getSemanticServices().getTypeInferrerServices(context.getTrace()); - JetType type = typeInferrerServices.getTypeWithNamespaces(namespaceScope, importedReference); - if (type != null) { - namespaceScope.importScope(type.getMemberScope()); - } - } - } - else { - ClassifierDescriptor classifierDescriptor = null; - NamespaceDescriptor namespaceDescriptor = null; - JetSimpleNameExpression referenceExpression = null; - - JetExpression importedReference = importDirective.getImportedReference(); - if (importedReference instanceof JetDotQualifiedExpression) { - JetDotQualifiedExpression reference = (JetDotQualifiedExpression) importedReference; - JetType type = context.getSemanticServices().getTypeInferrerServices(context.getTrace()).getTypeWithNamespaces(namespaceScope, reference.getReceiverExpression()); - JetExpression selectorExpression = reference.getSelectorExpression(); - if (selectorExpression != null) { - referenceExpression = (JetSimpleNameExpression) selectorExpression; - String referencedName = referenceExpression.getReferencedName(); - if (type != null && referencedName != null) { - classifierDescriptor = type.getMemberScope().getClassifier(referencedName); - namespaceDescriptor = type.getMemberScope().getNamespace(referencedName); - } - } - } - else { - assert importedReference instanceof JetSimpleNameExpression; - referenceExpression = (JetSimpleNameExpression) importedReference; - - String referencedName = referenceExpression.getReferencedName(); - if (referencedName != null) { - classifierDescriptor = outerScope.getClassifier(referencedName); - namespaceDescriptor = outerScope.getNamespace(referencedName); - } - } - - String aliasName = importDirective.getAliasName(); - if (aliasName == null) { - aliasName = referenceExpression != null ? referenceExpression.getReferencedName() : null; - } - if (classifierDescriptor != null) { - context.getTrace().record(BindingContext.REFERENCE_TARGET, referenceExpression, classifierDescriptor); - - if (aliasName != null) { - namespaceScope.importClassifierAlias(aliasName, classifierDescriptor); - } - } - if (namespaceDescriptor != null) { - if (classifierDescriptor == null) { - context.getTrace().record(BindingContext.REFERENCE_TARGET, referenceExpression, namespaceDescriptor); - } - if (aliasName != null) { - namespaceScope.importNamespaceAlias(aliasName, namespaceDescriptor); - } - } - } - } - } - private void createTypeConstructors() { for (Map.Entry entry : context.getClasses().entrySet()) { JetClass jetClass = entry.getKey(); diff --git a/compiler/testData/diagnostics/tests/regressions/kt762.jet b/compiler/testData/diagnostics/tests/regressions/kt762.jet new file mode 100644 index 00000000000..848d478794e --- /dev/null +++ b/compiler/testData/diagnostics/tests/regressions/kt762.jet @@ -0,0 +1,4 @@ +//KT-762 Wrong highlighting in imports (No errors reported on unresolved imports) +import aaa // must be an error + +fun main(args : Array) {} \ No newline at end of file From 41111c3bee291e087cbb6e5cf701ee0199c96fe0 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 7 Dec 2011 15:36:07 +0200 Subject: [PATCH 3/4] spectral norm benchmark --- examples/src/benchmarks/SpectralNorm.java | 169 ++++++++++++++++++++++ examples/src/benchmarks/SpectralNorm.kt | 128 ++++++++++++++++ 2 files changed, 297 insertions(+) create mode 100644 examples/src/benchmarks/SpectralNorm.java create mode 100644 examples/src/benchmarks/SpectralNorm.kt diff --git a/examples/src/benchmarks/SpectralNorm.java b/examples/src/benchmarks/SpectralNorm.java new file mode 100644 index 00000000000..c67eb068dd7 --- /dev/null +++ b/examples/src/benchmarks/SpectralNorm.java @@ -0,0 +1,169 @@ +/* +The Computer Language Benchmarks Game +http://shootout.alioth.debian.org/ + +Based on C# entry by Isaac Gouy +contributed by Jarkko Miettinen +Parallel by The Anh Tran +*/ + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.concurrent.CyclicBarrier; + +public class SpectralNorm +{ + private static final NumberFormat formatter = new DecimalFormat ("#.000000000"); + + public static void main (String[] args) + { + int n = 5500; + if (args.length > 0) n = Integer.parseInt (args[0]); + + final long millis = System.currentTimeMillis(); + System.out.println (formatter.format (spectralnormGame (n)) ); + long total = System.currentTimeMillis() - millis; + System.out.println("[SpectralNorm-" + System.getProperty("project.name")+ " Benchmark Result: " + total + "]"); + } + + + private static final double spectralnormGame (int n) + { + // create unit vector + double[] u = new double[n]; + double[] v = new double[n]; + double[] tmp = new double[n]; + + for (int i = 0; i < n; i++) + u[i] = 1.0; + + // get available processor, then set up syn object + int nthread = Runtime.getRuntime ().availableProcessors (); + Approximate.barrier = new CyclicBarrier (nthread); + + int chunk = n / nthread; + Approximate[] ap = new Approximate[nthread]; + + for (int i = 0; i < nthread; i++) + { + int r1 = i * chunk; + int r2 = (i < (nthread -1)) ? r1 + chunk : n; + + ap[i] = new Approximate (u, v, tmp, r1, r2); + } + + + double vBv = 0, vv = 0; + for (int i = 0; i < nthread; i++) + { + try + { + ap[i].join (); + + vBv += ap[i].m_vBv; + vv += ap[i].m_vv; + } + catch (Exception e) + { + e.printStackTrace (); + } + } + + return Math.sqrt (vBv/vv); + } + + + private static class Approximate extends Thread + { + private static CyclicBarrier barrier; + + private double[] _u; + private double[] _v; + private double[] _tmp; + + private int range_begin, range_end; + private double m_vBv = 0, m_vv = 0; + + + public Approximate (double[] u, double[] v, double[] tmp, int rbegin, int rend) + { + super (); + + _u = u; + _v = v; + _tmp = tmp; + + range_begin = rbegin; + range_end = rend; + + start (); + } + + public void run () + { + // 20 steps of the power method + for (int i = 0; i < 10; i++) + { + MultiplyAtAv (_u, _tmp, _v); + MultiplyAtAv (_v, _tmp, _u); + } + + for (int i = range_begin; i < range_end; i++) + { + m_vBv += _u[i] * _v[i]; + m_vv += _v[i] * _v[i]; + } + } + + /* return element i,j of infinite matrix A */ + private final static double eval_A (int i, int j) + { + int div = ( ((i+j) * (i+j+1) >>> 1) +i+1 ); + return 1.0 / div; + } + + /* multiply vector v by matrix A, each thread evaluate its range only */ + private final void MultiplyAv (final double[] v, double[] Av) + { + for (int i = range_begin; i < range_end; i++) + { + double sum = 0; + for (int j = 0; j < v.length; j++) + sum += eval_A (i, j) * v[j]; + + Av[i] = sum; + } + } + + /* multiply vector v by matrix A transposed */ + private final void MultiplyAtv (final double[] v, double[] Atv) + { + for (int i = range_begin; i < range_end; i++) + { + double sum = 0; + for (int j = 0; j < v.length; j++) + sum += eval_A (j, i) * v[j]; + + Atv[i] = sum; + } + } + + /* multiply vector v by matrix A and then by matrix A transposed */ + private final void MultiplyAtAv (final double[] v, double[] tmp, double[] AtAv) + { + try + { + MultiplyAv (v, tmp); + // all thread must syn at completion + barrier.await (); + MultiplyAtv (tmp, AtAv); + // all thread must syn at completion + barrier.await (); + } + catch (Exception e) + { + e.printStackTrace (); + } + } + } +} \ No newline at end of file diff --git a/examples/src/benchmarks/SpectralNorm.kt b/examples/src/benchmarks/SpectralNorm.kt new file mode 100644 index 00000000000..930302bba87 --- /dev/null +++ b/examples/src/benchmarks/SpectralNorm.kt @@ -0,0 +1,128 @@ +namespace spectralnorm + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.concurrent.CyclicBarrier; + +val formatter = DecimalFormat ("#.000000000"); + +fun main (args : Array) { + var n = 5500 + if (args.size > 0) + n = Integer.parseInt (args[0]); + + val millis = System.currentTimeMillis() + System.out?.println (formatter.format (spectralnormGame (n)) ) + val total = System.currentTimeMillis() - millis; + System.out?.println("[SpectralNorm-" + System.getProperty("project.name")+ " Benchmark Result: " + total + "]"); +} + +fun spectralnormGame(n: Int) : Double { + val u = DoubleArray(n) + val v = DoubleArray(n) + val tmp = DoubleArray(n) + + for(i in u.indices) { + u[i] = 1.0 + } + + val nthread = Runtime.getRuntime ().sure().availableProcessors (); + barrier = CyclicBarrier (nthread); + + val chunk = n / nthread + val ap = Array(nthread,{ + val r1 = it * chunk; + val r2 = if(it < (nthread -1)) r1 + chunk else nthread; + + Approximate (u, v, tmp, r1, r2) + }) + + var vBv = 0.dbl + var vv = 0.dbl; + for (i in 0..nthread-1) { + try { + ap[i].join (); + + vBv += ap[i].m_vBv; + vv += ap[i].m_vv; + } + catch (e: Exception ) + { + e.printStackTrace (); + } + } + + return Math.sqrt (vBv/vv); +} + +fun eval_A (i: Int, j: Int) = 1.0 / ( ((i+j) * (i+j+1) shr 1) +i+1 ) + +var barrier : CyclicBarrier? = null + +class Approximate(val u: DoubleArray, val v: DoubleArray, val _tmp: DoubleArray, val rbegin: Int, val rend: Int) : Thread() { + class object { + } + + var m_vBv = 0.0 + var m_vv = 0.0 + + { + start() + } + + override fun run () { + for(i in 0..10) { + MultiplyAtAv (u, _tmp, v); + MultiplyAtAv (v, _tmp, u); + } + + for(i in rbegin..rend) { + m_vBv += u[i] * v[i]; + m_vv += v[i] * v[i]; + } + } + +/* multiply vector v by matrix A, each thread evaluate its range only */ + fun MultiplyAv (v: DoubleArray, Av: DoubleArray) + { + for (i in rbegin..rend) + { + var sum = 0.0; + for (j in v.indices) + sum += eval_A (i, j) * v[j]; + + Av[i] = sum; + } + } + +/* multiply vector v by matrix A transposed */ + fun MultiplyAtv (v: DoubleArray, Atv: DoubleArray) + { + for (i in rbegin..rend) + { + var sum = 0.dbl + for (j in v.indices) + sum += eval_A (j, i) * v[j]; + + Atv[i] = sum; + } + } + +/* multiply vector v by matrix A and then by matrix A transposed */ + fun MultiplyAtAv (v: DoubleArray, tmp: DoubleArray, AtAv: DoubleArray) + { + try + { + MultiplyAv (v, tmp); + // all thread must syn at completion + barrier?.await (); + MultiplyAtv (tmp, AtAv); + // all thread must syn at completion + barrier?.await (); + } + catch (e: Exception) + { + e.printStackTrace (); + } + } +} \ No newline at end of file From 2270ab3cba26599287471c7ba5dc40cf21dced89 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 7 Dec 2011 17:37:54 +0400 Subject: [PATCH 4/4] KT-750 Type inference failed: Constraint violation --- .../testData/diagnostics/tests/regressions/kt750.jet | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/regressions/kt750.jet diff --git a/compiler/testData/diagnostics/tests/regressions/kt750.jet b/compiler/testData/diagnostics/tests/regressions/kt750.jet new file mode 100644 index 00000000000..9e9760acb17 --- /dev/null +++ b/compiler/testData/diagnostics/tests/regressions/kt750.jet @@ -0,0 +1,9 @@ +// +JDK +//KT-750 Type inference failed: Constraint violation +fun main(args : Array) { + var i : Int? = Integer.valueOf(100) + var s : Int? = Integer.valueOf(100) + + val o = i .sure() + s.sure() + System.out?.println(o) +}