Commit Graph

950 Commits

Author SHA1 Message Date
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
Doug Gregor
f3b20a4412 Omit needless words: strip context type from the name prefix consistently.
When the context type of a declaraton matches the result type,
strip off redundant type information at the beginning of the
declaration name if it is followed by a preposition. This covers the
class of transformations on performs on a class that produce a value
of the same type as that class, e.g., NSURL's "URLWithHTTPS" or
NSString's "stringByAppendingString".

When that preposition is the magical "By" and is followed by a gerund,
strip the "By" as well. Note that this is slightly more conservative
now for methods, which previously stripped based on the result type
(always). For example, in NSCalendar:

-  func adding(_: NSDateComponents, to: NSDate, options:
   NSCalendarOptions = [])
 -> NSDate?
+  func dateByAdding(_: NSDateComponents, to: NSDate, options:
   NSCalendarOptions
 = []) -> NSDate?

but it's more general for properties, e.g.,

-  @NSCopying var bezierPathByFlattening: NSBezierPath { get }
-  @NSCopying var bezierPathByReversing: NSBezierPath { get }
+  @NSCopying var byFlattening: NSBezierPath { get }
+  @NSCopying var reversing: NSBezierPath { get }

The important part is that the rules are more uniform and the code is
more regularly structured: we strip this leading type information when
it's redundant with the context and result type, regardless of whether
we have a property or a method, and the "By" rule is no longer special
in that regard.

Swift SVN r32129
2015-09-21 22:52:14 +00:00
Doug Gregor
627a1820b8 Omit needless words: split the base name when the first parameter is defaulted.
Split the base name at the last preposition, but *only* when the first
parameter is defaulted, because defaulted arguments might not show up
at the call site and the longer base name can feel odd in such
cases. With this, stop avoiding the argument label "with": it's fine
when we have actual context at the call site, and the "with: nil" case
no longer happens now that we're defaulting nil.

Swift SVN r32098
2015-09-20 05:16:46 +00:00
Doug Gregor
9d2dd5ea1e Zap unused variable; NFC
Swift SVN r32081
2015-09-18 21:51:50 +00:00
Doug Gregor
44b8d45288 Clean up inference of default arguments from imported APIs (mostly).
My temporary hackery around inferring default arguments from imported
APIs was too horrible. Make it slightly more sane by:

1) Actually marking these as default arguments in the type system,
rather than doing everything outside of the type system. This is a
step closer to what we would really do, if we go in this
direction. Put it behind the new -frontend flag
-enable-infer-default-arguments.

2) Only inferring a default argument from option sets and from
explicitly "nullable" parameters, as stated in the (Objective-)C API
or API notes. This eliminates a pile of spurious, non-sensical "=
nil"'s in the resulting output.

Note that there is one ugly tweak to the overloading rules to prefer
declarations with fewer defaulted arguments. This is a bad
implementation of what is probably a reasonable rule (prefer to bind
fewer default arguments), which intentionally only kicks in when we're
dealing with imported APIs that have default arguments.

Swift SVN r32078
2015-09-18 21:50:59 +00:00
Doug Gregor
838759d155 Omit needless words: remove arguments that match the default arguments.
For cases where the Clang importer provides a defaulted argument,
e.g., "[]" for option sets and "nil" for optionals, remove the
corresponding arguments at any call sites that simply specify "[]" or
"nil". Such arguments are basically noise, and tend to harm
readability when there are low-content argument labels like "with:" or
"for".

Some examples from Lister:

  self.updateUserActivity(AppConfiguration.UserActivity.watch,
                          userInfo: userInfo, webpageURL: nil)

becomes

  self.updateUserActivity(AppConfiguration.UserActivity.watch,
                          userInfo: userInfo)

and

  contentView.hitTest(tapLocation, with: nil)

becomes

  contentView.hitTest(tapLocation)

and

  document.closeWithCompletionHandler(nil)

becomes simply

  document.close()

and a whole pile of optional "completion handler" arguments go away.

Swift SVN r31978
2015-09-15 22:45:50 +00:00
Doug Gregor
2921614713 Omit needless words: use hanging "With" as a reason to name the first argument.
When omitting words from the end of the base name because it is
redundant with the type of the first parameter leaves a hanging "With"
in the base name, drop that "with" and instead use the tail of the
base name as the label for the first parameter. The poster child for
this is -copyWithZone, which now turns into "copy(zone:)":

-  func copyWith(_: NSZone) -> AnyObject
+  func copy(zone: NSZone) -> AnyObject

The intuition behind this change is that the "With" is stating that
the method isn't directly acting on its argument; rather, the argument
is something additional, and argument labels are a fine way to model this.

Swift SVN r31836
2015-09-10 00:03:12 +00:00
Doug Gregor
406e7c6e48 Omit needless words: generalize the scratch space for strings.
NFC for now, but we'll need to handle multiple strings soon.

