Commit Graph

696 Commits

Author SHA1 Message Date
Doug Gregor
a30ca2a60d Replace bool parameter to TypeChecker::conformsToProtocol() with an option set.
NFC; we can extend this option set more readily later.

Swift SVN r27894
2015-04-29 00:08:22 +00:00
Joe Pamer
006c182c13 Begin inferring throwing function types for closures. (There's more work to do here - hence the thin tests - but I need to investigate a couple of sema bugs before moving forward.)
Swift SVN r27438
2015-04-17 19:06:52 +00:00
Doug Gregor
41ae48b22e Start parsing 'throws' on initializers.
Introduce basic validation for throwing @objc initializers, e.g., a
failable @objc initializer cannot also be throwing. However,
Objective-C selector computation is broken.

Swift SVN r27292
2015-04-14 22:52:29 +00:00
Joe Groff
c0a2994564 AST: Start printing function types with @convention instead of old attributes.
And update tests to match.

Swift SVN r27262
2015-04-13 22:51:34 +00:00
Doug Gregor
5406878a7f Don't synthesize materializeForSet for members of protocol extensions.
They aren't dynamically dispatched contexts.

Swift SVN r27067
2015-04-07 06:01:17 +00:00
Devin Coughlin
bbdcf580ee Sema: Add @availability attributes when synthesizing materializeForSet accessors.
Synthesize implicit @availability attributes to make sure that a synthesized
materializeForSet accessor is available enough to access the underlying storage and its
getter and setter.

These synthesized attributes could trigger redundant diagnostics when a subclass gives
overriding getters or setters non-contravariant availability. We detect when this
happens and suppress the redundant diagnostics.

This commit also improves availability diagnostics in synthesized code. We now respect
synthesized @availability annotations on containing DeclContexts when determining the
potential OS versions that could be executed at invalid SourceLocations.

Swift SVN r27002
2015-04-05 03:02:20 +00:00
Joe Pamer
37d65f4202 Start tracking SourceLoc data for function "throws" annotations and eliminate the (now unnecessary) "Throws" bit from AbstractFunctionDecls.
Swift SVN r26955
2015-04-03 21:45:30 +00:00
Chris Lattner
79ed57f9f2 standardize naming of tuples and tuple patterns on "elements".
Previously some parts of the compiler referred to them as "fields",
and most referred to them as "elements".  Use the more generic 'elements'
nomenclature because that's what we refer to other things in the compiler
(e.g. the elements of a bracestmt).

At the same time, make the API better by providing "getElement" consistently
and using it, instead of getElements()[i].

NFC.



Swift SVN r26894
2015-04-02 20:23:49 +00:00
Chris Lattner
49d76f62d0 when setting up implicit memberwise ctors and accessors on subscripts, make sure to install
exposed keyword arguments into the TuplePatterns labels.

Switch tuple conversion to use tuplepattern labels instead of parameter labels.


Swift SVN r26861
2015-04-02 05:05:27 +00:00
Chris Lattner
9bdb4ea395 inline method into its only caller, NFC.
Swift SVN r26803
2015-04-01 05:29:34 +00:00
Devin Coughlin
8a8d53b10c Sema: Infer required availability for synthesized designated initializer overrides.
When synthesizing a designated initializer override, we now ensure that the synthesized
initializer has the same availability as the initializer it is overriding.

Swift SVN r26732
2015-03-30 22:06:06 +00:00
Doug Gregor
3d77855b31 Start allowing extensions of protocol types.
Remove the semantic restrictions that prohibited extensions of
protocol types, and start making some systematic changes so that
protocol extensions start to make sense:
  - Replace a lot of occurrences of isa<ProtocolDecl> and
    dyn_cast<ProtocolDecl> on DeclContexts to use the new
    DeclContext::isProtocolOrProtocolExtensionContext(), where we want
    that behavior to apply equally to protocols and protocol extensions.
  - Eliminate ProtocolDecl::getSelf() in favor of
    DeclContext::getProtocolSelf(), which produces the appropriate
    generic type parameter for the 'Self' of a protocol or protocol
    extension. Update all of the callers of ProtocolDecl::getSelf()
    appropriately.
  - Update extension validation to appropriately form generic
    parameter lists for protocol extensions.
  - Methods in protocol extensions always use the witnesscc calling
  convention.

At this point, we can type check and SILGen very basic definitions of
protocol extensions with methods that can call protocol requirements,
generic free functions, and other methods within the same protocol
extension.

Regresses four compiler crashers but improves three compiler
crashers... we'll call that "progress"; the four regressions all hit
the same assertion in the constraint system that will likely be
addressed as protocol extensions starts working.

Swift SVN r26579
2015-03-26 04:50:51 +00:00
Jordan Rose
f74bc7122c Split getAccessibility() into getFormalAccess() and getEffectiveAccess().
Currently a no-op, but effective access for entities within the current
module will soon need to take testability into account. This declaration:

  internal func foo() {}

has a formal access of 'internal', but an effective access of 'public' if
we're in a testable mode.

Part of rdar://problem/17732115 (testability)

Swift SVN r26472
2015-03-24 02:16:58 +00:00
Chris Lattner
59c22383fb Rework PatternBindingDecl to maintain a list of pattern/initexpr pairs inside of it.
Previously, a multi-pattern var/let decl like:
  var x = 4, y = 17

would produce two pattern binding decls (one for x=4 one for y=17).  This is convenient
in some ways, but is bad for source reproducibility from the ASTs (see, e.g. the improvements
in test/IDE/structure.swift and test/decl/inherit/initializer.swift).

The hardest part of this change was to get parseDeclVar to set up the AST in a way
compatible with our existing assumptions. I ended up with an approach that forms PBDs in 
more erroneous cases than before.  One downside of this is that we now produce a spurious
  "type annotation missing in pattern"
diagnostic in some cases.  I'll take care of that in a follow-on patch.





Swift SVN r26224
2015-03-17 16:14:18 +00:00
Chris Lattner
01f3e81aa5 Work on VarDecl:
- Rename getParentPattern() -> getParentPatternBinding(), since
   it returns the pattern binding, not the pattern.
 - Introduce new getParentPattern()/getParentInitializer() methods,
   covering the most common uses of getParentPatternBinding().

NFC.



Swift SVN r26175
2015-03-16 01:54:20 +00:00
Chris Lattner
8521916b86 change PatternBindingDecl to be created with a static "create" method instead
of using its ctor directly.  NFC, this is in prep for other changes coming.


Swift SVN r26174
2015-03-16 00:44:52 +00:00
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Denis Vnukov
9e76e2ba50 Minor (swift migrator related fixes):
Corrected several places where compiler generated AST nodes were not properly 
marked as implicit.

For interpolated strings also fixed string segment locations and made sure 
the first and last segments are preserved in AST even if they are empty.



Swift SVN r25983
2015-03-11 18:08:36 +00:00
Doug Gregor
2edae8c57d Add trivial accessors to static properties (as well as instance properties).
This is a better fix for rdar://problem/19941580: by providing trivial
accessors for all static properties, we have them there for any other
type checking that might need them, including the Objective-C selector
checking from the radar.

Swift SVN r25730
2015-03-04 00:30:46 +00:00
Jordan Rose
cee28189a2 Add a BodyKind for implicitly-synthesized memberwise initializers.
...rather than just assuming any initializer without a body that makes it
to SILGen is a memberwise initializer.

In the long term we want SILGen to stop handling these initializers, at
which point we can see if it makes sense to remove this body kind.

No intended functionality change.

Swift SVN r25723
2015-03-03 23:22:21 +00:00
Jordan Rose
511f19c6de Mark inherited required initializers as required.
rdar://problem/19793782 (and its dups)

Swift SVN r25704
2015-03-03 03:28:40 +00:00
Jordan Rose
1d03915dfc Make sure static vars in protocols don't get mutating setters.
Static members are never mutating, and having them be mutating in protocols
resulted in requirement mismatches.

rdar://problem/19887250

Swift SVN r25593
2015-02-27 04:54:01 +00:00
Xi Ge
7f668e7b49 Add isUserAccessible() in Decl to show whether swift users should know such decl exists. For instance, a.storage for lazy var a is a inaccessible decl. An implicit decl is not necessarily inaccessible, for instance, self.
Fixing rdar://18760063

Swift SVN r25556
2015-02-26 19:16:54 +00:00
Chris Lattner
accfcaa80f revert r25545, a different approach will be needed.
Swift SVN r25554
2015-02-26 18:18:30 +00:00
Chris Lattner
60e9b9dbbb Revert r25551, more investigation is required.
Swift SVN r25552
2015-02-26 18:04:08 +00:00
Chris Lattner
5cfc38f13d there is no need to validate stored properties when synthesizing a memberwise
initializer, it has already been done now.


Swift SVN r25551
2015-02-26 17:50:51 +00:00
Chris Lattner
c74848cf30 Rework how synthesis of initializers is performed. Before, we would do it before
type checking any of the members of the type being analyzed, which leads to the
really exciting task of generating (e.g.) a memberwise initializer when you don't
know what the types of the members are.

Instead, synthesize these after the members are checked, which makes the code flow
much more like the other code synthesis stuff we do, and paves the way for future
improvements. NFC except one diagnostic in an error case (due to an order-of-checking 
change).

This resolves <rdar://problem/16921173> implicit constructor synthesization works without validating the members of the type


Swift SVN r25545
2015-02-26 07:10:17 +00:00
Chris Lattner
56bf85cc5f fix <rdar://problem/19874152> Swift 1.2 struct memberwise initializer violates new sanctity of previously set let property
If a non-static 'let' field in a struct has an initializer, its implicitly generated
memberwise initializer should not override that.  Handle this by having the memberwise initializer
and default initializer use the initial value of the property instead of an argument to the init.

This would have been a lot easier if sema was synthesizing the ctor bodies instead of SILGen,
but here it is.  The missing case is narrow (let (x,y) = (1,2)) and for now we just disable
implicit synthesization of the incorrect case.



Swift SVN r25424
2015-02-20 07:26:57 +00:00
David Farler
a079cbc49a Generate trivial accessors for static stored properties in objc classes
Expose static stored properties in @objc classes as trivial class
methods.

static let i: T => +(T)i
static var i: T => +(T)i and +(void)setI:
static var k T { get set } => No change.

Fixes rdar://problem/19784053

Swift SVN r25152
2015-02-11 00:44:59 +00:00
Joe Groff
b0fabcef7b Sema: Force dynamic materializeForSet accessors to be statically dispatched.
The materializeForSet accessor for a `dynamic` property needs to dynamically invoke the getter and setter of the property in order to allow for runtime modification, so it doesn't need to be dynamically dispatched itself. If the property came from an imported ObjC class, then we can't dynamically dispatch it without polluting the selector namespace. Introduce a new 'ForcedStaticDispatch' bit and set it in order to force `dynamic` materializeForSet accessors to be statically dispatched. (They can't be `final` because it's legal to override a dynamic property.) If the property came from ObjC, register materializeForSet as an external declaration so it gets generated by SIL. Fixes rdar://problem/18706056.

Swift SVN r24930
2015-02-04 00:46:24 +00:00
Joe Groff
c1db47ed13 Sema: Synthesize materializeForSet with dynamically-dispatched accessors for dynamic properties.
Semantically, a dynamic property must always be dispatched in case it gets replaced at runtime, and an @NSManaged property may not have static accessors at all. Use ordinary access to the computed property accessors in materializeForSet when a property is dynamic or ObjC-originated. More rdar://problem/18706056.

There's still a problem--we try to vtable-dispatch materializeForSet, which is redundant for native classes, but impossible for imported ObjC classes. We should suppress this, but trying to make materializeForSet "final" breaks subclassing if the property is overridden.

This time, update test/SILGen/objc_properties.swift to avoid a false negative test failure.

Swift SVN r24888
2015-02-01 20:24:57 +00:00
Dmitri Hrybenko
2037fe0059 Revert "Sema: Synthesize materializeForSet with dynamically-dispatched accessors for dynamic properties."
This reverts commit r24882.  It broke SILGen/objc_properties.swift.

Swift SVN r24883
2015-02-01 03:17:49 +00:00
Joe Groff
456288fb6f Sema: Synthesize materializeForSet with dynamically-dispatched accessors for dynamic properties.
Semantically, a dynamic property must always be dispatched in case it gets replaced at runtime, and an @NSManaged property may not have static accessors at all. Use ordinary access to the computed property accessors in materializeForSet when a property is dynamic or ObjC-originated. More rdar://problem/18706056.

There's still a problem--we try to vtable-dispatch materializeForSet, which is redundant for native classes, but impossible for imported ObjC classes. We should suppress this, but trying to make materializeForSet "final" breaks subclassing if the property is overridden.

Swift SVN r24882
2015-02-01 02:09:27 +00:00
Joe Groff
0225fa568a Sema: Don't synth "materializeForSet" for @objc protocol requirements.
Another fix for rdar://problem/18706056. This should make @NSManaged properties work fine with @objc protocols, but I expect us to still be broken with native protocols.

Swift SVN r24862
2015-01-30 23:41:10 +00:00
Joe Groff
06fb3bdf8d Sema: Don't synth materializeForSet accessors on behalf of get-only property requirements.
One of several inefficiencies in the area exposed by rdar://problem/18706056.

Swift SVN r24858
2015-01-30 23:13:18 +00:00
Doug Gregor
b642c555be Allow one to change the argument labels of curried function parameters.
Curried function parameters (i.e., those past the first written
parameter list) default to having argument labels (which they always
have), but any attempt to change or remove the argument labels would
fail. Use the fact that we keep both the argument labels and the
parameter names in patterns to generalize our handling of argument
labels to address this problem.

The IDE changes are due to some positive fallout from this change: we
were using the body parameters as labels in code completions for
subscript operations, which was annoying and wrong.

Fixes rdar://problem/17237268.

Swift SVN r24525
2015-01-19 22:15:14 +00:00
John McCall
cae0f6e3db Add the ability for a owning addressor to return
a non-native owner.  This is required by Slice, which
will use an ObjC immutable array object as the owner
as long as all the elements are contiguous.

As part of this, I decided it was best to encode the
native requirement in the accessor names.  This makes
some of these accessors really long; we can revisit this
if we productize this feature.

Note that pinning addressors still require a native
owner, since pinning as a feature is specific to swift
refcounting.

Swift SVN r24420
2015-01-14 19:14:20 +00:00
John McCall
6a46350546 Bugfixes for the new addressor kinds.
Permit non-Ordinary accesses on references to functions,
with the semantics of devirtualizing the call if the
function is a class member.  This is important for
constructing direct call to addressors from synthesized
materializeForSet accessors: for one, it's more
performant, and for another, addressors do not currently
appear in v-tables.

Synthesize trivial accessors for addressed class members.
We weren't doing this at all before, and I'm still not
sure we're doing it right in all cases.  This is a mess.

Assorted other fixes.  The new addressor kinds seem
to work now.

Swift SVN r24393
2015-01-13 10:37:22 +00:00
John McCall
dc4431ebff Split addressors into unsafe, owning, and pinning variants.
Change all the existing addressors to the unsafe variant.

Update the addressor mangling to include the variant.

The addressor and mutable-addressor may be any of the
variants, independent of the choice for the other.

SILGen and code synthesis for the new variants is still
untested.

Swift SVN r24387
2015-01-13 03:09:16 +00:00
John McCall
f3dc58667d Improve the typing of materializeForSet callbacks to
use a thin function type.

We still need thin-function-to-RawPointer conversions
for generic code, but that's fixable with some sort of
partial_apply_thin_recoverable instruction.

Swift SVN r24364
2015-01-11 21:13:35 +00:00
Chris Willmore
03a6190a1f <rdar://problem/19031957> Change failable casts from "as" to "as!"
Previously the "as" keyword could either represent coercion or or forced
downcasting. This change separates the two notions. "as" now only means
type conversion, while the new "as!" operator is used to perform forced
downcasting. If a program uses "as" where "as!" is called for, we emit a
diagnostic and fixit.

Internally, this change removes the UnresolvedCheckedCastExpr class, in
favor of directly instantiating CoerceExpr when parsing the "as"
operator, and ForcedCheckedCastExpr when parsing the "as!" operator.

Swift SVN r24253
2015-01-08 00:33:59 +00:00
Chris Lattner
763bb6386f Implement parser, AST representation, type checker, etc support for generalized
if-let statements (also while and var, of course) that include multiple bindings
and where clauses.

SILGen support still remains, it currently just asserts on the new constructs.



Swift SVN r24239
2015-01-07 07:03:02 +00:00
Nadav Rotem
636c6bdf2b Use =default for the default destructor.
Swift SVN r24228
2015-01-06 23:27:36 +00:00
Nadav Rotem
efd3495f60 Add a virtual dtor to silence a warning.
Swift SVN r24217
2015-01-06 22:02:57 +00:00
John McCall
44eec49842 Change the signature of materializeForSet to return an
optional callback; retrofit existing implementations.

There's a lot of unpleasant traffic in raw pointers here
which I'm going to try to clean up.

Swift SVN r24123
2014-12-23 22:14:38 +00:00
John McCall
94e5d98ff7 Move 1700 lines of AST-synthesis code out of TypeCheckDecl.cpp.
It's only 6500 lines now!  So much better.

Swift SVN r23994
2014-12-17 23:42:36 +00:00