Commit Graph

5910 Commits

Author SHA1 Message Date
Max Moiseev
02006f20bc Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-09 16:05:03 -08:00
Chris Lattner
2aaccee71c Revert "Progress towards nested generic typealiases." apparently broke an LLDB test.
This reverts commit 2896fbb586.
2016-03-09 09:26:27 -08:00
Argyrios Kyrtzidis
345d05e2e9 Introduce an internal attribute '@_show_in_interface' to be used in stdlib for underscored protocols that
should be shown in the interface.

Also switch the option and hide all underscored protocols by default, unless they are marked with the new attribute.
2016-03-08 23:30:58 -08:00
Chris Lattner
2896fbb586 Progress towards nested generic typealiases. They still don't work, but add a
(failing) testcase and progress towards supporting them.
2016-03-08 23:19:56 -08:00
Ben Langmuir
95fed2af61 Make canSatisfy static 2016-03-08 20:49:23 -08:00
Michael Gottesman
5f72810ad3 Add a range adaptor for std::count and update various trivial usages in the compiler to use this API instead. 2016-03-08 14:58:13 -08:00
Max Moiseev
1fae0d1325 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-08 12:48:48 -08:00
Chris Lattner
aacdf62e8b fix some whitespace issues noticed by Slava. 2016-03-07 23:11:45 -08:00
Chris Lattner
14b5b0a73f Resolve the QoI fixme's from the generic typealiases testcase, by specifying the
correct location for generic parameters that cannot be inferred.  The cases that
I thought were incorrectly rejected on first glance were actually correct.
2016-03-07 23:04:38 -08:00
Chris Lattner
fe9fe47b7e Implement support for generic typealiases. 2016-03-07 22:20:16 -08:00
Xi Ge
fb00e16a22 IDETypeChecking: Never replace archetypes with type variables when checking if two types eqaul. 2016-03-07 13:27:54 -08:00
Max Moiseev
7fe6916bf6 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-07 12:10:47 -08:00
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
790cd28326 Sema: Synthesize materializeForSet for properties defined in extensions when inferring default witnesses
A computed property might have a getter and setter but no materializeForSet;
synthesize the decl so that we can emit a witness thunk and reference it
from the default witness table.

Default witnesses for storage is not quite hooked up yet, so this is NFC
for now.
2016-03-05 02:25:47 -08:00
Slava Pestov
051af61e48 Sema: Make sure mutating setters defined in extensions cannot witness class protocol setter requirements
Otherwise, we hit a problem because the setter is called with self passed as a
(single retainable pointer) value, but the witness expects a self value passed
indirectly.

