Commit Graph

82 Commits

Author SHA1 Message Date
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
Chris Lattner
dfaaebc0a0 Merge pull request #1333 from dduan/SE-0031
[SE-0031] Adjusting 'inout' Declarations for Type Decoration
2016-02-26 10:41:58 -08:00
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
practicalswift
ce6afbbc2d [gardening] Fix recently introduced typo: "createable" → "creatable" 2016-02-24 07:35:42 +01:00
Doug Gregor
38c1de69e4 Reinstate "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit ce7b2bcf09, tweaking
a few validation tests appropriately (1 crasher fixed, two -verify
tests that needed updating).
2016-01-14 00:21:48 -08:00
Erik Eckstein
ce7b2bcf09 Revert "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit 2b2e9dc80e.

It broke some compiler crasher tests
2016-01-13 20:42:58 -08:00
Slava Pestov
79b24fa7d1 Update availability tests to be independent of the actual deployment target
Use fictional version numbers in the 10.50..10.99 range.
2016-01-13 19:27:26 -08:00
gregomni
2b2e9dc80e [SR-511][Parse] Add 'associatedtype' keyword and fixit
Adds an associatedtype keyword to the parser tokens, and accepts either
typealias or associatedtype to create an AssociatedTypeDecl, warning
that the former is deprecated. The ASTPrinter now emits associatedtype
for AssociatedTypeDecls.

Separated AssociatedType from TypeAlias as two different kinds of
CodeCompletionDeclKinds. This part probably doesn’t turn out to be
absolutely necessary currently, but it is nice cleanup from formerly
specifically glomming the two together.

And then many, many changes to tests. The actual new tests for the fixits
is at the end of Generics/associated_types.swift.
2016-01-13 17:54:31 -08:00
Ge Sen
7ac02d54ba Erase redundant whitespaces. 2015-12-10 13:35:06 +08:00
Joe Groff
fbd2e4d872 Rename @asmname to @_silgen_name.
This reflects the fact that the attribute's only for compiler-internal use, and isn't really equivalent to C's asm attribute, since it doesn't change the calling convention to be C-compatible.
2015-11-17 14:13:48 -08:00
Devin Coughlin
1165aca545 [Sema] Suppress deprecation/availability diagnostics in branches with empty availability
We currently emit diagnostics for references to deprecated and potentially
unavailable APIs in branches that are dead for the current platform and
minimum deployment target. These dead branches can arise when sharing swift
source between targets or in playgrounds intended to be run on multiple OS
versions.

So for example, suppose the developer has the following code targeting
OSX 10.9+ and iOS 8.0+:

if #available(OSX 10.10, *) {
  // Use replacement API introduced in OSX 10.10/iOS 8.0
} else {
  // Use old API deprecated in OSX 10.10/iOS 8.0
}

