Commit Graph

889 Commits

Author SHA1 Message Date
Chris Lattner
21b2e9e057 add parser and AST representation support for where/else clauses on let/var decls, some highlights:
- Enhance PBD with a whereExpr/elseStmt field to hold this.
- Start parsing the pattern of let/var decls as a potentially refutable pattern.  It becomes 
  a semantic error to use a refutable pattern without an 'else' (diagnostics not in place yet).
- Change validatePatternBindingDecl to use 'defer' instead of a goto to ensure cleanups on exit.
- Have it resolve the pattern in a PBD, rewriting it from expressions into pattern nodes when valid.
- Teach resolvePattern to handle TypedPatterns now that they can appear (wrapping) refutable patterns.
- Teach resolvePattern to handle refutable patterns in PBD's without initializers by emitting a diagnostic
  instead of by barfing, fixing regressions on validation tests my previous patch caused, and fixing
  two existing validation test crashers.

Sema, silgen, and more tests coming later.




Swift SVN r26706
2015-03-29 22:08:44 +00:00
Arnold Schwaighofer
b9f795699f Revert "add parser and AST representation support for where/else clauses on let/var decls."
It breaks the validation test suite.

This reverts commit r26692.

rdar://20339903

Swift SVN r26700
2015-03-29 13:56:32 +00:00
Chris Lattner
9c53b659ee add parser and AST representation support for where/else clauses on let/var decls.
Sema, silgen, and more tests coming later.


Swift SVN r26692
2015-03-29 06:20:24 +00:00
Doug Gregor
ccde6bb87d Allow protocol extensions to add further constraints via a trailing where clause.
Start parsing a "trailing" where clause for extension declarations, which follows the extended type name and (optional) inheritance clause. Such a where clause is only currently permitted for protocol extensions right now.

When used on a protocol extension, it allows one to create a more-constrained protocol extension, e.g.,

  extension CollectionType where Self.Generator.Element : Equatable { ... }

which appears to be working, at least in the obvious cases I've tried.

More cleanup, tests, and penance for the previous commit's "--crash" introductions still to come.

Swift SVN r26689
2015-03-29 05:42:37 +00:00
Doug Gregor
d4d4694615 Introduce a few defensive null checks to fix 157 compiler crashers.
Swift SVN r26655
2015-03-27 23:57:45 +00:00
Doug Gregor
3d77855b31 Start allowing extensions of protocol types.
Remove the semantic restrictions that prohibited extensions of
protocol types, and start making some systematic changes so that
protocol extensions start to make sense:
  - Replace a lot of occurrences of isa<ProtocolDecl> and
    dyn_cast<ProtocolDecl> on DeclContexts to use the new
    DeclContext::isProtocolOrProtocolExtensionContext(), where we want
    that behavior to apply equally to protocols and protocol extensions.
  - Eliminate ProtocolDecl::getSelf() in favor of
    DeclContext::getProtocolSelf(), which produces the appropriate
    generic type parameter for the 'Self' of a protocol or protocol
    extension. Update all of the callers of ProtocolDecl::getSelf()
    appropriately.
  - Update extension validation to appropriately form generic
    parameter lists for protocol extensions.
  - Methods in protocol extensions always use the witnesscc calling
  convention.

At this point, we can type check and SILGen very basic definitions of
protocol extensions with methods that can call protocol requirements,
generic free functions, and other methods within the same protocol
extension.

Regresses four compiler crashers but improves three compiler
crashers... we'll call that "progress"; the four regressions all hit
the same assertion in the constraint system that will likely be
addressed as protocol extensions starts working.

Swift SVN r26579
2015-03-26 04:50:51 +00:00
Doug Gregor
cce9081fe2 Teach getSelfTypeForContainer() that it's rude to crash when the container has no type.
Fixes 68 crashes in the compiler-crashers suite.

Swift SVN r26564
2015-03-25 23:20:59 +00:00
Chris Willmore
1ee6f7e67c Implement syntax changes for in-place methods.
Rename 'assignment' attribute of infix operators to 'mutating'. Add
'has_assignment' attribute, which results in an implicit declaration of
the assignment version of the same operator. Parse "func =foo"
declaration and "foo.=bar" expression. Validate some basic properties of
in-place methods.

