Commit Graph

224 Commits

Author SHA1 Message Date
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
Joe Groff
26e55ce465 Sema: Initial parsing and synthesis for properties with behaviors.
Parse 'var [behavior] x: T', and when we see it, try to instantiate the property's
implementation in terms of the given behavior. To start out, behaviors are modeled
as protocols. If the protocol follows this pattern:

  ```
  protocol behavior {
    associatedtype Value
  }
  extension behavior {
    var value: Value { ... }
  }
  ```

then the property is instantiated by forming a conformance to `behavior` where
`Self` is bound to the enclosing type and `Value` is bound to the property's
declared type, and invoking the accessors of the `value` implementation:

  ```
  struct Foo {
    var [behavior] foo: Int
  }

  /* behaves like */

  extension Foo: private behavior {
    @implements(behavior.Value)
    private typealias `[behavior].Value` = Int

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

If the protocol requires a `storage` member, and provides an `initStorage` method
to provide an initial value to the storage:

  ```
  protocol storageBehavior {
    associatedtype Value

    var storage: Something<Value> { ... }
  }
  extension storageBehavior {
    var value: Value { ... }

    static func initStorage() -> Something<Value> { ... }
  }
  ```

then a stored property of the appropriate type is instantiated to witness the
requirement, using `initStorage` to initialize:

  ```
  struct Foo {
    var [storageBehavior] foo: Int
  }

  /* behaves like */

  extension Foo: private storageBehavior {
    @implements(storageBehavior.Value)
    private typealias `[storageBehavior].Value` = Int
    @implements(storageBehavior.storage)
    private var `[storageBehavior].storage`: Something<Int> = initStorage()

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

In either case, the `value` and `storage` properties should support any combination
of get-only/settable and mutating/nonmutating modifiers. The instantiated property
follows the settability and mutating-ness of the `value` implementation. The
protocol can also impose requirements on the `Self` and `Value` types.

Bells and whistles such as initializer expressions, accessors,
out-of-line initialization, etc. are not implemented. Additionally, behaviors
that instantiate storage are currently only supported on instance properties.
This also hasn't been tested past sema yet; SIL and IRGen will likely expose
additional issues.
2016-02-20 15:01:05 -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
Chris Lattner
5bad3ba49a Warn about implicit tuple splat in a parameter list. 2016-02-09 14:01:50 -08:00
Doug Gregor
5726e7a329 [Sema] Eliminate a foolish little infinite loop I introduced. 2016-01-28 19:49:03 -08:00
Doug Gregor
9a0241bb3b SE-0022: Address Jordan's review comments about #selector. 2016-01-28 12:09:57 -08:00
Doug Gregor
1a830fa541 SE-0022: Deprecate string-literal-as-selector in favor of #selector.
Introduce Fix-Its to aid migration from selectors spelled as string
literals ("foo:bar:", which is deprecated), as well as from
construction of Selector instances from string literals
(Selector("foo:bar"), which is still acceptable but not recommended),
to the #selector syntax. Jump through some hoops to disambiguate
method references if there are overloads:

    fixits.swift:51:7: warning: use of string literal for Objective-C
         selectors is deprecated; use '#selector' instead
      _ = "overloadedWithInt:" as Selector
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          #selector(Bar.overloaded(_:) as (Bar) -> (Int) -> ())

In the cases where we cannot provide a Fix-It to a #selector
expression, we wrap the string literal in a Selector(...) construction
to suppress the deprecation warning. These are also easily searchable
in the code base.

This also means we're doing more validation of the string literals
that go into Selector, i.e., that they are well-formed selectors and
that we know about some method that is @objc and has that
selector. We'll warn if either is untrue.
2016-01-28 10:58:27 -08:00
Doug Gregor
8336419844 Include completion source location information compound DeclNames.
When one spells a compound declaration name in the source (e.g.,
insertSubview(_:aboveSubview:), keep track of the locations of the
base name, parentheses, and argument labels.
2016-01-25 14:13:13 -08:00
practicalswift
33312eac6b [gardening] Remove unreachable/unused/redundant code
* Make parameter naming in forward declaration match definition
* Remove unused argument to function persistAsync(…)
* Remove unused enum ShouldHalt
* Remove unused enum class IsProtocol
* Remove unused function dumpTypeSubstitutionMap()
* Remove unused function template getFirstPairElt(…)
* Remove unused method addConstantWordInWords(…)
* Remove unused method asExistentialTI()
* Remove unused method currentTrackedState()
* Remove unused method getNumBodyParameters()
* Remove unused method getSuccIndex()
* Remove unused method getTypeOfDeclReference(…)
* Remove unused method hasStructWithAtMostOneNonTrivialField(…)
* Remove unused method initForDirectValues()
* Remove unused method nextIfNot(…)
* Remove unused method overwriteLoweredValue(…)
* Remove unused method removeColumn(…)
* Remove unused methods HasSingleDecl() and GetFirstDecl()
* Remove unused methods overwriteLoweredExplosion(…) and setLoweredSingleValue(…)
* Remove unused methods requireRetainablePointerValue(…), getMethodSelfInstanceType(…) and isSelfArchetype(…)
* Remove unused methods setAsEmptyDirect(), setAsSingleDirectUnmanagedFragileValue(…), setAsIndirectAddress(…) and getDirectValues()
* Remove unused struct CachedMemberInfo
* Remove unused struct CallEdit
* Remove unused struct ErrorImportInfo
* Remove unused synonym ConformancePair
* Remove unused variable SemaInfo
* Remove unused variable localDeclNameNode
* Remove unused variables kindToken and kindLoc
2016-01-22 09:43:24 +01:00
David Farler
e03a064586 Revert "REVERTME: Disallow partial application of super methods except for implicit self"
This reverts commit 7796d78008.

Now that multiple parameter list declarations are an error in Swift 3,
this diagnostic isn't necessary anymore.
2016-01-21 14:48:38 -08:00
Doug Gregor
fd3f03f3be Remove UnresolvedConstructorExpr.
UnresolvedConstructorExpr is not providing any value here; it's
essentially just UnresolvedDotExpr where the name refers to an
initializer, so use that instead. NFC
2016-01-20 17:09:02 -08:00
Doug Gregor
5f07f6b12f Remove all vestiges of UnresolvedSelectorExpr. NFC 2016-01-20 17:09:01 -08:00
Chris Lattner
8a41d14b2d Fix <rdar://problem/21429694> QoI: diagnostic for accessing private methods could be improved
When member lookup completely fails and when CSDiags is the one performing
the lookup, reissue another lookup that ignores access control.  This allows
it to find inaccessible members and diagnose them as such, instead of pretending
we have no idea what the user wants.  We now produce an error message like this:

main.swift:1:6: error: 'foo' is inaccessible due to 'private' protection level
 C().foo()
     ^
test.swift:1:35: note: 'foo' declared here
  internal class C { private func foo() {} }
                                  ^

instead of:

main.swift:1:2: error: value of type 'C' has no member 'foo'
 C().foo()
 ^~~ ~~~
2016-01-17 17:13:07 -08:00
David Farler
7796d78008 REVERTME: Disallow partial application of super methods except for implicit self
This adds a Sema check that super methods aren't partially applied,
since we are removing currying declaration syntax. Once that lands,
this can be reverted and the test removed.
2016-01-15 13:37:53 -08:00
gregomni
e52ddafe91 [SR-533] Improve C-style for fixits by also removing 'var' keyword from loop var decl. 2016-01-13 15:23:01 -08:00
Doug Gregor
5aa40dd0aa Extend DefaultArgumentKind with cases for nil, [], and [:].
Under -enable-infer-default-arguments, the Clang importer infers some
default arguments for imported declarations. Rather than jumping
through awful hoops to make sure that we create default argument
generators (which will likely imply eager type checking), simply
handle these cases as callee-side expansions.

This makes -enable-infer-default-arguments usable, fixing
rdar://problem/24049927.
2016-01-06 10:19:12 -08:00
Doug Gregor
f61893595d [Omit needless words] Don't ask for the StringRef of an empty identifier. 2016-01-04 10:08:48 -08:00
practicalswift
31ff35e1dd Use 80 column headers consistently. 2016-01-04 01:35:02 +01:00
Chris Lattner
6afe77d597 Eliminate the Parameter type completely - now ParameterList is just
an overblown array of ParamDecl*'s that also keeps track of parenlocs
and has helper methods.
2016-01-03 14:45:38 -08:00
Chris Lattner
65ce155c42 Re-convert omitNeedlessWords to be defined in terms of parameter lists
instead of types, with a bugfix.  We make sure to check for
DefaultArgumentKind::None instead of checking for the presence of a
default value.  These are different when dealing with deserialized
models and clang importer results.
2016-01-03 14:06:56 -08:00
Mark Lacey
006995716e Revert "Change omitNeedlessWords to be based on ParameterList instead of TupleType."
This reverts commit ccc1702a5d because it
breaks test/ClangModules/omit_needless_words.swift.
2016-01-01 16:52:23 -08:00
Chris Lattner
b170b700f8 move the rest of the state out of Parameter and into ParamDecl,
in prep for Parameter going away.  NFC.
2016-01-01 15:27:53 -08:00
Chris Lattner
ccc1702a5d Change omitNeedlessWords to be based on ParameterList instead of TupleType. 2016-01-01 13:19:34 -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
Chris Lattner
666a42f5c7 Remove the ability to map back from a ParamDecl to its enclosing Pattern. This
is used by precisely one thing (producing a warning in a scenario that is obsolete
because we deprecated the entire thing), so the complexity isn't worth it anymore.
2015-12-29 21:09:11 -08:00
gregomni
1a9822d841 Extend fix-its for C-style for loops to include loop var "+= 1"
Since the commit to deprecate “++” went through soon after the one to
deprecate C-style for loops, it’d be nice to auto-fix the for loops
even after previously changing ++ to += 1. So let’s do that.
2015-12-24 11:01:19 -08:00
Chris Lattner
443af4ad91 In the deprecation warnings for ++/-- on index types, emit a fixit hint to rewrite into
the correct call to .successor() or .predecessor().

This wraps up <rdar://problem/23708702> Emit deprecation warnings for ++/-- in Swift 2.2
2015-12-22 21:04:48 -08:00
Chris Lattner
6a63c77fce Produce fixit hints for deprecated ++/--, rewriting them into (e.g.) += 1.
This handles the case when we have int/fp uses that ignore the result of
the expression.  Index types are coming next.
2015-12-22 15:40:35 -08:00
Chris Lattner
2bfb788ef2 remove the logic for upgrading Swift 1's print that used "appendNewline"
to Swift 2 syntax that uses "terminator".  It is legacy, and removing it
allows simplifications.
2015-12-22 14:42:18 -08:00
Chris Lattner
6862abd7c4 refactor the diagAvailability() static function to be a member of the only
class that uses it, simplify code a bit.  NFC.
2015-12-22 14:42:18 -08:00
gregomni
392a1ff0ed Deprecation warnings for C-style for loops
Warns of deprecation, checks all the appropriate bits to see if we can
do an automatic fix, and generates fix-its if that is valid.

Also adds a note if the loop looks like it ought to be a simple
for-each, but really isn’t because the loop var is modified inside the
loop.
2015-12-17 10:58:59 -08:00
practicalswift
c6e8459187 Fix typos. 2015-12-14 11:13:30 +01:00
Chris Lattner
b0c76c64bb fix <rdar://problem/23853709> Compiler crash on call to unavailable "print" 2015-12-10 20:32:39 -08:00
Slava Pestov
478e1c7513 Partial application of @objc protocol methods 2015-12-10 16:39:48 -08:00
Johan K. Jensen
fa76656c82 Remove instances of duplicated words 2015-12-03 20:00:29 +01:00
Chris Lattner
2379928d07 Fix <rdar://22774938> QoI: "never used" in an "if let" should rewrite expression to use != nil
When we see an unused variable in a simple-enough "if/let" (also guard and
while of course), fixit it into a comparison against nil instead of replacing
the name of the variable with "_".  Also special case initialization with an
as? expression, since we can transform that into an "is" boolean test.
2015-11-29 21:49:36 -08:00
Doug Gregor
0f673f5d7a Omit needless words: distinguish class vs. instance properties for pruning.
A class method named "bezierPath" should not prevent
"appendBezierPath" from being stripped.
2015-11-16 16:15:26 -08:00
Doug Gregor
106bf80152 Omit needless words: don't prune "properties" of the context from the base name.
The properties of a context indicate those things that are considered
"contained within" the context (among other things). This helps us
avoid producing overly-generic names when we identify a redundancy in
the base name. For example, NSView contains the following:

  var gestureRecognizers: [NSGestureRecognizer]
  func addGestureRecognizer(gestureRecognizer: NSGestureRecognizer)
  func removeGestureRecognizer(gestureRecognizer: NSGestureRecognizer)

Normally, omit-needless-words would prune the two method names down to
"add" and "remove", respectively, because they restate type
information. However, this pruning is not ideal, because a view isn't
primarily a collection of gesture recognizers.

Use the presence of the property "gestureRecognizers" to indicate that
we should not strip "gestureRecognizer" or "gestureRecognizers" from
the base names of methods within that class (or its subclasses).

Note that there is more work to do here to properly deal with API
evolution: a newly-added property shouldn't have any effect on
existing APIs. We should use availability information here, and only
consider properties introduced no later than the entity under
consideration.
2015-11-16 15:27:38 -08:00
Chris Willmore
e7bdaeace2 Warn about indentation of returned expr in single expression closure.
If the returned expression has the same indentation as the "return"
keyword, warn. This warning already existed but wasn't happening
for single-expression closures. Move emission of the warning from Sema
to Parse.

<rdar://problem/16798323>
2015-11-06 17:17:45 -08:00
Slava Pestov
8b3753b995 AST: Split up RecursiveTypePropeties::isMaterializable() into sub-cases and fix a bug
We already had isLValue(), which implied !isMaterializable().
Now, add separate flags for hasInOut() and hasDefaultArg().
The old isMaterializable() predicate is still provided,
by testing for all three flags.

The new predicates are not used for now, but will be soon
as part of reabstraction thunk changes required for resilience.

Also two fixes:

1) Before this change RecursiveTypeProperties was incorrectly
sized at 8 bits when it actually needs 9. This fixes a regression
from SVN r31130, "Introduce a new UnresolvedType to the type system".

2) DynamicSelf::get() would clear out non-materializability
but not lvalue-ness of its base type, which looks like an old
copy/paste and not intentional. Stick an assert there instead.
2015-11-06 11:08:36 -08:00
Doug Gregor
7778790a68 Omit needless words: prepend "is" to Boolean property names.
Prepend       "is" to        Boolean property names (e.g., "empty" becomes
"isEmpty") unless the property name strongly indicates its Boolean
nature or we're   likely to ruin the name. Therefore, the  presence of
one of the following in  the property name will suppress   this
transformation:

* An auxiliary verb, such as "is", "has", "may", "should", or "will".

* A word ending in "s", indicating either a plural (for which
  prepending "is" would be incorrect) or a verb in the continuous
  tense (which indicates its Boolean nature, e.g., "translates" in
  "translatesCoordinates").

Swift SVN r32458
2015-10-06 07:03:17 +00:00
Doug Gregor
93a24c44b0 Omit needless words: be more consistent about AnyObject -> "Object".
Swift SVN r32450
2015-10-05 22:53:53 +00:00
Doug Gregor
b9b4939d22 Omit needless words: don't use typedef names as type names.
Typedefs provide weak type information in both C and Swift, so don't
use the names of typedefs when omitting needless words. This improves
a number of APIs where it looked like the words were redundant, but
the type system was deceiving us. For example:

-  func setHolding(_: NSLayoutPriority, forSubviewAt: Int)
+  func setHoldingPriority(_: NSLayoutPriority, forSubviewAt: Int)

Swift SVN r32449
2015-10-05 22:53:49 +00:00
Doug Gregor
ed68fff2cb Don't try to omit needless words on invalid declarations.
Swift SVN r32442
2015-10-05 19:41:22 +00:00
Doug Gregor
44e34850ae Omit needless words: give initial Boolean parameters argument labels.
When the first parameter of a function has Boolean type, try to create
an argument label for it. We start with the (normally non-API)
parameter name as the argument label, then try to match that against
the end of the base name of the method to eliminate redundancy. Add a
little magic, and here are some diffs:

    -  func openUntitledDocumentAndDisplay(_: Bool) throws -> NSDocument
    +  func openUntitledDocument(display _: Bool) throws -> NSDocument

    -  func fontMenu(_: Bool) -> NSMenu?
    -  func fontPanel(_: Bool) -> NSFontPanel?
    +  func fontMenu(create _: Bool) -> NSMenu?
    +  func fontPanel(create _: Bool) -> NSFontPanel?

    -  func lockFocusFlipped(_: Bool)
    +  func lockFocus(flipped _: Bool)

    -  func rectForSearchTextWhenCentered(_: Bool) -> NSRect
    +  func rectForSearchText(whenCentered _: Bool) -> NSRect

    -  func dismissPreviewAnimated(_: Bool)
    -  func dismissMenuAnimated(_: Bool)
    +  func dismissPreview(animated _: Bool)
    +  func dismissMenu(animated _: Bool)

Swift SVN r32392
2015-10-01 23:34:21 +00:00
Doug Gregor
169581f73b Omit needless words: don't inject argument labels for trailing closures.
We were getting this wrong when there were no parentheses.

Swift SVN r32289
2015-09-28 23:44:01 +00:00
Doug Gregor
7d6babd53a Omit needless words NFC: factor out options for OmissionTypeName.
Swift SVN r32139
2015-09-22 00:35:07 +00:00
Doug Gregor
0e26956851 Omit needless words: function pointers/references map to "Function".
Swift SVN r32135
2015-09-21 23:38:52 +00:00