I hit this while testing other stuff related to resilient default implementations.
2016-03-05 02:25:46 -08:00
Chris Lattner
868a795566 Introduce a new class between TypeDecl and NominalTypeDecl named GenericTypeDecl.
This factors the DeclContext and generic signature behavior out of NTD, allowing
it to be reused in the future.  NFC.
2016-03-04 23:09:15 -08:00
Greg Titus
f6c59815ca Merge pull request #1504 from gregomni/sr-839
[SR-839][Sema] Better fixits for optional expressions
2016-03-04 18:16:36 -08:00
Doug Gregor
f189f8de72 Merge pull request #1495 from kballard/errortype-nserror-domain
[Sema] Synthesize domain of @objc enums properly
2016-03-04 15:22:47 -08:00
Xi Ge
8bde763ba2 IDETypeChecking: make parameter consistently pass reference. NFC 2016-03-03 18:02:13 -08:00
Xi Ge
326514e397 ModulePrinting: when printing synthesized extensions, avoid printing requirements that are known to be true. 2016-03-03 17:42:35 -08:00
Chris Lattner
c3c6beac72 Generalize the @noescape attribute to be a type attribute allowed
in arbitrary places.  This fixes a regression caught by SR-770 that
would otherwise be introduced by us removing automatic currying syntax,
it allows the use of @noescape on typealiases (resolving SR-824),
allows @noescape on nested function types (fixing rdar://19997680)
and allows @noescape to be used on local variables (fixing
rdar://19997577).

At this point, @noescape should stop being a decl attribute, but I'll bring
that up on swift-evolution.
2016-03-03 13:50:40 -08:00
Ben Langmuir
ceb1069199 Prefer original spelling of 'static' vs. 'class'
Try to match the original spelling of static/class in diagnostics and
when printing the AST. Also fixes cases with
PrintOptions.PrintImplicitAttrs = false, where we would just print
'class', which was not valid code.
2016-03-03 13:48:30 -08:00
Max Moiseev
cf4bafe9e3 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-03 13:22:03 -08:00
Slava Pestov
0b320a6d5b Sema: Implement DefaultWitnessChecker
Now that WitnessChecker is separate from ConformanceChecker, implement
a DefaultWitnessChecker subclass which performs default witness
resolution.

This populates the recently-added ProtocolDecl::DefaultWitnesses map.

Unlike ConformanceChecker, the DefaultWitnessChecker looks up the witness
in any protocol extensions of the protocol, matching the context archetypes
of the requirement against the witness.

For now, we infer default witnesses for all protocols, but don't do
anything with that information. An upcoming SILGen patch will start to
emit thunks and add tests.
2016-03-03 06:59:55 -08:00
Slava Pestov
681a613396 Sema: Correct handling of Self-derived types in default witness matching
We want to use the context archetypes of the requirement in this case.

NFC until the next patch lands.
2016-03-03 06:59:55 -08:00
Slava Pestov
36ef7eb0f0 Sema: Add ProtocolDecl::DefaultWitnesses
NFC for now, since the code that uses this is not checked in yet.
2016-03-02 22:12:21 -08:00
Slava Pestov
32bcbae50d Sema: Refactor some code in conformance checker for use in default witness matching
NFC for now.
2016-03-02 22:12:20 -08:00
Janek Spaderna
15f128bf4f [Sema] Don't crash on mutating getters on constants
With this change the base expression of the subscript (beeing already
revalidated earlier in the function) is passed to diagnoseUnviableLookupResults
to emit a fitting diagnostic.
2016-03-03 00:09:32 +01:00
Xi Ge
2f41702dd9 IDETypeChecking: Add two utilities canPossiblyConvertTo() and canPossiblyEqual(). 2016-03-02 12:28:02 -08:00
practicalswift
85da0acbff [gardening] Fix recently introduced typo: "an parameter" → "a parameter" 2016-03-02 09:34:52 +01:00
gregomni
098f8e0ebf [SR-839][Sema] Better fixits for optional expressions
In member ref expressions, if the base is optional, and the expected
expression result is either optional or unknown, suggest a fixit that
makes it into an optional chain expr rather than force unwrapping.

Since in many cases the actual fixit is emitted during diagnosis, and
thus, while type checking sub exprs with no contextual type specified
(so nothing to check for preferring optionality), we also need an
additional flag to pass down from FailureDiagnosis for whether we
prefer to fix as force unwrapping or optional chaining.

I attempted to do this same job via providing a convert type but
setting the ConvertTypeIsOnlyAHint flag on the type checker, but
unfortunately there are a lot of other moving parts that look at that
type, even if it is only supposed to be a hint, so an additional flag
to the CS ended up being cleaner.
2016-03-01 22:57:24 -08:00
Kevin Ballard
0ae11d4216 Merge pull request #1280 from kballard/better_rethrows_analysis
[Sema] Allow catch blocks to rethrow errors when safe
2016-03-01 22:43:50 -08:00
Xi Ge
4e8c8ffe07 IDETypeChecking: Add two utilities to IDETypeChecking.h which are isEqual() and LookUpTypeInContext(). NFC 2016-03-01 15:44:41 -08:00
Max Moiseev
859db53d87 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-01 12:56:26 -08:00
Joe Groff
e94fcace3f Sema: Infrastructure for DI-style initialization through behaviors.
Allow a behavior protocol to declare an `initStorage` implementation with a parameter. If we have an initializer expression, use `initStorage(initExpr)` to initialize the storage; otherwise, remember the storage declaration and its initializer. Definite
initialization will have to use these to insert the initialization operation for the behavior property at the right place.
2016-03-01 11:51:25 -08:00
Joe Groff
a4fa786a6f Handle a parameter to property behaviors.
For prototyping purposes, we only support a single trailing closure parameter to begin with.
2016-03-01 11:51:25 -08:00
Kevin Ballard
736b618bb4 [Sema] Synthesize domain of @objc enums properly
The domain constant generated by PrintAsObjC always has the value
`"ModuleName.TypeName"`, but the actual value of the derived
`_NSErrorDomain` constant from the `_BridgedNSError` protocol was
implemented as `"\(self)"`. This meant that any time that the default
string representation of the enum type did not match the format
`"ModuleName.TypeName"`, the generated domain constant did not match the
actual error domain. This could happen when compiling programs at the
command-line that define the error (as the module name is typically
omitted there), or if the enum was nested inside another @objc
declaration (as the qualified type name there includes the parent type
declaration).

Fixes SR-700.
2016-03-01 00:10:15 -08:00
gregomni
545a3f913d [SR-832][Sema] Fix for function type args passed to @autoclosure params
Passing a function type to an @autoclosure param would always fail to
type check because of the attempt to decompose the parallel structure
of the two (both being functions). In this case, though, we don’t want
to do any such thing, we want to allow the ExprRewriter to explicitly
insert an AutoClosureExpr in coerceToType, as it would do with any
other non-function arg type.
2016-02-29 23:43:37 -08:00
Max Moiseev
a49dab6bf8 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-29 12:08:52 -08:00
Slava Pestov
bc1f236778 Sema: Split off WitnessChecker from ConformanceChecker, NFC
With resilient protocols, we have to emit a default witness table
for SILGen, independent of any one conformance.

This patch introduces a WitnessChecker base class, which contains
parts of the ConformanceChecker responsible for lookup and matching
of witnesses.

A default witness lookup will be performed inside the protocol's
DeclContext in order to find extension methods, without opening any
associated types.
2016-02-28 23:52:35 -08:00
Slava Pestov
6812b6926b Sema: Cleanups and minor fixes for protocol 'Self' types
We had four duplicated implementations of checking how a protocol
requirement uses 'Self', all slightly wrong or incomplete:

- When deciding if the protocol type can be used as an existential.
  This one would just ignore 'Self' in the return type of a method
  completely, which was incorrect for cases where 'Self' is
  contravariant but part of the return value, for example:

  func foo() -> (Self -> ())

- When deciding if a member access can be performed on an existential
  value. This is distinct from the former, because the member may
  have been defined in a protocol extension, in which case it cannot
  be used even if the protocol type can be used as an existential.
  Unfortunately, this implementation was overly conservative, and
  would reject uses of 'Self' where Sema could in fact erase the
  existential type, for example:

  func foo() -> Self??
  func foo() -> Self.Type
  func foo() -> (Self, Self)

  This function handled function return types correctly, effectively
  plugging the leak in the previous code. It did lead to inconsistent
  behavior with protocols that had contravariant Self in requirements
  though; sometimes we would diagnose uses of the existential type,
  other times we would only complain about specific members.

- When deciding if a method in a non-final class can model a protocol
  requirement. This one was the most elaborate one, but here
  contravariance and uses of associated types are actually okay, so
  it was written to pick up covariant 'Self' only. However, it also
  did not handle metatypes and tuples.

- When opening the type of member of an existential, we would check
  if the return value was 'Self' or an optional of 'Self', but again
  this check was too conservative, so after the previous three were
  fixed, we could reference members on existentials that did not
  have a correct opened type.

Now, these have been combined into one check. To fix some crashes,
Sema's implementation of erasing existentials now relies on
coerceToType() instead of hand-rolling a few coercions of its own,
and wrapping the rest in CovariantFunctionConversionExpr, which
didn't make much sense if the result was not a function type.

SILGen still does not support function type conversions where an
existential return value is being erased; these would silently
miscompile before, but crash with an assertion now, because they
are correctly modeled as a FunctionConversionExpr, and not
CovariantFunctionConversionExpr.
2016-02-28 23:52:35 -08:00
Chris Lattner
19b6ab8408 fix <rdar://problem/24251022> Swift 2: Bad Diagnostic Message When Adding Different Integer Types
Previously we would produce:

t.swift:3:3: error: binary operator '+=' cannot be applied to operands of type 'Int' and '_'
a += a + b
~ ^  ~~~~~

with a candidate set to follow.  Now we properly match up the inout/lvalue type and produce
the following more specific diagnostic:

t.swift:3:10: error: cannot convert value of type 'UInt32' to expected argument type 'Int'
a += a + b
         ^

pointing the the "b".
2016-02-28 20:52:33 -08:00
Chris Lattner
0a8db5daf6 Fix <rdar://problem/23899799> Simple closure expression crashed SourceKit
which is a straight-forward logic bug that the static analyzer would have caught.
2016-02-27 22:41:00 -08:00
Xi Ge
b109145dfc [TypeChecker] Add null check to fix a crash. 2016-02-27 21:38:55 -08:00
Andrew Trick
445cdff829 Merge pull request #1468 from atrick/attr-kind
Move enums into AttrKind.h.
2016-02-27 21:20:11 -08:00
Dmitri Gribenko
a9f8d97d3e Replace 'unsigned int' with 'unsigned'
'unsigned' is more idiomatic in LLVM style.
2016-02-27 16:20:27 -08:00
practicalswift
34188788a1 [gardening] Sort file listings in CMakeLists.txt files 2016-02-27 19:50:30 +01:00
Kevin Ballard
88a45b6bd0 [Sema][SR-681] Allow catch blocks to rethrow errors when safe
A catch block can only be entered if the do block threw an error. In a
rethrows function, if the do block throws an error only under rethrows
conditions, then the catch block can only be entered under rethrows
conditions, which means the catch block can unconditionally throw and
it's still safe.

This enables code that looks like

```swift
func foo(f: () throws -> Void) rethrows {
    do {
        try f()
    } catch is SomeError {
        throw OtherError()
    }
}
```
2016-02-26 23:19:13 -08:00
Andrew Trick
ff02652108 Move enums into AttrKind.h.
This reorganization allows adding attributes that refer to types.
I need this for a @_specialize attribute with a type list.

PrintOptions.h and other headers depend on these enums. But Attr.h
defines a lot of classes that almost never need to be included.
2016-02-26 21:10:22 -08:00
Joe Pamer
927d7f254d When analyzing an expression for linked sub-expressions, consider each component of an IfExpr separately.
This fixes rdar://problem/24707022
2016-02-25 17:18:07 -08:00