Compiling the above for iOS and with a miniminum deployment of 8.0 will
result in a deprecation diagnostic for the use of the API in the else branch
even though that branch will never execute. (Note that the branch is not dead on
the OSX target because the minimum deployment target is 10.9. For OSX we also
won't emit a deprecation warning because the API wasn't deprecated until 10.10.

This commit updates availability checking to create refinement contexts with
empty OS version ranges for #available() else branches when those branches
will definitely not execute. It suppresses deprecation and potential
availability diagnostics within those contexts. It does not suppress diagnostics
for references to APIs annotated as unconditionally unavailable (i.e., with
@available(OSX, unavailable, ...) and friends).

rdar://problem/22307360

Swift SVN r31631
2015-09-02 18:18:30 +00:00
Chris Lattner
649e5f937d generalize the existing ? and + constraints on expected diagnostics to
a simpler and more general * constraint.  This paves the way for other improvements.



Swift SVN r30622
2015-07-25 05:23:30 +00:00
Devin Coughlin
3944a1620d Sema: Update diagnostic text for when @available cannot be applied to a variable.
This changes the diagnostic text when a variable cannot be marked potentially unavailable
so that it does not explicitly use the long-style form of @available:

It changes:
  "stored properties cannot be marked potentially unavailable with 'introduced='"
to:
  "stored properties cannot be marked potentially unavailable with '@available'"

Swift SVN r30446
2015-07-21 16:33:51 +00:00
Devin Coughlin
be17941059 Sema: Build type refinement contexts for functions without bodies.
We were skipping building refinement contexts for function declarations without bodies
(such as protocol requirements and @asmname functions). These contexts are needed to
determine whether parameter and return types are available in those declaration.

Swift SVN r30380
2015-07-19 02:09:17 +00:00
Devin Coughlin
78c1961195 Sema: Add Fix-It suggesting @available() to lazy properties
It is safe to add an @available attribute to lazy properties, so suggest a
Fix-It to do so if the property references a potentially unavailable symbol.

rdar://problem/20968204

Swift SVN r30321
2015-07-17 19:01:14 +00:00
Devin Coughlin
538df11695 Sema: Update availability diagnostic for accessor override to mention accessor kind
Update the diagnostic emitted when an accessor override is less available than the
declaration it overrides to mention the accessor kind and the name of the property rather
than just the accessor declaration name, which is '_'.

rdar://problem/20427938

Swift SVN r30319
2015-07-17 17:17:00 +00:00
Devin Coughlin
217d17d6e6 Sema: Consider availability of protocol declaration when diagnosing protocol witness availability.
Now that we allow types to conform to protocols that are less available than the
type itself, we should consider the availability of the protocol when
determining whether a witness is sufficiently available to satisfy the protocol
requirement.

In particular, we should allow the following:

@available(iOS 9.0, *)
class UIRefrigerator { }

@available(iOS 9.0, *)
protocol UIRefrigeratorDelegate {
  func refrigeratorDidCool(refrigerator: UIRefrigerator)
}

@available(iOS 7.0, *)
class MyViewController : UIViewController, UIRefrigeratorDelegate {
  @available(iOS 9.0, *)
  func refrigeratorDidCool(refrigerator: UIRefrigerator) {
    ...
  }
}

This is safe because MyViewController's refrigeratorDidCool() witness is
available everywhere the protocol is available. To check this, we now
additionally intersect the availability of the protocol with the availabilities
of the witness and the requirement before checking containment.

Swift SVN r29996
2015-07-08 22:43:27 +00:00
Devin Coughlin
f93515fa8d Sema: Allow types to conform to protocols that are less available than the type itself.
The previous rules for availability checking prevented types from conforming to protocols
that were less available than the type itself. This was too restrictive, because we want
to allow developers to add additional behavior to their existing classes to
conform to new protocols while still employing older functionality from those classes on
older OSes:

@available(iOS 9.0, *)
protocol UIRefrigeratorDelegate { }

@available(iOS 8.0, *)
class MyViewController : UIViewController, UIRefrigeratorDelegate { }

We now support this idiom (and adding conformnces via protocol extensions) by allowing
references to potentially unavailable protocols inside inheritance clauses.

It is still an availability error to refer to potentially unavailable protocols in other
settings -- so, for example, attempting to cast to an unavailable protocol will cause
a compile-time error.

rdar://problem/21718497

Swift SVN r29958
2015-07-08 04:22:14 +00:00
Devin Coughlin
550b9feb05 [Sema] Improve clarity of fix-it hint to add #available.
As suggested by Jordan, change "add if #available version check" to
"add 'if #available' version check" to make it easier to read.

Swift SVN r29650
2015-06-25 01:03:50 +00:00
Devin Coughlin
5a71aaef6c [Sema] Update availability fix-it to be consistent.
Change the fix-it suggesting adding #available to be consistent with the fix-it
adding @available.

This changes "guard with version check" to "add if #available version check".

rdar://problem/21275857

Swift SVN r29648
2015-06-25 00:23:02 +00:00
Devin Coughlin
8e071b1148 Revert "[Sema] Suppress warning about useless availability checks in playgrounds and script mode."
As Jordan notes, this uses the wrong check: isScriptMode() is also true for
main.swift in an app or command-line tool.

This reverts commit 8a72f4357bc04a386cf82acd1de06896515ab5a8.

Swift SVN r29583
2015-06-23 23:39:44 +00:00
Devin Coughlin
200b55be70 [Sema] Suppress warning about useless availability checks in playgrounds and script mode.
We normally report a warning when a #available() check will always be true
because of the minimum deployment target. These warnings are potentially
annoying when the developer either cannot change the minimum deployment target
from the default (as in playgrounds) or when doing so is burdensome (as for
command-line scripts, which would require passing a target triple) -- so
suppress them.

rdar://problem/21324005

Swift SVN r29582
2015-06-23 23:31:12 +00:00
Slava Pestov
67a1f24d59 Sema: 'dynamic' attribute now diagnoses if Foundation is not imported
This prevents us from seeing a less useful error message from SILGen
further down the line.

Also fix a bug where @objc without importing Foundation was not diagnosed
after the first top-level form. Some tests were relying on this behavior,
so fix those tests, either by splitting off the objc parts of the test, or
just by passing the -disable-objc-attr-requires-foundation-module flag.

Fixes <rdar://problem/20660270>.

Swift SVN r29359
2015-06-10 01:18:27 +00:00
Joe Groff
bebfa969bd Sema: Allow 'x.init' references on metatype expressions.
If 'x.init' appears as a member reference other than 'self.init' or 'super.init' within an initializer, treat it as a regular static member lookup for 'init' members. This allows a more explicit syntax for dynamic initializations; 'self.someMetatype()' looks too much like it's invoking a method. It also allows for partial applications of initializers using 'someMetatype.init' (though this needs some SILGen fixes, coming up next). While we're in the neighborhood, do some other correctness and QoI fixes:

- Only lookup initializers as members of metatypes, not instances, and add a fixit (instead of crashing) to insert '.dynamicType' if the initializer is found on an instance.
- Make it so that constructing a class-constrained archetype type correctly requires a 'required' or protocol initializer.
- Warn on unused initializer results. This seems to me like just the right thing to do, but is also a small guard against the fact that 'self.init' is now valid in a static method, but produces a newly-constructed value instead of delegating initialization (and evaluating to void).

Swift SVN r29344
2015-06-08 04:11:28 +00:00
Devin Coughlin
7e6a40de7d QoI: Improve diagnostic when protocol witness is less available than protocol requires.
Instead of the rather inscrutable "'foo()' is less available than protocol requires" be
more helpful: "protocol 'HasFoo' requires 'foo()' to be available on OS X 10.9 and newer".

Also add a note indicating where the conformance was introduced, as this may be in a
separate location from both the witness and requirement.

rdar://problem/20610411

Swift SVN r29028
2015-05-26 07:48:45 +00:00
Devin Coughlin
4e9cc49a2b [Sema] Handle multiple guard #available() statements in same block.
The type refinement context builder now handles the case where multiple refinement
contexts have scopes ending after the same statement. This fixes a crash when we had
two guard #available()s in the same block.

rdar://problem/21012706

Swift SVN r28763
2015-05-19 07:19:06 +00:00
Devin Coughlin
c9952640bb Support short-form @available attributes.
Allow availability attributes of the form:

  @available(iOS 8.0, OSX 10.10, *)
  func foo() { }

This form is intended for use by third-party developers when annotating their
own declarations and uses the same syntax as #available(). This annotation
says that on iOS foo() is available on version 8.0 and newer; on OSX it is
available on 10.10; and on any other un-mentioned platform it considered
available on the minimum deployment target and greater. Just like with

For now, we support this form during parsing by synthesizing multiple implicit
long-form @available attributes. So, for example, the above annotation will
synthesize two implicit attributes:

  @available(iOS, introduced=8.0)
  @available(OSX, introduced=10.10)
  func foo() { }

Synthesizing attributes in this way is not ideal -- it makes for a poor Fix-It
experience, among other things -- but it exposes the short-form syntax with
minimal invasiveness.

rdar://problem/20938565

Swift SVN r28647
2015-05-15 23:36:11 +00:00
Devin Coughlin
9f4233bdc1 Move validation of #available() from Sema to Parse
In anticipation of adding short-form @available() annotations, move validation
of #available() platform/version lists out of Sema and into the parser. This
also enables slightly more graceful recovery from errors.

Swift SVN r28637
2015-05-15 20:08:25 +00:00
Devin Coughlin
32935bdaec Sema: Build refinement context for bodies of while loops
This allows #available() to be used in the guard condition of a while loop:

while i > 10, #available(iOS 8.0, *) {
  // Use APIs available on iOS 8.0 and above.
}

