808 Commits

Author SHA1 Message Date
Robert Widmann
96f2a04f55 Merge pull request #6490 from modocache/ast
[SR-2757][Sema] Mark VarDecl in capture lists
2017-01-05 21:36:16 -07:00
Robert Widmann
dfa41d625b Optimize Condition Resolution (#6279)
* Pack the bits for IfConfigDecls into Decl

* Don't open symbols into a module when evaluating canImport statements

The module loaders now have API to check whether a given module can be
imported without importing the referenced module.  This provides a
significant speed boost to condition resolution and no longer
introduces symbols from the referenced module into the current context
without the user explicitly requesting it.

The definition of ‘canImport’ does not necessarily mean that a full
import without error is possible, merely that the path to the import is
visible to the compiler and the module is loadable in some form or
another.

Note that this means this check is insufficient to guarantee that you
are on one platform or another.  For those kinds of checks, use
‘os(OSNAME)’.
2017-01-05 12:08:54 -07:00
Brian Gesiak
4108e1d9af [Sema] Mark VarDecl in capture lists
Fixes SR-2757.

Variables in capture lists are treated as 'let' constants, which can
result in misleading, incorrect diagnostics. Mark them as such in order
to produce better diagnostics, by adding an extra parameter to the
VarDecl initializer.

Alternatively, these variables could be marked as implicit, but that
results in other diagnostic problems: capture list variables that are
never used produce warnings, but these warnings aren't normally emitted for
implicit variables. Other assertions in the compiler also misfire when
these variables are treated as implicit.

Another alternative would be to walk up the AST and determine whether
the `VarDecl`, but there doesn't appear to be a way to do so.
2017-01-01 12:41:06 -05:00
Brian Gesiak
1e001d434a Merge pull request #6298 from modocache/sr-2605
[Parse] Improve diagnostics for if-condition-else
2016-12-19 15:40:28 -05:00
Robert Widmann
9fd7b5a29c Validate the entire #if configuration
Being lazy here just means we don’t validate the entire if
configuration.  We need to be able to emit diagnostics even in
condition clauses with indeterminate expressions.
2016-12-19 02:32:47 -05:00
Brian Gesiak
d44dc26d1b [Parse] Improve diagnostics for if-condition-else
Addresses SR-2605.

If you're refactoring a `guard` into an `if`, it's easy to
accidentally end up with invalid code like this in the transition:

```
if condition else {
}
```

The parser rightly complains about the `else`, but has poor recovery
afterward, interpreting the following brace as a closure, which may in
turn lead to other unhelpful errors after:

```
error: expected '{' after 'if' condition
    if condition else {
                 ^

error: braced block of statements is an unused closure
    if condition else {
                      ^
```

Improve the diagnostics to instead explicitly detect this pattern and
mark it as an error.
2016-12-15 19:51:17 -05:00
Robert Widmann
cededef0d6 Add condition resolution as a new phrase post-parse
An unfortunately necessary thing to delay defrosting function bodies as
long as we can.
2016-12-14 15:39:19 -05:00
Robert Widmann
72beb9d80d Extract common code into StmtTransformer 2016-12-14 14:59:47 -05:00
Robert Widmann
a060eb5aca [SE-0075] Transfer the power of config resolution to Namebinding
This completely removes Parse’s ability to make any judgement calls
about compilation conditions, instead the parser-relevant parts of
‘evaluateConditionalCompilationExpr’ have been moved into
‘classifyConditionalCompilationExpr’ where they exist to make sure only
decls that we want to parse actually parse later.

The condition-evaluation parts have been moved into NameBinding in the
form of a Walker that evaluates and collapses IfConfigs.  This walker
is meant as an homage to PlaygroundLogger.  It should probably be
factored out into a common walker at some point in the future.
2016-12-14 14:59:47 -05:00
Robert Widmann
4426e410e5 [SE-0075] Do the naive thing.
Implemented in the naive way by way of the current ASTContext’s
‘LoadedModules’.  In theory this list should be in one-to-one
correspondence with the definition of “importable” as any module that
is required for compilation is loaded *before* we start parsing the
file.  This means we don’t have to load the module, just check that
it’s in the set of loaded modules.

Note that this approach will most likely fall apart if submodules are
introduced and are lazily loaded.  At that point this will need to be
refactored into a format that defers the checking of conditions
sometime before or during typechecking (after namebinding).
2016-12-14 14:59:47 -05:00
Rintaro Ishizaki
42ebd38c29 [Parse] Skip non-case-label statements in switch (#6215) 2016-12-13 03:37:09 +09:00
practicalswift
8962c8819f Merge pull request #6139 from practicalswift/remove-never-read-variables
[gardening] Remove never-read variables + tighten scope.
2016-12-12 13:32:36 +01:00
practicalswift
850ef04895 [gardening] Remove never-read bool HaveFirst. 2016-12-08 13:36:17 +01:00
Hugh Bellamy
732e3c5fdc Fix warnings building swift/Parse on Windows using MSVC 2016-12-07 22:20:49 +00:00
Rintaro Ishizaki
ec53b6885f [Parse] Let #if diretives be parsed in parseBraceItems()
parseBraceItems() has specific logic for pasing conditional compilation blocks.
Withoutout that, decralations in the block cannot be propagated to outside.
For instance:

  FOO: #if true
  func foo() {}
  #endif
  foo() // error: use of unresolved identifier 'foo'
2016-12-06 22:28:53 +09:00
Rintaro Ishizaki
54efbbbf87 [Parse] Let #sourceLocation be parsed in parseBraceItems()
Returning 'makeParserSuccess()' as a 'ParserResult<Stmt>' causes assertion error.

  Assertion `Status.isError()' failed
2016-12-06 22:28:53 +09:00
Argyrios Kyrtzidis
fc678740db [code-completion] Avoid a tentantive parse when in code-completion mode and make sure CodeCompletionExpr has a token range.
The tentantive parse is used for diagnostic purposes but can cause code-completion to delay the same decl twice.
The range of CodeCompletionExpr was previously character range which invalidated invariants of the AST.

Fixes:
	validation-test/IDE/crashers_fixed/084-swift-parser-consumedecl.swift
	validation-test/IDE/crashers_fixed/104-swift-gettypeofcompletioncontextexpr.swift
2016-12-05 17:09:49 -08:00
Slava Pestov
0f7a455d7d AST: Don't call setType() on AbstractFunctionDecls and EnumElementDecls 2016-11-29 03:05:33 -07:00
Rintaro Ishizaki
9b243526b8 [AST] Hold element name information in TupleTypeRepr
For now, only 'TupleTypeRepr::isParentType()' is using this informations.
Next commit will eliminate NamedTypeRepr.
2016-11-26 14:15:29 +09:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
David Farler
f450f0ccdf Revert "Preserve whitespace and comments during lexing as Trivia"
This reverts commit d6e2b58382.
2016-11-18 13:23:31 -08:00
David Farler
44f15558d6 Revert "Refactor: Rename Parser::consumeToken, consumeLoc. Add consumeToken API."
This reverts commit 39bfc123a3.
2016-11-18 13:23:31 -08:00
David Farler
ff98e5fd81 Merge pull request #5811 from bitjammer/syntax
Preserve source trivia in the lexer
2016-11-16 11:47:32 -08:00
Rintaro Ishizaki
887fb3750a [Parse] Fix range containment problem in statement condition
For constructing the dummy expression for missing initializer,
use the end location of the pattern instead of the location of the
current token.

Previous behavior used to build AST like:

  { if let x [eof]
       |---|       Pattern range
             |---| Dummy introducer range
    |------------| IfStmt range
  |--------|       BraceStmt range

Now:

  { if let x [eof]
       |---|       Pattern range
           |       Dummy introducer range
    |------|       IfStmt range
  |--------|       BraceStmt range
2016-11-16 10:30:15 +09:00
David Farler
39bfc123a3 Refactor: Rename Parser::consumeToken, consumeLoc. Add consumeToken API.
These APIs return SourceLocs, and eventually the Parser should consume
tokens, which now include source trivia such as whitespace and comments,
and package them into a purely syntactic tree.  Just a tiny step. NFC.
2016-11-15 16:11:57 -08:00
David Farler
d6e2b58382 Preserve whitespace and comments during lexing as Trivia
Store leading a trailing "trivia" around a token, such as whitespace,
comments, doc comments, and escaping backticks. These are syntactically
important for preserving formatting when printing ASTs but don't
semantically affect the program.

Tokens take all trailing trivia up to, but not including, the next
newline. This is important to maintain checks that statements without
semicolon separators start on a new line, among other things.

Trivia are now data attached to the ends of tokens, not tokens
themselves.

Create a new Syntax sublibrary for upcoming immutable, persistent,
thread-safe ASTs, which will contain only the syntactic information
about source structure, as well as for generating new source code, and
structural editing. Proactively move swift::Token into there.

Since this patch is getting a bit large, a token fuzzer which checks
for round-trip equivlence with the workflow:

fuzzer => token stream => file1
  => Lexer => token stream => file 2 => diff(file1, file2)

Will arrive in a subsequent commit.

This patch does not change the grammar.
2016-11-15 16:11:57 -08:00
Graydon Hoare
1af5c856fa Support #if swift(subminor-version), rdar://problem/28786959 / SR-2908. 2016-10-24 18:12:45 -07:00
Doug Gregor
861d5d33d6 [Parser] Minor location tweak to aid the scope map. 2016-10-17 22:13:52 -07:00
practicalswift
566bfc0d56 [gardening] Fix typos. 2016-10-13 22:19:08 +02:00
Graydon Hoare
77cad91716 Parse and print @available(swift N) / @available(swift, ...) 2016-10-12 11:20:43 -07:00
Graydon Hoare
66f2027f62 s/Version/PlatformVersion/ to availability specs, add LanguageVersion. 2016-10-12 11:20:42 -07:00
Rintaro Ishizaki
07e66a4506 [Parse] Dead code elimination (#5041) 2016-09-29 11:01:28 +09:00
Jordan Rose
1cffaed907 [Parse] 'where' to ',' fix-it should remove a preceding space. (#4944)
rdar://problem/27709302
2016-09-23 10:54:17 -07:00
Graydon Hoare
8970d44675 Add "-swift-version <n>" that sets LangOpts.EffectiveLanguageVersion.
This flag switches the "effective language version" of the compiler,
at least to any version supported (as of this change: "3" or "3.0").

At the moment nothing uses it except the language version build
configuration statements (#if swift(...)) and various other places
that report, encode, or otherwise check version numbers.

In the future, it's intended as scaffolding for backwards compatibility.

Fixes SR-2582
2016-09-20 15:11:37 -07:00
Doug Gregor
7cb130254d [Scope map/parser/AST] Miscellaneous cleanups to avoid producing invalid source ranges.
The scope map relies fairly deeply on having reasonable source ranges
for AST nodes. Fix the construction and query of source ranges in a
few places throughout the parser and AST to provide stronger
invariants.
2016-09-08 14:27:02 -07:00
Doug Gregor
250e6edf7e Merge pull request #4628 from jtbandes/diag-operator
[QoI] improve diagnostics for operator declarations; unify parsing code
2016-09-06 08:38:58 -07:00
Jacob Bandes-Storch
d6590cd781 [QoI] improve diagnostics for operator declarations; unify parsing code 2016-09-04 22:17:29 -07:00
Doug Gregor
d1ce3b82ef Add support for the C-style 'for' statement. 2016-09-02 17:13:05 -07:00
Michael Ilseman
abea7199a7 [SE-0099 Restructuring Condition Clauses] Flip the switch (#3441)
Now that some time has passed, switch the warnings to errors,
completing SE-0099.
2016-07-27 15:00:10 -07:00
Doug Gregor
0b04c0c0a4 Merge pull request #3741 from rintaro/sr-2080-fixit-initializer-delegation
[SR-2080][Parse] Improve fix-it for initializer delegation
2016-07-26 08:22:30 -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
Rintaro Ishizaki
e6b38e32e8 [Parse][SR-2080] Improve fix-it for initializer delegation
Suggest inserting `super.` only if it's in designated *class* initializer,
otherwise `self.`.
2016-07-25 21:09:03 +09:00
Jordan Rose
d6a726e6fe SE-0106: Add "macOS" as an alias of "OSX" for #if.
...by canonicalizing it to the known platform name. This isn't a
wonderful answer, but it preserves the invariant that a platform
condition has at most one value.

A later commit will switch which one is the default.
2016-07-07 14:57:57 -07:00
Ben Langmuir
75619c02c4 [CodeCompletion] Improve StmtCondition recovery for code-completion
In the condition expressions of if, while and guard statements we were
throwing away the AST if there was a parse error in the condition, or
the brace statement was missing.  This led to poor code-completion for
unresolved members (enums, options sets) because we couldn't find the
parent expression to type-check.

There are a few minor diagnostic changes because we now do more
type-checking of these conditions, particularly if they end up
containing an unused closure.

SR-2001
2016-07-06 14:08:53 -07:00
Chris Lattner
1b2da8c084 Remove the #setline directive, which was formerly known as #line and is now
known as #sourceLocation.  #setline was an intermediate but never endorsed state.

Upgrade the migration diagnostics for SE-0066 and SE-0049 to be errors instead of warnings.
2016-07-02 16:34:19 -07:00
Chris Lattner
45f2cfaaa0 Implement SE-0099, but where the migration diagnostics are left as warnings
for now.  I'll upgrade them to errors in a week or two to give downstream
projects a chance to update.
2016-07-02 15:44:57 -07:00
Bryan Chan
bb3486df93 Add new _endian() platform condition check; valid values are "little" and "big". 2016-05-24 19:39:15 -04:00
Slava Pestov
170992c39f AST: Add Throws flag and ThrowsLoc to AbstractFunctionDecl
The verifier now asserts that Throws, ThrowsLoc and isBodyThrowing()
match up.

Also, add /*Label=*/ comments where necessary to make the long argument
lists easier to read, and cleaned up some inconsistent naming conventions.

I caught a case where ClangImporter where we were passing in a loc as
StaticLoc instead of FuncLoc, but probably this didn't affect anything.
2016-05-21 12:51:50 -07:00
Ben Langmuir
95cc4525d4 [CodeCompletion] Fix completion in repeat statements
Improve the error recovery so that we get back either a RepeatWhileStmt,
or at least a BraceStmt, which code-completion can use to dig out the
local variables.

rdar://problem/25912182
2016-04-25 14:41:28 -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