Commit Graph

275 Commits

Author SHA1 Message Date
Huon Wilson
19d785018c [Sema] Use the conformance decl in more diagnostics/validity checks in conformance synthesis. 2018-05-09 09:55:41 +10:00
Huon Wilson
47e643af0e [Sema] Fix synthesis of Equatable and Hashable conformances in extensions of generic types.
https://bugs.swift.org/browse/SR-6803
2018-05-07 09:41:25 +10:00
Huon Wilson
3488a3a53b [Sema] Allow synthesis of protocol conformances in extensions.
This works for all protocols except for Decodable on non-final classes, because
the init requirement has to be 'required' and thus in the type's declaration.

Fixes most of https://bugs.swift.org/browse/SR-6803.
2018-05-07 09:41:25 +10:00
Huon Wilson
5e094bdaa2 [Sema] Factor out "can't derive in extension" logic.
NFC.
2018-05-07 09:11:08 +10:00
Huon Wilson
cfd068de6a [Sema] Migrate almost everything to use DerivedConformance. 2018-05-07 09:11:07 +10:00
Huon Wilson
41942c9276 [Sema] Create a "DerivedConformance" class, to store common values.
Instead of passing around a TypeChecker and three Decls (the nominal type, the
protocol, and the decl declaring the conformance) everywhere, we can just pass
one object.

This should be [NFC].
2018-05-07 09:10:06 +10:00
Karoy Lorentey
fa264204e2 [SE-0206][Sema] Add separate hash(into:) impl for enums with no associated values
For enums with no associated values, it is better to move the hasher.combine call out of the switch statement, like this:

func hash(into hasher: inout Hasher) {
  let discriminator: Int
  switch self {
    case a: discriminator = 0
    case b: discriminator = 1
    case c: discriminator = 2
  }
  hasher.combine(discriminator)
}

This enables the optimizer to replace the switch statement with a simple integer promotion, restoring earlier behavior.
2018-04-24 17:43:22 +01:00
Karoy Lorentey
23c1b24e6a [SE-0206][Sema] Update Hashable synthesizer to support hash(into:)
This removes the default implementation of hash(into:), and replaces it with automatic synthesis built into the compiler. Hashable can now be implemented by defining either hashValue or hash(into:) -- the compiler supplies the missing half automatically, in all cases.

To determine which hash(into:) implementation to generate, the synthesizer resolves hashValue -- if it finds a synthesized definition for it, then the generated hash(into:) body implements hashing from scratch, feeding components into the hasher. Otherwise, the body implements hash(into:) in terms of hashValue.
2018-04-24 17:42:42 +01:00
Jordan Rose
28a1dc7770 Use 'fileprivate' for synthesized members of 'private' decls (#15980)
Since 'private' means "limit to the enclosing scope (and extensions
thereof)", putting it on a member means that the member can't be
accessed everywhere the type might show up. That's normally a good
thing, but it's not the desired effect for synthesized members used
for derived conformances, and when it comes to class initializers this
actually violates AST invariants.

rdar://problem/39478298
2018-04-17 15:19:35 -07:00
Jordan Rose
701975ad1d Add parsing support for @unknown (SE-0192)
This is our first statement attribute, made more complicated by the
fact that a 'case'/'default' isn't really a normal statement. I've
chosen /not/ to implement a general statement attribute logic like we
have for types and decls at this time, but I did get the compiler
parsing arbitrary attributes before 'case' and 'default'. As a bonus,
we now treat all cases within functions as being switch-like rather
than enum-like, which is better for recovery when not in a switch.
2018-04-05 16:35:14 -07:00
Jordan Rose
f26664b1f1 [AST] Adjust the API of CaseLabelItem for unknown
Get these simple changes out of the way first. No functionality change.
2018-04-04 16:19:11 -07:00
Slava Pestov
fd79f66d39 Sema: Keep track of per-source file synthesized declarations
This allows conformance checking to be performed after all
members of a type have been type checked.
2018-04-02 23:17:58 -07:00
Slava Pestov
3bcdf1a027 AST: New ASTContext::addSynthesizedDecl() method
This adds the declaration to the external declarations list only
if it was part of an imported declaration, eliminating some
boilerplate.
2018-04-02 23:17:58 -07:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Robert Widmann
03580d2fe5 Add a parameter list to EnumElementDecl
This models, but does not plumb through, default arguments.
2018-03-28 00:05:56 -04:00
Slava Pestov
a8602f8eb8 Sema: Call setValidationStarted() on various synthesized FuncDecls 2018-03-21 23:54:47 -07:00
Huon Wilson
e307e54098 [AST] Explicitly track things marked __owned. 2018-03-08 12:36:24 +11:00
Mark Lacey
22dde52450 Remove unused static function whose last use was removed in https://github.com/apple/swift/pull/14386. 2018-02-10 21:13:50 -08:00
Xiaodi Wu
8014793cf1 Remove final _mixInt in synthesized hashValue and improve the _combineHashValues magic number 2018-02-03 00:06:48 -06:00
John McCall
7f0f8830cd Split AccessorDecl out from FuncDecl. NFC.
This has three principal advantages:

