Commit Graph

828 Commits

Author SHA1 Message Date
Robert Widmann
8d23005cbe Improve fallthrough diagnostics 2016-07-30 13:00:53 -07:00
Robert Widmann
9c83e7223e Fixup diagnostics around type(of:) and dynamicType handling
Be laxer about the parsing for type(of:) so as not to get in the way of
other declarations that may use ‘type’ as their base name.
2016-07-30 03:24:05 -07:00
Robert Widmann
4e5665a8bd [SE-0096] Remove support for dynamicType member 2016-07-30 03:24:05 -07:00
Robert Widmann
80fb5c1c5a Remove usage of getMetatypeLoc() 2016-07-29 16:59:14 -07:00
Richard Wei
5358c11519 [SE-0096] [Parse] Implement type(of:) expression 2016-07-29 16:57:54 -07:00
Doug Gregor
b40c89d4c6 [AST] Carry AST names through call expressions. 2016-07-26 11:23:57 -07:00
Doug Gregor
5cce5c4d1d [Parser] Refactor parsing of calls and call-like expressions.
Rather than parsing the call arguments (or similar, e.g., subscript)
as a parenthesized expression or tuple, then later reworking that
ParenExpr/TupleExpr if a trailing closure comes along, then digging
through that ParenExpr/TupleExpr to pull out the arguments and
trailing closure... just parse the expression list and trailing
closure together, then directly form the appropriate AST node with
arguments/labels/label locations/trailing closure.

Fixes rdar://problem/19804707, which is an issue where trailing
closures weren't working with unresolved member expressions (e.g.,
".foo {... }"), and is a stepping-stone to SE-0111.
2016-07-26 08:40:11 -07:00
Doug Gregor
dad155705d [SE-0111] Store argument labels directly on object literal expressions. 2016-07-25 23:14:41 -07:00
Doug Gregor
5ca9f8852e [SE-0111] Store argument labels directly on unresolved member expressions. 2016-07-25 23:14:41 -07:00
Doug Gregor
33d8b469d4 [SE-0111] Store argument labels directly on subscript expressions.
Factor out the trailing storage of call arguments, since we'll need it
for a few different kinds of expression nodes. Use it for both
CallExpr (which already had this storage, albeit with a specialized
implementation) and now SubscriptExpr.
2016-07-25 23:14:41 -07:00
Doug Gregor
604adff1bd [SE-0111] Capture argument labels directly in CallExpr.
Yet another step on the way to SE-0111, capture the argument labels
(and their locations) directly in CallExpr, rather than depending on
them being part of the tuple argument.
2016-07-25 23:14:41 -07:00
Doug Gregor
fdcf45b497 [AST] Introduce factory methods to create CallExprs.
Introduce several new factory methods to create CallExprs, and hide
the constructor. The primary reason for this refactor is to start
moving clients over to the factory method that takes the call
arguments separately from the argument labels. Internally, it
repackages those arguments into a TupleExpr or ParenExpr (as
appropriate) so the result ASTs are the same. However, this will make
it easier for us to tease out the arguments themselves in the
implementation of SE-0111.
2016-07-25 13:27:35 -07:00
Joe
67dccb283e [SE-0095] Code feedback changes; Any is parsed as a keyword
- Any is made into a keyword which is always resolved into a TypeExpr,
allowing the removal of the type system code to find TheAnyType before
an unconstrained lookup.
- Types called `Any` can be declared, they are looked up as any other
identifier is
- Renaming/redefining behaviour of source loc methods on
ProtocolCompositionTypeRepr. Added a createEmptyComposition static
method too.
- Code highlighting treats Any as a type
- simplifyTypeExpr also does not rely on source to get operator name.
- Any is now handled properly in canParseType() which was causing
generic param lists containing ‘Any’ to fail
- The import objc id as Any work has been relying on getting a decl for
the Any type. I fix up the clang importer to use Context.TheAnyType
(instead of getAnyDecl()->getDeclaredType()). When importing the id
typedef, we create a typealias to Any and declare it unavaliable.
2016-07-19 12:01:37 -07:00
Joe
a6dad0091b [SE-0095] Initial parsing implementation for '&' composition syntax
This commit defines the ‘Any’ keyword, implements parsing for composing
types with an infix ‘&’, and provides a fixit to convert ‘protocol<>’