Swift SVN r31834
2015-09-10 00:03:09 +00:00
Chris Lattner
8ccb551e77 fix <rdar://problem/22101775> error: '&' can only appear immediately in a call argument list
a case where we would reject a perfectly fine use of &.


Swift SVN r31747
2015-09-07 23:06:09 +00:00
Doug Gregor
7960ca1619 Omit needless words: strip out prefixes that are redundant with the result type.
When the prefix of a method/property name is restating the result
type, followed by "By" and then a gerund, drop everything up to the
gerund. For example:

  func stringByAppendingString(string: String) -> String

becomes

  func appending(string: String) -> String

Swift SVN r31683
2015-09-04 00:07:38 +00:00
Doug Gregor
c439f0f5f0 Omit needless words: remove plural forms of element names when the type is a collection.
When the type name we're looking at is a collection of some element
type, also try to match the plural form of the element type name. For
example:

-  func deselectItemsAtIndexPaths(_: Set<NSIndexPath>)
+  func deselectItemsAt(_: Set<NSIndexPath>)

Swift SVN r31666
2015-09-03 17:06:50 +00:00
Chris Lattner
66a3e8ba14 Fix <rdar://problem/22344208> QoI: Warning for unused capture list variable should be customized
Where before we produced an invalid and misleading diagnostic, produce a correct one.


Swift SVN r31599
2015-09-01 06:12:39 +00:00
Chris Lattner
f062afb712 fix rdar://22240342 - Crash in diagRecursivePropertyAccess
Swift SVN r31595
2015-09-01 02:55:38 +00:00
Chris Lattner
bdb2edb198 With yaks properly groomed and infrastructure in place, we can now improve
diagnostics around invalid references to unavailable declarations, resolving
<rdar://problem/22491394> References to unavailable decls sometimes diagnosed as ambiguous

and a complex case exposed working through rdar://21928143.



Swift SVN r31587
2015-08-30 22:45:37 +00:00
Doug Gregor
6e1b8323f7 Revert "Omit needless words: don't drop the first parameter name for failability initializers."
This reverts commit r31431. This isn't a good heuristic.

Swift SVN r31449
2015-08-25 14:36:04 +00:00
Doug Gregor
419fc39309 Extend -Womit-needless-words to warn about uses as well.
Swift SVN r31448
2015-08-25 14:16:53 +00:00
Doug Gregor
532ff92b79 Factor out the decision of whether to omit words from the actual Fix-Its. NFC
Swift SVN r31447
2015-08-25 14:16:47 +00:00
Doug Gregor
8ac10845b6 Omit needless words: don't drop the first parameter name for failability initializers.
Swift SVN r31431
2015-08-24 18:26:27 +00:00
Argyrios Kyrtzidis
44f2dfe0d9 Follow-up for r31237, address feedback by Jordan.
Swift SVN r31239
2015-08-14 01:48:50 +00:00
Argyrios Kyrtzidis
ab9375a3ec [Sema] Add a diagnostic fixit for the "print(_:appendNewline:)" -> "print(_:terminator:)" change.
Swift SVN r31237
2015-08-14 01:05:37 +00:00
Doug Gregor
561d5fd4a9 Add an opt-in warning to complain about needless words in declarations.
The new option -Womit-needless-words finds places where names are
redundant with type information, producing warnings and Fix-Its to
shorten the names. Part of rdar://problem/22232287, to help bring
the same heuristics we're applying in the Clang importer to the user's
Swift code.

Swift SVN r31234
2015-08-13 23:39:35 +00:00
Jordan Rose
74c634524e Pull 'try' et al inside RebindSelfInConstructorExprs.
And give a proper warning when you use 'try?' in a non-failable init.

And do the right thing when trying to SILGen 'try?' delegating to a
failable throwing init.

And make sure DI understands that this is, in fact, an initialization.

More rdar://problem/21692467

Swift SVN r31060
2015-08-06 21:02:43 +00:00
Jordan Rose
6b12b9130e Disallow nesting 'self.init' or 'super.init' inside other expressions.
In addition to being confusing, it makes it harder to implement
'try? self.init(...)' properly. (Next commit!)

Swift SVN r31034
2015-08-05 22:17:41 +00:00
Chris Lattner
f2746eaf7b Add inout expr validation to the SyntacticUseRestrictions checker
in MiscDiagnostics, allowing us to correctly and consistently reject 
InOutExprs that do not appear in argument contexts.


Swift SVN r30746
2015-07-29 01:02:46 +00:00
Slava Pestov
380fc28690 Sema: Use correct expression for location info
Fixes a small nit from r30685, and adds a test for using the
property as an inout.

Swift SVN r30686
2015-07-27 03:20:48 +00:00