- It gives some additional type-safety when working
  with known accessors.

- It makes it significantly easier to test whether a declaration
  is an accessor and encourages the use of a common idiom.

- It saves a small amount of memory in both FuncDecl and its
  serialized form.
2018-01-12 14:20:27 -05:00
Xiaodi Wu
30d5e344b1 Rename the combine hashes function and use a common implementation 2017-12-10 02:13:25 -06:00
Nobuo Saito
18815fb78c Improve sample code of deriveHashable_hashValue (#13273)
Seems `SomeEnumWithAssociatedValues` implements require `result` initial value.
2017-12-06 09:07:26 +09:00
Graydon Hoare
6d46fb144f [AST] Move copyFormalAccess to ValueDecl. 2017-11-06 14:31:21 -08:00
Tony Allevato
13ad81a173 Synthed hashValue now calls _mixForSynthesizedHashValue
For enums, we now only call _mixInt on the computed hash value if
it has associated values; for enums without associated values, we
use the ordinal alone, as before.
2017-09-24 10:57:31 -07:00
Tony Allevato
f2c434a038 Merge branch 'master' into synthesize-equatable-hashable 2017-09-21 22:54:36 -07:00
Slava Pestov
10bd85cb34 Sema: Fix Equatable, Hashable, RawRepresentable derivation for @_versioned enums
The synthesized declarations should inherit the @_versioned attribute
from the type, just like they inherit access control.

Fixes <rdar://problem/34342955>.
2017-09-17 21:35:55 -07:00
Slava Pestov
39872d9a96 Sema: Remove "at least internal" check for derived conformance declarations
It looks like at some point derived conformances were in their own
FileUnit, but this is no longer the case.
2017-09-17 21:35:55 -07:00
Tony Allevato
3731382bf6 Merge branch 'master' into synthesize-equatable-hashable 2017-09-09 14:23:13 -07:00
Tony Allevato
f37759784d [Sema] Apply review feedback
- Remove unnecessary helper function
- Remove explicit ConcreteDeclRef ctor calls
- Clean up some function casts
2017-09-09 14:13:31 -07:00
Jordan Rose
1c651973c3 Excise "Accessibility" from the compiler (2/3)
"Accessibility" has a different meaning for app developers, so we've
already deliberately excised it from our diagnostics in favor of terms
like "access control" and "access level". Do the same in the compiler
now that we aren't constantly pulling things into the release branch.

This commit changes the 'Accessibility' enum to be named 'AccessLevel'.
2017-08-28 11:34:44 -07:00
Jordan Rose
5f30eac288 Excise "Accessibility" from the compiler (1/3)
"Accessibility" has a different meaning for app developers, so we've
already deliberately excised it from our diagnostics in favor of terms
like "access control" and "access level". Do the same in the compiler
now that we aren't constantly pulling things into the release branch.

This commit changes the names of methods, fields, a few local
variables, and even a swift-ide-test flag. The full list is below.

accessibilityForDiagnostics -> accessLevelForDiagnostics
checkAccessibility -> checkAccess
checkGenericParamAccessibility -> checkGenericParamAccess
checkTypeAccessibility -> checkTypeAccess
checkWitnessAccessibility -> checkWitnessAccessibility
computeAccessibility -> computeAccessLevel
computeDefaultAccessibility -> computeDefaultAccessLevel
fixItAccessibility -> fixItAccess
getAccessibilityString -> getAccessLevelString
getAccessibilityStrictly -> getAccessLevelStrictly
getAccessibilityUID -> getAccessLevelUID
getActualAccessibility -> getActualAccessLevel
getDefaultAccessibility -> getDefaultAccessLevel
getMaxAccessibility -> getMaxAccessLevel
getOverridableAccessibility -> getOverridableAccessLevel
getRawStableAccessibility -> getRawStableAccessLevel
getSetterAccessibility -> getSetterFormalAccess
hasAccessibility -> hasAccess
hasDefaultAccessibility -> hasDefaultAccessLevel
inferAccessibility -> inferAccessLevel
inferDefaultAccessibility -> inferDefaultAccessLevel
inferSetterAccessibility -> inferSetterAccessLevel
overwriteAccessibility -> overwriteAccess
overwriteSetterAccessibility -> overwriteSetterAccess
printAccessibility -> printAccess
requiredAccessibilityForDiagnostics -> requiredAccessForDiagnostics
resolveAccessibility -> resolveAccessControl
setAccessibility -> setAccess
setSetterAccessibility -> setSetterAccess
setDefaultAndMaxAccessibility -> setDefaultAndMaxAccess
validateAccessibility -> validateAccessControl

Accessibility -> AccessLevel
AccessibilityFilter -> AccessFilter
IgnoreAccessibility -> IgnoreAccessControl
NL_IgnoreAccessibility -> NL_IgnoreAccessControl
PrintAccessibility -> PrintAccess
PrintInternalAccessibilityKeyword -> PrintInternalAccessKeyword
SetterAccessibility -> SetterAccessLevel

setterAccessibility -> setterAccess
storedPropertyAccessibility -> storedPropertyAccess

-print-accessibility -> -print-access
2017-08-28 11:11:57 -07:00
Tony Allevato
d9827accc4 [Sema] inout/ASTNode fixes after rebasing 2017-08-08 22:02:23 -07:00
Tony Allevato
fca78a3634 [Sema] Fix type checking for enums defined in other files 2017-08-08 18:30:22 -07:00
Tony Allevato
fd93e03768 [Sema] Forbid synthesis in an extension
The exception is if the conformance is already redundant (for example,
the implicit synthesis for RawRepresentable enums).
2017-08-08 18:30:22 -07:00
Tony Allevato
a3db968057 Move derivesProtocolConformances from AST to Sema 2017-08-08 18:30:22 -07:00
Tony Allevato
aa93ee52e1 Formatting and other cleanup. 2017-08-08 18:30:22 -07:00
Tony Allevato
425de49454 [Sema] Synthesize Eq/Hash for enums with payloads, structs 2017-08-08 18:20:58 -07:00
Robert Widmann
e5918f70e8 [NFC] Refactor self type computation to return a Param
Remove a user of InOutType::get and flip a few users of
FunctionType::get to pass Params instead of naked input
types.
2017-07-23 21:36:16 -07:00
Robert Widmann
4da853e7cb Rename Specifier::None to Specifier::Owned 2017-07-05 14:02:26 -07:00
Robert Widmann
ac5594dabe Use a meaningful representation of parameter specifiers
In anticipation of future attributes, and perhaps the ability to
declare lvalues with specifiers other than 'let' and 'var', expand
the "isLet" bit into a more general "specifier" field.
2017-06-29 16:03:49 -07:00
Rintaro Ishizaki
5d478bdb3b [Parse] Allow #if to guard switch case clauses
Resolves:
https://bugs.swift.org/browse/SR-4196
https://bugs.swift.org/browse/SR-2
2017-06-17 10:03:03 +09:00
Jordan Rose
4468ad2028 [Sema] Preparations for removal of getName on ValueDecl (#9972)
With the introduction of special decl names, `Identifier getName()` on
`ValueDecl` will be removed and pushed down to nominal declarations
whose name is guaranteed not to be special. Prepare for this by calling
to `DeclBaseName getBaseName()` instead where appropriate.
2017-05-28 22:36:39 -07:00
Mark Lacey
de10c41915 Fix another case where the type checker will set the correct types.
We should not be providing types that we can/should derive
automatically.
2017-04-29 23:06:17 -07:00
Mark Lacey
9e38a8efea Do not hard-code a result type that the type checker will fill in.
Small clean-up.
2017-04-29 21:37:01 -07:00
Graydon Hoare
e6027eeb4e Derive @_implements(Equatable, ==(_:_:)) _DerivedEnumEquals, not func==. 2017-04-18 23:21:44 -07: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
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
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Robert Widmann
96f2a04f55 Merge pull request #6490 from modocache/ast
[SR-2757][Sema] Mark VarDecl in capture lists
2017-01-05 21:36:16 -07:00
Slava Pestov
064fda52d2 AST: Remove Type::getCanonicalTypeOrNull()
Not sure why but this was another "toxic utility method".
Most of the usages fell into one of three categories:

- The base value was always non-null, so we could just call
  getCanonicalType() instead, making intent more explicit

- The result was being compared for equality, so we could
  skip canonicalization and call isEqual() instead, removing
  some boilerplate

- Utterly insane code that made no sense

There were only a couple of legitimate uses, and even there
open-coding the conditional null check made the code clearer.

Also while I'm at it, make the SIL open archetypes tracker
more typesafe by passing around ArchetypeType * instead of
Type and CanType.
2017-01-04 01:08:29 -08:00