rdar://problem/20936160

Swift SVN r28623
2015-05-15 14:33:37 +00:00
Devin Coughlin
424b442db2 Sema: Don't suggest Fix-It to mark stored property as potentially unavailable
Stop suggesting Fix-Its to add @available() to stored properties. We don't allow this
yet, so the "fixed" code would still emit an error when compiled.

rdar://problem/20939804

Swift SVN r28604
2015-05-15 03:03:20 +00:00
Chris Lattner
8a7b3f414e Revise the parser and AST representation of #available to be part of StmtCondition
instead of being an expression.

To the user, this has a couple of behavior changes, stemming from its non-expression-likeness.
 - #available cannot be parenthesized anymore
 - #available is in its own clause, not used in a 'where' clause of if/let.

Also, the implementation in the compiler is simpler and fits the model better.  This
fixes:
<rdar://problem/20904820> Following a "let" condition with #available is incorrectly rejected



Swift SVN r28521
2015-05-13 19:00:40 +00:00
Ted Kremenek
62feb5c949 Change @availability to @available.
This came out of today's language review meeting.
The intent is to match #available with the attribute
that describes availability.

This is a divergence from Objective-C.

Swift SVN r28484
2015-05-12 20:06:13 +00:00
Chris Lattner
4366da9250 more testcase updates for upcoming diagnostics change.
Swift SVN r28409
2015-05-11 06:05:00 +00:00
Dmitri Hrybenko
f46f16ae82 stdlib: implement new print() API
rdar://20775683

