Commit Graph

179 Commits

Author SHA1 Message Date
Chris Lattner
8ad365b30e Rework ReplaceDependentTypes to call into applyUnboundGenericArguments
to remap an unbound generic type into a bound generic type.  This shares
a common codepath to make way for future progress.

This exposed a case where we'd produce invalid ASTs in some testcases
in the validation tests because started asserting on the invalid code.
Solve this by detecting the problem and producing a circularity error.

Overall, NFC.
2016-03-06 22:50:53 -08:00
Slava Pestov
5e020e62da Nuke more trivial usages of getDeclaredTypeInContext(), NFC 2016-02-16 23:08:57 -08:00
gregomni
37d05ea0dc Improved handling of mixed lvalues & rvalues in tuple exprs
My previous commit here didn’t work correctly for nested tuples, both
because it didn’t recurse into them to propagate access kind correctly
and because an outer TupleIndex overload (when indexing into the nested
tuple) could still be expecting an lvalue type.

This fix is much better. ConstraintSystem::resolveOverload now
correctly always expects rvalue types from rvalue tuples. And during
applyMemberRefExpr, if the overload expects an rvalue but the tuple
contains lvalues, coerceToType() correctly does any recursive munging
of the tuple expr required.
2016-02-15 20:59:53 -08:00
Daniel Duan
efe230774b [AST] rename some isXXX methods to getAsXXX
There's a group of methods in `DeclContext` with names that start with *is*,
such as `isClassOrClassExtensionContext()`. These names suggests a boolean
return value, while the methods actually return a type declaration. This
patch replaces the *is* prefix with *getAs* to better reflect their interface.
2016-02-11 16:23:40 -08:00
Slava Pestov
27da265abb Refactor some random usages of contextual types, NFC 2016-01-27 23:22:33 -08:00
Joe Pamer
762fc2a0ab - Do not update the work list whilst contracting edges.
- Temporarily disable contraction of conversion constraints.
2016-01-22 12:34:33 -08:00
John McCall
1f3b3142b4 Distinguish conformance and superclass generic requirements.
As part of this, use a different enum for parsed generic requirements.

NFC except that I noticed that ASTWalker wasn't visiting the second
type in a conformance constraint; fixing this seems to have no effect
beyond producing better IDE annotations.
2016-01-11 16:07:37 -08:00
Jacob Bandes-Storch
b97cb48c2b [AST] Fix inconsistent handling of generic args & params during BoundGenericType::getSubstitutions
A decl’s full GenericSignature is set during validateGenericTypeSignature().

Then during ConstraintSystem::openTypes(), in ReplaceDependentTypes, the GenericArgs list is built from the generic signature (via getGenericParamTypes()) and passed into a new BoundGenericType.

In BoundGenericType::getSubstitutions(), the GenericArgs are assumed to match getGenericParamsOfContext()->getParams().

However, in reality, the GenericArgs include all levels of generic args, whereas getGenericParamsOfContext() are the params of the innermost context only, so the params array is accessed past its end.

This commit changes NominalTypeDecl::getGenericParamTypes() to return the innermost params, in order to match the output of BoundGenericType::getGenericArgs(). For clarity and to hopefully prevent future confusion, we also rename getGenericParamTypes() to getInnermostGenericParamTypes().
2016-01-09 02:19:28 -08:00
Doug Gregor
2a626b40ae [Constraint system] Remove some unused code. NFC
We don't do substitutions based on archetypes here any more.
2016-01-03 22:28:01 -08:00
Doug Gregor
e1fe27bd5f [Constraint system] Eliminate DependentTypeOpener. NFC
Eliminate the last client of DependentTypeOpener,
RequirementTypeOpener, which tracked the opened Self type when doing
witness/requirement matching and substituted in the known type
witnesses for that protocol. It had a bunch of dead logic hanging
around from the days where we used the constraint system to deduce
type witnesses. Now, a simple substitution suffices.

With its last client gone, remove DependentTypeOpener as well.
2016-01-02 21:51:22 -08:00
Doug Gregor
75cb941440 [Constraint System] Kill the ArchetypeOpener; bind type variables instead. NFC
The ArchetypeOpener was used only to replace dependent types with
archetypes (or concrete types) within the opening context. We can do
the same simply by letting the constraint system create type variables
and then binding those type variables to the appropriate
archetypes/concrete types in that context.

