Commit Graph

144 Commits

Author SHA1 Message Date
Alexey Andreev
8fe96664b7 Fix initialization order of enum with companion object in JS BE
Make enum entries initialize before companion object. This helps
in situation when companion object initializer refers to enum fields.
JVM be generates <clinit> method which first initializes all enum fields
and then runs companion object initializer. This commit introduces the
similar behaviour in JS BE. The old behaviour was: initialize companion
object in constructor. In enum, constructor is called to initialize
enum fields, so previously companion object was initialized first,
which is incorrect.

See KT-16745
2017-03-28 11:32:46 +03:00
Alexey Andreev
0606ebe0dc Fix suspend function with default argument
In JS BE, fix translation of suspend function with default argument
inherited from interface.

See KT-16658
2017-03-28 11:32:45 +03:00
Alexey Andreev
6ba3812582 Fix inlining of tail call suspend function
When inlining tail-call suspend function to regular
suspend function in JS BE, don't forget to insert suspension point.
See KT-16951
2017-03-28 11:32:45 +03:00
Alexey Andreev
466540399c Fix translation of copy() fun in data class
Fix translation of copy() function in data class which has secondary
constructor in JS BE. See KT-16717.
2017-03-28 11:32:44 +03:00
Anton Bannykh
9b34e21619 JS: fixed <Type>Array.iterator methods; added -Xtypedarray compiler key
The <Type>Array.iterator used to lack next<Type>() method (KT-16626).

The -Xtypedarray compiler key enables translation of primitive arrays
to TypedArrays, and primitive array`is`-checks (KT-15358, KT-14007,
KT-14614, KT-16056).
2017-03-27 23:09:34 +03:00
Alexander Udalov
82c5c586b1 Minor, remove unnecessary test data files 2017-03-22 20:11:02 +03:00
Mikhail Zarechenskiy
bfe2ddf7c1 Introduce language feature for array literals in annotations 2017-03-22 17:59:58 +03:00
Mikhail Zarechenskiy
0f1acab40d Support collection literals in the JVM backend 2017-03-22 17:59:58 +03:00
Yan Zhulanow
e25e19c4d6 Refactoring: Remove light analysis test data
The light analysis test data is not needed anymore cause the light analysis result is now automatically checked against the one from the full analysis.
2017-03-21 20:49:36 +03:00
Mikhael Bogdanov
496a21254b Black box update 2017-03-20 18:46:01 +01:00
Pavel V. Talanov
d34b73befb Light class codegen: all objects are considered static
Simplify code handling access flag computation
Fix a problem where kotlin nested object wasn't producing a nested light class
2017-03-15 20:55:01 +03:00
Mikhael Bogdanov
ce3b455f57 Fix for KT-16801: Accessors of @PublishedApi property gets mangled
#KT-16801 Fixed
2017-03-13 10:51:10 +01:00
Dmitry Petrov
c46164481a KT-15871 Unnecessary boxing for equality operator on inlined primitive values
Allow kotlin.jvm.internal.Intrinsics#areEqual for boxed values.
Rewrite to primitive equality.

NB we can't do that for Float and Double, because java.lang.Float#equals
and java.lang.Double#equals behave differently from primitive equality comparisons.
2017-03-13 09:04:31 +03:00
Dmitry Petrov
ec403bfdbc KT-16245 Redundant null-check generated for a cast of already non-nullable value
KT-16194 Code with unnecessary safe call contains redundant boxing/unboxing for primitive values
KT-12839 Two null checks are generated when manually null checking platform type

Recognize some additional cases of trivial null checks and trivial instance-of checks.

A variable is "checked for null", if it is:
- a function parameter checked with 'INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull'
- checked for nullability with 'IFNULL/IFNONNULL'
- checked for nullability with 'INSTANCEOF'
  (if objectref is instance-of T, then objectref is non-null)

Before analyzing nullability, introduce synthetic assumptions for execution branches
where a variable is guaranteed to be null or not null. For example, the following bytecode:

     ALOAD 1 // Ljava/lang/String;
     IFNULL L
     <non-null branch>
  L:
     <null branch>

