Commit Graph

591 Commits

Author SHA1 Message Date
Brent Royal-Gordon
c504eb1b0a Warn if magic identifiers don’t match
When wrapping a function which is supposed to capture the caller’s location, there’s always a risk that the wrapper won’t capture the information the wrapped function wants; for instance, you might pass `(…, line, column)` where the callee expected `(…, column, line)`.

This commit emits a warning when a call passes an explicit argument to something that has a default argument, and that explicit argument is itself a parameter with a default argument, and both parameters use magic identifiers, but they use *different* magic identifiers.  This is partially in support of concise #file, but applies to all magic identifiers.

Fixes rdar://problem/58588633.
2020-01-21 14:28:16 -08:00
Frederick Kellison-Linn
71697c37ca Allow implicit self in escaping closures when self usage is unlikely to cause cycle (#23934)
* WIP implementation

* Cleanup implementation

* Install backedge rather than storing array reference

* Add diagnostics

* Add missing parameter to ResultFinderForTypeContext constructor

* Fix tests for correct fix-it language

* Change to solution without backedge, change lookup behavior

* Improve diagnostics for weak captures and captures under different names

* Remove ghosts of implementations past

* Address review comments

* Reorder member variable initialization

* Fix typos

* Exclude value types from explicit self requirements

* Add tests

* Add implementation for AST lookup

* Add tests

* Begin addressing review comments

* Re-enable AST scope lookup

* Add fixme

* Pull fix-its into a separate function

* Remove capturedSelfContext tracking from type property initializers

* Add const specifiers to arguments

* Address review comments

* Fix string literals

* Refactor implicit self diagnostics

* Add comment

* Remove trailing whitespace

* Add tests for capture list across multiple lines

* Add additional test

* Fix typo

* Remove use of ?: to fix linux build

* Remove second use of ?:

* Rework logic for finding nested self contexts
2019-12-20 02:38:41 +00:00
Brent Royal-Gordon
6a8598a99c [NFC] Remove DeclNameRef staging calls 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
da88512eda [NFC] Take DeclNameRef in UnqualifiedLookup and lookupQualified() 2019-12-11 00:55:17 -08:00
Brent Royal-Gordon
9a57554bdf [NFC] DeclNameRef-ify qualified and unqualified lookup 2019-12-11 00:55:17 -08:00
Rintaro Ishizaki
83f9a52cb8 Merge pull request #28362 from owenv/improve_trailing_closure_in_condition_diag_2
[Parse] Reapply trailing closure in condition fix and downgrade to warning
2019-11-22 17:05:48 +09:00
Owen Voorhees
a3946cdb50 Revert "Revert "[Diagnostic] Improve diagnostic for trailing closures in statement conditions (#25165)""
This reverts commit e3a6b67c63.
2019-11-19 13:24:20 -08:00
Hamish Knight
2853615880 Add type to package inputs to UnqualifiedLookupRequest
This allows us use an OptionSet parameter for
the request (as currently we can't directly use it
as a parameter due to not having an == definition
for it). It also allows us to regain default
arguments for the source loc and flag parameters.
2019-11-15 16:35:14 -08:00
Hamish Knight
3aa7158d6c Switch UnqualifiedLookup callers over to UnqualifiedLookupRequest 2019-11-15 14:25:42 -08:00
Robert Widmann
911e749e4e [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/2528469
2019-11-11 12:35:03 -08:00
Suyash Srijan
fab6ce9587 [MiscDiagnostics] Diagnose passing a non-@objc dynamic KeyPath property to KVO observe method 2019-11-08 18:52:43 +00:00
Hamish Knight
be8415371a performSyntacticExprDiagnostics doesn't need a TypeChecker 2019-11-07 12:41:37 -08:00
Hamish Knight
bd6fd38eb2 performStmtDiagnostics doesn't need a TypeChecker 2019-11-07 12:41:37 -08:00
Hamish Knight
d8dfbbcd9f performAbstractFuncDeclDiagnostics doesn't need a TypeChecker 2019-11-07 12:41:37 -08:00
Hamish Knight
fb55c032f8 diagnoseUnownedImmediateDeallocation doesn't need a TypeChecker 2019-11-07 12:41:37 -08:00
Hamish Knight
b57c86dc1c fixItEncloseTrailingClosure doesn't need a TypeChecker 2019-11-07 12:41:36 -08:00
Hamish Knight
cb0c9adc46 Use getXXXType over getXXXDecl in a bunch of places 2019-11-07 08:26:08 -08:00
Hamish Knight
18088b04f0 [AST] Consolidate Obj-C types on ASTContext
This commit moves the getNSObjectType and
getObjCSelectorType methods from TypeChecker
onto ASTContext. In addition, it moves the
FOR_KNOWN_FOUNDATION_TYPES macro into a separate
file to define each of the Obj-C type decls
we want to have access to.
2019-11-07 08:26:08 -08:00
Robert Widmann
7b9d28d7ba Make diagnoseSelfAssignment a utility 2019-11-06 15:08:59 -08:00
Robert Widmann
4996858c4d Re-implement isInvalid for ValueDecls 2019-10-30 15:09:14 -07:00
Robert Widmann
da2b063af9 Make calls to lookupMember actually look static 2019-10-30 12:55:42 -07:00
Robert Widmann
bcf03aa019 Make getDeclTypeCheckingSemantics a utility 2019-10-30 12:55:10 -07:00
Hamish Knight
642f58d641 Make checkUnsupportedProtocolType a static member 2019-10-30 07:56:32 -07:00
Robert Widmann
3b829943af Uniformly iterate over the pattern binding entry indices
Clarify a bunch of C-style for loops and remove a ton of references to getPatternList().
2019-10-17 13:39:07 -07:00
Robert Widmann
56b6e53dae Remove raw references to PatternBindingEntry APIs
Switch most callers to explicit indices.  The exceptions lie in things that needs to manipulate the parsed output directly including the Parser and components of the ASTScope.  These are included as friend class exceptions.
2019-10-17 13:31:14 -07:00
Robert Widmann
7c1e3949ed [NFC] Refactor Override Fixit Emission
Refactor diagnostic emission so it lazily emits notes and fixits.
This is a necessary evil in a world where an arbitrary request can also
emit diagnostics, especially circularity diagnostics.  In the future, we
should assert in the evaluator that there is no active diagnostic to
catch the rest of these.
2019-10-15 17:28:00 -07:00
Robert Widmann
742f6b2102 Drastically Simplify VarDecl Validation
This is an amalgam of simplifications to the way VarDecls are checked
and assigned interface types.

First, remove TypeCheckPattern's ability to assign the interface and
contextual types for a given var decl.  Instead, replace it with the
notion of a "naming pattern".  This is the pattern that semantically
binds a given VarDecl into scope, and whose type will be used to compute
the interface type. Note that not all VarDecls have a naming pattern
because they may not be canonical.

Second, remove VarDecl's separate contextual type member, and force the
contextual type to be computed the way it always was: by mapping the
interface type into the parent decl context.

Third, introduce a catch-all diagnostic to properly handle the change in
the way that circularity checking occurs.  This is also motivated by
TypeCheckPattern not being principled about which parts of the AST it
chooses to invalidate, especially the parent pattern and naming patterns
for a given VarDecl.  Once VarDecls are invalidated along with their
parent patterns, a large amount of this diagnostic churn can disappear.
Unfortunately, if this isn't here, we will fail to catch a number of
obviously circular cases and fail to emit a diagnostic.
2019-10-14 12:06:50 -07:00
Slava Pestov
044204a5d5 Sema: Don't synthesize null bodies for invalid accessors
We don't want hasBody() == true and getBody() == nullptr.
2019-10-03 17:11:44 -04:00
Slava Pestov
bbce38409c Sema: Decouple availability checking from the TypeChecker 2019-09-24 22:51:23 -04:00
Robert Widmann
f053f9c480 Port getInterfaceType() patterns in libSema 2019-09-23 16:49:29 -07:00
Robert Widmann
e0a41b19cb Break some cycles
Computing the generic signature changes the way that cycles appear in the compiler.  For now, break these cycles.  We should investigate each place where hasComputedGenericSignature() is used in service of breaking cycles.  See rdar://55263708
2019-09-16 12:50:24 -07:00
Jordan Rose
dc59cd2043 [Sema] Stop visiting existential exprs an extra time checking for uses (#27150)
This was causing an exponential amount of time traversing the AST with
deeply chained protocol extension methods, such as in the
TestCodableRouter.swift test in Kitura.

If the OpaqueValueExpr is referenced more than once within the
OpenExistentialExpr it'll still get visited more than once, but that
doesn't seem to happen in practice. If it turns out to be a problem,
we can weaken the assertion I'm adding here.

https://bugs.swift.org/browse/SR-11012
2019-09-12 20:21:22 -07:00
Slava Pestov
4668def6b3 Sema: Split addImplicitConstructors() into separate functions for structs and classes 2019-09-03 22:39:35 -04:00
Slava Pestov
2dbeeb0d3f AST: Make SubstFlags::UseErrorType the default behavior
We've fixed a number of bugs recently where callers did not expect
to get a null Type out of subst(). This occurs particularly often
in SourceKit, where the input AST is often invalid and the types
resulting from substitution are mostly used for display.

Let's fix all these potential problems in one fell swoop by changing
subst() to always return a Type, possibly one containing ErrorTypes.

Only a couple of places depended on the old behavior, and they were
easy enough to change from checking for a null Type to checking if
the result responds with true to hasError().

Also while we're at it, simplify a few call sites of subst().
2019-08-22 01:07:50 -04:00
Slava Pestov
80ccbe5116 AST: Stop passing around a LazyResolver in name lookup
Note that in all cases it was either nullptr or ctx.getLazyResolver().
While passing in nullptr might appear at first glance to mean something
("don't type check anything"), in practice we would check for a nullptr
value and pull out ctx.getLazyResolver() instead. Furthermore, with
the lazy resolver going away (at least for resolveDeclSignature() calls),
it won't make sense to do that anymore anyway.
2019-08-19 23:00:57 -04:00
Slava Pestov
19d283d9dc AST: Replace ImplicitlyUnwrappedOptionalAttr with Decl::{is,set}ImplicitlyUnwrappedOptional() 2019-08-15 18:41:41 -04:00
Slava Pestov
2ef101c815 Sema: Don't add local functions to TC.definedFunctions
Instead, check them and their error handling right away.

In addition to fixing the crash in the radar, this also causes
us to emit unused variable warnings in functions containing
local functions.

Eventually, TC.definedFunctions should go away altogether.

Fixes <rdar://problem/53956342>.
2019-08-07 00:37:21 -04:00
Slava Pestov
64c32c695b AST: Remove a few utility methods from AbstractStorageDecl
Since the return value of getAccessor() depends on mutable state, it
does not make sense in the request evaluator world. Let's begin by
removing some utility methods derived from getAccessor(), replacing
calls to them with calls to getAccessor().
2019-08-01 18:31:58 -04:00
Slava Pestov
a532a325e1 AST: Move a few methods from VarDecl down to ParamDecl 2019-07-22 20:19:09 -04:00
Doug Gregor
ff2cb3305d [Type checker] Factor "type check function body until" into a request.
Extend the "type check function body" request to also cover the case
where we have a specific ending source location. Fold all of this
functionality into a single request, so we consistently go through a
request to compute a type-checked function body.
2019-07-12 17:40:53 -07:00
Jordan Rose
e3a6b67c63 Revert "[Diagnostic] Improve diagnostic for trailing closures in statement conditions (#25165)"
This reverts commit 4da49fcfee.
2019-06-28 13:59:07 -07:00
Owen Voorhees
4da49fcfee [Diagnostic] Improve diagnostic for trailing closures in statement conditions (#25165)
This improves the diagnostic for trailing closures in statement conditions to catch cases where one or more trailing closures are used in a chain composed of multiple calls
2019-06-28 09:08:50 -07:00
gregomni
92eb874e0e Insert access level fixits in front of 'override', if it's there, instead of
after. This is so that code produced by this fixit matches the same keyword
order as code produced by autocompleting a method decl in a new subclass.
2019-06-27 06:57:02 -07:00
Slava Pestov
5826db5d56 AST: Add AbstractFunctionDecl::hasDynamicSelfResult() and use it
This calculates a result directly from the function's result type
instead of checking a bit that was previously set by the type
checker. Also, always returns true for constructors to simplify
some callers.
2019-06-26 01:10:12 -04:00
Jordan Rose
9e471e4124 Don't offer a fix-it to convert to a 'some' type (#25619)
There's no way to spell an opaque type in a cast, and that means
there's no way to force the conversion today (even if you know the
types are dynamically the same).

Part of rdar://problem/50346954
2019-06-19 19:17:32 -07:00
Parker Schuh
823ba0bb73 Convert ObjectLiteralExpr to not use tc.callWitness() or generate a SemanticExpr.
For reference, all other literal types except dictionaries and string
interpolation have been converted over to this form.
2019-06-11 02:47:32 -07:00
Suyash Srijan
9ae2c85991 [Typechecker] Use CallArgs for checking duplicate label & add new test cases 2019-05-17 23:38:34 +01:00
Suyash Srijan
92d41ef166 [Typechecker] Fix a crash 2019-05-17 00:57:40 +01:00
Suyash Srijan
b2c86709b1 [Typechecker] Check for CallExpr only, not ApplyExpr. Also, check for EnumElementDecl separately 2019-05-17 00:52:43 +01:00