Not yet implemented: automatic generation of wrapper for =foo() if foo()
is implemented, or vice versa; likewise for operators.

Swift SVN r26508
2015-03-25 00:22:41 +00:00
Jordan Rose
a0c64d7533 Teach getEffectiveAccess() to respect -enable-testing.
SIL seems to be doing the right thing here already, which is great!

Part of rdar://problem/17732115. We'll be able to really see this working
with the next change: allowing references to testable things when using
"@testable import".

Swift SVN r26473
2015-03-24 02:17:01 +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
Doug Gregor
e5a589e6d3 Ensure that generic parameters of extensions of generics get the appropriate DeclContext.
Swift SVN r26417
2015-03-22 12:35:28 +00:00
Doug Gregor
7708c5a65e Start moving away from (Nominal)?TypeDecl::getProtocols().
Instead, use other entry points, particularly those that use the conformance lookup table.

Swift SVN r26412
2015-03-22 12:35:21 +00:00
Devin Coughlin
bd88d3f777 Sema: Suggest availability Fix-Its in proper location for enum cases.
The commit fixes availability Fix-Its on enum elements to suggest a new availability
attribute on the enum case (which is where attributes live in concrete syntax) rather than
on the enum element (which is where they are attached in the abstract syntax tree).

Swift SVN r26401
2015-03-22 02:25:20 +00:00
Ben Langmuir
fff41e472e Hide _UnderscoredProtocols in code completion
We explicitly whitelist these "stdlib private" decls in interface
generation, because they may contain methods that users are required to
implement.  But in code-completion, there's no good reason to show them.
We still show completions for the methods themselves if you complete on
a public protocol that inherits from the private protocol. So,

<complete> => doesn't show _CollectionType

let a: CollectionType = ...
a.<complete> => *does* show startIndex, which comes from _CollectionType

rdar://problem/20086106

Swift SVN r26355
2015-03-20 17:14:08 +00:00
Doug Gregor
3f6a14ade6 Eliminate NominalTypeDecl::(get|set)Conformances.
We now access the conformances of a nominal type through the
conformance lookup table, so there is no reason to continue storing
conformances directly on the nominal type declaration, which was
error-prone regardless. This mirrors the change to ExtensionDecl from
my previous commit.

Swift SVN r26354
2015-03-20 16:32:25 +00:00
Doug Gregor
9bd774fd57 Eliminate ExtensionDecl::(get|set|)Conformances.
Stop storing a conformances array on ExtensionDecls. Instead, always use the conformance lookup table to retrieve conformances (which is lazy and supports multi-file, among other benefits).

As part of this, space-optimize ExtensionDecl's handling of conformance loaders. When one registers a conformance loader, it goes into a DenseMap on ASTContext and gets erased once we've loaded that data, so we get two words worth of space back in each ExtensionDecl.

Swift SVN r26353
2015-03-20 16:32:21 +00:00
Doug Gregor
c0d783fe40 Start serializing the reference components of extension declarations.
Specifically, start serializing the generic parameter list, even
though we basically throw it away during deserialization.

Swift SVN r26318
2015-03-19 22:09:59 +00:00
Doug Gregor
1511d7a15a Start building conformances when requested in the AST.
Previously, we would require the type checker to be able to build a
conformance, which meant we would actually have to lie in the AST
about having a conformance (or crash; we did the form). Now, we can
form the conformance in the AST and it will be checked in the type
checker when needed. The intent here is to push conformance creation
into the conformance lookup table.

To get here, we had to stop relying on the broken, awful,
ASTContext-wide conformance "cache". A proper cache can come back once
the model is sorted out.

Swift SVN r26250
2015-03-18 04:31:22 +00:00
Doug Gregor
a90c306d6c Compute the "existential conforms to self" bit lazily.
Instead of relying on Sema to set the existential-conforms-to-self bit, compute it lazily in the AST. This is far cleaner and more dependable than the previous solution.

