Commit Graph

215 Commits

Author SHA1 Message Date
Doug Gregor
a30ca2a60d Replace bool parameter to TypeChecker::conformsToProtocol() with an option set.
NFC; we can extend this option set more readily later.

Swift SVN r27894
2015-04-29 00:08:22 +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
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
Doug Gregor
126e404fe5 Reimplement inference of type witnesses with a separate non-recursive pass.
Inference of type witnesses for associated types was previously
implemented as part of value witness matching in the constraint
solver. This led to a number of serious problems, including:
  - Recursion problems with the solver hunting for a type witness,
  which triggers more attemts to match value witnesses...
  - Arbitrarily crummy attempts to break the recursion causing
  type-check failures in fun places.
  - Ordering dependencies abound: different results depending on which
  value witnesses were satisfied first, failures because of the order
  in which we attempted to infer type witnesses, etc.

This new implementation of type witness inference uses a separate pass
that occurs whenever we're looking for any type witness, and solves
all of the type witnesses within a given conformance
simultaneously. We still look at potential value witnesses to infer
type witnesses, but we match them structurally, without invoking the
constraint solver.

There are a few caveats to this implementation:
  * We're not currently able to infer type witnesses from value
  witnesses that are global operators, so some tricks involving global
  operators (*cough* ~> *cough*) might require some manually-specified
  type witnesses. Note that the standard library doesn't include any
  such cases.

  * Yes, it's another kind of solver. At simple one, fortunately.

On the other hand, this implementation should be a big step forward:
  * It's far more predictable, order-invariant, and non-recursive.
  * The diagnostics for failures to infer type witnesses have
  improved.

Fixes rdar://problem/20598513.

Swift SVN r27616
2015-04-23 00:20:05 +00:00
Doug Gregor
41ae48b22e Start parsing 'throws' on initializers.
Introduce basic validation for throwing @objc initializers, e.g., a
failable @objc initializer cannot also be throwing. However,
Objective-C selector computation is broken.

Swift SVN r27292
2015-04-14 22:52:29 +00:00
Joe Pamer
37d65f4202 Start tracking SourceLoc data for function "throws" annotations and eliminate the (now unnecessary) "Throws" bit from AbstractFunctionDecls.
Swift SVN r26955
2015-04-03 21:45:30 +00:00
Joe Groff
a46c445937 Sema: Derive ErrorType conformances for enums.
Provide compiler-synthesized implementations of ErrorType that use the type name as domain and a per-case integer as code. (TBD would be some mapping of the associated data to userInfo in Cocoa.)

Swift SVN r26780
2015-03-31 22:10:21 +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
Chris Lattner
8521916b86 change PatternBindingDecl to be created with a static "create" method instead
of using its ctor directly.  NFC, this is in prep for other changes coming.


Swift SVN r26174
2015-03-16 00:44:52 +00:00
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Joe Groff
6235a348c8 Sema: Fix some regressions in RawRepresentable derivation for enums.
Fix a mismatch in the generated interface type and contextual type for the init(rawValue:) initializer; the former was lacking the '(rawValue:)' label and freaking out SILGen's abstraction lowering. When deriving the body of init(rawValue:), substitute the (interface) raw type into the initializer's context, so that we can successfully compare it to other contextualized types in the body's context. Fixes <rdar://problem/19986886>.

Swift SVN r25775
2015-03-05 02:16:35 +00:00
Doug Gregor
b642c555be Allow one to change the argument labels of curried function parameters.
Curried function parameters (i.e., those past the first written
parameter list) default to having argument labels (which they always
have), but any attempt to change or remove the argument labels would
fail. Use the fact that we keep both the argument labels and the
parameter names in patterns to generalize our handling of argument
labels to address this problem.

The IDE changes are due to some positive fallout from this change: we
were using the body parameters as labels in code completions for
subscript operations, which was annoying and wrong.

Fixes rdar://problem/17237268.

Swift SVN r24525
2015-01-19 22:15:14 +00:00
Jordan Rose
2b0fbcbe80 Looking up conformances for a type isn't always a public use of the type.
Specifically, it's not when
- the conformance is being used within a function body (test included)
- the conformance is being used for or within a private type (test included)
- the conformance is being used to generate a diagnostic string