- Updated tests & stdlib for new composition syntax
- Provide errors when compositions used in inheritance.
Any is treated as a contextual keyword. The name ‘Any’
is used emit the empty composition type. We have to
stop user declaring top level types spelled ‘Any’ too.
2016-07-19 12:01:02 -07:00
Rintaro Ishizaki
4bf1c34f80 [Parse/Sema][SR-1672] Improve diagnostics for trailing closures in stmt-condition (#3184)
Fix-it suggests normal argument expression, instead of of enclosing whole
expression with parens.

* Moved diagnostic logic to Sema, because we have to add correct argument
  label for the closure.

    if arr.starts(with: IDs) { $0.id == $2 } { ... }
                           ~~^
                           , isEquivalent:  )

* We now accept trailing closures for each expressions and right before `where`
  clause, as well as closures right before the body.

    if let _ = maybeInt { 1 }, someOtherCondition { ... }
                       ~^
                       (     )

    for let x in arr.map { $0 * 4 } where x != 0 { ... }
                        ~^
                        (          )
2016-07-09 12:51:51 +09:00
Harlan
162648d219 Add fixit for '-> throws' to 'throws ->' (#3335) 2016-07-05 11:29:55 -07:00
Chris Lattner
45118037cc When we lex an invalid leftbound dot, instead of emitting an error and swallowing it
silently, have the lexer return it as an unknown token.  Enhance the expr parser to
detect these things and squash any expression in progress into an ErrorExpr.  This
allows us to silence really bad downstream errors.  For example, on:

struct S { func f() {} }
func f() {
  _ = S.
}

we formerly produced:

 x.swift:5:8: error: expected member name following '.'
 x.swift:5:7: error: expected member name or constructor call after type name
 x.swift:5:7: note: add arguments after the type to construct a value of the type
 x.swift:5:7: note: use '.self' to reference the type object

we now emit just the first one.  This fixes:
<rdar://problem/22290244> QoI: "UIColor." gives two issues, should only give one
2016-07-03 16:04:35 -07:00
Alex Hoppen
884af9ccab [Parser] Make #selector a primary expression
This allows #selector to be followed by a trailing `?` in case statements as reported in SR-1827
2016-06-27 22:11:58 +02:00
Doug Gregor
814a08a5da [IDE] Code completion for Objective-C #keyPath expressions.
Implement code completion support for Objective-C #keyPath
expressions, using semantic analysis of the partially-typed keypath
argument to provide an appropriate set of results (i.e., just
properties and types).

This implements all of the necessary parts of SE-0062 / SR-1237 /
rdar://problem/25710611, although at some point I'd like to follow it
up with some warnings to help migrate existing string literals to
2016-05-21 22:28:51 -07:00
Doug Gregor
9f0cec4984 SE-0062: Implement #keyPath expression.
Implement the Objective-C #keyPath expression, which maps a sequence
of @objc property accesses to a key-path suitable for use with
Cocoa[Touch]. The implementation handles @objc properties of types
that are either @objc or can be bridged to Objective-C, including the
collections that work with key-value coding (Array/NSArray,
Dictionary/NSDictionary, Set/NSSet).

Still to come: code completion support and Fix-Its to migrate string
literal keypaths to #keyPath.

Implements the bulk of SR-1237 / rdar://problem/25710611.
2016-05-18 23:30:15 -07:00
Alex Hoppen
a0a74aeb46 SE-0064 / SR-1239: Code completion for #selector of property getters/setters.
Implement basic code completion support for #selector with property
getters/setters. The vast majority of this implementation comes from
Alex Hoppen (@ahoppen), with only a handful of my own tweaks. Alex has
more interesting ideas on improving this that I wasn't quite ready to
commit to, so this is more basic than the overall goal.
2016-05-11 23:03:37 -07:00
Alex Hoppen
d2e045c8b5 Implement SE-0064 / SR-1239: #selector for property getters and setters
Implements the core functionality of SE-0064 / SR-1239, which
introduces support for accessing the Objective-C selectors of the
getter and setter of an @objc property via #selector(getter:
propertyName) and #selector(setter: propertyName).