Eliminate the two DependentTypeOpener entry points that were only used
by the ArchetypeOpener.
2016-01-02 15:28:12 -08:00
Chris Lattner
a30ae2bf55 Merge pull request #836 from zachpanz88/new-year
Update copyright date
2015-12-31 19:36:14 -08:00
Chris Lattner
7daaa22d93 Completely reimplement/redesign the AST representation of parameters.
Parameters (to methods, initializers, accessors, subscripts, etc) have always been represented
as Pattern's (of a particular sort), stemming from an early design direction that was abandoned.
Being built on top of patterns leads to patterns being overly complicated (e.g. tuple patterns
have to have varargs and default parameters) and make working on parameter lists complicated
and error prone.  This might have been ok in 2015, but there is no way we can live like this in
2016.

Instead of using Patterns, carve out a new ParameterList and Parameter type to represent all the
parameter specific stuff.  This simplifies many things and allows a lot of simplifications.
Unfortunately, I wasn't able to do this very incrementally, so this is a huge patch.  The good
news is that it erases a ton of code, and the technical debt that went with it.  Ignoring test
suite changes, we have:
   77 files changed, 2359 insertions(+), 3221 deletions(-)

This patch also makes a bunch of wierd things dead, but I'll sweep those out in follow-on
patches.

Fixes <rdar://problem/22846558> No code completions in Foo( when Foo has error type
Fixes <rdar://problem/24026538> Slight regression in generated header, which I filed to go with 3a23d75.

Fixes an overloading bug involving default arguments and curried functions (see the diff to
Constraints/diagnostics.swift, which we now correctly accept).

Fixes cases where problems with parameters would get emitted multiple times, e.g. in the
test/Parse/subscripting.swift testcase.

The source range for ParamDecl now includes its type, which permutes some of the IDE / SourceModel tests
(for the better, I think).

Eliminates the bogus "type annotation missing in pattern" error message when a type isn't
specified for a parameter (see test/decl/func/functions.swift).

This now consistently parenthesizes argument lists in function types, which leads to many diffs in the
SILGen tests among others.

This does break the "sibling indentation" test in SourceKit/CodeFormat/indent-sibling.swift, and
I haven't been able to figure it out.  Given that this is experimental functionality anyway,
I'm just XFAILing the test for now.  i'll look at it separately from this mongo diff.
2015-12-31 19:24:46 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Slava Pestov
d6ea5d8717 Sema: Chain all generic parameter lists
Previously, methods on DeclContext for getting generic parameters
and signatures did not walk up from type contexts to function
contexts, or function contexts to function contexts.

Presumably this is because SIL doesn't completely support nested
generics yet, instead only handling these two special cases:

- non-generic local function inside generic function
- generic method inside generic type

For local functions nested inside generic functions, SIL expects
the closure to not have an interface type or generic signature,
even if the contextual type signature contains archetypes.
This should probably be revisited some day.

Recall that these cases are explicitly rejected by Sema diagnostics
because they lack SIL support:

- generic function inside generic function
- generic type inside generic function

After the previous patches in this series, it becomes possible to
construct types that are the same as before for the supported uses of
nested generics, while introducing a more self-consistent conceptual
model for the unsupported cases.

Some new tests show we generate diagnotics in various cases that
used to crash.

The conceptual model might still not be completely right, and of
course SIL, IRGen and runtime support is still missing.
2015-12-16 11:32:56 -08:00
Slava Pestov
74e575e638 Sema: Add DeclContext::getGenericTypeContextDepth()
Now that generic signatures of types include generic parameters
introduced by outer generic functions, we need to know to skip
them when forming bound generic types or substitutions.

Add a function that computes the depth of the innermost generic
context that is not a generic type context.
2015-12-15 22:59:38 -08:00
Slava Pestov
f43a5fec87 Sema: Remove dead code, NFC 2015-12-15 22:59:38 -08:00
Doug Gregor
881484bfd2 Clean up deduplication of dynamic AnyObject lookup results.
Use ObjCSelector rather than some seriously dodgy string
manipulation. Should be NFC.
2015-12-14 15:29:35 -08:00
Ben Langmuir
4082355244 Split KnownProtocolKind enum case from protocol name
This avoids us using reserved identifiers as the enum case names of all
our underscored protocols like _ObjectiveCBridgeable. I used the
convention PROTOCOL_WITH_NAME to mirror how the known identifiers work.