We're still a bit imprecise in some places (checking ObjC bridging), but
in general this means less of an issue for checking literals.

Swift SVN r23700
2014-12-05 00:23:24 +00:00
Joe Groff
32e6db3f2d Parse signs onto float literals the same way we handle integer literals.
Provides consistency in behavior, particularly in enum raw values, where we reject non-literals. Factor out a common NumberLiteralExpr base for integer and float literals that handles the common sign and representation stuff. Fixes rdar://problem/16504472.

Swift SVN r23390
2014-11-18 00:55:02 +00:00
John McCall
6923101341 Rename "AccessKind" to "AccessSemantics". NFC.
There are a lot of different ways to interpret the
"kind" of an access.  This enum specifically dictates
the semantic rules for an access:  direct-to-storage
and direct-to-accessor accesses may be semantically
different from ordinary accesses, e.g. if there are
observers or overrides.

Swift SVN r22290
2014-09-25 23:12:39 +00:00
John McCall
8cae5ba1d0 Generalize 'isDirectPropertyAccess' to allow for
direct (i.e. non-polymorphic) access to accessor
functions, and use this in materializeForSet for
computed properties.

Swift SVN r22059
2014-09-18 05:51:32 +00:00
John McCall
75050f8166 Generate an implicit 'materializeForSet' accessor
along with getters and setters.

Just generate it for now.

Swift SVN r22011
2014-09-17 08:08:03 +00:00
Joe Groff
8338e69c86 Actually set up the 'rawValue' argument label on derived enum initializers.
Somehow, protocol conformance checking didn't actually care that this was missing...

Swift SVN r21898
2014-09-12 01:29:51 +00:00
Joe Groff
491e47cb83 Rename RawRepresentable.raw -> rawValue.
Per API review with Ali. While we're here, give the initializer a corresponding 'rawValue' argument label, and change the associated type name to RawValue to match.

Swift SVN r21888
2014-09-11 20:13:47 +00:00
Joe Groff
eb14f28622 Add back accidentally dropped source line.
Swift SVN r21768
2014-09-07 18:01:00 +00:00
Joe Groff
419ba5cbea Change RawRepresentable to use failable initializers and property requirements.
Redefine the RawRepresentable protocol to use an 'init?' method instead of 'fromRaw(Raw)', and a 'raw' get-only property instead of 'toRaw()'. Update the compiler to support deriving conformances for enums and option sets with the new protocol. rdar://problem/18216832

Swift SVN r21762
2014-09-06 18:40:14 +00:00
Jordan Rose
6cca3529eb Compute accessibility for all ValueDecls, and serialize it properly.
No validation is done yet on whether the user-specified access control makes
sense in context, but all ValueDecls should at least /have/ accessibility now.

/Still/ no tests yet. They will be much easier to write once we're actually
enforcing access control and/or printing access control.

Swift SVN r19143
2014-06-24 21:32:13 +00:00
Chris Lattner
60fc0e6cd2 Implement <rdar://problem/16951729> nil should be a literal type
This is all goodness, and eliminates a major source of implicit conversions.
One thing this regresses on though, is that we now reject "x == nil" where
x is an option type and the element of the optional is not Equtatable.  If
this is important, there are ways to enable this, but directly testing it as
a logic value is more straight-forward.

This does not include support for pattern matching against nil, that will be
a follow on patch.



Swift SVN r18918
2014-06-15 22:59:03 +00:00
Doug Gregor
9cfb1b5ca4 Keep track of the locations of the element names in a TupleExpr.
As part of this, use tail allocation to reduce the memory footprint of
TupleExprs. Use factory methods to make it easier to construct.

I'll be using this information in a follow-on patch. SourceKit
probably wants it as well.