Swift SVN r26225
2015-03-17 16:34:26 +00:00
Chris Lattner
59c22383fb Rework PatternBindingDecl to maintain a list of pattern/initexpr pairs inside of it.
Previously, a multi-pattern var/let decl like:
  var x = 4, y = 17

would produce two pattern binding decls (one for x=4 one for y=17).  This is convenient
in some ways, but is bad for source reproducibility from the ASTs (see, e.g. the improvements
in test/IDE/structure.swift and test/decl/inherit/initializer.swift).

The hardest part of this change was to get parseDeclVar to set up the AST in a way
compatible with our existing assumptions. I ended up with an approach that forms PBDs in 
more erroneous cases than before.  One downside of this is that we now produce a spurious
  "type annotation missing in pattern"
diagnostic in some cases.  I'll take care of that in a follow-on patch.





Swift SVN r26224
2015-03-17 16:14:18 +00:00
Xi Ge
9cdffd2b28 [SyntaxColoring] Highlight specifier words in infix operator decls.
When used inside infix operator decls, associativity, precedence and
assignment are highlighted as keywords.

rdar://18833967

Swift SVN r26188
2015-03-16 20:02:44 +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
Doug Gregor
3e4632e4d6 Reinstate "Centralize the logic for synthesized conformances.""
This reinstates r26115 along with a small fix to the synthesization of
witnesses (an existing bug exposed by this change).

Swift SVN r26146
2015-03-15 04:15:36 +00:00
Erik Eckstein
fe84d94938 Revert "Centralize the logic for synthesized conformances."
It causes some fails in compiler_crashers:

    Swift :: compiler_crashers/0986-swift-unboundgenerictype-get.swift
    Swift :: compiler_crashers/1103-swift-unboundgenerictype-get.swift
    Swift :: compiler_crashers/1223-swift-lexer-leximpl.swift
    Swift :: compiler_crashers/1276-swift-metatypetype-get.swift
    Swift :: compiler_crashers/1287-swift-printingdiagnosticconsumer-handlediagnostic.swift



Swift SVN r26136
2015-03-14 13:00:13 +00:00
Doug Gregor
78892d78c0 Centralize the logic for synthesized conformances.
This is effectively NFC, but we had two implementations of "figure out
the protocols that this type should implicitly conform to". The one in
the conformance table is what will matter going forward.

Swift SVN r26115
2015-03-13 23:37:21 +00:00
Doug Gregor
9271a24a92 Introduce a protocol conformance registry for nominal types.
(Note that this registry isn't fully enabled yet; it's built so that
we can test it, but has not yet taken over the primary task of
managing conformances from the existing system).

The conformance registry tracks all of the protocols to which a
particular nominal type conforms, including those for which
conformance was explicitly specified, implied by other explicit
conformances, inherited from a superclass, or synthesized by the
implementation.

The conformance registry is a lazily-built data structure designed for
multi-file support (which has been a problematic area for protocol
conformances). It allows one to query for the conformances of a type
to a particular protocol, enumerate all protocols to which a type
conforms, and enumerate all of the conformances that are associated
with a particular declaration context (important to eliminate
duplicated witness tables).

The conformance registry diagnoses conflicts and ambiguities among
different conformances of the same type to the same protocol. There
are three common cases where we'll see a diagnostic:

1) Redundant explicit conformance of a type to a protocol:

    protocol P { }
    struct X : P {  }
    extension X : P { } // error: redundant explicit conformance

2) Explicit conformance to a protocol that collides with an inherited
  conformance:

    protocol P { }
    class Super : P { }
    class Sub : Super, P { } // error: redundant explicit conformance

3) Ambiguous placement of an implied conformance:

    protocol P1 { }
    protocol P2 : P1 { }
    protocol P3 : P1 { }

    struct Y { }
    extension Y : P2 { }
    extension Y : P3 { } // error: ambiguous implied conformance to 'P1'

  This happens when two different explicit conformances (here, P2 and
  P3) placed on different declarations (e.g., two extensions, or the
  original definition and other extension) both imply the same
  conformance (P1), and neither of the explicit conformances imply
  each other. We require the user to explicitly specify the ambiguous
  conformance to break the ambiguity and associate the witness table
  with a specific context.