Swift SVN r32924
2015-10-27 23:10:36 +00:00
Ben Langmuir
2d465b8a32 Remove unused literal convertible protocol descriptions
Formerly used in creating friendly type-checker diagnostics, these seem
to be dead now.

Swift SVN r32921
2015-10-27 21:52:53 +00:00
Dmitri Hrybenko
2e51d23875 Un-ifdef object literals
Swift SVN r32880
2015-10-25 07:50:53 +00:00
Chris Willmore
49e5130103 Allow inout closure param type to be inferred from context or usage.
Introduce a new constraint kind, BindParam, which relates the type of a
function parameter to the type of a reference to it from within the
function body. If the param type is an inout type, the ref type is an
lvalue type with the same underlying object type; otherwise the two
types must be the same. This prevents DeclRefExprs from being inferred
to have inout type in some cases.

<rdar://problem/15998821> Fail to infer types for closure that takes an inout argument

Swift SVN r32183
2015-09-23 18:46:12 +00:00
Chris Lattner
16a51639ef Fix <rdar://problem/22519983> QoI: Weird error when failing to infer archetype
Introduce a new "OpenedGeneric" locator for when openGeneric opens a generic
decl into a plethora of constraints, and use this in CSDiags to distinguish 
whether a constraint refers to an Expr as a whole or an "aspect" of the constraint.

Use that information in FailureDiagnosis::diagnoseGeneralConversionFailure
to know whether (as a fallback) we can correctly re-typecheck an entire expr 
to obtain a missing type.  If we are talking about an aspect of the expr, then
this clearly won't work.

The upshot of this is that where we previously compiled the testcase in 22519983
to:

y.swift:31:9: error: type '(inout _) -> Bool' does not conform to protocol 'RawRepresentable'
let a = safeAssign
        ^

we now produce the somewhat more useful:
y.swift:31:9: error: argument for generic parameter 'T' could not be inferred
let a = safeAssign
        ^