Swift SVN r17129
2014-05-01 00:16:36 +00:00
Doug Gregor
72e32c93af Make argument names default to keyword arguments in the cases where Objective-C has names.
Introduce a model where an argument name is a keyword argument if: 

  - It is an argument to an initializer, or
  - It is an argument to a method after the first argument, or
  - It is preceded by a back-tick (`), or
  - Both a keyword argument name and an internal parameter name are
    specified. 

Provide diagnostics Fix-Its to clean up cases where the user is
probably confused, i.e.,

  - "_ x: Int" -> "x: Int" where "x" would not have been a keyword
  argument anyway
  - "x x: Int" -> "`x: Int"

This covers the compiler side of <rdar://problem/16741975> and
<rdar://problem/16742001>.

Update the AST printer to print in this form, never printing just 
a type for a parameter name because we're also going to adopt
<rdar://problem/16737312> and it was easier to move the tests once
rather than twice.

Standard library and test updates coming separately.




Swift SVN r17056
2014-04-30 00:04:04 +00:00
Chris Lattner
7de9c0802a Peel off almost all of the uses of MetaTypeExpr, replacing
them with uses of TypeExpr instead.  The remaining uses of 
MetaTypeExpr (which will be renamed soon) are places where we
are applying the ".dynamicType" virtual property to an expression.

Unadorned uses of types in code, e.g. the Int in "Int.self" are
now represented with TypeExpr.

One unfortunate travesty that doing this work revealed is that we
are extremely sloppy and terrible about maintaining location information
in implicitly generated decls, and our invariants vary quite a bit.  This
is really horrible, but I'm not sure whether I'll go fix the hacks or not.

This patch perpetuates the existing crimes, but makes them more visible.

NFC!




Swift SVN r16646
2014-04-22 05:15:44 +00:00
Chris Lattner
0e7489c9d5 implement AST support for labels on loops & switch statements. This also
improves location information to track the label location in the AST.  We
don't currently track the location of the colon, but that would be trivial
to drop in if it is interesting.


Swift SVN r16608
2014-04-21 04:56:55 +00:00
Doug Gregor
40b2c9f7a4 Create ParamDecls for function parameters in more places.
Swift SVN r16584
2014-04-20 05:57:34 +00:00
Doug Gregor
6b3ef547ec Replace "Members" arrays with an intrusive linked list.
The use of ASTContext-allocated arrays to store the members of nominal
type declarations and the extensions thereof is an
abomination. Instead, introduce the notion of an "iterable"
declaration context, which keeps track of the declarations within that
context (stored as a singly-linked list) and allows iteration over
them. When a member is added, it will also make sure that the member
goes into the lookup table for its context immediately.

This eliminates a ton of wasted memory when we have to reallocate the
members arrays for types and extensions, and moves us toward a much
more sane model. The only functionality change here is that the Clang
importer no longer puts subscript declarations into the wrong class,
nor does it nested a C struct within another C struct.



Swift SVN r16572
2014-04-19 23:37:06 +00:00
Doug Gregor
36889a2b1a Introduce DeclIterator and DeclRange for iterating over decl members.
NFC. DeclRange is a range over DeclIterators, and is used rather than
ArrayRef<Decl*> to retrieve the members of a nominal type declaration
or extension thereof. The intent is to change the representation of
DeclRange next.


Swift SVN r16571
2014-04-19 18:53:20 +00:00
Doug Gregor
8e597cc695 Eliminate argument parameter patterns.
Swift SVN r16444
2014-04-17 05:20:14 +00:00
Doug Gregor
db439c1a4a Start introducing declaration names for more function declarations.
Swift SVN r16396
2014-04-16 04:52:07 +00:00
Jordan Rose
fc6b20369f Only generate derived conformance bodies for decls in the primary source file.
...and for decls imported from Clang, because those don't have home locations.

Fixes <rdar://problem/15936403>

Swift SVN r15831
2014-04-02 21:00:49 +00:00
Jordan Rose
107eeefd77 Allow AbstractFunctionDecls to have their bodies "synthesized" on demand.
...then use this functionality for derived conformances for RawRepresentable.

No functionality change because these bodies are always forced right now.

Swift SVN r15828
2014-04-02 21:00:42 +00:00
Joe Groff
9f7dab725c Make the ASTContext parameter to MetatypeType::get optional for type-checked types.
We can just get it from the instance type, if the instance type has been fully initialized, which is the case except during parsing of type decls when the decls' own types are being formed.

Swift SVN r15598
2014-03-29 02:50:30 +00:00
Dmitri Hrybenko
11fea869c1 Change 'switch' not to fall through between empty cases and always require at
least one statement per case

rdar://16301313


Swift SVN r15266
2014-03-20 11:44:59 +00:00
Doug Gregor
8cf018a1d2 Give Pattern::clone() an OptionSet rather than a bool; it's going to get more interesting.
Swift SVN r15061
2014-03-14 18:31:21 +00:00
Chris Lattner
18886e9a44 use the TupleExpr empty-tuple convenience ctor in a few obvious places.
Swift SVN r15021
2014-03-14 00:41:53 +00:00
Dmitri Hrybenko
cd48638f9a Fix a use-after-free caused by creating DerivedFileUnit on the fly
Sema was creating DerivedFileUnit on the fly, while something else is iterating
over FileUnits in the module.  The fix is to create DerivedFileUnit in advance.
This change immediately uncovered a lot of code that assumed that the module
consists of a single FileUnit at certain conditions.  This patch also fixes
that code (SourceKit patch is separate, not sending it).

The test change is because now operator == on NSObjects is correctly recognised
as coming from a system module.

rdar://16153700, rdar://16227621, possibly rdar://16049613


Swift SVN r14692
2014-03-05 22:24:25 +00:00
Joe Groff
8e6b353542 Derive conformances of Equatable and Hashable for simple enums.
If an enum has no cases with payloads, make it implicitly Equatable and Hashable, and derive default implementations of '==' and 'hashValue'. Insert the derived '==' into module context wrapped in a new DerivedFileUnit kind, and arrange for it to be codegenned with the deriving EnumDecl by adding a 'DerivedOperatorDecls' array to NominalTypeDecls that gets visited at SILGen time.

Swift SVN r14471
2014-02-27 20:28:38 +00:00
Chris Lattner
1344319677 Rename the internal compiler lexicon from val -> let.
Swift SVN r14408
2014-02-26 21:21:18 +00:00
Jordan Rose
1aab6d4687 Derive protocol conformances for types declared in other files in the target.
Normally, protocol conformances are only checked for types and extensions
declared in the current file. However, for protocols that can derive their
conformances (like RawRepresentable), other files in the target might
expect to be able to use the protocol members (like toRaw), which aren't
written in the source. Force the derivation if this is the case.

Also, move the /other/ set of RawRepresentable tests from test/Parse to
test/Sema.

<rdar://problem/15936403>

Swift SVN r14162
2014-02-20 19:47:42 +00:00
Doug Gregor
8bc87bbe1e fromRaw() witnesses can only be synthesized when the raw type is Equatable.
Swift SVN r14067
2014-02-19 01:09:43 +00:00
Doug Gregor
a341300cb0 Switch ClassDecl::getSuperclass() and EnumDecl::getRawType() to interface types.
This has the nice "side effect" of making overrides of generic
functions work better, fixing the remaining type checker issues from
<rdar://problem/15836098>.

Also synthesize the types for derived RawRepresentable conformances
directly rather than going through the type checker, so we get the
interface types right. 



Swift SVN r14066
2014-02-19 00:54:04 +00:00
Chris Lattner
28903887e7 Rename the internal compiler lexicon from let -> val.
Swift SVN r13992
2014-02-17 16:48:21 +00:00
Dmitri Hrybenko
571c9b3c5e Split 'type' keyword into 'static' and 'class'
rdar://15911697


Swift SVN r13908
2014-02-14 14:50:32 +00:00
Chris Lattner
70d547c1da remove a bunch of code that is manually monkeying around with DeclContexts,
now that they are implicitly updated.  This exposes two things:
1) we're unncessarily serializing selfdecls in ctors and dtors.
2) The index pattern of a SubscriptDecl has no sensible DeclContext that 
   owns variables in it.

I'll deal with the first tomorrow, I'm not sure what to do with
the second one.


Swift SVN r13703
2014-02-09 07:36:18 +00:00
Doug Gregor
d52cec4b20 Eliminate a pile of literal identifiers for self, init, destructor, etc.
... because I can't stomach adding another one of these.


Swift SVN r12687
2014-01-22 01:09:49 +00:00
Doug Gregor
23aeafc195 Finish switching "static" over to "type" in examples, proposals, etc.
Swift SVN r12048
2014-01-08 05:28:59 +00:00