Commit Graph

370 Commits

Author SHA1 Message Date
Doug Gregor
dccf3155f1 SE-0022: Implement parsing, AST, and semantic analysis for #selector. 2016-01-26 21:12:04 -08:00
Doug Gregor
7d70b704e4 Merge commit '5e11e3f7287427d386636a169c4065c0373931a8' into swift-3-api-guidelines 2016-01-19 23:18:20 -08:00
Chris Lattner
7c247f2096 Further improve the error message when self escapes in a root class before
its fields are initialized.  Before:

   t.swift:18:24: error: variable 'self.B' captured by a closure before being initialized

after:

t.swift:19:24: error: 'self' captured by a closure before all members were initialized
    self.A.withCString {  cString -> () in
                       ^
t.swift:14:7: note: 'self.B' not initialized
  var B: Int
      ^

This drives home the fact that 'self' is being captured here, not the individual
properties.
2016-01-16 22:54:27 -08:00
Michael Ilseman
de0062e437 [Diagnostics] Remove all categories
Diagnostic categories are entirely unused and arguably useless as
implemented, as they merely denote the sub-component of the
compiler.

As far as categorizing warnings are concerned, I'm abandoning the
effort for now, as the utility is marginal and Swift and the Swift
compiler are probalby not ready for these to be nailed down. For the
sake of cleanliness, the CATEGORY field is also stripped from
WARNINGS.

If there's a need for automatic identifying of compiler sub-components
for diagnstics in the future, there are better ways to do this.

NFC
2016-01-14 14:39:18 -08:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
practicalswift
1339b5403b Consistent use of header comment format.
Correct format:
//===--- Name of file - Description ----------------------------*- Lang -*-===//
2016-01-04 13:26:31 +01:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Max Moiseev
2f7b64e475 Merge remote-tracking branch 'origin' into swift-3-api-guidelines 2015-12-21 12:02:13 -08:00
practicalswift
cd7d8dfaff Fix alignment as requested by @gribozavr in #692 2015-12-21 08:54:24 +01:00
practicalswift
176f487d76 Fix incorrect filenames in headers. 2015-12-20 23:59:05 +01:00
Dmitri Gribenko
3d0ad16094 Unsafe[Mutable]Pointer.memory => .pointee 2015-12-16 15:50:31 -08:00
Joe Groff
6b55e87d2f SILGen: Emit mark_function_escape when capturing variable boxes.
There's no longer a direct use of the variable's address when we invoke the closure, but we have a handy mark_function_escape instruction to mark the use without requiring merging analysis of the box and its contents. This also gives us a slightly more accurate error message when a variable is prematurely captured, noting the variable was captured by a closure instead of just generically used.
2015-12-08 14:35:47 -08:00
Slava Pestov
4a11785144 DI: Fix [derivedselfonly] initializers and clean up diagnostics
Designated initializers in NSManagedObject subclasses, or other
classes with @requires_stored_property_inits cannot assign to
stored properties before doing a super.init() delegation.
Assignments after are OK.

DI did the wrong thing by creating a bitmap with only one bit,
but proceeding down the usual designated initializer path.

Now, handle [derivedselfonly] more like a convenience initializer,
with no stored property access allowed prior to delegation.

Also clean up duplicated diagnostics between designated and
convenience initializers, and make convenience initializers
diagnose method calls or property accesses on self in the same
manner as designated initializers.

Fixes <rdar://problem/22110837>.

Swift SVN r32858
2015-10-24 03:55:56 +00:00
Slava Pestov
f7481acc99 DI: Only emit 'self consumed' error once
Swift SVN r32689
2015-10-14 22:50:13 +00:00
Slava Pestov
f524802e12 DI: Add support for early return from designated initializers
Fixes <rdar://problem/18216578>.

Swift SVN r32678
2015-10-14 06:36:56 +00:00
Slava Pestov
f74a3c96d1 Re-apply "Use the 'self consumed' analysis to diagnose some invalid usages of self"
The problem was an uninitialized memory access. Callers were passing
the address of a local variable to isInitializedAtUse(), but this
function only stored to the pointer conditionally. Serendipitously
(or not), in the debug build the initial value was always zero.

Swift SVN r32599
2015-10-10 06:38:19 +00:00
Erik Eckstein
5447d08241 Revert "Use the 'self consumed' analysis to diagnose some invalid usages of self"
It broke some regression tests.



Swift SVN r32593
2015-10-10 05:06:04 +00:00
Slava Pestov
194c4b1c74 Use the 'self consumed' analysis to diagnose some invalid usages of self
Since a delegation consumes the self value, there's no self value available
in a catch block. Previously we would crash at runtime.

The diagnostics suck right now, so this is mostly for testing and
completeness.

Fixes <rdar://problem/22946400>.

Swift SVN r32583
2015-10-09 22:18:10 +00:00
Slava Pestov
d466d9b3aa SILGen: Re-work C function pointer conversion diagnostics
This removes the partially-correct ABI check in Sema and diagnoses
unsupported conversions in SILGen instead. The new check is more
accurate and correctly diagnoses conversions of DeclRef's to
ABI-incompatible @convention(c) types.

This also fixes two cases where we used to crash but could instead
emit a trivial cast:

- Conversions between ABI-compatible (but not identical)
  @convention(c) types
- Conversions of a DeclRef to an ABI-compatible (but not identical)
  @convention(c) type

Fixes <rdar://problem/22470105>.

Swift SVN r32163
2015-09-22 21:08:31 +00:00
Chris Lattner
b9ef0c40e1 fix <rdar://problem/20065892> Diagnostic for an uninitialized constant calls it a variable
Swift SVN r31691
2015-09-04 21:06:15 +00:00
Chris Lattner
e1ef96221a improve the DI diagnostic for uses of self in a non-delegating protocol initializer,
which is most likely because someone forgot to chain to self.init completely.



Swift SVN r30187
2015-07-14 17:50:59 +00:00
Chris Lattner
35d0b01420 improve QoI for enums that need to be marked indirect, but suggesting a fixit and having
a more specific diagnostic.


Swift SVN r29878
2015-07-02 00:53:13 +00:00
Chris Lattner
4602d51231 Improve the diagnostic for an init in a protocol extension that doesn't
chain to another self.init, hopefully resolving confusion about what
has to be done here.

This fixes <rdar://problem/21295093> Swift protocol cannot implement default initializer


Swift SVN r29571
2015-06-23 16:40:26 +00:00
Chris Lattner
f3a03aeaf2 Make the diagnostic for a guard whose 'else' clause falls through more
explicit to nudge people in the right direction of how to use it.
Fixes <rdar://problem/21355414> Error on forgetting to return on a guard



Swift SVN r29552
2015-06-22 21:38:55 +00:00
Chris Lattner
05bf79404a missed a file :-(
Swift SVN r28836
2015-05-20 18:22:46 +00:00
Slava Pestov
42d7c7251c SILGen: better diagnostic for code after break and throw
Swift SVN r28689
2015-05-18 00:26:19 +00:00
Slava Pestov
cbbde4739e SILGen: remove dead diagnostics for error handling
Looks like Sema is checking all of this stuff now. NFC

Swift SVN r28571
2015-05-14 17:22:44 +00:00
Chris Lattner
37f5452d15 require -> guard.
Swift SVN r28223
2015-05-06 22:53:38 +00:00
Chris Lattner
ba4186befb remove parser support for let/else. I left in a cheesy fixit to help
migrate code, but it isn't great.  It needs to be removed before wwdc
(tracked by 20794825)


Swift SVN r28099
2015-05-03 21:46:05 +00:00
Chris Lattner
0011b3ae21 rename "unless" to "require" and give it an 'else' keyword.
Swift SVN r28059
2015-05-02 00:16:44 +00:00
Chris Lattner
e1e83eb8ce Reject 'unless' blocks that fall through.
Unless is now done, we just need to decide what color to paint it.


Swift SVN r27996
2015-04-30 21:12:39 +00:00
Chris Lattner
16522c3758 fix <rdar://problem/19623566> Obscure diagnostic for assigning negative numbers to unsigned
We now produce:
t.swift:1:15: error: negative integer '-9223372036854775808' overflows when stored into unsigned type 'UInt'
var x: UInt = -0x8000_0000_0000_0000
              ^



Swift SVN r27797
2015-04-27 01:18:47 +00:00
Chris Lattner
fa4f1d86ee Recommit r27756 with an update to diagnostic_constant_propagation_int.swift, which is iOS only.
Swift SVN r27761
2015-04-26 15:56:41 +00:00
Chris Lattner
5fa05e93ca implement SILGen support for 'defer' statement. The only
missing piece now is Sema support for detecting invalid exits
out of defer bodies.  That said, SILGen will also detect it,
and produce an error if sema misses something, e.g.:

t.swift:11:23: error: defer statement is not allowed to be exited
  while false { defer { break } }
                      ^
t.swift:12:9: error: defer statement is not allowed to be exited
  defer { return }
        ^

we should still diagnose these in Sema for better QoI of course.

This wraps up: <rdar://problem/17302850> Add a defer keyword to swift



Swift SVN r27760
2015-04-26 15:50:34 +00:00
Ted Kremenek
4a227f2661 Speculatively revert "fix <rdar://problem/18213320> enum with raw values that are too big are not diagnosed"
This looks like it caused test 'SILPasses/diagnostic_constant_propagation_int.swift' to fail.

Swift SVN r27757
2015-04-26 15:12:58 +00:00
Chris Lattner
d5ae69b276 fix <rdar://problem/18213320> enum with raw values that are too big are not diagnosed
On that testcase, we now generate:

t.swift:8:22: error: integer literal '123456' overflows when stored into 'UInt8'
  case tooFarByFar = 123456
                     ^
t.swift:7:8: error: integer literal '256' overflows when stored into 'UInt8'
  case twoHundredFiftySix
       ^

instead of spitting out some warnings with no source loc (which Xcode eats).


This patch:
 - Propagates source locations for literals when synthesizing code in various places,
   so we get the right diagnostic at the right spot.
 - Improves the constant folding SIL Pass to print the value overflowing, which is
   necessary for cases with an implicit value (like 256 above), and is general goodness
   for the QoI of the diagnostic anyway.



Swift SVN r27756
2015-04-26 06:21:23 +00:00
Chris Lattner
61afbe993f Fix <rdar://problem/20193929> Can't "return nil" in a convenience initializer
This falls out of the rework I did of 'self' in initializers.  We now correctly
dealloc_ref the allocated object on the failure path of a convenience init.



Swift SVN r27752
2015-04-26 05:13:48 +00:00
John McCall
a67452c72c SILGen for throwing foreign errors out of @objc thunks.
Also, create a new file specifically for the foreign-error
code.

Swift SVN r27347
2015-04-16 05:16:54 +00:00
John McCall
79cc258f65 Lower 'throws' to an error result.
Swift SVN r27108
2015-04-08 00:09:29 +00:00
Chris Lattner
fc0fb4e34b Reject let/else blocks when the 'else' falls through.
Swift SVN r27058
2015-04-07 03:54:00 +00:00
Chris Lattner
7c189eb5bb improve diagnostic and add test for unreachable default.
Swift SVN r26678
2015-03-28 05:45:36 +00:00
Chris Lattner
0b2312edfe diagnose unreachable cases in switches, fixing:
<rdar://problem/20253447> `case let Case` without bindings incorrectly matches other cases




Swift SVN r26675
2015-03-28 04:31:38 +00:00
Jordan Rose
8247c2d357 If imported calls return nil for 'NSArray * __nonnull', pretend it was [].
...and similar for NSDictionary and NSSet.

For APIs that don't have a reason to distinguish "empty" and "absent" cases,
we encourage standardizing on "empty" and marking the result as non-optional
(or in Objective-C, __nonnull). However, there are system APIs whose
implementations currently do return nil rather than an empty collection
instance. In these cases, we recommend /changing/ the API to return the
appropriate "empty" value instead.

However, this can cause problems for backwards-deployment: while the API is
truly non-optional on system vN, a program may encounter a nil return value
if run on system vN-1. Objective-C can generally deal with this (especially
if the only thing you do is ask for the count or try to iterate over the
collection) but Swift can't. Therefore, we've decided to "play nice" and
accept nil return values for the collection types (NSArray, NSDictionary,
and NSSet) and implicitly treat them as "empty" values if they are the
result of an imported function or method.

Note that the current implementation has a hole regarding subscript getters,
since we still make an AST-level thunk for these in the Clang importer.
We can probably get rid of those these days, but I didn't want to touch
them at this point. It seems unlikely that there will be a subscript that
(a) is for a collection type, and (b) mistakenly returned nil in the past
rather than an empty collection.

There's another hole where an ObjC client calls one of these mistakenly-nil-
returning methods and then immediately hands the result off by calling a
Swift method. However, we have to draw the line somewhere.

(We're actually going to do this for strings as well; coming soon.)

rdar://problem/19734621

Swift SVN r26479
2015-03-24 03:36:45 +00:00
John McCall
a0a16d78d2 Implement the do/catch statement. Tests to follow.
This patch also introduces some SILGen infrastructure for
dividing the function into "ordinary" and "postmatter"
sections, with error-handling-like stuff going into the
final section.  Currently, this is largely undermined by
SILBuilder, but I'm going to fix that in a follow-up.

Swift SVN r26422
2015-03-23 02:08:26 +00:00
Chris Lattner
c2b19659bf significantly improve the QoI of DI's diagnostics when emitting errors due to "return nil"
in a failable initializer.  Previously, it would always just say "properties of a class 
instance must be initialized before returning nil".

Now, in designated inits, it adds to that message with notes for each member that is not
initialized but must be.  It also adds a note saying that super.init() must be called if
that is the problem.

In convenience inits, it says:
 "failable convenience initializer must delegate to self.init() before returning nil"

For example, in:

class B : A {
  var x : Int
     override init?() { return nil }

We now produce:

t.swift:13:15: error: all stored properties of a class instance must be initialized before returning nil from an initializer
t.swift:11:7: note: 'self.x' not initialized
t.swift:13:15: note: super.init must be called before returning nil

I don't see any radars about this, but someone was confused on the dev forums:
https://devforums.apple.com/message/1113604#1113604



Swift SVN r26166
2015-03-15 22:12:11 +00:00
Luqman Aden
353ec039ed Fix grammar for error message.
Swift SVN r24833
2015-01-29 23:27:48 +00:00
Luqman Aden
040df3d400 Error out upon trying to shift by an amount larger than or equal to the
type's size.

Fixes <rdar://problem/19622546>

Swift SVN r24832
2015-01-29 23:09:51 +00:00
Chris Lattner
f28c1b3a3d remove inadvertently added diagnostic
Swift SVN r24031
2014-12-19 06:54:26 +00:00
Chris Lattner
3a7d8df92b fix <rdar://problem/19259730> Using mutating methods in a struct initializer with a let property is rejected
while we're at it, improve the QoI for actually-invalid mutating method calls in struct initalizers.


Swift SVN r24030
2014-12-19 06:52:37 +00:00
Chris Lattner
a048b078e3 Implement: <rdar://problem/16181314> don't require immediate initialization of 'let' values
... now that we have an exquisitely shaved yak.

This provides a simple and uniform model for "let" constants: they are always either
immediately initialized in their declaration, or they are initialized dynamically
exactly once before any use.  

This is a simple generalization of our current model for initializers, but enables
the use of let constants in more cases in local context, e.g. patterns like this:

   let x : SomeThing

   if condition {
     x = foo()
   } else {
     x = bar()
   }
   use(x)

Previously this would have to be declared a "var" for no good reason: the value is
only ever initialized, never actually mutated.

The implementation of this is reasonably straight-forward now that the infrastructure
is in place: Sema treats 'let' constants as "settable" if they lack an initializer
(either in the declaration or in a non-PBD binding).  This exposes them as an lvalue
at the AST level.  SILGen then lowers these things to an alloc_stack, and DI enforces
the "initialization only" requirement that it already enforces for uninitialized 'let'
properties in structs/classes.



Swift SVN r23916
2014-12-13 07:17:44 +00:00