Introduce a bunch of QoI around mistakes using #selector to refer to a
property without the "getter:" or "setter:", using Fix-Its to help the
user get it right. There is more to do in this area, still, but we
have an end-to-end feature working.

Much of the implementation and nearly all of the test cases are from
Alex Hoppen (@ahoppen). I've done a bit of refactoring, simplified the
AST representation, and replaced Alex's custom
expression-to-declaration logic with an extension to the constraint
solver. The last bit might be short-lived, based on swift-evolution
PR280, which narrows the syntax of #selector considerably.
2016-05-11 16:51:27 -07:00
Chris Lattner
14c7a3dafe implement SE-0071 - Allow (most) keywords in member references 2016-05-02 22:31:14 -07:00
Chris Lattner
7035da2786 fix a refactoro that I introduced in 561c379 that broke IDE/complete_at_top_level.swift. 2016-05-02 21:48:40 -07:00
Chris Lattner
561c379eba refactor the parsing logic for "x.y" to detangle it a bit, NFC. 2016-05-02 21:33:52 -07:00
practicalswift
540cb751ac [gardening] Fix recently introduced typo: "adavances" → "advances"
[gardening] Fix recently introduced typo: "compie" → "compile"

[gardening] Fix recently introduced typo: "consise" → "concise"

[gardening] Fix recently introduced typo: "dengerate" → "degenerate"

[gardening] Fix recently introduced typo: "encodeded" → "encoded"

[gardening] Fix recently introduced typo: "equvalent" → "equivalent"

[gardening] Fix recently introduced typo: "guaranted" → "guaranteed"

[gardening] Fix recently introduced typo: "overridde" → "override"

[gardening] Fix recently introduced typo: "subsquence" → "subsequence"

[gardening] Fix recently introduced typo: "substraction" → "subtraction"

