Commit Graph

1030 Commits

Author SHA1 Message Date
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
Slava Pestov
b24671c818 Sema: Fix false positives for 'storing property in willSet' diagnostic
Fixes <rdar://problem/21392221>.

Swift SVN r30685
2015-07-27 01:55:31 +00:00
Joe Groff
0b1283b1c9 Have 'defer' statements cons up func decls instead of closure literals.
The defer body func is only ever fully applied, so SILGen can avoid allocating a closure for it if it's declared as a 'func', making it slightly more efficient at -Onone.

Swift SVN r30638
2015-07-25 21:28:06 +00:00
Chris Lattner
9fd3f3bf23 Reimplement checking for incorrect partial applications in MiscDiagnostics (instead of
CSApply).  The code is somewhat simpler there, more correct (fixing the FIXME in the 
testcase) and doesn't run afoul of CSDiags trying to type check partial AST 
subexpressions.


Swift SVN r30425
2015-07-21 00:50:05 +00:00
Devin Coughlin
c1caddae62 Remove experimental support for treating unavailable symbols as optional.
This has always been off by default and is a language direction we have decided not to
pursue.

Swift SVN r30355
2015-07-18 01:56:25 +00:00
Jordan Rose
5c71b75b25 Add @warn_unqualified_access, and apply it to imported methods named 'print'.
Otherwise, people subclassing NSView will accidentally call NSView.print
when they're trying to call Swift.print.

rdar://problem/18309853

Swift SVN r30334
2015-07-17 22:02:35 +00:00
Doug Gregor
65dd63c502 Don't suggest '()' after a protocol reference, because it doesn't make sense.
Fixes rdar://problem/18349412.

Swift SVN r30200
2015-07-14 22:07:04 +00:00
Chris Lattner
9ae1ff707c Add a new DisableStructuralChecks option to typeCheckExpr which allows CSDiag to
avoid emitting this diagnostics.  They are always irritating to it because they
are duplicative with whatever the actual type constraint problem is.  It also
interferes with there recursive structure of the problem.

Disabling them also allows us to shorten the black list of expr nodes that cannot
be handled in typeCheckIndependentSubExpression.



Swift SVN r30122
2015-07-11 22:28:40 +00:00
Joe Groff
1a237b0ec2 Sema: Allow @available attribute to provide both a rename and a message.
Swift SVN r30007
2015-07-09 01:20:12 +00:00
Chris Lattner
acecd187a3 fix <rdar://problem/21553065> Spurious diagnostic: '_' can only appear in a pattern or on the left side of an assignment
... by reimplementing the DiscardAssignmentExpr checker in MiscDiagnostics 
instead of being in CSApply (which gets run on partial expression ASTs).

Also, when type checking and salvaging an expression fails, do not generate 
structural diagnostics in addition to the type checker diagnostics, they are 
just noise.



Swift SVN r29937
2015-07-07 05:52:17 +00:00
Chris Lattner
3ad108b0be Reapply r29419:
Enhance fixItRemove() to be a bit more careful about what whitespace it leaves around: if the thing it is removing has leading and trailing whitespace already, this nukes an extra space to avoid leaving double spaces or incorrectly indented results.  

This includes an extra fix for looking off the start of a buffer, which extractText doesn't and can't handle.

This fixes <rdar://problem/21045509> Fixit deletes 'let' from non-binding 'if case let' statements, but leaves an extra space




Swift SVN r29449
2015-06-17 16:31:26 +00:00
Ted Kremenek
d13549e607 Revert "enhance fixItRemove() to be a bit more careful about what whitespace it leaves around:"
This was breaking the bots.

Swift SVN r29432
2015-06-17 02:20:52 +00:00
Chris Lattner
6b3167ab36 enhance fixItRemove() to be a bit more careful about what whitespace it leaves around:
if the thing it is removing has leading and trailing whitespace already, this nukes
an extra space to avoid leaving double spaces or incorrectly indented results.  This
fixes <rdar://problem/21045509> Fixit deletes 'let' from non-binding 'if case let' statements, but leaves an extra space



Swift SVN r29419
2015-06-17 00:55:59 +00:00
Slava Pestov
8504517e15 Sema: Fix opened existentials leaking out in diagnostics
getTypeOfIndependentSubExpression() might replace some values in the
given expression with OpaqueValueExprs, but if the type checker
decided to insert the corresponding OpenExistentialExpr at the top
level, the AST would now be in an inconsistent state, since
getTypeOfIndependentSubExpression() does not return a new expression
to the caller.

Ideally we would separate out type checking from expression rewriting
so that the latter is only performed when we know the expression
type checks, but that is a bigger project. For now, erase open
existentials after re-typechecking a sub-expression.

Fixes <rdar://problem/20598568>.

Swift SVN r29400
2015-06-16 17:25:46 +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
Chris Lattner
16b4ad5ce1 Generalize r29106 to handle all of the different kinds of statements that have patterns
in a more robust way.  Also, add testcases for them all.



Swift SVN r29199
2015-06-01 02:52:30 +00:00
Chris Lattner
7ca647002d fix <rdar://problem/21176945> mutation through ? not considered a mutation
Swift SVN r29194
2015-05-31 21:26:51 +00:00
Argyrios Kyrtzidis
514a57d02a [Sema] Find the 'var' location of 'catch' variables so that the warning "variable x was never mutated; consider changing to 'let’ constant",
can add a fixit for it.

rdar://21137461

Swift SVN r29106
2015-05-28 17:16:16 +00:00
Chris Willmore
52d441ba61 Have a bit per PatternBindingEntry saying whether the corresponding
initializer has been type-checked, rather than a bit for the entire
PatternBindingDecl.

<rdar://problem/21057425> Crash while compiling attached test-app.

Swift SVN r29049
2015-05-27 01:31:28 +00:00
Chris Lattner
dd594f6790 fix <rdar://problem/21091625> Bogus 'never mutated' warning when protocol variable is mutated only by mutating method
we need special magic to look through OpaqueValueExprs.



Swift SVN r28994
2015-05-24 21:16:36 +00:00
Chris Lattner
560d264a84 fix some funky indentation, NFC.
Swift SVN r28993
2015-05-24 21:03:53 +00:00