Commit Graph

3502 Commits

Author SHA1 Message Date
Slava Pestov
51aebd2127 AST: Fix name lookup from within lazy property initializers
Allow instance properties and methods to be referenced from
within a lazy property initializer, with or without explicit
'self.' qualification.

The old behavior in Swift 3 was an incredible combination
of odd quirks:

- If the lazy property had an explicitly-written type, it was
  possible to reference instance members from the initializer
  expression by explicitly prefixing 'self.'.

- However, if the lazy property type is inferred, it would
  first be type checked in the initializer context, which
  has no 'self' available.

- Unqualified references to instance members did not work
  at all, because name lookup thought the "location" of the
  lookup was outside of the body of the getter.

- Unqualified references to static properties worked, however
  unqualified references to static methods did not, and
  produced a bogus diagnostic, because one part of the name
  lookup code thought that initializers were "instance
  context" and another thought they were "static context".

This patch improves on the old behavior with the following
fixes:

- Give PatternBindingInitializers associated with lazy
  properties an implicit 'self' declaration for use by
  name lookup.

- In order to allow "re-parenting" the initializer after it
  has been type checked into the body of the getter, "steal"
  the initializer's 'self' when buiding the getter.

- Fix up name lookup and make it aware of the implicit
  'self' decl of a PatternBindingInitializer.

This improves upon an earlier fix for this issue by Doug Gregor
which only worked with ASTScope enabled; the new fix is more
general and shares logic between the two name lookup
implementations.

Fixes <rdar://problem/16888679>, <https://bugs.swift.org/browse/SR-48>,
<https://bugs.swift.org/browse/SR-2203>,
<https://bugs.swift.org/browse/SR-4663>, and the countless other
dupes of this issue.
2017-05-24 18:59:48 -07:00
Doug Gregor
f0abcac42c Diagnose unsound uses of same-type-to-Self requirements with non-final classes.
Protocol requirements involving same-type-to-Self constraints cannot
be witnessed by declarations in non-final classes that have the same
form of same-type requirement to the corresponding class type, because
it creates a soundness hole with subclasses:

    protocol Q {
      func foo<T: P>(_: T, _: T.T) where T.T == Self
    }

    class C: Q {
      func foo<T: P>(_: T, _: C) where T.T == C {}
    }

    class D: C {
      // in D, T.T == D does not hold
    }

Warn about this in Swift 3 compatibility mode, error on it in Swift 4
mode. When possible, provide a note + Fix-It suggesting that the
same-type constraint might be weakened to a superclass constraint
(which works with subclassing).

Fixes rdar://problem/30398503.
2017-05-22 09:31:27 -07:00
Doug Gregor
ff40d177c6 Merge pull request #9651 from DougGregor/lazy-generic-env-deserialization
[Serialization] Stop pre-loading generic environments.
2017-05-16 16:14:49 -07:00
Doug Gregor
f792aeaf1a [AST] Teach the AST verifier not to deserialize generic environments.
The AST verifier was causing deserialization of generic environments,
which slows things down considerably and affects our ability to test
for laziness in deserialization. Prevent it from doing so---and only
do the extra checkig if something else deserialized the generic
environment already.

... except there are some cases where it happens through means that
are harder to control (e.g., the AST walker for patterns) that need
more thought.
2017-05-16 11:03:15 -07:00
swift-ci
04de6586ef Merge pull request #9631 from DougGregor/gsb-perf 2017-05-15 22:59:03 -07:00
Doug Gregor
fef69478f6 [GSB] Introduce computeGenericSignature() for generic signature creation.
The GenericSignatureBuilder requires `finalize()` to be called before a
generic signature can be retrieved with `getGenericSignature()`. Most of the former isn’t strictly needed unless you want a generic signature, and the 
latter is potentially expensive. `computeGenericSignature()` combines the two
operations together, since they are conceptually related. Update most of the
callers to the former two functions to use `computeGenericSignature()`.
2017-05-15 17:16:50 -07:00
Joe Groff
6eaa9ee03c Don't misclassify protocol extension properties as polymorphic.
Part of fixing rdar://problem/32201589.
2017-05-15 14:12:41 -07:00
Jordan Rose
8a314d9440 Disallow subclassing when a class is missing vtable entries.
This isn't an inherent limitation of the language---in fact, it would
be a problem for library evolution if you had to know a superclass's
full vtable contents to generate the vtable for a subclass. However,
that's exactly where we are today, and that's not going to change for
Swift 4.

