Commit Graph

591 Commits

Author SHA1 Message Date
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
Chris Lattner
d6ba8666ca various parts of the compiler are doggedly trying to figure out
whether a getter is mutating or a setter is nonmutating, centralize
it into methods on AbstractStorageDecl, NFC.


Swift SVN r28823
2015-05-20 05:00:13 +00:00
Chris Lattner
20d161cfbf fix <rdar://problem/18123596> unexpected error on self. capture inside class method
We no do not require "self." for closures capturing self in static/class methods.
While we do actually capture the metatype more than we should (rdar://21030087),
this doesn't matter to the developer, since this capture cannot cause a cycle
in the reference graph that they should have to reason about.



Swift SVN r28804
2015-05-20 00:27:34 +00:00
Devin Coughlin
1867b1418b Support escape sequences in @available() messages.
Allow API providers to use escaped characters in the messages of @available()
attributes. For example, for a reference to a declaration annotated with:

@available(*, unavailable, message="use print(\"\")")
public func println() {
...
}

the message displayed to the user will be:

'println() is unavailable: use print("")'.

This also supports unicode escapes (e.g. \u{1F43C}).

A complication is that AvailableAttrs imported from clang already have their
messages unescaped, so this patch keeps track of whether an availability
attribute came from clang and, if so, does not unescape the message again when
emitting the diagnostic. We could avoid this extra book-keeping by immediately
unescaping Swift messages during parsing, keeping them in an unescaped
canonical form, and then escaping them in ASTPrinter when we print the
attributes. rdar://problem/20985357 tracks keeping AvailableAttrs messages in
a canonical form.

rdar://problem/20789407

Swift SVN r28658
2015-05-16 01:51:51 +00:00
Chris Lattner
9bbce68ec8 fix <rdar://problem/20894455> "variable was never mutated" diagnostic does not take #if into account
Swift SVN r28633
2015-05-15 18:30:32 +00:00
Chris Lattner
1e934dd432 fix <rdar://problem/20927707> QoI: 'variable was never mutated' noisy when only part of a destructured tuple is mutated
If a 'var' pattern binding contains multiple vardecls and some are mutated
but others aren't, don't warn about the non-mutated ones as being potential 
candidates for 'let's since there is no great way to rewrite the code to
silence the warning.


Swift SVN r28629
2015-05-15 17:48:34 +00:00
Chris Lattner
5685121505 fix <rdar://problem/20911927> False positive in the "variable was never mutated" warning with IUO
Swift SVN r28583
2015-05-14 22:13:08 +00:00
Slava Pestov
93dc28be59 Revert "Unused binding analysis now recurses into type declarations"
This reverts revision r28480, after mailing list discussion it is
agreed that this is a bad idea.

Note that a test had to be updated adding a new expected warning.

Swift SVN r28515
2015-05-13 17:12:58 +00:00
Ted Kremenek
62feb5c949 Change @availability to @available.
This came out of today's language review meeting.
The intent is to match #available with the attribute
that describes availability.

This is a divergence from Objective-C.

Swift SVN r28484
2015-05-12 20:06:13 +00:00
Slava Pestov
80140b0689 Unused binding analysis now recurses into type declarations
While we don't support classes that close over values in the outer
scope just yet, make the analysis handle it, to avoid emitting
spurious diagnostics about unused values.

For example, this would formerly daignose that 'x' was both unused
and that the closure was invalid:

func brigand() {
  let x = 0
  class Sword {
    func attack() {
      print(x)
    }
  }
}

Swift SVN r28480
2015-05-12 18:19:36 +00:00
Chris Lattner
ddd6192094 Implement 3 prominent feature requests:
<rdar://problem/15975935> warning that you can use 'let' not 'var'
<rdar://problem/18876585> Compiler should warn me if I set a parameter as 'var' but never modify it
<rdar://problem/17224539> QoI: warn about unused variables

This uses a simple pass in MiscDiagnostics that walks the body of an
AbstractFunctionDecl.  This means that it doesn't warn about unused properties (etc),
but it captures a vast majority of the cases.

It also does not warn about unused parameters (as a policy decision) because it is too noisy,
there are a variety of other refinements that could be done as well, thoughts welcome.



Swift SVN r28412
2015-05-11 06:26:05 +00:00
John McCall
54d3d5f95a Stop randomly stripping IdentityExprs from recursive positions
within accessors.

Improves source fidelity and fixes problems with error handling
(because TryExpr and ForceTryExpr are IdentityExprs).

Swift SVN r28298
2015-05-07 22:41:43 +00:00
Doug Gregor
2653a6569b Eliminate ModuleExpr; DeclRefExpr is good enough for anyone.
Swift SVN r28285
2015-05-07 21:10:53 +00:00
Jordan Rose
841c4c7e63 Be more aggressive about access control consistency when offering warnings.
Otherwise, we'll get assertion failures and verifier errors down the line.

rdar://problem/20689300

Swift SVN r28144
2015-05-05 01:41:10 +00:00
Chris Lattner
8494651d22 Substantially rework CSApply's building of argument lists for calls.
This fixes <rdar://problem/20494437> SILGen crash handling default arguments
again, and includes a fix for MiscDiagnostics to look through the generated
TupleShuffleExprs in @noescape processing (which tripped up XCTest).

This fixes <rdar://problem/16860940> QoI: Strict keyword arguments loses type sugar in calls
where we'd lose some type sugar.

This fixes sanity in the ASTs: ScalarToTupleExpr now always has consistent
types between its argument and result, so we can turn on AST Verification of it.




Swift SVN r27827
2015-04-27 23:02:09 +00:00
Chris Lattner
e58020b9ab defer using ClosureExpr's is an implementation detail, don't require self. qualification inside of them.
Swift SVN r27768
2015-04-26 18:10:36 +00:00
Doug Gregor
4e0e32197f Extend 'availability' attribute with an unconditional 'deprecated' option.
Allow an unversioned 'deprecated' attribute to specify unconditional
deprecation of an API, e.g.,

  @availability(*, deprecated, message="sorry")
  func foo() { }

Also support platform-specific deprecation, e.g.,

  @availability(iOS, deprecated, message="don't use this on iOS")
  func bar() { }

Addresses rdar://problem/20562871.

Swift SVN r27355
2015-04-16 06:36:45 +00:00
Doug Gregor
921855ee0d Revert "Extend 'availability' attribute with an unconditional 'deprecated' option."
This reverts r27339; it broke an iOS test.

Swift SVN r27343
2015-04-16 03:36:40 +00:00
Doug Gregor
b4b5dbb5d8 Extend 'availability' attribute with an unconditional 'deprecated' option.
Allow an unversioned 'deprecated' attribute to specify unconditional
deprecation of an API, e.g.,

  @availability(*, deprecated, message="sorry")
  func foo() { }

Also support platform-specific deprecation, e.g.,

  @availability(iOS, deprecated, message="don't use this on iOS")
  func bar() { }

Addresses rdar://problem/20562871.

Swift SVN r27339
2015-04-15 23:59:20 +00:00
Devin Coughlin
d08b98b1ca Sema: Turn on availability checking by default
Enable checking for uses of potentially unavailable APIs. There is
a frontend option to disable it: -disable-availability-checking.

This commit updates the SDK overlays with @availability() annotations for the
declarations where the overlay refers to potentially unavailable APIs. It also changes
several tests that refer to potentially unavailable APIs to use either #available()
or @availability annotations.

Swift SVN r27272
2015-04-14 06:44:01 +00:00
Devin Coughlin
53fd9fb59c Sema: Finish staging back in availability checking in synthesized functions
Remove the suppression of deprecation and potential unavailability diagnostics in
synthesized functions. We still suppress some explicit unavailability diagnostics -- those
in synthesized functions in synthesized functions that are lexically contained in
declarations that are themselves annotated as unavailable. For these cases, the right
solution <rdar://problem/20491640> is to not synthesize the bodies of these functions in
the first place.

rdar://problem/20024980

Swift SVN r27203
2015-04-10 05:19:04 +00:00
Devin Coughlin
cd79e0ba4d Sema: Suppress explicit unavailability diagnostics in unavailable synthesized functions.
Suppress diagnostics about references to unavailable declarations when all
of the following conditions are met:

(1) the reference is in synthesized code;
(2) the reference is in a synthesized method; and
(3) the reference is inside a declaration marked as unavailable.

This, combined with prior work to infer availability annotations for synthesized
methods should eliminate the spurious diagnostics we have seen (e.g.,
rdar://problem/20154926) for references to unavailable types without the hammer
of suppressing all references to unavailable declarations in synthesized
functions. The right thing to do here is to not synthesize the bodies of these
functions in the first place. I've filed <rdar://problem/20491640> to track
this.

We will continue to emit an error for references to unavailable declarations
in non-implicit code, even if the reference is inside a declaration marked
unavailable.

Swift SVN r27197
2015-04-10 02:07:24 +00:00
Devin Coughlin
8c50f67120 Sema: Factor out common logic for finding enclosing declaration availability
A common operation for availability checking is determining whether some
lexically enclosing declaration matches a condition. This commit factors
out that logic. NFC.

Swift SVN r27181
2015-04-09 21:56:22 +00:00
Doug Gregor
762d1d9474 Opened existential metatypes always involve members.
Swift SVN r27172
2015-04-09 18:09:04 +00:00
Jordan Rose
182ef27f95 [ClangImporter] Handle __attribute__((availability(swift, unavailable))).
This is the new and improved version of
__attribute__((annotate("swift1_unavailable"))), with the "improved" being
specifically that the 'availability' attribute supports a message.

This requires a corresponding Clang commit.

Swift side of rdar://problem/18768673.

Swift SVN r27053
2015-04-07 02:40:22 +00:00
Devin Coughlin
3422577752 Sema: Do not emit deprecation warnings for references inside deprecated declarations
We now disregard deprecation warnings if the reference to a deprecated symbol is lexically
contained in a declaration that is itself deprecated on all deployment targets.

Swift SVN r26693
2015-03-29 06:54:26 +00:00
Devin Coughlin
bdc7475d6b Sema: Add temporary flag to stage in availability checking in implicit functions
This flag enables checking of availability (deprecation, explicit unavailability,
and potential unavailability) in synthesized functions. The flag will go away once this
checking is fully staged in.

Swift SVN r26624
2015-03-27 06:48:50 +00:00
Jordan Rose
f74bc7122c Split getAccessibility() into getFormalAccess() and getEffectiveAccess().
Currently a no-op, but effective access for entities within the current
module will soon need to take testability into account. This declaration:

  internal func foo() {}

has a formal access of 'internal', but an effective access of 'public' if
we're in a testable mode.

Part of rdar://problem/17732115 (testability)

Swift SVN r26472
2015-03-24 02:16:58 +00:00
Devin Coughlin
02f542c2d0 [Sema]Suppress availability diagnostics inside synthesized functions.
This commit suppresses errors for references to unavailable symbols inside
implicit functions.

This is a quick hack to fix a hit-listed radar <rdar://problem/20007266> where
the compiler was emitting spurious errors for enums explicitly marked
unavailable in Objective-C and whose nil literal conformance is synthesized by
the importer. These errors could occur when user code made no apparent reference
to the enum in question and instead only referred to an imported class that
itself referred to the enum in a method signature.

We will need to do something systematic about availability and deprecation
diagnostics in synthesized code.  In particular, we should make sure that:
(1) we never emit code that references explicitly unavailable symbols;
(2) that the user never gets an error about symbol that they did not explicitly type; and
(3) that errors can dealt with via the appropriate availability check or annotation. I'm
tracking this with radar rdar://problem/20024980.

rdar://problem/20007266

Swift SVN r26251
2015-03-18 05:51:01 +00:00
Doug Gregor
7677a454ea Move "non-self-conforming protocol type" check out of type validation.
Having semantic checking in type validation introduces the potential for more recursion, triggering crashes. By moving this semantic restriction out to a later stage, we make it more robust. Fixes 6 compiler crashers, although it regressed one compiler crasher that hits a different known issue (assertions in addGenericParameters when we have multiple parameters at the same depth).

Swift SVN r26226
2015-03-17 16:34:28 +00:00
Chris Lattner
01f3e81aa5 Work on VarDecl:
- Rename getParentPattern() -> getParentPatternBinding(), since
   it returns the pattern binding, not the pattern.
 - Introduce new getParentPattern()/getParentInitializer() methods,
   covering the most common uses of getParentPatternBinding().

NFC.



Swift SVN r26175
2015-03-16 01:54:20 +00:00
Chris Lattner
3e3f568179 By far, the most common use of Lexer::getLocForEndOfToken is in
conjunction with .fixItInsert().  As such, introduce a helper named
.fixItInsertAfter() that does what we all want.  Adopt this in various
places around the compiler.  NFC.



Swift SVN r26147
2015-03-15 05:30:04 +00:00
Devin Coughlin
749f8500d6 Sema: Emit Fix-Its for potential unavailability
We now suggest up to three Fix-Its for each reference to a potentially
unavailable symbol: one to wrap the reference in an if #os(...) { ... }
guard (if possible), one to add an @availability attribute to an enclosing
property or method (if possible), and one to add an @availability attribute
to an enclosing class/struct/extension, etc. or global function.

The goal here is not to infer the "best" Fix-It but rather to ensure
discoverability of #os() and @availability attributes. We want the user, when
faced with an availability diagnostic, to be aware of the tools in her toolbox
to deal with it.

This is still missing QoI improvements, including Fix-Its to update
existing @availability attributes and more precise wording in diagnostics
(e.g, "initializer" instead of function, "class" instead of "type"). These
improvements will come in later commits.

Swift SVN r26073
2015-03-12 23:51:11 +00:00
Ted Kremenek
9e7471d68b Revert "Revert "Rework processing of @noescape closures a bit to fix rdar://19981118:""
Turns out llvm::DataLayoutPass is used in other places, so the bots are still unhappy.
Re-applying the original change so we can fix the problem holistically.

Swift SVN r25761
2015-03-04 20:15:45 +00:00
Ted Kremenek
6781ac7bec Revert "Rework processing of @noescape closures a bit to fix rdar://19981118:"
This is breaking the testing bot because DataLayoutPass was just removed from LLVM trunk.
Chris is the best one to fix this change, but we need to get the bots green.

Swift SVN r25760
2015-03-04 20:03:33 +00:00
Chris Lattner
ab43e444d6 Rework processing of @noescape closures a bit to fix rdar://19981118:
- Have Sema, not SILGen decide if a vardecl can be captured by address
  instead of by-box.  This is a non-local property that is best computed
  during capture set formation.  Sema captures this as a bit on the new
  CapturedValue entry.
- Rework some diagnostic emission to centralize a class of noescape 
  diagnostics in capture set calculation.  Previously, funcdecl closures
  produced their diagnostics there, but ClosureExprs produced them in
  MiscDiagnostics (NFC for this part).

This fixes <rdar://problem/19981118> Swift 1.2 beta 2: Closures nested in @noescape closures copy, rather than reference, captured vars.



Swift SVN r25759
2015-03-04 19:30:36 +00:00
Chris Lattner
3489840b8d rework our representation of capture lists to compute the "isDirect" bit up front
when computing the list.  This simplifies getLocalCaptures to *just* filter out
global captures, and paves the way for other enhancements.  NFC.


Swift SVN r25739
2015-03-04 01:56:48 +00:00
Devin Coughlin
1e5daf7557 [Sema] Suppress deprecation warnings inside implicit functions
This commit suppresses deprecation warnings for references to deprecated symbols
by climbing the DeclContext hierarchy to determine if the warning location is
inside an implicit function.

This is a quick fix to get rid of spurious warnings for deprecated enums whose
nil literal conformance is synthesized by the importer. These warnings would
occur even when user code made no apparent reference to the enum in question and
instead only referred to an imported class that itself referred to the enum in a
method signature. This quick fix intentionally drops some valid warnings
(for example, for a synthesized call to a deprecated initializer in a super
class). rdar://problem/20024980 tracks adding these special purpose warnings
back.

rdar://problem/20007266

Swift SVN r25720
2015-03-03 21:16:01 +00:00
Jordan Rose
746d835ab9 Don't require "self." in local functions.
(unless the local functions are themselves within closures not inferred
@noescape)

Swift SVN r25513
2015-02-24 21:51:13 +00:00
Chris Lattner
eba7aade70 fix <rdar://problem/19763732> False positive in @noescape analysis triggered by default arguments
This was a bug in miscdiagnostics not handling parenexprs inside of scalartotuple expr.


Swift SVN r25455
2015-02-21 01:38:06 +00:00