Swift SVN r28309
2015-05-08 01:37:59 +00:00
Chris Lattner
37f5452d15 require -> guard.
Swift SVN r28223
2015-05-06 22:53:38 +00:00
Devin Coughlin
8a28d46be2 Sema: Teach availability checking about require/else.
We now introduce a TypeRefinementContext for the fallthrough branch of a require/else
statement that continues until the end of the BraceStmt that contains the RequireStmt. The
body of  the else is checked in the context that contains the RequireStmt.

This enables availability checking with early return:

require #available(iOS 8.0, *) else { return }

Swift SVN r28113
2015-05-04 05:44:06 +00:00
Devin Coughlin
d04cc58c6c Sema: Refine availability context in IfStmt guard conditions following #available()
Loosen restrictions on where #available() can appear in IfStmt guards and refine the
context for guard StmtConditionElements following an availability check.

This enables #available() to be combined with if let optional binding:

if #available(iOS 8.0, *),
   let x = someIOS8API() {
  // Do more iOS 8 stuff
}

and

if let x = someIOS7API() where #available(iOS 8.0, *),
   let y = someIOS8API() {
  // Do more iOS 8 stuff
}

Swift SVN r28096
2015-05-03 20:27:29 +00:00
Devin Coughlin
37088d4fb9 Parse: Update parsing of #available(...) to no longer user >= version comparison
Change the syntax of availability queries from #available(iOS >= 8.0, OSX >= 10.10, *) to

This change reflects the fact that now that we spell the query '#available()' rather than
'#os()', the specification is about availability of the APIs introduced in a particular OS
release rather than an explicit range of OS versions on which the developer expects the
code to run.

There is a Fix-It to remove '>=' to ease adopting the new syntax.

Swift SVN r28025
2015-05-01 05:34:10 +00:00
Devin Coughlin
6b1969af22 [AST] Make MemberRefExpr source range include start of implicit base expression if valid.
Change how MemberRefExpr and DynamicMemberRefExpr calculate their starting locations so
that even if their base expression is implicit, they will use its starting location if
that location is valid rather than falling back to the start of the name of the member.

Without this change, the Fix-It to suggest wrapping a nested member reference (where the
base is an implicit LoadExpr) in 'if #available(...)' would be inserted in the middle of
the expression.

rdar://problem/20662960

Swift SVN r27799
2015-04-27 02:16:12 +00:00
Devin Coughlin
3de8ec3bca Sema: Consider availability of conforming type when diagnosing protocol witness availability
To be safe, protocol witnesses need to be as available as their requirements.
Otherwise, the programmer could access an unavailable declaration by upcasting
to the protocol type and accessing the declaration via its requirement.

Prior to this commit, we enforced safety by requiring that the annotated
available range of a requirement must be completely contained within the
annotated available range of the witness.

However, there are cases where this requirement is too restrictive. Suppose
there is some super class Super with an availability-restricted method f():

class Super {
  @availability(iOS, introduced=6.0)
  void func f() { ... }
}

Further, suppose there is a protocol HasF with unrestricted availability:

protocol HasF {
  void func f()
}

and then a limited-availability class Sub extends Super and declares a
conformance to HasF:

@availability(iOS, introduced=8.0)
class Sub: Super, HasF {

}

Sub does conform to HasF: the witness for HasF's f() requirement is Super's f().
But Super's f() is less available (iOS 6 and up) than HasF's f() requires
(all versions) and so--prior to this commit--the compiler would emit
an error.

This error is too conservative. The conforming type, Sub,
is only available on iOS 8.0 and later. And, given an environment of iOS 8.0
and later, the availability of the requirement and the witness is the same, so
the conformance is safe.