One small hole in the Swift 3 / Swift 4 story.

More rdar://problem/31878396
2017-05-10 17:03:32 -07:00
Jordan Rose
4cdb597b23 Rename VTablePlaceholderDecl to MissingMemberDecl.
As such, we no longer insert two placeholders for initializers that
need two vtable slots; instead we record that in the
MissingMemberDecl. I can see MissingMemberDecl growing to be something
we'd actually show to users, that can be used for other kinds of
declarations that don't have vtable entries, but for now I'm not going
to worry about any of that.
2017-05-10 11:55:55 -06:00
Jordan Rose
82930033d2 Add VTablePlaceholderDecl (but don't hook it up to anything yet). 2017-05-10 11:55:54 -06:00
Michael Gottesman
86620aaa7e Merge pull request #9318 from practicalswift/redundant-types-in-casts
[gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
2017-05-05 13:37:12 -07:00
practicalswift
492f5cd35a [gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
Replace `NameOfType foo = dyn_cast<NameOfType>(bar)` with DRY version `auto foo = dyn_cast<NameOfType>(bar)`.

The DRY auto version is by far the dominant form already used in the repo, so this PR merely brings the exceptional cases (redundant repetition form) in line with the dominant form (auto form).

See the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names) for a general discussion on why to use `auto` to avoid redundant repetition of type names.
2017-05-05 09:45:53 +02:00
Jordan Rose
6c098033de Lift the decision of whether a method needs a vtable slot up to AST.
This lets us serialize that decision, which means we can conceivably
/change/ the decision in later versions of the compiler without
breaking existing code. More immediately, it's groundwork that will
eventually allow us to drop decls from the AST without affecting
vtable layout.

This isn't actually a great answer; what we really want is for SIL
vtables to be serialized consistently and treated as the point of
truth. But that would be more change than we're comfortable taking in
the Swift 4 timeframe.

First part of rdar://problem/31878396.
2017-05-04 17:49:47 -07:00
Slava Pestov
b5721e8d8e AST: Remove AnyObject protocol 2017-05-02 19:45:00 -07:00
Slava Pestov
0d4a0b7dc1 AST: Update ProtocolDecl::requiresClass() for primitive AnyObject 2017-05-01 16:43:51 -07:00
Doug Gregor
447dce6c1f Merge pull request #9004 from itaiferber/swift-archival-serialization
Swift Archival & Serialization API
2017-04-28 20:02:33 -07:00
Graydon Hoare
b5292f04f8 Add an assortment of new "always-on" metrics. 2017-04-28 13:56:13 -07:00
Jordan Rose
dbc148bfb3 [ClangImporter] Tag compatibility aliases as such...
...so we can avoid showing an 'aka' for them...unless they are
themselves typealiases.
2017-04-26 13:07:03 -07:00
Itai Ferber
c515b53fcc Remove unnecessary enum case check 2017-04-26 11:06:38 -07:00
Itai Ferber
e2318392be Add CodingKey and *codable derived conformance
* Allow CodingKey conformance to be automatically derived for enums
  which have no raw type (with no associated values) and which have
  a raw type of String or Int.
* Allow Encodable and Decodable conformance to be automatically derived
  for classes and structs with Encodable/Decodable properties
* Add initial unit tests for verifying derived conformance
2017-04-25 13:13:35 -07:00
Jordan Rose
7397340ca8 Don't inherit convenience inits if a designated init is missing. (#8708)
(which can happen if an imported class has un-importable initializers)

Our initializer model guarantees that it's safe to inherit convenience
initializers when a subclass has implemented all designated
initializers, since each convenience initializer will be implemented
by calling one of the designated initializers. If one of the
designated initializers /can't/ be implemented in Swift, however,
then inheriting the convenience initializer would not be safe.

This is potentially a source-breaking change, so the importer will
only actually record that it failed to import something in when
compiling in Swift 4 mode.

rdar://problem/31563662
2017-04-24 11:47:54 -07:00
Doug Gregor
facf1996b1 [Clang importer] Don't transfer IBAction attributes that don't make sense.
Fixes an error emitted on declarations imported from Objective-C,
rdar://problem/31776315.
2017-04-22 21:38:23 -07:00
Graydon Hoare
c25210f6eb Move IsUserAccessible bit from VarDecl up to ValueDecl (and adjust bits). 2017-04-18 23:21:43 -07:00
Graydon Hoare
3a79d83233 Forward declare TypeRepr in TypeLoc.h, adjust other code to compensate. 2017-04-18 11:12:53 -07:00
Huon Wilson
53b4e7044f Merge pull request #8822 from huonw/symbol-list-5
Yet more TBDGen work: mostly classes.
2017-04-18 11:04:57 -07:00
Doug Gregor
17846e2be1 [GSB] Cope with recursive requirements by delaying them.
Rather than detecting recursion and bailing early, delay requirements
that would form recursive types. Note that we aren't actually
processing them later.
2017-04-17 23:13:21 -07:00
Huon Wilson
5ce7289db8 [AST] No redundant clang node check for metaclass ObjC-ness.
It seems that any type that has a clang node will have a non-NonObjC
ObjCClassKind, so this Or expression was essentially `foo() || true`.
2017-04-17 17:15:16 -07:00
Huon Wilson
d26c9791e5 [AST] Classes know their appropriate metaclass. 2017-04-17 11:28:55 -07:00
Doug Gregor
faa0401598 [GSB] Allow requirement inference for synthesized requirements. 2017-04-14 17:19:02 -07:00
practicalswift
7684e73388 Merge pull request #8706 from practicalswift/excess-logic
[gardening] Remove redundant logic
2017-04-12 09:51:49 +02:00
Robert Widmann
5b3c4b6844 [NFC] Use the presence of an argument type to check for associated values (#8679)
* Use the presence of an argument type to check for associated values

hasOnlyCasesWithoutAssociatedValues returns true for any serialized
enum declaration whether or not it has cases.  This never really came
up because it's mostly relevant to Sema's proto-deriving mechanism.  Fix
this by using the presence of the case's argument type instead.

* Separate checks for presence of cases and enum simplicity

Necessary because the old behavior was an artifact of the
implementation.
2017-04-11 20:13:17 -04:00
practicalswift
5e255e07d7 [gardening] Remove redundant logic 2017-04-11 23:04:55 +02:00
Slava Pestov
ad417c96ad AST: Update ProtocolCompositionType::requiresClass() for subclass existentials
Whether a protocol composition requires a class is no longer
solely a property of what protocols it contains; now, a
protocol composition consisting of a non-class constrained
protocol and a superclass should return true from requiresClass().

This is done by looking at the ExistentialLayout, instead of
walking the members directly.
2017-04-10 17:05:43 -07:00
Slava Pestov
37491e63ac AST: Refactor existential type accessors on TypeBase and CanType 2017-04-03 23:14:25 -07:00
Slava Pestov
9a3f0fc527 Sema: Fix for SourceKit crash in inheritsSuperclassInitializers()
Two problems:

1) We were tearing down the type checker too early, before all
   relevant decls in other files had been type checked, so there
   was no LazyResolver available for use in the ASTContext.

   This might explain the crashes coming through
   diagnoseUnintendedObjCMethodOverrides().

2) When a lazy resolver was set in the ASTContext, we were not
   using it in the case where nullptr was passed in as the
   'resolver' parameter to inheritsSuperclassInitializers().

   This might fix the crashes where we were coming in from other
   code paths, but I'm less confident about this case.

Possibly fixes <rdar://problem/29043074>, <rdar://problem/30976765>,
<rdar://problem/31122590>, and <rdar://problem/31286627>, and the
many other similar-looking dupes.
2017-03-31 00:04:17 -07:00
Xi Ge
75e191f5ac AST: Summarize a commonly used call chain in AbstractFunctionDecl. NFC (#8424) 2017-03-30 10:51:10 -07:00
Slava Pestov
a79ef97c32 AST: Repent for my sins by adding new assertions
Since the previous fix is essentially a hack to dodge an
assertion, I'm adding stronger assertions to
AbstractTypeParamDecl::getSuperclass() and
getConformingProtocols().
2017-03-30 02:03:53 -07:00
Slava Pestov
91b980b94d SILGen: Dynamic, curry, foreign thunks should be serializable
This fixes a crash when referencing partially-applied methods
from @_inlineable functions.

Also, curry thunks for private methods do not need shared
linkage; private is sufficient.
2017-03-29 21:35:25 -07:00
Slava Pestov
53759f7126 Use ValueDecl::isDynamic() instead of getAttrs().hasAttribute<DynamicAttr>() 2017-03-29 00:46:43 -07:00
Erik Eckstein
d70bfc5de2 rename namespace NewMangling -> Mangle 2017-03-20 10:09:30 -07:00
David Hart
ecdcac5aa2 Resolve crasher 28704 (#8158)
This crash happens because shouldAddSelfFixit (a check for the expected_self_before_reference diagnotic) performs name lookup inside the parser, which causes a search for extensions, which triggers the mangler, which can get confused if we are in an unfinished declaration (a PatternBindingInitilizer in this case).

A cleaner fix would be to move that logic inside Sema, but it would require much more refactoring that I don’t feel comfortable doing. Instead, this band-aid checks if the innermost type is inside a local context. If that’s the case, we can ignore the search for extensions.
2017-03-17 18:43:23 -07:00
Huon Wilson
ca3a398b4a Merge pull request #8021 from huonw/protocol-where-clause
Parse/typecheck/print where clauses on protocols
2017-03-15 11:00:46 -07:00
Slava Pestov
f0e3459068 AST: Fix compiler_crasher regression from associated type where clause work
New GenericTypeParamDecls should always be created with an
InvalidDepth, to prevent canonicalization before the generic
parameter list has been type checked.
2017-03-14 00:10:43 -07:00
Slava Pestov
aaa995a626 AST: Remove unnecessary AllocateCopy() calls
A GenericSignature stores parameters and requirements inside its own
allocation, so there's no need to copy them.
2017-03-14 00:10:43 -07:00
Slava Pestov
162b2d252e AST: Include gardening to minimize dependencies on Expr.h
A lot of files transitively include Expr.h, because it was
included from SILInstruction.h, SILLocation.h and SILDeclRef.h.

However in reality most of these files don't do anything
with Exprs, especially not anything in IRGen or the SILOptimizer.

Now we're down to 171 files in the frontend which depend on
Expr.h, which is still a lot but much better than before.
2017-03-12 22:26:56 -07:00
Alex Hoppen
dd1c661c16 Merge pull request #8015 from ahoppen/isOperator-gardening
[Gardening] Change calls of getName().isOperator() to isOperator()
2017-03-10 21:10:33 +00:00
Alex Hoppen
1b7055db5a [Gardening] Change calls of getName().isOperator() to isOperator() 2017-03-10 09:06:20 +00:00
Huon Wilson
54f247693c [AST]/[Parse] parse where clauses on protocol declarations. 2017-03-09 16:08:16 -08:00
Slava Pestov
df81e51f36 AST: Add TypeBase::getRootGenericParam() utility method 2017-03-08 13:54:29 -08:00
Doug Gregor
dbf5782d56 [GSB] Provide a root potential archetype for requirement-signature sources. 2017-02-28 09:42:06 -08:00