Commit Graph

620 Commits

Author SHA1 Message Date
Robert Widmann
987cd55f50 [NFC] Drop llvm::Expected from Evaluation Points
A request is intended to be a pure function of its inputs. That function could, in theory, fail. In practice, there were basically no requests taking advantage of this ability - the few that were using it to explicitly detect cycles can just return reasonable defaults instead of forwarding the error on up the stack.

This is because cycles are checked by *the Evaluator*, and are unwound by the Evaluator.

Therefore, restore the idea that the evaluate functions are themselves pure, but keep the idea that *evaluation* of those requests may fail. This model enables the best of both worlds: we not only keep the evaluator flexible enough to handle future use cases like cancellation and diagnostic invalidation, but also request-based dependencies using the values computed at the evaluation points. These aforementioned use cases would use the llvm::Expected interface and the regular evaluation-point interface respectively.
2020-03-26 23:08:02 -07:00
Robert Widmann
f2a1abc5dd [NFC] Refactor Side-Effecting Requests to be Explicitly so
Introduce evaluator::SideEffect, the type of a request that performs
some operation solely to execute its side effects. Thankfully, there are
precious few requests that need to use this type in practice, but it's
good to call them out explicitly so we can get around to making them
behave much more functionally in the future.
2020-03-26 22:55:20 -07:00
Dan Zheng
e5cb871428 [AutoDiff upstream] Add flag-gated AdditiveArithmetic derivation. (#30628)
Add `AdditiveArithmetic` derived conformances for structs, gated by the
`-enable-experimential-additive-arithmetic-derivation` flag.

Structs whose stored properties all conform to `AdditiveArithmetic` can derive
`AdditiveArithmetic`:
- `static var zero: Self`
- `static func +(lhs: Self, rhs: Self) -> Self`
- `static func -(lhs: Self, rhs: Self) -> Self`
- An "effective memberwise initializer":
  - Either a synthesized memberwise initializer or a user-defined initializer
    with the same type.

Effective memberwise initializers are used only by derived conformances for
`Self`-returning protocol requirements like `AdditiveArithmetic.+`, which
require memberwise initialization.

Resolves TF-844.
Unblocks TF-845: upstream `Differentiable` derived conformances.
2020-03-25 10:31:50 -07:00
Roopesh Chander
9c2dca7d6a [Property wrappers] Fix handling of @autoclosure in init(wrappedValue:)
If the 'wrappedValue:' parameter is an escaping autoclosure, and a
struct property is marked with that property wrapper, the memberwise
initializer of the struct is now synthesized with an escaping
autoclosure for that property.
2020-03-20 12:20:52 -07:00
Robert Widmann
de72824b04 [Gardening] Canonicalize usages of ASTContext::Stats 2020-02-27 17:12:58 -08:00
Robert Widmann
b0d741d7d1 Refactor AreAllStoredPropertiesDefaultInitableRequest for Property Wrappers
Do a bit of cleanup here. Also expand the storage check to query the
backing variable for any attached property wrappers. If these are not
default-initialized or default initializable then the generated
initializer will contain unfixable DI errors.

Resolves rdar://58495602
2020-02-18 17:14:34 -08:00
Slava Pestov
3c86061de8 Sema: Fix module interface printing of inherited generic initializers
Make sure we use the sugared form of GenericTypeParamType and not
the canonical type.

Fixes <rdar://problem/58578998>.
2020-01-29 12:32:22 -08:00
Brent Royal-Gordon
0c478b6be6 Revert "Merge pull request #28665 from CodaFi/the-phantom-menace"
This reverts commit 43a3ab7e35, reversing
changes made to 4f39d9c749.

# Conflicts:
#	include/swift/AST/Attr.def
#	lib/AST/Attr.cpp
#	lib/Serialization/Deserialization.cpp
#	lib/Serialization/ModuleFormat.h
#	lib/Serialization/Serialization.cpp
2020-01-15 15:28:42 -08:00
Harlan Haskins
283854a012 [Sema] Requestify hasMissingDesignatedInitializers
We’re going to start serializing this for public types that have non-public-or-@usableFromInline initializers, so turn it into a request that we can query and cache it in the existing bit.
2020-01-06 10:15:07 -08:00
Slava Pestov
7626f9de4f AST: Small cleanups
- Allow AbstractClosureExpr to be created with null body
- Split up ParameterList::CloneFlags::Inherited
2019-12-13 15:39:47 -05:00
Robert Widmann
785bda0250 Discard ResolveEncodable and ResolveDecodable 2019-12-10 16:28:51 -08:00
Doug Gregor
f51119ad6b [Property wrappers] Sync up implicit "nil" initialization defaulting.
Perform implicit "nil" initialization for a wrapped property only when
the memberwise initializer's parameter is of the wrapped type.
2019-12-05 22:04:18 -08:00
Hamish Knight
7ae3d1f8d3 Requestify default initializer context creation
This commit adds a request that computes
the initializer context for a parameter with a
default expr or stored property default.

This avoids having to compute them for synthesized
decls and is a step towards requestifying default
argument parsing.
2019-11-11 13:49:06 -08:00
Robert Widmann
d4906ac10b Rationalize Implicit Member Synthesis Somewhat
Codable's deep magic currently forces conformance checks in the middle
of name lookup in order to inject CodingKeys into lookup results.  This
is compounded by the fact that this lookup fixup is occuring
incrementally, meaning depending on order of requirements being looked
up, Decl::getMembers() will give you a different answer.

Compounding this, NameLookup relied on the LazyResolver to formalize
this layering violation, and relied on implicit laziness to guard
against re-entrancy.

The approach is multi-pronged:
1) Shift the layering violation into the request evaluator
2) Spell out the kinds of resolution we support explicitly (make them
easier to find and kill)
3) Remove the LazyResolver entrypoint this was relying on
4) Split off the property wrappers part into its own utility
2019-11-05 09:10:13 -08:00
Hamish Knight
40231991ae [Sema] Diagnose unsound pointer conversions (#27695)
[Sema] Diagnose unsound pointer conversions
2019-11-04 17:18:57 -08:00
Slava Pestov
ae8c85ef80 Sema: Remove some unnecessary (void) getInterfaceType() calls 2019-11-04 14:59:01 -05:00
Hamish Knight
8cccbe0118 [Sema] Infer @_nonEphemeral for various parameters
These include memberwise initializers for pointer properties and enum
case constructors for pointer payloads. In both cases, the pointer is
being immediately escaped, so don't allow the user to pass a temporary
pointer value.
2019-11-03 08:42:26 -08:00
Robert Widmann
a29bfb4434 Fold resolveImplicitConstructors into resolveImplicitMember 2019-11-01 18:11:24 -07:00
Robert Widmann
572bc52be0 Drop the type checker out of the multi-conformance checker 2019-11-01 17:58:49 -07:00
Robert Widmann
0af343469b Pre-Request Cleanup For Implicit Constructors
Push through an easy refactoring to the way we validate and install
implicit constructors.  This patch would be NFC but for a regression
test that now must diagnose.  #26159 changed validation order in such
a way that the code in validation-test-macosx-x86_64/compiler_crashers_2_fixed/0124-sr5825.swift
used to be accepted.  This patch once again changes validation order, so
we now reject this code, restoring the behavior seen on all prior
versions of Swift.

On its face, this test should work.  In order for it to do so, witness
matching has to be smarter about the declarations it asks for their
interface type, or it will risk these circular constructions
accidentally being accepted or rejected on a whim.
2019-10-31 13:13:08 -07:00
Hamish Knight
16cff49e65 [Sema] Requestify inheritsSuperclassInitializers (#27936)
* [Sema] Factor out shouldAttemptInitializerSynthesis

This makes sure we don't attempt to synthesize
a memberwise or default initializer for an invalid
decl, or one in a module interface.

* [Sema] Requesify inheritsSuperclassInitializers

This commit introduces a request for computing
whether a class inherits both designated and
convenience initializers from its superclass.

The shared logic of finding initializers which the
subclass hasn't overriden has been factored out
into `collectNonOveriddenSuperclassInits`.

* Cleanup addImplicitInheritedConstructorsToClass

This commit removes some code that's no longer
needed. In addition, now that we've requestified
`inheritsSuperclassInitializers`, we can directly
diagnose on non-inherited required convenience
inits within the loop.

* Inherited init synthesis no longer deals with clang decls

Now that the computation of
`inheritsSuperclassInitializers` has been split off
into a request, we can avoid calling
`addImplicitInheritedConstructorsToClass` for clang
decls.

* Address review feedback

Continue to cache the InheritsSuperclassInits bit
on the AST.
2019-10-31 11:07:06 -07:00
Robert Widmann
b849e51768 Use operator bool to claw back some readability 2019-10-29 16:56:21 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Hamish Knight
ce2c2ca719 Return early from addImplicitInheritedConstructorsToClass
Now that it's only dealing with adding inherited
constructors, bail out early if we don't have a
superclass.

The diff for this is best viewed without whitespace
changes.
2019-10-25 11:23:28 -07:00
Hamish Knight
961c16a2cc [Sema] Requestify default init synthesis
This commit adds two requests, one to compute
whether or not a decl should have a default
initializer, and another to synthesize it.

This then allows us to remove the default constructor
synthesis from addImplicitConstructorsToClass as
well as completely eliminate addImplicitConstructorsToStruct.
For now, we can just force the requests in
addImplicitConstructors.
2019-10-25 11:23:28 -07:00
Hamish Knight
ffd5a9cfd6 [Sema] Requestify memberwise init synthesis
This commit adds two requests, one to compute
whether or not a decl should have a memberwise
initializer, and another to synthesize it.
2019-10-25 11:20:39 -07:00
Hamish Knight
d8477d51f2 [Sema] Requestify hasUserDefinedDesignatedInit 2019-10-25 11:20:38 -07:00
Hamish Knight
704766cee3 [Sema] Requestify areAllStoredPropertiesDefaultInitializable 2019-10-25 11:20:38 -07:00
Hamish Knight
0c7f1c6040 [Sema] Remove some dead code
isMemberwiseInitialized already returns false for
the case where the var `isLet() && isParentInitialized()`,
so this check isn't reachable.

Also remove an unnecessary `continue`.
2019-10-25 11:20:38 -07:00
Hamish Knight
8adf3400d4 Make addImplicitConstructors a static method 2019-10-22 09:23:35 -07:00
Robert Widmann
497a2227ba [NFC] Remove AbstractFunctionDecl::computeType()
Its functionality is entirely subsumed by InterfaceTypeRequest.
2019-10-21 12:15:50 -07:00
Robert Widmann
3b829943af Uniformly iterate over the pattern binding entry indices
Clarify a bunch of C-style for loops and remove a ton of references to getPatternList().
2019-10-17 13:39:07 -07:00
Robert Widmann
56b6e53dae Remove raw references to PatternBindingEntry APIs
Switch most callers to explicit indices.  The exceptions lie in things that needs to manipulate the parsed output directly including the Parser and components of the ASTScope.  These are included as friend class exceptions.
2019-10-17 13:31:14 -07:00
Robert Widmann
2516089cb6 Clean up the pseudo-clone-constructor for ParamDecl 2019-10-11 11:51:50 -07:00
Robert Widmann
060cbb293f [NFC] Downgrade The TypeLoc in VarDecl to a TypeRepr
TypeCheckPattern used to splat the interface type into this, and
different parts of the compiler would check one or the other.   There is
now one source of truth: The interface type.  The type repr is now just
a signal that the user has written an explicit type annotation on
a parameter.  For variables, we will eventually be able to just grab
this information from the parent pattern.
2019-10-11 11:15:51 -07:00
Slava Pestov
d8b61ff24b Sema: Peel off typeCheckParameterList()'s specifier computation into a request
Since getSpecifier() now kicks off a request instead of always
returning what was previously set, we can't pass a ParamSpecifier
to the ParamDecl constructor anymore. Instead, callers either
call setSpecifier() if the ParamDecl is synthesized, or they
rely on the request, which can compute the specifier in three
specific cases:

- Ordinary parsed parameters get their specifier from the TypeRepr.

- The 'self' parameter's specifier is based on the self access kind.

- Accessor parameters are either the 'newValue' parameter of a
  setter, or a cloned subscript parameter.

For closure parameters with inferred types, we still end up
calling setSpecifier() twice, once to set the initial defalut
value and a second time when applying the solution in the
case that we inferred an 'inout' specifier. In practice this
should not be a big problem because expression type checking
walks the AST in a pre-determined order anyway.
2019-10-10 15:00:07 -04:00
Slava Pestov
64224cadcc Merge pull request #27553 from slavapestov/circular-validation-cleanups-4
Circular validation cleanups, part 4
2019-10-08 10:46:42 -04:00
Slava Pestov
a260d0a445 AST: getInterfaceType() returns ErrorType instead of Type() on circularity 2019-10-08 01:37:09 -04:00
Hamish Knight
3f66608cb5 Check for stubs in NeedsNewVTableEntryRequest
This lets us remove `setNeedsNewVTableEntry`.
2019-10-03 19:13:39 -07:00
Slava Pestov
6702d38d87 Sema: Remove most calls to Decl::setValidationToChecked() 2019-10-03 17:11:45 -04:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
Robert Widmann
f053f9c480 Port getInterfaceType() patterns in libSema 2019-09-23 16:49:29 -07:00
Robert Widmann
aad82fd9c5 [NFC] Remove unnecessary validation noise
Drop some callers to validateDecl and resolveDeclSignature that did not
actually need the interface type.
2019-09-21 09:56:31 -07:00
Robert Widmann
b135928125 Drop CheckingWithValidSignature from the validation state machine
Now that the generic signature is computable on demand, this predicate is doubly useless.  All of the callers intended to ask "hasInterfaceType" anyways.
2019-09-20 22:22:49 -07:00
Jordan Rose
8d7f1b7c5d [AST] Separate SourceFile from FileUnit.h
Like the last commit, SourceFile is used a lot by Parse and Sema, but
less so by the ClangImporter and (de)Serialization. Split it out to
cut down on recompilation times when something changes.

This commit does /not/ split the implementation of SourceFile out of
Module.cpp, which is where most of it lives. That might also be a
reasonable change, but the reason I was reluctant to is because a
number of SourceFile members correspond to the entry points in
ModuleDecl. Someone else can pick this up later if they decide it's a
good idea.

No functionality change.
2019-09-17 17:54:41 -07:00
Harlan Haskins
c82c9b8210 [ModuleInterfaces] Remove references to 'parseable' interfaces everywhere
Now that we've settled on Module Interface as the name, let's remove the
vestiges of "Parseable Interfaces"
2019-09-13 14:55:48 -07:00
Slava Pestov
f292c1b240 AST: Remove GenericContext::setGenericEnvironment()
This was a transitional step before converting all callers to
store a generic signature instead of an environment.
2019-09-06 17:16:04 -04:00
Slava Pestov
1e94466bcc AST: Replace GenericSignature::createGenericEnvironment() with getGenericEnvironment()
This memoizes the result, which is fine for all callers; the only
exception is open existential types where each new open existential
now explicitly gets a unique generic environment, allocated by
calling GenericEnvironment::getIncomplete().
2019-09-06 17:16:03 -04:00
Slava Pestov
4668def6b3 Sema: Split addImplicitConstructors() into separate functions for structs and classes 2019-09-03 22:39:35 -04:00
Slava Pestov
c87e1a23db Sema: Try harder not to page in all members of imported classes 2019-09-03 22:39:35 -04:00