is transformed to

     ALOAD 1
     IFNULL L1
     NEW java/lang/String
     ASTORE 1            // tells analyzer that variable 1 is non-null
     <non-null branch>
  L:
     <null branch>
  L1:
     ACONST_NULL
     ASTORE 1            // tells analyzer that variable 1 is null
     GOTO L

After the analysis is performed on a preprocessed method,
remember the results for "interesting" instructions
and revert the preprocessing transformations.

After that, perform bytecode transformations as usual.

Do not transform INSTANCEOF to-be-reified, because reification at call site
can introduce null checks. E.g.,

    inline fun <reified T> isNullable() = null is T
    ...
    assert(isNullable<String?>())
2017-03-13 09:04:31 +03:00
Dmitry Petrov
3c09a26e16 KT-5248 Don't wrap variable if it is captured only in inlined closures
Remove non-escaping Ref's on bytecode postprocessing pass.
2017-03-13 09:04:31 +03:00
Dmitry Petrov
3fc106572e Make redundant null check optimization independent of boxing optimization algorithm.
Run DCE after each single redundant null check optimization pass.
2017-03-13 09:04:31 +03:00
Denis Zharkov
2b21280ba9 Unwrap underlying typealias constructor earlier
The problem is very subtle (see the test): when generating a signature
for an object literal we also were mapping its super-class
(a type alias here).

Although we did unwrap its underlying constructor to map it properly
we did too late (after obtaining value parameters from the type alias constructor descriptor).

Another problem is that TypeAliasConstructorDescriptor.getOriginal
in the case does return itself, while it's expected to return
unsubstituted version

Note: everything works for common calls for such constructors
because they mapped through mapCallableMethod which contains
another custom unwrapping of type alias constructors

 #KT-16555 Fixed
2017-03-10 13:45:37 +03:00
Mikhael Bogdanov
5e80d80797 Fix for KT-16732: Type 'java/lang/Number' (current frame, stack[0]) is not assignable to 'java/lang/Character
#KT-16732 Fixed
2017-03-10 10:04:29 +01:00
Dmitry Petrov
6a68eb218f KT-16077 Redundant private getter for private var in a class within a JvmMultifileClass annotated file 2017-03-09 17:22:28 +03:00
Dmitry Petrov
f950ff4b8f 'ConstructorDescriptor#getConstructedClass()' should be used to obtain a descriptor for constructed class
(it can be different from 'getContainingDeclaration()' in case of type alias constructor).

KT-15109 Subclass from a type alias with named parameter in constructor will produce compiler exception
KT-15192 Compiler crashes on certain companion objects: "Error generating constructors of class Companion with kind IMPLEMENTATION"
2017-03-09 09:23:38 +03:00
Dmitry Petrov
11caa03427 KT-16713 Insufficient maximum stack size
1. Analyze method node with fake jumps for loops to make sure that
all instructions reachable only through break/continue jumps are processed.
2. Fix stack for break/continue jumps.
3. Drop fake jumps for loops, analyze method node again.
4. Fix stack for try/catch and beforeInline.
2017-03-08 09:56:08 +01:00
Alexey Andreev
8567db10b5 JS: fix coroutine transformation of callable references to local functions. See KT-16164 2017-03-07 10:46:07 +03:00
Mikhail Zarechenskiy
d573962259 Add test for class delegation with private constructor
#KT-16583 Obsolete
2017-03-02 18:34:59 +03:00
Denis Zharkov
6fb83c2ba3 Force wrapping java classes from annotation methods into KClasses
Before this change such wrapping happened only during coercion,
i.e. when a call-site expected a KClass instance.

But when call-site expects Any, for example, no wrapping happened,
and raw j.l.Class instance was left on stack.

The solution is to put wrapping code closer to generation of annotation's
method call itself to guarantee that necessary wrapping will happen.

 #KT-9453 Fixed
