Commit Graph

603 Commits

Author SHA1 Message Date
Sho Ikeda
cea6c03eb2 [gardening] Use !empty() over size() > 0 2018-03-08 09:21:09 +09:00
Slava Pestov
26e69d9667 Sema: Diagnose self.init and super.init inside closures
Fixes <rdar://problem/27420414>.
2018-03-06 18:20:07 -08:00
Mark Lacey
d63bb3fc53 Remove most uses of OptionalTypeKind.
What remains are places where we are conflating optionality with
either nullability or failability.
2018-02-10 16:24:09 -08:00
Mark Lacey
be8defb29e Rename lookThroughAllAnyOptionalTypes to lookThroughAllOptionalTypes. 2018-02-05 23:59:01 -08:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Hamish
416cb7f156 [Sema] Update Optional-to-Any tests for #14299
As IUOs types are no longer generated, we don't show them in diagnostics.
2018-02-02 20:00:02 +00:00
Hamish
7d3785a15e [Sema] Enable Optional-to-Any diagnostics for IUOs
For example, the implicit coercion from `Int!` to `Any` now produces a warning. Previously a warning wasn't emitted despite the optional being implicitly erased to `Any` (but warnings were emitted in some other cases, such as `Int?!` to `Any?`; this commit fixes that inconsistency).
2018-02-02 14:07:03 +00:00
Hamish
115836210f [Sema] Improve Optional-to-Any diagnostics for collections
When implicitly coercing a collection of optional elements to a collection of less-optional `Any` elements, e.g `[Any?]` to `[Any]`, emit a slightly better diagnostic:

Expression implicitly coerced from '[Any?]' to '[Any]'

and allow for silencing of the diagnostic with an explicit coercion, e.g `as [Any]`.
2018-02-02 14:07:02 +00:00
Hamish
016af6bfae [Sema] Improve Optional-to-Any diagnostics for nested optionals
When implicitly coercing a nested optional to a less optional `Any`, e.g `Any??` to `Any?`, emit a slightly better diagnostic:

Expression implicitly coerced from 'Any??' to 'Any?'

and allow for silencing of the diagnostic with an explicit coercion, e.g `as Any?`.
2018-02-02 14:07:01 +00:00
Hamish
1891c3820b [Sema] Change Optional-to-Any diagnostics to take a variable destination type
This will let us customise the diagnostic for collections and nested optionals.
2018-02-02 14:07:00 +00:00
Hamish
e0f9732eed [Sema] Refactor Optional-to-Any diagnostic logic (NFC) 2018-02-02 14:07:00 +00:00
Mark Lacey
3aa8cfd72d Fix one source of problems with allowing @noescape functions to escape.
We have other issues with Any and generics, but those will require
some additional work to get reasonable diagnostics.

Fixes part of rdar://24097075.
2018-01-23 09:26:11 -08:00
gregomni
0c3c0fd59b Support for fallthrough into cases with pattern variables. 2018-01-20 11:10:00 -08:00
John McCall
7f0f8830cd Split AccessorDecl out from FuncDecl. NFC.
This has three principal advantages:

- It gives some additional type-safety when working
  with known accessors.

- It makes it significantly easier to test whether a declaration
  is an accessor and encourages the use of a common idiom.

- It saves a small amount of memory in both FuncDecl and its
  serialized form.