Swift SVN r26067
2015-03-12 21:11:23 +00:00
Doug Gregor
a47f30f6aa Factor out NominalTypeDecl::prepareExtensions(). NFC
Swift SVN r26064
2015-03-12 21:11:02 +00:00
Denis Vnukov
9e76e2ba50 Minor (swift migrator related fixes):
Corrected several places where compiler generated AST nodes were not properly 
marked as implicit.

For interpolated strings also fixed string segment locations and made sure 
the first and last segments are preserved in AST even if they are empty.



Swift SVN r25983
2015-03-11 18:08:36 +00:00
Jordan Rose
cee28189a2 Add a BodyKind for implicitly-synthesized memberwise initializers.
...rather than just assuming any initializer without a body that makes it
to SILGen is a memberwise initializer.

In the long term we want SILGen to stop handling these initializers, at
which point we can see if it makes sense to remove this body kind.

No intended functionality change.

Swift SVN r25723
2015-03-03 23:22:21 +00:00
Doug Gregor
db51abba9a Fix Objective-C printing of init(forFun: ()) initializers.
This is the rest of rdar://problem/19973250, thank you Jordan!

Swift SVN r25623
2015-02-27 22:54:45 +00:00
Doug Gregor
8fcd016b9b Use the @objc name of a property for getters, setters, and Objective-C metadata
Fixes rdar://problem/19963219.

Swift SVN r25622
2015-02-27 22:36:53 +00:00
Doug Gregor
09efc9d78e Allow to one to define zero-parameter initializers with Objective-C names longer than "init".
There are a handful of Objective-C initializers with names like
"initForMemory" that take no parameters. The Clang importer has long
been importing them with a single parameter of type (), e.g.,

  init(forMemory: ())

At some point, our @objc checking got stricter and started rejecting
parameters of type (), making it impossible to define such an
initializer in Swift. Codify this case in @objc checking, fixing
rdar://problem/19973250.

Swift SVN r25611
2015-02-27 21:50:04 +00:00
Xi Ge
7f668e7b49 Add isUserAccessible() in Decl to show whether swift users should know such decl exists. For instance, a.storage for lazy var a is a inaccessible decl. An implicit decl is not necessarily inaccessible, for instance, self.
Fixing rdar://18760063

Swift SVN r25556
2015-02-26 19:16:54 +00:00
Doug Gregor
7ef5ba9ff9 Perform override checking prior to (and independent of) @objc computation.
Always perform override checking based on the Swift type
signatures, rather than alternately relying on the Objective-C
selectors. This ensures that we get consistent override behavior for
@objc vs. non-@objc declarations throughout, and we separately make
sure that the Objective-C names line up.