This false alarm arises in UIKit, where Super is UIView, HasF
is UIGestureRecognizerDelegate, and f() is gestureRecognizerShouldBegin().

The fix is to change the safety requirement for protocol witnesses:
we now require that the intersection of the availabilities of the conforming
type and the protocol requirement is fully contained in the intersection of the
availabilities of the conforming type and the witness. It does not matter if
the containment does not hold for versions on which the conforming type is not
available.

rdar://problem/20693144

Swift SVN r27712
2015-04-24 23:12:19 +00:00
Devin Coughlin
721d1870aa [Sema] Change Fix-It for availability version check to include '*'
Update the Fix-It thats suggests adding a #available() check to include the
wildcard query '*', which is now required. We were omitting it before, which
meant that after applying this Fix-It the developer would get a second Fix-It
to add the '*'.

rdar://problem/20601569

Swift SVN r27514
2015-04-21 01:13:30 +00:00
Devin Coughlin
31413bb01c [Sema] Honor -disable-availability-checking flag for protocol conformances
Suppress API availability diagnostics about protocol conformances when the
-disable-availability-checking frontend flag is passed. We weren't
checking the flag before, so there was no way to disable this diagnostic.

I've added a test to make sure this flag is honored. We can remove the test when
we remove the flag, which is temporary.

Swift SVN r27439
2015-04-17 20:04:10 +00:00
Devin Coughlin
d08b98b1ca Sema: Turn on availability checking by default
Enable checking for uses of potentially unavailable APIs. There is
a frontend option to disable it: -disable-availability-checking.

This commit updates the SDK overlays with @availability() annotations for the
declarations where the overlay refers to potentially unavailable APIs. It also changes
several tests that refer to potentially unavailable APIs to use either #available()
or @availability annotations.

Swift SVN r27272
2015-04-14 06:44:01 +00:00
Devin Coughlin
978dd3a357 Sema: Check protocol conformances for potential unavailability.
Make sure that a witness declaration is at least as available as
the protocol requirement declaration. This is analogous to requiring that an
override is at least as available as the base declaration.

We don’t use the RequirementMatch machinery to mark a candidate as unsafe but
rather let the witness be chosen and then diagnose for potential unavailability.
That is, potential unavailability will not affect the search for a candidate
witness to a requirement. This is less expressive (users cannot write a protocol
that selects an available implementation) but is more predictable.

Swift SVN r27256
2015-04-13 19:44:47 +00:00
Devin Coughlin
a4847e9c31 Sema: Recommit "Disallow potential unavailability on stored properties" without Fix-It
For now, disallow potential unavailability on stored properties. We will
eventually want to support this. Unfortunately, doing so will require changes to
definite initialization and (probably) deinitialization.

This is the same as the reverted r27216 except that it does not suggest a FixIt
to make the property lazy.

Swift SVN r27223
2015-04-10 23:19:47 +00:00
Devin Coughlin
54475d66c3 Revert "Sema: Disallow potential unavailability on stored properties."
This reverts r27216. Jordan thinks suggesting a FixIt to make a stored property
lazy is too big of a semantic change.

Swift SVN r27222
2015-04-10 23:19:46 +00:00
Devin Coughlin
38463fac83 Sema: Disallow potential unavailability on stored properties.
For now, disallow potential unavailability on stored properties. We will
eventually want to support this. Unfortunately, doing so will require changes to
definite initialization and (probably) deinitialization.

We can safely support potential unavailability on lazily initialized
properties (and statics), so suggest a Fix-It that adds a lazy attribute when
the property declaration allows for it.

Swift SVN r27216
2015-04-10 22:17:03 +00:00
Devin Coughlin
e6344938c3 Sema: Disallow potential unavailability on script-mode globals
In source files that are in script mode, global variable initialization
expressions are eagerly executed. For this reason, disallow @availability
attributes having 'introduced=' on script-mode globals.

I had to rejigger a fair number of potential unavailability tests because they
weren't written with this distinction in mind.

Swift SVN r27137
2015-04-08 21:29:14 +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
Devin Coughlin
8b5f6fec60 Rename '#os' to '#available'
The API review list found it confusing that if #os() and #if os() looked so similar, so
change the availability checking query to be spelled #available:

if #available(iOS >= 9.0, *) {
  ...
}

Swift SVN r26995
2015-04-04 23:33:13 +00:00