Commit Graph

702 Commits

Author SHA1 Message Date
Slava Pestov
73deb7e833 Sema: Introduce a simpler form of checkGenericArguments() 2021-05-17 16:34:18 -04:00
Slava Pestov
379220b9e2 Sema: Fix a couple of issues with implicit overrides of generic designated initializers
If a base class initializer was not generic but had a contextual 'where'
clause, we would unconditionally inherit the 'where' clause in the override as
part of the fix for another issue (https://bugs.swift.org/browse/SR-14118).

However, it is possible that some of those requirements are satisfied by
the derived class itself. For example:

    class Base<T> {
        init() where T : AnyObject {}
    }

    class Derived : Base<AnyObject> {}

In this case, the implicit override Derived.init() has no generic requirements
at all. We used to assert because we would create a GSB with no generic
parameters.

It was also possible for a base class initializer to have generic
requirements that are not satisfied by the derived class. In this case,
we need to drop the initializer from consideration altogether.

While I'm here, I improved the comments in computeDesignatedInitOverrideSignature(),
which is the new name for configureGenericDesignatedInitOverride().

Fixes rdar://problem/77285618.
2021-05-14 19:02:57 -04:00
Hamish Knight
d309caea11 [AST] Remove 'is super' bit on ApplyExpr
This appears to be equivalent to
`getArg()->isSuperExpr()`. Let's use that instead.
2021-04-26 12:06:52 +01:00
Slava Pestov
1b91218633 AutoDiff: Don't check hasInterfaceType() in ResolveEffectiveMemberwiseInitRequest::evaluate()
This check made this request depend on evaluation order, sometimes
synthesizing a second memberwise initializer if the previous one's
interface type had not been computed yet.
2021-04-21 00:07:28 -04:00
Azoy
9ed732f0ab Introduce isDecl and getDeclType
fix enum logic issue

fix tests

guard against null types
2021-04-20 02:22:16 -04:00
Kavon Farvardin
4f6eb85fe3 allow type initializers to be 'async'
implicit calls to an async super.init are not allowed
2021-04-15 10:08:53 -07:00
Holly Borla
1efb638681 [Sema] Add a helper function to get the innermost declaration with an
@available annotation.
2021-04-05 15:39:10 -07:00
Holly Borla
21a86b5d2f [NFC][Property Wrappers] Split PropertyWrapperBackingPropertyInfoRequest into
two separate requests - one to synthesize the auxiliary declarations, and
another to compute how the backing storage is initialized.
2021-03-19 13:03:13 -07:00
Holly Borla
13692fefde [Property Wrappers] Store property wrapper "init from projection" expressions
in PropertyWrapperBackingPropertyInfo.
2021-02-25 18:35:14 -08:00
Slava Pestov
20d4a3e50b Sema: Inherited designated initializers should inherit the 'rethrows' attribute
I found this by inspection. We did not mark the override as 'rethrows'
when the base designated initializer was 'rethrows'.

The FIXMEs in the test case are a separate issue, tracked by
https://bugs.swift.org/browse/SR-680.
2021-02-15 22:15:53 -05:00
Robert Widmann
013127a947 Fix a Simple Crash-On-Invalid For @_spi
The parser attempts to recover here by producing a broken VarDecl with
no parent pattern. The DeclChecker sees this when trying to install
a default initializer in the linked regression test and crashes because
it tries to look into a null parent pattern.

rdar://74154023
2021-02-09 12:36:50 -08:00
Slava Pestov
3dfbf156c7 Sema: Fix inherited designated init synthesis when there's a 'where' clause but no new generic parameters
SE-0267 allows where clauses on declarations without their own generic
parameters, which means we can longer assume that if getGenericParams()
returns nullptr on a constructor, then the constructor has the same
generic signature as its parent class.

Instead, explicitly compare the superclass constructor's generic
signature with that of the superclass, and if they don't match,
compute a new generic signature for the override.

Fixes https://bugs.swift.org/browse/SR-14118 / rdar://73764827.
2021-02-02 23:12:30 -05:00
Doug Gregor
3dc4c7868c Mark more declarations as synthesized 2021-01-20 12:56:31 -08:00
Slava Pestov
c8aee4180e Sema: Fix a couple of crash-on-invalid problems with class inheritance
It is possible for ClassDecl::getSuperclassDecl() to succeed but for
ClassDecl::getSuperclass() to fail. This happens if the superclass is
a generic type and one of the generic arguments could not be resolved,
or does not satisfy the generic requirements, for example; in that
case, a BoundGenericType cannot be formed.

In a couple of places we were not prepared for this possibility.
Let's recover by making judicious use of ErrorType.

Fixes <rdar://problem/73169149>.
2021-01-17 18:06:13 -05:00
Doug Gregor
6d41524fe6 [SE-0289] Finish renaming source code, tests to "result builders" 2020-10-20 22:18:51 -07:00
Doug Gregor
0d568a93d4 [SE-0289] Update diagnostics & many other strings to "result builders" 2020-10-20 21:44:09 -07:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Doug Gregor
4ddf6e565a Teach SuperclassDeclRequest to always diagnose circularity.
Rather than relying on clients to cope with the potential for circular
inheritance of superclass declarations, teach SuperclassDeclRequest to
establish whether circular inheritance has occurred and produce "null"
in such cases. This allows other clients to avoid having to think about

To benefit from this, have SuperclassTypeRequest evaluate
SuperclassDeclRequest first and, if null, produce a Type(). This
ensures that we don't get into an inconsistent situation where there
is a superclass type but no superclass declaration.
2020-09-29 17:42:17 -07:00
Doug Gregor
bec0aae7e5 [Function builders] Add support for function builders on stored struct properties.
Implements SR-13188, which is part of the function builders proposal
under review.
2020-09-26 21:56:12 -07:00
Doug Gregor
0f18948bd5 [Concurrency] Allow @objc async methods.
Allow the declaration of @objc async methods, mapping them to a
completion-handler API in Objective-C. This covers most of the
checking and semantics within the type checker:
* Declaring @objc async methods and checking their parameter/result types
* Determining the default Objective-C selector by adding
completionHandler/WithCompletionHandler as appropriate
* Determining the type of the completion handler parameter
* Inferring @objc from protocol requirements
* Inferring @objc from an overridden method
2020-09-08 10:15:24 -07:00
Slava Pestov
397e36a6f0 Sema: Remove TypeChecker::lookupConstructor() 2020-08-18 02:00:20 -04:00
Slava Pestov
45fc0bc4db Sema: Replace some calls to getDeclaredType() with getDeclaredInterfaceType() 2020-07-31 13:39:02 -04:00
Holly Borla
61cb9a5f4f Merge pull request #32866 from hborla/property-wrapper-autoclosure
[Property Wrappers] Use autoclosure information from CSApply to compute `VarDecl::getPropertyWrapperInitValueInterfaceType`
2020-07-17 09:16:05 -07:00
Holly Borla
a6c33a9611 [Property Wrappers] Use autoclosure information from CSApply to compute
`VarDecl::getPropertyWrapperInitValueInterfaceType`.
2020-07-13 17:22:29 -07:00
Brent Royal-Gordon
745dbef3ef Use file IDs in generated code
Such as force unwraps, as! casts, etc.
2020-07-13 14:05:13 -07:00
Slava Pestov
3aa33250ec Sema: Sink DiscriminatorFinder down into DebuggerTestingTransform 2020-06-25 19:35:06 -04:00
Dan Zheng
f9c5d7ae6c [AutoDiff] Derive Differentiable.zeroTangentVectorInitializer. (#31823)
`Differentiable` conformance derivation now supports
`Differentiable.zeroTangentVectorInitializer`.

There are two potential cases:
1. Memberwise derivation: done when `TangentVector` can be initialized memberwise.
2. `{ TangentVector.zero }` derivation: done as a fallback.

`zeroTangentVectorInitializer` is a closure that produces a zero tangent vector,
capturing minimal necessary information from `self`.

It is an instance property, unlike the static property `AdditiveArithmetic.zero`,
and should be used by the differentiation transform for correctness.

Remove `Differentiable.zeroTangentVectorInitializer` dummy default implementation.

Update stdlib `Differentiable` conformances and tests.
Clean up DerivedConformanceDifferentiable.cpp cruft.

Resolves TF-1007.
Progress towards TF-1008: differentiation correctness for projection operations.
2020-05-29 01:59:52 -07:00
Slava Pestov
742bd98402 Sema: Remove ConformanceCheckOptions::SkipConditionalRequirements
All callers can trivially be refactored to use ModuleDecl::lookupConformance()
instead. Since this was the last flag in ConformanceCheckOptions, we can remove
that, too.
2020-04-25 00:14:24 -04:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Hamish Knight
6a7b2e0040 [Sema] Handle metatypes in buildSelfReference
Before attempting to get the superclass of a
self parameter type, check to see if we have a
metatype, and perform the necessary unwrapping and
re-wrapping if needed.
2020-04-03 09:18:58 -07:00
Robert Widmann
1f904ca86d Merge pull request #30669 from CodaFi/consteval
[NFC] A Handful of Sweeping Evaluator Cleanups
2020-03-27 09:43:28 -07:00
Dan Zheng
bbe86e908d [AutoDiff upstream] Add Differentiable protocol derived conformances. (#30671)
Add `AdditiveArithmetic` derived conformances for structs and classes, gated by
the `-enable-experimental-differentiable-programming` flag.

Structs and classes whose stored properties all conform to `Differentiable` can
derive `Differentiable`:

- `associatedtype TangentVector: Differentiable & AdditiveArithmetic`
  - Member `TangentVector` structs are synthesized whose stored properties are
    all `var` stored properties that conform to `Differentiable` and that are
    not `@noDerivative`.
- `mutating func move(along: TangentVector)`

The `@noDerivative` attribute may be declared on stored properties to opt out of
inclusion in synthesized `TangentVector` structs.

Some stored properties cannot be used in `TangentVector` struct synthesis and
are implicitly marked as `@noDerivative`, with a warning:

- `let` stored properties.
  - These cannot be updated by `mutating func move(along: TangentVector)`.
- Non-`Differentiable`-conforming stored properties.

`@noDerivative` also implies `@_semantics("autodiff.nonvarying")`, which is
relevant for differentiable activity analysis.

Add type-checking and SILGen tests.

Resolves TF-845.
2020-03-27 06:40:04 -07:00
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