y.swift:27:6: note: in call to function 'safeAssign'
func safeAssign<T: RawRepresentable>(inout lhs: T) -> Bool {
     ^



Swift SVN r31620
2015-09-02 05:15:22 +00:00
Slava Pestov
7e7191478a AST: Split off a new replaceSelfParameterType() method and clean up some code, NFC
Swift SVN r31478
2015-08-26 04:21:12 +00:00
Chris Willmore
51f08e0285 Add FileReference object literals and _FileReferenceLiteralConvertible protocol.
<rdar://problem/21781451> Add file literal to Swift

Swift SVN r31232
2015-08-13 22:38:55 +00:00
Jordan Rose
953424072e Guard "object literals" feature with SWIFT_ENABLE_OBJECT_LITERALS.
This is not a feature we're releasing at the moment, so provide a way
to turn it off.

rdar://problem/21935551

Swift SVN r30966
2015-08-04 00:16:52 +00:00
Joe Pamer
d4e165688b When opening the type of a member reference for a given overload, and the context of the self type is a class-constrained existential, do not automatically wrap the self type in an inout. Doing so leads to mismatched expectations during constraint application, which will result in a compiler crash. (rdar://problem/22012606)
Swift SVN r30724
2015-07-28 21:12:21 +00:00
Chris Lattner
a8d9aec957 revert r30641, it wasn't correct. We produce better diagnostics for the
cases I was worried about anyway now.


Swift SVN r30651
2015-07-26 05:33:33 +00:00
Chris Lattner
babcbe0c76 fix a bug in the type checker handling references to instance methods with metatype
bases.  Consider this example (reduced from NameBinding/name_lookup.swift):

class ThisBase1 {
  func baseFunc0() {}
}

class ThisDerived1 : ThisBase1 {
  class func staticTestSelf1(a : ThisBase1) {
    let x = self.baseFunc0
    x(a)()
 }
}

The type checker was incorrectly blasting over the self type of the 'self.baseFunc0'
reference, giving 'x' a type of "ThisDerived -> () -> ()" instead of the correct
type of "ThisBase -> () -> ()" and rejecting the testcase.

I'm not confident that this is the right fix, review greatly appreciated!




Swift SVN r30641
2015-07-25 23:27:43 +00:00
Chris Lattner
0001dc27bb remove support for the experiemental "character literals" feature.
Swift SVN r30509
2015-07-22 22:35:19 +00:00
Devin Coughlin
c1caddae62 Remove experimental support for treating unavailable symbols as optional.
This has always been off by default and is a language direction we have decided not to
pursue.

Swift SVN r30355
2015-07-18 01:56:25 +00:00
Slava Pestov
fd141bead9 Sema: Remove unused inExpression parameter from ASTContext::getBridgedToObjC(), NFC
Progress on <rdar://problem/21215099>.

Swift SVN r29968
2015-07-08 06:49:27 +00:00
Doug Gregor
3023a710fc Split TypeBase::isDependentType() into isTypeParameter() and hasTypeParameter().
The isDependentType() query is woefully misunderstood. Some places
seem to want it to mean "a generic type parameter of dependent member
type", which corresponds to what is effectively a type parameter in
the language, while others want it to mean "contains a type parameter
anywhere in the type". Tease out these two meanings in
isTypeParameter() and hasTypeParameter(), respectively, and sort out
the callers.

Swift SVN r29945
2015-07-07 21:20:54 +00:00
Doug Gregor
d44651f366 Allow type witness resolution for unqualified lookups.
Fixes rdar://problem/21512599.

Swift SVN r29894
2015-07-02 18:29:13 +00:00
Doug Gregor
2038a8e837 Handle nested types of concrete types in the constraint solver.
... I have no idea why we were foolishly mapping such things to their
parent type, which made no sense whatsoever. Fixes
rdar://problem/21621421.

Swift SVN r29882
2015-07-02 04:45:15 +00:00
Slava Pestov
ee07ddc2bf Sema: Fix crash calling protocol extension methods returning Self on existential
In r26737, Sema was changed to not wrap Self occurring in a protocol
extension in a DynamicSelf. The commit message was rather terse but
I believe this is because the metadata for Self is bound to the static
base type, not the runtime base type.

However, we still need to substitute Self in the return type for the
static base type in the case where the base is an existential,
otherwise we get an open existential type leaking out.

Also remove the default argument for replaceCovariantResultType(),
every call site passed in a value and it seems bad to omit it on
accident.

Fixes <rdar://problem/21433694>.

Swift SVN r29802
2015-06-30 05:36:00 +00:00
Doug Gregor
2e42a61486 Drop the correct 'Self' constraint for a protocol member.
We were dropping *all* constraints on the Self type when forming
constraints from a protocol member, which included the extra
constraints provided by constrained extensions. Only drop the actual
protocol in which the member occurs (or the protocol extended by the
containing extension).

Fixes rdar://problem/21401180 and the 8 dupes I've found so far.

Swift SVN r29708
2015-06-25 22:42:46 +00:00
Slava Pestov
665b870f7f Sema: Fix yet another crash when accessing nested types of an existential
We diagnose usage of invalid existential types but we might still
try to compute substitutions. Just whip up an ErrorType instead.

Fixes <rdar://problem/16803384>.

Swift SVN r29567
2015-06-23 06:39:22 +00:00
Chris Willmore
0e05470e5f Take care to preserve parentheses when opening generic types. Consider
lvalues when compiling list of partial-match overloads in diagnosis.
(This is a reapplication of commits r29462 and r29469.)

Also, fix the following tests:

    stdlib/FixedPointDiagnostics.swift.gyb
    stdlib/NumericDiagnostics.swift.gyb

<rdar://problem/17875634> can't append to array of tuples

Swift SVN r29493
2015-06-18 18:58:49 +00:00
Mark Lacey
f7ce9aab07 Revert "Take care to preserve parentheses when opening generic types. Consider lvalues when compiling list of partial-match overloads in diagnosis."
This reverts commit r29462 because it looks like it breaks the following
tests:
    Swift :: stdlib/FixedPointDiagnostics.swift.gyb
    Swift :: stdlib/NumericDiagnostics.swift.gyb

Swift SVN r29484
2015-06-18 06:51:34 +00:00
Chris Willmore
8d4f70580f Take care to preserve parentheses when opening generic types. Consider
lvalues when compiling list of partial-match overloads in diagnosis.

<rdar://problem/17875634> can't append to array of tuples

Swift SVN r29462
2015-06-17 22:59:35 +00:00
Slava Pestov
72c44b4c84 Sema: Fix getTypeOfMemberReference() for unbound instance methods
Always rewrite the Self type to the base type. Previously we only
did it if the method had a dynamic self return. This caused some
confusing behavior in this case:

class C {
  func m1() {}
  func m2() -> Self {}
}
class D : C {}

The types of D.m1 and D.m2 are:
- D.m1: C -> () -> ()
- D.m2: D -> () -> D

For protocols, this also meant that the type of an instance method
reference P.f had an open existential that could "leak out" of
the OpenExistentialExpr. Now, P.f will have type P -> ... -> ...,
however using such a reference still crashes, just in SILGen
instead of Sema, because we don't generate the right thunks yet.

Progress on <rdar://problem/21289579>.

Swift SVN r29447
2015-06-17 07:42:43 +00:00
Slava Pestov
7319a97ab4 Sema: Rewrite witness method calls as ApplyExpr + DeclRefExpr
Special-casing these as MemberRefExprs created an asymmetry
where unbound archetype instance methods (<T : P> T.f) could
not be represented. Treating class and protocol methods
uniformly also eliminates a handful of special cases around
MemberRefExpr.

SILGen's RValue and call emission peepholes now have to know
about DeclRefExprs that point to protocol methods.

Finally, generalize the diagnostic for partially applied
mutating methods to any partially applied function with an
inout parameter, since this is not supported.

Fixes <rdar://problem/20564672>.

Swift SVN r29298
2015-06-04 15:57:58 +00:00
Chris Willmore
1efc1430b1 Followup to r28883: pack SavedTypeVariableBinding::Options with TypeVar
and do less work in setMustBeMaterializableRecursive().

Swift SVN r28890
2015-05-21 22:40:10 +00:00
Chris Willmore
df9136e0e0 When unifying two type variables, make sure the new representative type
variable has the must-be-materializable bit set if the old one does.
When assigning a fixed type to a type variable that must be
materializable, transfer the bit to any type variables within the fixed
type, as appropriate.

Add Options field to SavedTypeVariableBinding to save/restore type
variable options during solution.

<rdar://problem/21026806> Propagate MustBeMaterializable bit among type variables appropriately

Swift SVN r28883
2015-05-21 19:53:48 +00:00
Chris Willmore
5905d07a08 Don't bind generic type parameters to non-materializable types.
Add a new option, TVO_MustBeMaterializable, to
TypeVariableType::Implementation, and set it for type variables
resulting from opening a generic type. This solution isn't complete (we
don't yet copy the non-materializable bit on unification of type
variables, and it's possible to bind a must-be-materializable type
variable to a type with type variables that later get bound to
non-materializable types) but it addresses all reported crashes for this
issue.

<rdar://problem/20807269> Crash in non-materializable type

Swift SVN r28792
2015-05-19 21:50:26 +00:00
David Farler
dbd78643ce Build fix: Adventure triggers nullptr dereference
Fix a nullptr dereference when looking for a base expression
in a member access. Remove use of Optional<T*>, it wasn't providing
any value versus nullptr checking.

Swift SVN r28648
2015-05-15 23:36:33 +00:00
David Farler
80571cf916 Check base expr when determining mutability of a member
When in an initializer, we allow setting into immutable properties
provided that the type of base in `base.member` matches that of that
initializer's containing type. This was an approximation for allowing
full access into `self` during initialization but this doesn't work when
passing in a different struct of the same type because that struct
should be still be immutable.

Check whether the base of the member access is the implicit self
parameter of the initializer before allowing mutation.

rdar://problem/19814302

Swift SVN r28634
2015-05-15 18:48:54 +00:00
Doug Gregor
2f0c1b151e Use the archetype for unqualified lookup within protocols and extensions thereof.
When performing unqualified lookup within a type context (or method
thereof) that is a protocol or a protocol extension, use the Self
archetype of the protocol or extension so we look in types implied by
the requirements as well. Part of rdar://problem/20509152, fixing the
example provided in rdar://problem/20694545.

Swift SVN r28363
2015-05-09 03:42:16 +00:00
Doug Gregor
2653a6569b Eliminate ModuleExpr; DeclRefExpr is good enough for anyone.
Swift SVN r28285
2015-05-07 21:10:53 +00:00