[gardening] Fix recently introduced typo: "whiltespace" → "whitespace"
2016-05-01 20:07:18 +02:00
Ted Kremenek
b8bbed8c13 [WIP] Implement SE-0039 (Modernizing Playground Literals) (#2215)
* Implement the majority of parsing support for SE-0039.

* Parse old object literals names using new syntax and provide FixIt.

For example, parse "#Image(imageLiteral:...)" and provide a FixIt to
change it to "#imageLiteral(resourceName:...)".  Now we see something like:

test.swift:4:9: error: '#Image' has been renamed to '#imageLiteral
var y = #Image(imageLiteral: "image.jpg")
        ^~~~~~ ~~~~~~~~~~~~
        #imageLiteral resourceName

Handling the old syntax, and providing a FixIt for that, will be handled in a separate
commit.

Needs tests.  Will be provided in later commit once full parsing support is done.

* Add back pieces of syntax map for object literals.

* Add parsing support for old object literal syntax.

... and provide fixits to new syntax.

Full tests to come in later commit.

* Improve parsing of invalid object literals with old syntax.

* Do not include bracket in code completion results.

* Remove defunct code in SyntaxModel.

* Add tests for migration fixits.

* Add literals to code completion overload tests.

@akyrtzi told me this should be fine.

* Clean up response tests not to include full paths.

* Further adjust offsets.

* Mark initializer for _ColorLiteralConvertible in UIKit as @nonobjc.

* Put attribute in the correct place.
2016-04-25 07:19:26 -07:00
Chris Willmore
02a6be6d01 Allow parsing of function types in expr position (#2273)
Previously it was not possible to parse expressions of the form

    [Int -> Int]()

because no Expr could represent the '->' token and be converted later
into a FunctionTypeRepr. This commit introduces ArrowExpr which exists
solely to be converted to FunctionTypeRepr later by simplifyTypeExpr.

https://bugs.swift.org/browse/SR-502
2016-04-22 21:53:26 -07:00
Xi Ge
a41c9eb8e6 [Sema] Suggested by Jordan, refine a recently added diagnosis message. 2016-04-18 16:55:03 -07:00
Xi Ge
6a660e23d9 [Sema] Suggested by Jordan, use another diagnosis message when fixit is available so that they are more cohesive. 2016-04-15 16:37:38 -07:00
Xi Ge
152eeaa1fe [Sema] Add a fixit for diagnosis "variable used within its own initial value".
This fixit checks if a decl with the identical name can be found in the parent type
context; if can, we add "self." to try to resolve the issue. rdar://25389852
2016-04-14 15:55:01 -07:00
practicalswift
d00a5ef814 [gardening] Weekly gardening: typos, duplicate includes, header formatting, etc. 2016-03-24 22:41:10 +01:00
Chris Lattner
30ec0f4128 Fix <rdar://23036383> QoI: Invalid trailing closures in stmt-conditions produce lowsy diagnostics
It is a common problem that people use a call to a function with a
trailing closure in a if condition, foreach loop, etc.  These don't allow
trailing closures, because they are ambiguous with the brace-enclosed body
of the conditional statement.

In an effort to improve QoI on this, perform lookahead to disambiguate the most common
form of this, in a very narrow situation.  This allows us to produce a nice error
with fixit hints like:

t.swift:26:25: error: trailing closure requires parentheses for disambiguation in this context
for _ in numbers.filter {$0 > 4} {
                        ^

instead of spewing out this garbage:

t.swift:26:26: error: anonymous closure argument not contained in a closure
for _ in numbers.filter {$0 > 4} {
                         ^
t.swift:26:33: error: consecutive statements on a line must be separated by ';'
for _ in numbers.filter {$0 > 4} {
                                ^
                                ;
t.swift:26:34: error: statement cannot begin with a closure expression
for _ in numbers.filter {$0 > 4} {
                                 ^
t.swift:26:34: note: explicitly discard the result of the closure by assigning to '_'
for _ in numbers.filter {$0 > 4} {
                                 ^
                                 _ =
t.swift:26:34: error: braced block of statements is an unused closure
for _ in numbers.filter {$0 > 4} {
                                 ^
t.swift:26:18: error: type '(@noescape (Int) throws -> Bool) throws -> [Int]' does not conform to protocol 'Sequence'
for _ in numbers.filter {$0 > 4} {
                 ^
t.swift:26:34: error: expression resolves to an unused function
for _ in numbers.filter {$0 > 4} {
                                 ^
2016-03-22 22:06:06 -07:00
Ben Langmuir
f59b897bb3 Fix my recently introduced 80-col violation 2016-03-14 23:18:43 -07:00
Ben Langmuir
ff895d2f5e [CodeCompletion] Fix completion in string literal interpolation at top-level
Mostly this was just returning the ParserStatus bits that we got from
parseExprList from parseExprStringLiteral. The rest was just cleaning up
places that didn't handle EOF very well, which is important here because
the code completion token is buried in the string literal, so the
primary lexer will walk past it.

rdar://problem/17101944
2016-03-14 23:13:15 -07:00
Doug Gregor
d52530970e Merge pull request #1550 from kballard/use-declname-for-scope
[Parse] Fix lookup of foo(_:bar:) expressions
2016-03-14 11:36:04 -07:00
Ben Langmuir
d12a2d7df2 [CodeCompletion] Fix completion in top-level 'is' and 'as' expressions
We forgot to return a code-completion status, so we didn't get to it in
the second phase.

"a" as <here>

rdar://problem/24376361
2016-03-07 21:16:26 -08:00
Kevin Ballard
502b159400 [Parse] Store DeclNames in Scope instead of Identifiers
This fixes a problem where compound names like `foo(_:bar:)` were being
resolved to variables declared with the same base name.

Fixes most of SR-880.
2016-03-04 23:42:17 -08:00
Jordan Rose
6272941c5c Rename "build configurations" to "conditional compilation blocks".
...because "build configuration" is already the name of an Xcode feature.

- '#if' et al are "conditional compilation directives".
- The condition is a "conditional compilation expression", or just
  "condition" if it's obvious.
- The predicates are "platform conditions" (including 'swift(>=...)')
- The options set with -D are "custom conditional compilation flags".
  (Thanks, Kevin!)

I left "IfConfigDecl" as is, as well as SourceKit's various "BuildConfig"
settings because some of them are part of the SourceKit request format.
We can change these in follow-up commits, or not.

rdar://problem/19812930
2016-02-12 11:09:26 -08:00
Doug Gregor
ab31f08848 [Parser] Disambiguate between object literals and collection literals.
Now that we have expressions that start with #, the [# introducer for
object literals is no longer guaranteed to indicate an object
literal. For example:

    [#line, #column]

is an array literal and

    [#line : #column]

is a dictionary literal. Use additional lookahead in the parser to
disambiguate these cases from object literals. Fixes
rdar://problem/24533081.
2016-02-05 17:14:20 -08:00
Chris Lattner
0619e57a61 Switch the stdlib to use #file instead of __FILE__, and deprecate the __FILE__ identifiers.
This also updates the tests that would otherwise fail.
2016-02-04 15:08:31 -08:00
Chris Lattner
8dedfb31e3 Add support for #file/#line, etc according to SE-0028. __FILE__ and friends
are still accepted without deprecation warning as of this patch.
2016-02-04 14:22:22 -08:00
Chris Lattner
758791f99c Improve error recovery for && in stmt-condition when followed by
let/var/case.  Thanks to Radek Pietruszewski for noticing this!
2016-02-03 12:38:56 -08:00
Chris Lattner
0d57fe5b34 Fix <rdar://problem/24467411> QoI: Using "&& #available" should fixit to comma
This tweet: https://twitter.com/radexp/status/694561060230184960 pointed out
the sad truth that most people don't know that stmt-condition can contain
(including a fixit) when they try to use && instead of commas between clauses.

Before:

t.swift:4:16: error: #available may only be used as condition of an 'if', 'guard' or 'while' statement
  if x == y && #available(iOS 9, *) { }
               ^
t.swift:5:27: error: expected '{' after 'if' condition
  if #available(iOS 9, *) && x == y {}
                          ^
t.swift:5:37: error: braced block of statements is an unused closure
  if #available(iOS 9, *) && x == y {}
                                    ^
t.swift:5:37: error: expression resolves to an unused function
  if #available(iOS 9, *) && x == y {}
                                    ^~

After:

t.swift:4:13: error: expected ',' joining parts of a multi-clause condition
  if x == y && #available(iOS 9, *) { }
            ^~
            ,
t.swift:5:27: error: expected ',' joining parts of a multi-clause condition
  if #available(iOS 9, *) && x == y {}
                          ^~
                          ,
2016-02-02 22:20:20 -08:00
Doug Gregor
f5cb1151c1 [Code completion] Code complete compound function names within #selector.
When we're code completing a postfix or dot expression inside the
subexpression of an #selector expression, prefer compound function
names. This helps us write, e.g.,

  #selector(UIView.

and get completions such as "insertSubview(_:aboveSubview:)". Fixes
rdar://problem/24470075.
2016-02-02 16:06:32 -08:00
Denis Vnukov
4f92a08987 Adding a location of the var/let/inout to ParamDecl 2016-01-27 13:43:42 -08:00
Denis Vnukov
db6582c72b Fixed several AST nodes wrongly marked with implicit/explicit flag 2016-01-27 13:43:39 -08:00
Doug Gregor
dccf3155f1 SE-0022: Implement parsing, AST, and semantic analysis for #selector. 2016-01-26 21:12:04 -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
Doug Gregor
53ed3416a5 [Parser] unqualified-identifier --> unqualified-decl-name. NFC 2016-01-21 15:06:09 -08:00