summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/typechecker/Typers.scala
Commit message (Collapse)AuthorAgeFilesLines
...
* | Clarify how/when typedFunction unrolls eta-expansionAdriaan Moors2016-03-311-5/+20
| | | | | | | | | | | | | | Jason points out the recursion will be okay if type checking the function inside the eta-expansion provides fully determined argument types, as the result type is not relevant for this phase of typedFunction.
* | typedFunction undoes eta-expansion regardless of expected typeAdriaan Moors2016-03-301-30/+31
| | | | | | | | | | | | When recovering missing argument types for an eta-expanded method value, rework the expected type to a method type.
* | Keep Function when CBN arg thunk targets a SAMAdriaan Moors2016-03-301-24/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The body of `def delay[T](v: => T) = (v _): F0[T]` becomes `() => v` during `typedEta`, and then uncurry considers whether to strip the function wrapper since `v` is known to be a `Function0` thunk. Stripping is sound when the expected type is `Function0` for this expression, but that's no longer a given, since we could be expecting any nullary SAM. Also sweep up a bit around `typedEta`. Encapsulate the, erm, creative encoding of `m _` as `Typed(m, Function(Nil, EmptyTree))`.
* | LMF cannot instantiate SAM of trait with non-trait superclassAdriaan Moors2016-03-291-6/+30
| | | | | | | | | | | | | | | | | | | | | | | | Also, drop AbstractFunction for parent of anonymous subclass of function type that must have its class spun up at compile time (rather than at linkage time by LambdaMetaFactory). This revealed an old problem with typedTemplate, in which parent types may be normalized at the level of trees, while this change does not get propagated to the class's info in time for the constructor to be located when we type check the primary constructor.
* | SAM conversion can be disabled using `-Xsource:2.11`Adriaan Moors2016-03-261-0/+2
| | | | | | | | For completeness, `-Xsource:2.11 -Xexperimental` does enable it.
* | Refactor: simplify fallbackAfterVanillaAdapt.Adriaan Moors2016-03-261-99/+106
| | | | | | | | | | | | | | | | Trying to figure out if we can avoid adapting to SAM, and just type them once and for all in typedFunction. Looks like overload resolution requires SAM adaptation to happen in adapt. Cleaned up while I was in the area.
* | SAM conversion precedes implicit view application (as in dotty).Adriaan Moors2016-03-261-8/+8
| | | | | | | | | | | | | | | | This reflects the majority vote on the PR. DSLs that need their implicit conversions to kick in instead of SAM conversion, will have to make their target types not be SAM types (e.g., by adding a second abstract method to them).
* | Track Function's SAM symbol & target type using an attachmentAdriaan Moors2016-03-261-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We cannot use the expected type to track whether a Function node targets a SAM type, as the expected type may be erased (see test for an example). Thus, the type checker attaches a SAMFunction attachment to a Function node when SAM conversion is performed in adapt. Ideally, we'd move to Dotty's Closure AST, but that will need a deprecation cycle. Thanks to Jason for catching my mistake, suggesting the fix and providing the test. Both the sam method symbol and sam target type must be tracked, as their relationship can be complicated (due to inheritance). For example, the sam method could be defined in a superclass (T) of the Function's target type (U). ``` trait T { def foo(a: Any): Any } trait U extends T { def apply = ??? } (((x: Any) => x) : U).foo("") ``` This removes some of the duplication in deriving the sam method from the expected type, but some grossness (see TODO) remains.
* | Don't adapt erroneous tree to SAM type.Adriaan Moors2016-03-261-2/+2
| | | | | | | | | | | | Do not report second error. Go straight to the exit. Based on review by Jason.
* | Jason's review feedback (ThisReferringMethodTraverser)Adriaan Moors2016-03-261-5/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Re-simplify logging; - Remove unused method valueTypeToObject; - Limit ThisReferringMethodTraverser to material parts of the AST Limit ThisReferringMethodTraverser's analysis to only look at template-owned anonfun method bodies, to make sure it's fairly low overhead. AFAICT, part of the complexity of this analysis stems from the desire to make all the lambda impl methods static in `() => () => 42`: https://gist.github.com/062181846c13e65490cc. It would possible to accumulate the knowledge we need during the main transform, rather than in an additional pass. We'd need to transform template bodies in such a way that we we process definitions of anonfun methods before usages, which would currently amount to transforming the stats in reverse.
* | More fixes based on feedback by LukasAdriaan Moors2016-03-261-14/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | Crucially, the fully-defined expected type must be checked for conformance to the original expected type!! The logic in adaptToSam that checks whether pt is fully defined probably needs some more thought. See pos/t8310 for a good test case. Argument type checking is a challenge, as we first check against a lenient pt (this lenient expected type has wildcards, and thus is not fully defined, but we should still consider sam adaptation a success even if we end up with wildcards for some unknown type parameters, they should be determined later).
* | Refactoring. Simplify inferImplicit's boolean leversAdriaan Moors2016-03-261-34/+32
| |
* | For backwards compat, sammy comes lastAdriaan Moors2016-03-261-7/+7
| |
* | Treat `Function` literals uniformly, expecting SAM or FunctionN.Adriaan Moors2016-03-261-118/+41
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | They both compile to INDY/MetaLambdaFactory, except when they occur in a constructor call. (TODO: can we lift the ctor arg expression to a method and avoid statically synthesizing anonymous subclass altogether?) Typers: - no longer synthesize SAMs -- *adapt* a Function literal to the expected (SAM/FunctionN) type - Deal with polymorphic/existential sams (relevant tests: pos/t8310, pos/t5099.scala, pos/t4869.scala) We know where to find the result type, as all Function nodes have a FunctionN-shaped type during erasure. (Including function literals targeting a SAM type -- the sam type is tracked as the *expected* type.) Lift restriction on sam types being class types. It's enough that they dealias to one, like regular instance creation expressions. Contexts: - No longer need encl method hack for return in sam. Erasure: - erasure preserves SAM type for function nodes - Normalize sam to erased function type during erasure, otherwise we may box the function body from `$anonfun(args)` to `{$anonfun(args); ()}` because the expected type for the body is now `Object`, and thus `Unit` does not conform. Delambdafy: - must set static flag before calling createBoxingBridgeMethod - Refactored `createBoxingBridgeMethod` to wrap my head around boxing, reworked it to generalize from FunctionN's boxing needs to arbitrary LMF targets. Other refactorings: ThisReferringMethodsTraverser, TreeGen.
* | Review feedback from LukasAdriaan Moors2016-03-261-1/+4
| |
* | Refactoring. Sweep Sammy's backyard.Adriaan Moors2016-03-261-21/+18
| |
* | SI-9449 sam expansion for explicitly eta-expanded methodAdriaan Moors2016-03-261-2/+10
| |
* | Refactor typedFunction, rework synthesizeSAMFunction for sammyAdriaan Moors2016-03-261-188/+186
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `typedFunction` uniformly recognizes Single Abstract Method types and built-in `FunctionN` types, type checking literals regardless of expected type. `adapt` synthesizes an anonymous subclass of the SAM type, if needed to meet the expected (non-`FunctionN`) type. (Later, we may want to carry `Function` AST nodes with SAM types through the whole pipeline until the back-end, and treat them uniformly with built-in function types there too, emitting the corresponding `invokedynamic` & `LambdaMetaFactory` bytecode. Would be faster to avoid synthesizing all this code during type checking...) Refactor `typedFunction` for performance and clarity to avoid non-local returns. A nice perk is that the error message for missing argument types now indicates with `<error>` where they are missing (see updated check file). Allow pattern matching function literals when SAM type is expected (SI-8429). Support `return` in function body of SAM target type, by making the synthetic `sam$body` method transparent to the `enclMethod` chain, so that the `return` is interpreted in its original context. A cleaner approach to inferring unknown type params of the SAM method. Now that `synthesizeSAMFunction` operates on typed `Function` nodes, we can take the types of the parameters and the body and compare them against the function type that corresponds to the SAM method's signature. Since we are reusing the typed body, we do need to change owners for the symbols, and substitute the new method argument symbols for the function's vparam syms. Impl Notes: - The shift from typing as a regular Function for SAM types was triggered by limitation of the old approach, which deferred type checking the body until it was in the synthetic SAM type subclass, which would break if the expression was subsequently retypechecked for implicit search. Other problems related to SAM expansion in ctor args also are dodged now. - Using `<:<`, not `=:=`, in comparing `pt`, as `=:=` causes `NoInstance` exceptions when `WildcardType`s are encountered. - Can't use method type subtyping: method arguments are in invariant pos. - Can't use STATIC yet, results in illegal bytecode. It would be a better encoding, since the function body should not see members of SAM class. - This is all battle tested by running `synthesizeSAMFunction` on all `Function` nodes while bootstrapping, including those where a regular function type is expected. The only thing that didn't work was regarding Function0 and the CBN transform, which breaks outer path creation in lambdalift.
* | Refactor. Extract mkLiteralUnit and mkUnitBlockAdriaan Moors2016-03-261-1/+1
| |
* | Merge pull request #5053 from som-snytt/issue/9314Lukas Rytz2016-03-231-7/+13
|\ \ | | | | | | SI-9314 Marginal edge case to warn-missing-interp
| * | SI-9314 No warn on ${nonid}Som Snytt2016-03-231-7/+11
| | | | | | | | | | | | | | | Use the sym test on an expr that happens to be a subset of idents and is not in scope. Other `${ operator_* }` warn.
| * | SI-9314 Ignore "${}"Som Snytt2016-03-201-3/+5
| | | | | | | | | | | | | | | | | | | | | As an Easter egg, let "${} $x" forego the check on `x`. In other words, empty expression interpolation looks too degenerate to check.
| * | SI-9314 Don't warn on "$pkg"Som Snytt2016-03-201-1/+1
| | | | | | | | | | | | | | | Edge cases of things not to warn about include package names.
* | | Merge pull request #5043 from dongjoon-hyun/fix_typos_in_spec_and_commentsJason Zaugg2016-03-211-1/+1
|\ \ \ | | | | | | | | Fix some typos in `spec` documents and comments.
| * | | Fix some typos in `spec` documents and comments.Dongjoon Hyun2016-03-151-1/+1
| |/ /
* / / New trait encoding: use default methods, jettison impl classesJason Zaugg2016-03-181-2/+2
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Until now, concrete methods in traits were encoded with "trait implementation classes". - Such a trait would compile to two class files - the trait interface, a Java interface, and - the implementation class, containing "trait implementation methods" - trait implementation methods are static methods has an explicit self parameter. - some methods don't require addition of an interface method, such as private methods. Calls to these directly call the implementation method - classes that mixin a trait install "trait forwarders", which implement the abstract method in the interface by forwarding to the trait implementation method. The new encoding: - no longer emits trait implementation classes or trait implementation methods. - instead, concrete methods are simply retained in the interface, as JVM 8 default interface methods (the JVM spec changes in [JSR-335](http://download.oracle.com/otndocs/jcp/lambda-0_9_3-fr-eval-spec/index.html) pave the way) - use `invokespecial` to call private or particular super implementations of a method (rather `invokestatic`) - in cases when we `invokespecial` to a method in an indirect ancestor, we add that ancestor redundantly as a direct parent. We are investigating alternatives approaches here. - we still emit trait fowrarders, although we are [investigating](https://github.com/scala/scala-dev/issues/98) ways to only do this when the JVM would be unable to resolve the correct method using its rules for default method resolution. Here's an example: ``` trait T { println("T") def m1 = m2 private def m2 = "m2" } trait U extends T { println("T") override def m1 = super[T].m1 } class C extends U { println("C") def test = m1 } ``` The old and new encodings are displayed and diffed here: https://gist.github.com/retronym/f174d23f859f0e053580 Some notes in the implementation: - No need to filter members from class decls at all in AddInterfaces (although we do have to trigger side effecting info transformers) - We can now emit an EnclosingMethod attribute for classes nested in private trait methods - Created a factory method for an AST shape that is used in a number of places to symbolically bind to a particular super method without needed to specify the qualifier of the `Super` tree (which is too limiting, as it only allows you to refer to direct parents.) - I also found a similar tree shape created in Delambdafy, that is better expressed with an existing tree creation factory method, mkSuperInit.
* | SI-9658 Fix crosstalk between partial fun. and GADT matchJason Zaugg2016-03-041-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | When typechecking the synthetic default case of a pattern matching anonymous partial function, we failed to create a new `Context`. This led to crosstalk with the management of the saved type bounds of an enclosing GADT pattern match. This commit avoids the direct call to `typeCase` and instead indirects through `typedCases`, which spawns a new nested typer context, and hence avoids the crosstalk when `restoreSavedTypeBounds` runs.
* | SI-9540 typedFunction is erasure awareAdriaan Moors2016-02-121-2/+9
| | | | | | | | | | | | | | | | | | | | When typer is running during erasure, must assign erased FunctionType in typedFunction. This removes a bunch of unneeded casts now we no longer assign a half-erased FunctionType. I poked around a bit, and it looks like erasure doesn't want typer to erase built-in types (like Unit/Any/Nothing). They are already treated specially during erasure.
* | Merge pull request #4917 from retronym/ticket/9629Jason Zaugg2016-01-291-4/+4
|\ \ | | | | | | SI-9629 Emit missing 'pattern must be a value' error
| * | SI-9629 Emit missing 'pattern must be a value' errorJason Zaugg2016-01-251-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | The error used to only be emitted for top-level patterns. This commit moves it into `typedInternal` so it works for nested patterns. It uses the typer mode to know when to fire.
* | | Avoid exhaustivity warning in typedTemplateJason Zaugg2016-01-291-1/+1
|/ /
* | Remove unused imports and other minor cleanupsSimon Ochsenreither2015-12-181-2/+2
| | | | | | | | | | | | | | | | | | | | - Language imports are preceding other imports - Deleted empty file: InlineErasure - Removed some unused private[parallel] methods in scala/collection/parallel/package.scala This removes hundreds of warnings when compiling with "-Xlint -Ywarn-dead-code -Ywarn-unused -Ywarn-unused-import".
* | Merge pull request #4822 from retronym/ticket/9178Lukas Rytz2015-11-181-1/+8
|\ \ | | | | | | SI-9178 Don't eta expand to an Function0-like SAM expected type
| * | SI-9178 Don't eta expand param-less method types to SAMsJason Zaugg2015-10-271-1/+8
| | | | | | | | | | | | | | | | | | Otherwise, we can end up with a subtle source incompatibility with the pre-SAM regime. Arguably we should phase out eta expansion to Function0 as well, but I'll leave that for another day.
* | | SI-9535 correct bytecode and generic signatures for @throws[TypeParam]Lukas Rytz2015-10-261-1/+1
|/ / | | | | | | | | | | | | For @throws[E] where E is not a class type, GenASM incorrectly writes the non-class type to the classfile. GenBCode used to crash before this commit. Now GenBCode correctly emits the erased type (like javac) and adds a generic signature.
* | Allow @inline/noinline at callsites (in addition to def-site)Lukas Rytz2015-10-201-0/+8
| | | | | | | | | | | | | | Allow annotating individual callsites @inline / @noinline using an annotation ascription c.foo(): @inline
* | Merge commit 'bb3ded3' into merge-2.11-to-2.12-oct-5Lukas Rytz2015-10-051-0/+1
|\|
| * Improve presentation compilation of annotationsJason Zaugg2015-09-241-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A trio of problems were hampering autocompletion of annotations. First, given that that annotation is written before the annotated member, it is very common to end parse incomplete code that has a floating annotation without an anotatee. The parser was discarding the annotations (ie, the modifiers) and emitting an `EmptyTree`. Second, the presetation compiler was only looking for annotations in the Modifiers of a member def, but after typechecking annotations are moved into the symbol. Third, if an annotation failed to typecheck, it was being discarded in place of `ErroneousAnnotation`. This commit: - modifies the parser to uses a dummy class- or type-def tree, instead of EmptyTree, which can carry the annotations. - updates the locator to look in the symbol annotations of the modifiers contains no annotations. - uses a separate instance of `ErroneousAnnotation` for each erroneous annotation, and stores the original tree in its `original` tree.
| * Fix typos in spec, docs and commentsMichał Pociecha2015-08-231-3/+3
| |
* | SI-9479 Fix regression w. inherited overloads in package objectJason Zaugg2015-09-191-1/+1
| | | | | | | | | | | | | | | | | | | | One shouldn't base any decisions of the owner of an overloaded symbol. Instead, the owner of each of the alternatives should be considered. This gotcha is super easy to forget, as I did with my change to simplify the way we detect whether we need to add the `.package` prefix to a tree.
* | Merge remote-tracking branch 'origin/2.11.x' into 2.12.xSeth Tisue2015-09-081-3/+3
| | | | | | | | | | | | | | | | only trivial merge conflicts here. not dealing with PR #4333 in this merge because there is a substantial conflict there -- so that's why I stopped at 63daba33ae99471175e9d7b20792324615f5999b for now
* | Merge remote-tracking branch 'origin/2.11.x' into 2.12.xSeth Tisue2015-08-201-1/+1
|\| | | | | | | | | | | | | | | | | | | all conflicts were because the changes changed code that doesn't exist anymore in 2.12; they were resolved with `git checkout --ours` c201eac changed bincompat-forward.whitelist.conf but I dropped the change in this merge because it refers to AbstractPromise which no longer exists in 2.12
| * ScalaDoc fixes for compilerJanek Bogucki2015-07-291-1/+1
| |
* | Merge pull request #4652 from retronym/ticket/9408Lukas Rytz2015-07-231-0/+2
|\ \ | | | | | | SI-9408 Avoid capturing outer class in local classes.
| * | SI-9408 Record known subclasses of local classesJason Zaugg2015-07-231-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using the same facility that we use to record subclasses of sealed classes, record the subclasses of term-owned ("local") classes. I have changed existing callers of `children` to use `sealedChildren` so we don't start using this new information in pattern matching and type pattern checkability analysis. The following commit will build on this to infer finality of local classes in the context of outer pointer elision in the constructors phase.
* | | Merge remote-tracking branch 'origin/2.11.x' into ↵Jason Zaugg2015-07-231-1/+1
|\ \ \ | |/ / |/| / | |/ merge/2.11.x-to-2.12.x-20152307
| * SI-9401 Avoid SOE with array + missing classtagJason Zaugg2015-07-161-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The implicit classtags required by the Array constructor are not expressed in the type signature of its constructor, and instead are summoned by a special case in the typechecker. This special case entails replacing the `new Array` tree with `implicitly[T].newArray(size)`, handled in `ArrayInstantiation`. This tree is recursively typechecked. However, if the implicit materialization/search fails, an error is issued to the current reporter and the original tree is marked with an error type. As above, this is recursively typechecked. In the normal course of affairs, the recursive typecheck of the erroneous tree would be a noop (the tree already has a type!). However, if we are both in silent mode (in which errors are buffered) and in retyping mode (in which the typer clears the type and symbols of trees), we were getting into an cycle. In the enclosed test, retyping mode was trying to recover from: Resetting.this.gencastarray_=(new Array[T](0).<ERROR>) By inserting a suitable a view: implicitly[Resetting => { def gencastarray_=(AT)}]( Resetting.this ).gencastarray_=(new Array[T](0)) Where AT is the type found by retypechecking the argument. It is during the argument retypechecking that we fell into cycle. Crazily enough, in 2.11.0, the ensuing `StackOverflowError` was being caught and treated as a failure. We would then back out of the retyping mode, and issue the error from the the very first attempt at the implicit search. This fragile state of affairs was disrupted by a refactoring to the error reporting system in 725c5c9, after which the SOE crashed the compiler. This commit avoids recursively typechecking error typed trees.
* | Merge branch '2.11.x' into merge/2.11.x-to-2.12.x-20150624Jason Zaugg2015-06-241-2/+2
|\|
| * Fix another several typosMichał Pociecha2015-06-181-1/+1
| | | | | | | | | | | | I just used text search to check whether there are no more typos like these corrected by janekdb, and by the way fixed also some other ones which I saw.
| * Fix some typos (a-c)Janek Bogucki2015-06-181-1/+1
| |