This also allows us to inherit @objc'ness correctly (which didn't
quite work before), including inferring the Objective-C selector/name
(the actual subject of rdar://problem/18998564).

Fixes rdar://problem/18998564.

Swift SVN r25392
2015-02-19 06:29:34 +00:00
Doug Gregor
a34a457ce7 Teach TypeDecl::getDeclared(Interface)Type() about ErrorType.
Propagate the error rather than crashing. Fixes 25 crashes from the
test suite, covered by rdar://problem/19883429.

Swift SVN r25384
2015-02-19 00:58:42 +00:00
Michael Gottesman
77b2e21eb5 Move buildForwardingSubstitutions onto GenericParamList::getForwardingSubstitutions(). NFC.
Swift SVN r25358
2015-02-17 23:01:02 +00:00
Doug Gregor
96a2659223 Serialize initializer stub implementations.
Fixes the cross-module initializer inheritance issues implied by
rdar://problem/19794036.

Swift SVN r25336
2015-02-17 00:38:21 +00:00
Argyrios Kyrtzidis
944e20949a [AST/IDE] Use TypeReprs to keep track of the type components in an ExtensionDecl.
This allows preserving type info and proper annotation of the extension's type components.

Swift SVN r25309
2015-02-16 08:36:16 +00:00
Roman Levenstein
7ced3da7e1 @transparent on declarations of vars with getters/setters inside extensions should not be ignored.
Swift SVN r25270
2015-02-13 15:31:44 +00:00
Doug Gregor
38a4f0c9fe Revert "Match Objective-C names in overrides as part of validation, not override checking."
This reverts r25243, per rdar://problem/19816977.

Swift SVN r25253
2015-02-12 22:01:19 +00:00
Doug Gregor
b25eda36fa Match Objective-C names in overrides as part of validation, not override checking.
Previously, we were using the Objective-C names to help determine
whether a declaration is an override or not. This is broken, because
we should determine overrides based on the Swift rules for
overriding, then (later) check that the Objective-C runtime will see
the same override behavior that the Swift runtime does. Address this
problem, both by taking the Objective-C selector out of the equation
when matching overrides (except for diagnostic purposes) and by
performing better validation of the Objective-C names for the
overriding vs. overridden methods/properties.

The motivating case here (from rdar://problem/18998564) is an
Objective-C initializer:

  -(instancetype)initString:(NSString *)string;

When trying to override this in a Swift subclass, one naturally
writes:

  override init(string: String)

which implicitly has the selector initWithString:. We ended up in an
unfortunate place where we rejected the override (because the
selectors didn't match) with a crummy diagnostic, but omitting the
"override" would result in a different conflict with the superclass.

Now, we'll treat this as an override and complain that one needs to
rename the method by adding "@objc(initString:)" (with a Fix-It, of
course). This fixes rdar://problem/18998564, but it is not ideal: the
complete solution (covered by rdar://problem/19812955) involves
reworking the dance between override and @objc so that we compute
'override' first (ignoring @objc-ness entirely), and let the
@objc'ness of the overridden declaration both imply @objc for the
overriding declaration and implicitly fix the selector. However, such
a change is too risky right now, hence the radar clone.

Swift SVN r25243
2015-02-12 17:46:56 +00:00
Joe Pamer
4d86ddc692 When checking if a decl with trivial accessors is settable, make sure it actually has a setter. Not doing so can lead to improper projections from rvalue to lvalue, and cause crashes during SIL generation. (rdar://problem/19781739)
Swift SVN r25215
2015-02-12 01:17:09 +00:00
Denis Vnukov
ec839a691f Fix for rdar://19773050, Swift 1.2b1: Compiler crash with errant curly brace
Handle error type in enum value declaration



Swift SVN r25193
2015-02-11 21:08:30 +00:00
Chris Lattner
38dbd74ccf fix <rdar://problem/19770775> Deferred initialization of let bindings rejected at top level in playground
by looking through TopLevelCode decls.


Swift SVN r25169
2015-02-11 07:10:00 +00:00
Doug Gregor
20bc247494 Use unified logic for determining whether a subscript index is bridged to an object type.
Fixes rdar://problem/19772357.

Swift SVN r25145
2015-02-10 23:56:07 +00:00
Doug Gregor
fc3a5a726a Make sure we don't add bogus same-type requirements to a generic signature.
Fixes rdar://problem/19137463.

Swift SVN r24972
2015-02-04 21:07:43 +00:00
John McCall
9e26ecf2af Make ArchetypeType::NestedType its own proper type
with more explicit/semantic conversions in and out.

Using a PointerUnion with overlapping pointer types
is both error-prone and pretty close to illegible.

Swift SVN r24707
2015-01-24 13:05:38 +00:00
Denis Vnukov
45c2d9741f Fix for rdar://problem/19539259, Fuzzing Swift: AST validation failed in getSignatureSourceRange(...)
Handle cases in AbstractFunctionDecl::getSignatureSourceRange() when param pattern elements do 
not have valid start/end locations.



Swift SVN r24688
2015-01-23 18:27:02 +00:00
Jordan Rose
66d0eccad2 Refactor the handling of *ApplicationMain classes.
Rather than keeping just a "main class" in every module, track the "main file"
that's responsible for producing the module's entry point. This covers both
main source files and files containing classes marked @UIApplicationMain or
@NSApplicationMain.

This should have no functionality change, but is preparation for the next
commit, where we will preserve some of this information in serialization.

Swift SVN r24529
2015-01-19 23:08:54 +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