2018-01-12 14:20:27 -05:00
John McCall
02d7fe36ba Extend the addObserver/removeObserver to AnyObject lookup idioms.
rdar://33850465
2017-11-29 18:04:22 -05:00
Doug Gregor
3882c2922c [Type checker] Teach optional-promotion warning to understand member operators.
Fixes a diagnostics regression for expressions such as "x != nil"
(where x is non-optional), due to moving Optional's == into Optional
itself.
2017-11-17 10:52:05 -08:00
Greg Parker
e8475cc130 Revert "Use conditional conformances to implement Equatable for Optional, Array and Dictionary" 2017-11-15 14:17:22 -08:00
Doug Gregor
c8e21e7b57 [Type checker] Teach optional-promotion warning to understand member operators.
Fixes a diagnostics regression for expressions such as "x != nil"
(where x is non-optional), due to moving Optional's == into Optional
itself.
2017-11-14 16:23:20 -08:00
Doug Gregor
a49455e3b2 [Clang importer] Move the "all properties" lists into the name importer.
Swift's ASTContext contained all of the logic to find the complete list
of properties for an Objective-C class, which is used by the Clang importer
to influence the mapping of Objective-C names into Swift. Swift's
ASTContext also included a *cache* for this information, indexed by
the Clang `ObjCInterfaceDecl *`. However, this cache was getting
populated/queried from the Clang importer's name importer, such that
the keys would be Clang declarations used to build modules and then
deallocated. If that memory eventually gets reused for a different
`ObjCInterfaceDecl *`, we would get incorrect/stale all-properties
information.