2017-03-02 15:19:09 +03:00
Anton Bannykh
9e5ecc11b7 JS: fixed Double.NaN behaviour (KT-13610). 2017-03-02 14:29:50 +03:00
Denis Zharkov
ecec87cbc7 Refine signature calculation for methods with default parameters
The problem was that he number of mask parameters for defaults when
generating methods declaration was being calculated upon resulting signature
(with additional parameters: extension receivers, enum name/ordinal),
while on call-sites the masks number was calculated by the arguments number
in resolved call, i.e. by the number of real value parameters.

And because of the additional synthetic parameters (like enum.ordinal) these
two numbers could be different.

The solution is just to use value parameters number in both places.
Note, that we only count value parameters from the original sourse
declaration, ignoring synthetic ones generated by backend (e.g.
Continuation for suspend functions)

 #KT-14565 Fixed
2017-02-28 10:42:07 +03:00
Denis Zharkov
9e03b712de Use flexible upper bound of parameter's type for vararg argument
See the issue and the test. The problem was that when generating
call to `foo` method in member scope of `AT<*>` its resulting descriptor
after substitution and approximation was: fun foo(x: Nothing..Array<out Nothing>).

This signature is correct, but when using this parameter type
for generating a vararg argument the assertion is violated that
the type of the argument must be an array
(by default we're using lower flexible bound everywhere)

The solution is using upper bound for flexible types that should
always have a form of Array<out T> for varargs (even for such corner cases)
both for Kotlin and Java declarations.

 #KT-14607 Fixed
2017-02-28 10:40:54 +03:00
Mikhael Bogdanov
d24ebf711b Fix for KT-16441: NoSuchFieldError: $$delegatedProperties when delegating through provideDelegate in companion object
#KT-16441 Fixed
2017-02-21 13:01:40 +01:00
Denis Zharkov
85f9e2e47b Fix CCE in generated code that happens when comparing boxed chars
The problem was that when obtaining char from the wrapper,
codegen used int as expected type that led
to a ClassCastException: java.lang.Character cannot be cast to java.lang.Number

The solution is using coercion to chars, it's still correct,
because of implicit widening coercion in JVM from C to I

 #KT-15105 Fixed
2017-02-21 12:33:45 +03:00
Denis Zharkov
0d8f64353a Fix JVM signature mapping for multi-dimensional arrays
#KT-11314 Fixed
2017-02-17 17:50:43 +03:00
Alexander Udalov
1ee2053a16 Make callable references Serializable on JVM
#KT-11254 Fixed
2017-02-16 18:00:18 +03:00
Alexander Udalov
ba84338862 Support smart cast for nullability in LHS of class literal
#KT-16291 Fixed
2017-02-15 20:43:25 +03:00
Alexander Udalov
8e3cb912c7 Make Ref classes for shared vars Serializable
#KT-14566 Fixed
2017-02-15 10:46:48 +03:00
Denis Zharkov
5f843f6759 Fix intrinsic for String.plus for explicit calls
#KT-14567 Fixed
2017-02-10 16:05:15 +03:00
Denis Zharkov
fcb4db0226 Remove redundant ACC_VARARGS on methods generated with @JvmOverloads
#KT-15424 Fixed
2017-02-10 16:05:15 +03:00
Denis Zharkov
1f179a6f01 Add test on obsolete issue
#KT-15196 Obsolete
2017-02-10 16:05:15 +03:00
Denis Zharkov
b989bfce59 Fix VerifyError with @JvmStatic annotated getter
The problem was that for property getter 'context.getContextDescriptor()'
references the containing property, while 'context.getFunctionDescriptor()'
the accessor itself

 #KT-15594 Fixed
2017-02-10 16:05:15 +03:00
Alexander Udalov
6d16ea0a3a Fix reflection on top level declarations from other modules
The main problem here is that moduleName that is being passed to KPackageImpl
is useless: as can be seen in
ClosureCodegen.generateCallableReferenceDeclarationContainer, the name of the
current module is always written to the class file for a callable reference,
not the name of the module of the referenced declaration. This resulted in
reflection not loading the correct .kotlin_module file and subsequently not
finding the required file facade for a top-level function.

The commit does not fix the issue with the incorrect module name written in the
back-end, but workarounds it. It turns out, reflection can figure out the name
of the module of the referenced declaration itself by parsing the header from
the given java.lang.Class object for a single-file/multi-file package facade
and extract the package_module_name protobuf extension. Similar code was
already there in Member.getKPackage() in ReflectJvmMapping.kt but it did not
support multi-file classes, of which there are a lot in the standard library;
this is now supported

 #KT-12630 Fixed
 #KT-14731 Fixed
2017-02-08 19:23:18 +03:00
Mikhail Zarechenskiy
e7ea076093 Do not apply constant folding for non built-in functions
#KT-15872 Fixed
2017-02-08 15:42:39 +03:00
Denis Zharkov
80638ebc99 Prohibit unsupported suspend operators
contains/get/set operators don't work properly on both backends

Also add box test checking that 'compareTo' operator works just fine

 #KT-16219 Fixed
2017-02-08 11:07:27 +03:00
Mikhail Zarechenskiy
d7093db5c5 Allow to use emptyArray in annotation as argument
#KT-14236 Fixed
2017-02-07 14:07:20 +03:00
Denis Zharkov
cc7f0e2d83 Fix codegen problem with safe-call on suspension point with two receivers
#KT-16145 Fixed
2017-02-07 11:56:37 +03:00
Denis Zharkov
258ee0db75 Introduce startCoroutineUninterceptedOrReturn coroutine intrinsic
#KT-15716 Fixed
2017-02-07 11:07:04 +03:00
Denis Zharkov
29b0b30031 Make createCoroutine return a safe continuation
#KT-15718 Fixed
2017-02-07 11:06:50 +03:00
Alexander Udalov
8014000bd1 Update light analysis test data 2017-02-06 19:41:32 +03:00
Dmitry Petrov
e0cea468fa KT-15862 Inline generic functions can unexpectedly box primitives
Previous version of the boxing/unboxing analysis treated merging boxed and non-boxed values as a hazard.
If such merged values are not used (e.g., early return + local variables reused in inlined calls),
corresponding boxing/unboxing operations still can be optimized out.

All information related to boxed value usage by instructions is moved to 'BoxedValueDescriptor'.
Introduce "tainted" (and "clean") boxed values, with the following rules:

  merge(B, B) = B, if unboxed types are compatible,
                T, otherwise

  merge(B, X) = T

  merge(T, X) = T

  where
    X is a non-boxed value,
    B is a "clean" boxed value,
    T is a "tainted" boxed value.

Postpone decision about value merge hazards until a "tainted" value is used.
2017-02-06 16:22:26 +03:00
Alexander Udalov
b784680fe2 Fix NPE on equals/hashCode for callable references without kotlin-reflect
#KT-11316 Fixed
 #KT-15847 Fixed
2017-02-03 19:35:16 +03:00
Alexander Udalov
0db60bf6cb Do not always generate synthetic "$annotations" as private
Since annotations are a part of the declaration, they must have the same
visibility as the declaration in the bytecode. Otherwise obfuscators like
Proguard might strip the "$annotations" method and no annotations would be
found via Kotlin reflection

 #KT-15993 Fixed
2017-02-03 19:35:15 +03:00
Alexey Andreev
97b6c3013a JS: fix destructuring declaration and increment in coroutines. See KT-16058 2017-02-03 11:09:10 +03:00
Denis Zharkov
cc28fecacd Fix VerifyError in coroutines caused by null spilling
While within a method by the JVM spec null-value has a special
Nothing-like type, when we spill it for a coroutine, we must choose
some real type to CHECKCAST to after restoring the variable's value.

But the problem is that such a real type depends on usage of that null value,
and there may be more than one usage.

The solution is not to spill such variables into fields, but instead
init them with ACONST_NULL after each suspension point

 #KT-16122 Fixed
2017-02-03 10:32:28 +03:00