Almost Surely fixes rdar://problem/35347167, which is a
nondeterministic failure where UIView's `addGestureRecognizer(_:)` gets
occasionally imported as `add(_:)`.
2017-11-08 15:25:15 -08:00
Hamish
d734631120 [Sema] Allow non-escaping functions to be passed as subscript arguments 2017-11-02 11:04:53 +00:00
Tony Allevato
715ba632dd Merge branch 'master' into synthesize-equatable-hashable 2017-10-09 15:57:36 -07:00
Brian King
a5a6684fce Improve diagnostic language 2017-09-26 20:47:09 -04:00
Brian King
c5397ba41b Clean up implementation 2017-09-26 20:46:40 -04:00
Brian King
4f20f670e7 Generate a fix-it to change the first getter usage into the setter parameter. 2017-09-25 20:19:36 -04:00
Brian King
b894bd038b Update error message 2017-09-25 19:21:16 -04:00
Brian King
f1f64ce8a6 Change warning to only be reported if the parameter is unused but the getter is used. 2017-09-25 19:20:58 -04:00
Brian King
a79d2a8c44 Generate a warning if the implicit setter argument 'newValue' is not used. 2017-09-25 19:20:50 -04:00
Tony Allevato
f74b3fb3c1 [Sema] Only emit warnings for implicit self use in getters
This fixes a pre-existing bug where implicit DeclRefExprs involving
*any* expression of the same type within a getter were flagged with
warnings, even if they were references to something other than "self".
2017-09-24 17:31:19 -07:00
Slava Pestov
55916fa128 Sema: Remove usages of getDeclaredTypeOfContext() 2017-09-19 22:12:29 -07:00
John McCall
2d3d6addc0 Delay the validaton of storage accessors until finalization.
The base mutability of storage is part of the signature, so be sure
to compute that during validation.  Also, serialize it as part of
the storage declaration, and fix some places that synthesize
declarations to set it correctly.
2017-09-10 04:56:02 -04:00
Robert Widmann
abce63a1b3 [NFC] Scale back more uses of InOutType 2017-09-05 21:56:58 -04:00
Jordan Rose
f8b7db4e76 Excise the terms "blacklist" and "whitelist" from Swift source. (#11687)
The etymology of these terms isn't about race, but "black" = "blocked"
and "white" = "allowed" isn't really a good look these days. In most
cases we weren't using these terms particularly precisely anyway, so
the rephrasing is actually an improvement.
2017-08-30 09:28:00 -07:00
Jordan Rose
449cd98997 Excise "Accessibility" from the compiler (3/3)
"Accessibility" has a different meaning for app developers, so we've
already deliberately excised it from our diagnostics in favor of terms
like "access control" and "access level". Do the same in the compiler
now that we aren't constantly pulling things into the release branch.

Rename AccessibilityAttr to AccessControlAttr and
SetterAccessibilityAttr to SetterAccessAttr, then track down the last
few uses of "accessibility" that don't have to do with
NSAccessibility. (I left the SourceKit XPC API alone because that's
supposed to be more stable.)
2017-08-28 13:27:59 -07:00
Jordan Rose
1c651973c3 Excise "Accessibility" from the compiler (2/3)
"Accessibility" has a different meaning for app developers, so we've
already deliberately excised it from our diagnostics in favor of terms
like "access control" and "access level". Do the same in the compiler
now that we aren't constantly pulling things into the release branch.

This commit changes the 'Accessibility' enum to be named 'AccessLevel'.
2017-08-28 11:34:44 -07:00
Jordan Rose
5f30eac288 Excise "Accessibility" from the compiler (1/3)
"Accessibility" has a different meaning for app developers, so we've
already deliberately excised it from our diagnostics in favor of terms
like "access control" and "access level". Do the same in the compiler
now that we aren't constantly pulling things into the release branch.

This commit changes the names of methods, fields, a few local
variables, and even a swift-ide-test flag. The full list is below.

accessibilityForDiagnostics -> accessLevelForDiagnostics
checkAccessibility -> checkAccess
checkGenericParamAccessibility -> checkGenericParamAccess
checkTypeAccessibility -> checkTypeAccess
checkWitnessAccessibility -> checkWitnessAccessibility
computeAccessibility -> computeAccessLevel
computeDefaultAccessibility -> computeDefaultAccessLevel
fixItAccessibility -> fixItAccess
getAccessibilityString -> getAccessLevelString
getAccessibilityStrictly -> getAccessLevelStrictly
getAccessibilityUID -> getAccessLevelUID
getActualAccessibility -> getActualAccessLevel
getDefaultAccessibility -> getDefaultAccessLevel
getMaxAccessibility -> getMaxAccessLevel
getOverridableAccessibility -> getOverridableAccessLevel
getRawStableAccessibility -> getRawStableAccessLevel
getSetterAccessibility -> getSetterFormalAccess
hasAccessibility -> hasAccess
hasDefaultAccessibility -> hasDefaultAccessLevel
inferAccessibility -> inferAccessLevel
inferDefaultAccessibility -> inferDefaultAccessLevel
inferSetterAccessibility -> inferSetterAccessLevel
overwriteAccessibility -> overwriteAccess
overwriteSetterAccessibility -> overwriteSetterAccess
printAccessibility -> printAccess
requiredAccessibilityForDiagnostics -> requiredAccessForDiagnostics
resolveAccessibility -> resolveAccessControl
setAccessibility -> setAccess
setSetterAccessibility -> setSetterAccess
setDefaultAndMaxAccessibility -> setDefaultAndMaxAccess
validateAccessibility -> validateAccessControl

Accessibility -> AccessLevel
AccessibilityFilter -> AccessFilter
IgnoreAccessibility -> IgnoreAccessControl
NL_IgnoreAccessibility -> NL_IgnoreAccessControl
PrintAccessibility -> PrintAccess
PrintInternalAccessibilityKeyword -> PrintInternalAccessKeyword
SetterAccessibility -> SetterAccessLevel

setterAccessibility -> setterAccess
storedPropertyAccessibility -> storedPropertyAccess

-print-accessibility -> -print-access
2017-08-28 11:11:57 -07:00
Mark Lacey
e726ad47fe Merge pull request #11556 from rudkx/fix-rdar33296619
[Type checker] Guard against recursing into non-single-expression clo…
2017-08-23 21:25:02 -07:00
Mark Lacey
b6dd52a531 [Type checker] Guard against recursing into non-single-expression closures.
In diagnoseUnintendedOptionalBehavior, guard against recursing into
non-single-expression closures. If we recurse into these, we can end
up hitting malformed ASTs that we do not expect. These happen when we
successfully type-check the code outside of the closure, but the
closure body has errors.

There are potentially other similar issues in other miscellaneous
diagnostics walks.

I opened https://bugs.swift.org/browse/SR-5758 to remind us to review
these and ensure they do not have similar issues.
2017-08-23 17:12:04 -07:00
Joe Groff
90377199cf Sema: Track ReferenceWritableKeyPath applications as reads of their base.
Fixes SR-5673 | rdar://problem/33881533.
2017-08-21 15:06:18 -07:00
Rintaro Ishizaki
344c860c53 [Sema] Remove C-style for statement diagnostics from Sema 2017-08-04 23:27:22 +09:00
Robert Widmann
28537d9d75 Push __shared through Sema
* Update usage checking to account for __shared parameters as immutable

* Allow pattern type checking to resolve Shared parameters to the appropriate parameter specifiers

* Add the __shared protocol requirement restriction
2017-07-23 21:47:25 -07:00
John McCall
c0b3bf1711 Suppress access enforcement when an l-value is converted to a pointer
just for pointer identity.

The current technique for deciding whether that's the case is *extremely*
hacky and need to be replaced with an attribute, but I'm reluctant to
take that on so late in the schedule.  The hack is terrible but not too
hard to back out in the future.  Anyone who names a method like this just
to get the magic behavior knows well that they are not on the side of
righteousness.

rdar://33265254
2017-07-21 23:40:04 -04:00
Robert Widmann
8cdddef2f8 Refactor Params to use flags
Also, begin to pass around base types instead of raw InOutType types.  Ideally, only Sema needs to deal with them, but this means that a bunch of callers need to unwrap any inouts that might still be lying around before forming these types.

Multiple parts of the compiler were slicing, dicing, or just dropping these flags.  Because I intend to use them for the new function type representation, I need them to be preserved all across the compiler.  As a first pass, this stubs in what will eventually be structural rules as asserts and tracks down all callers of consequence to conform to the new invariants.

This is temporary.
2017-07-19 09:49:32 -07:00
Slava Pestov
83684eb08a Sema: Remove LookupResult::Entry in favor of AST's LookupResultEntry
Both types store a base declaration and a result declaration;
we can remove one. Soon, it will change to store a base
declaration context, instead.
2017-07-18 22:09:19 -07:00
John McCall
dcc65ca39e Fix the fix-it for replacing C-style for loops, enhance it to handle
inclusive ranges, and actually add a test to catch future regressions.

rdar://33329301
2017-07-18 00:46:51 -04:00
Robert Widmann
957d633185 Rename getInOutOrLValueObjectType to getWithoutSpecifierType
Prepares the AST for a future in which more than just inout and
@lvalue need to be stripped off of ephemeral types.
2017-07-06 09:35:04 -07:00
Alex Hoppen
0b286151cc [Diag] Diagnostic adjustment for special DeclNames
The diagnostic warn_unqualified_access cannot exist with special
DeclNames and shall thus take an Identifier as parameter
2017-07-01 13:29:02 +02:00
Alex Hoppen
949968a182 Adjust printing to take into account special DeclNames
Print DeclBaseNames using a new userFacingStr() method to prepare for
DeclBaseNames that are not backed by Identifiers
2017-06-24 11:39:09 +02:00
Huon Wilson
787736d6b8 [Sema] Suggest #selector(self.foo) instead of #selector(TypeName.foo) when possible.
When inside a declaration or extension of TypeName, humans usually don't write
the full typename like #selector(TypeName.foo), but instead prefer the neater
form #selector(self.foo). The compiler has enough information to do this too.

Fixes rdar://problem/25284692 .
2017-06-23 14:55:53 -07:00
John McCall
c131ea4d18 Implement the restriction on calls to non-escaping function parameters from the SE-0176 revision. 2017-06-20 17:29:20 -04:00
Slava Pestov
a6339be1cd Sema: Fix historical quirk with @escaping diagnostics
Saying "implicitly non-escaping because it was declared @autoclosure"
does not make sense. Since Swift 3, parameters of function type are
non-escaping by default, whether or not they are @autoclosure.

We would also inhibit the fixit for inserting @escaping if the
@autoclosure attribute was present. Again, a holdover from the
Swift 2 days, when @autoclosure implied @noescape and the special
@autoclosure(escaping) attribute was used to define an escaping
autoclosure.
2017-06-17 00:48:07 -07:00