Commit Graph

1659 Commits

Author SHA1 Message Date
Slava Pestov
d470e9df4d AST: Split off ArgumentShuffleExpr from TupleShuffleExpr
Right now we use TupleShuffleExpr for two completely different things:

- Tuple conversions, where elements can be re-ordered and labels can be
  introduced/eliminated
- Complex argument lists, involving default arguments or varargs

The first case does not allow default arguments or varargs, and the
second case does not allow re-ordering or introduction/elimination
of labels. Furthermore, the first case has a representation limitation
that prevents us from expressing tuple conversions that change the
type of tuple elements.

For all these reasons, it is better if we use two separate Expr kinds
for these purposes. For now, just make an identical copy of
TupleShuffleExpr and call it ArgumentShuffleExpr. In CSApply, use
ArgumentShuffleExpr when forming the arguments to a call, and keep
using TupleShuffleExpr for tuple conversions. Each usage of
TupleShuffleExpr has been audited to see if it should instead look at
ArgumentShuffleExpr.

In sequent commits I plan on redesigning TupleShuffleExpr to correctly
represent all tuple conversions without any unnecessary baggage.

Longer term, we actually want to change the representation of CallExpr
to directly store an argument list; then instead of a single child
expression that must be a ParenExpr, TupleExpr or ArgumentShuffleExpr,
all CallExprs will have a uniform representation and ArgumentShuffleExpr
will go away altogether. This should reduce memory usage and radically
simplify parts of SILGen.
2019-03-21 02:18:41 -04:00
Azoy
6f7d20b99e Synthesize default values for memberwise init
Introduce stored property default argument kind

Fix indent

Assign nil to optionals with no initializers

Don't emit generator for stored property default arg

Fix problem with rebase

Indentation

Serialize stored property default arg text

Fix some tests

Add missing constructor in test

Print stored property's initializer expression

cleanups

preserve switch

complete_constructor

formatting

fix conflict
2019-03-13 18:57:36 -05:00
Parker Schuh
5160da6a2e FloatLiteralExpr now is lowered directly into SIL.
For context, String, Nil, Bool, and Int already behave this way.

Note: Swift can compile against 80 or 64 bit floats as the builtin
literal type. Thus, it was necessary to capture this bit somehow in the
FloatLiteralExpr. This was done as another Type field capturing this
info.
2019-03-01 09:01:30 -08:00
Joe Groff
bb67cf815c Merge pull request #21355 from technicated/tuple-keypaths-2
Tuple KeyPaths
2019-02-25 12:56:05 -08:00
Daniel Williams
1d88c47aaf [Sema] SR-9851 - Add parens when needed for nil coalescing fixits
https://bugs.swift.org/browse/SR-9851
2019-02-21 21:03:29 -08:00
Pavel Yaskevich
875932b3bf [CSApply] Account for failures in argument application of unresolved member calls
While attempting to form a call of an unresolved member, the fact
that argument application could fail has to be accounted for.

Resolves: rdar://problem/48114578
2019-02-19 13:02:01 -08:00
David Zarzycki
9926baebd3 [Sema] NFC: Use exhaustive switches in coerceToType() 2019-02-19 10:08:51 -05:00
Andrea Tomarelli
ede47cafbd Partial AST & Sema implementation of TKP 2019-02-18 09:04:42 +01:00
David Zarzycki
c037886c2b [Sema] NFC: refactor coerceToType() to use switch statments
The current series of "unrelated" `if` statements makes understanding
and updating this function harder than necessary. By using two `switch`
statements, we can avoid these problems and as a bonus, generate more
efficient code gen.
2019-02-17 12:49:21 -05:00
Parker Schuh
b12fcb50db IntegerLiteralExpr now is lowered directly into SIL.
For context, String, Nil, and Bool already behave this way.

Note: Before it used to construct (call, ... (integer_literal)), and the
call would be made explicit / implicit based on if you did eg: Int(3) or
just 3. This however did not translate to the new world so this PR adds
a IsExplicitConversion bit to NumberLiteralExpr. Some side results of
all this are that some warnings changed a little and some instructions are
emitted in a different order.
2019-02-14 11:54:16 -08:00
Azoy
e9f4217f03 remove lookupBoolType
diagnose broken bool

fix
2019-02-09 00:13:51 -06:00
Pavel Yaskevich
1d42e16ad2 [ConstraintSystem] Detect invalid implicit ref to initializer on non-const metatype
Situations like:

```swift
struct S {}
func foo(_ s: S.Type) {
  _ = s()
}
```

Used to be diagnosed in solution application phase, which means that
solver was allowed to formed an incorrect solution.
2019-02-07 00:17:07 -08:00
Pavel Yaskevich
6754b86507 Merge pull request #22379 from xedin/rdar-47787705
[ConstraintSystem] Detect invalid initializer references early
2019-02-06 17:16:20 -08:00
Pavel Yaskevich
9a1e92ec0a [AST] Add getDecl parameter to Expr::isTypeRerefence
So it could be extended to support not yet fully type-checked
AST when used by constraint system solver.
2019-02-05 18:09:51 -08:00
Pavel Yaskevich
e8c8c0b705 [CSApply] Remove obsolete diagnoseInvalidDynamicConstructorReferences 2019-02-05 10:52:52 -08:00
Pavel Yaskevich
1d8cee9cb4 [ConstraintSystem] Detect invalid initializer references early
Currently invalid initializer references are detected and
diagnosed in solution application phase, but that's too
late because solver wouldn't have required information while
attempting to determine the best solution, which might result
in viable solutions being ignored in favour of incorrect ones e.g.

```swift
protocol P {
  init(value: Int)
}

class C {
  init(value: Int, _: String = "") {}
}

func make<T: P & C>(type: T.Type) -> T {
  return T.init(value: 0)
}
```

In this example `init` on `C` would be preferred since it
comes from the concrete type, but reference itself is invalid
because it's an attempt to construct class object using
metatype value via non-required initalizer.

Situations like these should be recognized early and invalid
use like in case of `C.init` should be ranked lower or diagnosed
if that is the only possible solution.

Resolves: rdar://problem/47787705
2019-02-05 10:25:36 -08:00
Parker Schuh
d8bff8ddc9 BooleanLiteralExpr now is lowered directly into SIL.
Instead of constructing calls to ExpressibleByBooleanLiteral.init(booleanLiteral: ...) in CSApply.cpp, just
annotate BooleanLiteralExpr with the selected constructor and do the actual construction during SILGen.

For context, StringLiteralExpr and NilLiteralExpr already behave this way.
2019-01-31 09:56:00 -08:00
Parker Schuh
6ca70c6720 NilLiteralExpr now is lowered directly into SIL.
Instead of constructing calls to
ExpressibleByNilLiteral.init(nilLiteral: ()) in CSApply.cpp, just
annotate NilLiteralExpr with the selected construtor and do the actual
construction during SILGen.

For context, StringLiteralExpr already behaves this way.
2019-01-28 10:00:52 -08:00
Pavel Yaskevich
aa8ccfd57d [CSFix] Introduce non-fatal "warning" fixes
The intention here is to be able to detect warnings earlier and
move some of the logic from CSApply and MiscDiagnostics to solver.

Warning fixes still lead to solution being applied to AST.
2019-01-18 14:08:08 -08:00
Pavel Yaskevich
5426e0df9e [AST] Decl::is*AccessibleFrom methods should respect access control flag
Otherwise integrated REPL and other tools like LLDB would produce
incorrect results or crash.

Resolves: rdar://problem/30933988
2019-01-11 17:30:10 -08:00
Slava Pestov
1026968616 Sema: Remove ConversionRestrictionKind::TupleToTuple 2019-01-10 08:44:18 -05:00
Slava Pestov
28343c9480 Sema: Remove ConversionRestrictionKind::LValueToRValue 2019-01-10 08:44:18 -05:00
Slava Pestov
9cf1efbe77 Sema: Remove bogus call to requestNominalLayout() 2019-01-08 00:20:18 -05:00
Suyash Srijan
f30ba3069b [Sema] Add warning for ambiguous value assignment when using Optional (#21621)
* [sema] emit a diag if the enum case matches Optional<T>.none

* [test] update tests

* [sema] fix indent

* [test] fix indent

* [test] add more test cases

* [test] add even more test cases

* [sema] move the check to CSApply

* [diag] update diagnostic message

* [test] update tests

* [test] fix conflicts

* [diag] reflow lines

* [sema] reindent using spaces

* [test] adds new line

* [diag] update diagnostic message

* [sema] add support for structs as well

* [test] add more test cases

* [sema] check for enum assoc values

* [test] add more test cases

* [diag] add fixit notes

* [sema] emit fix its

* [diag] rename diag names

* [sema] fit within 80 char line limit

* [sema] use baseUnwrappedType's name directly

* [test] adds nested generic enum tests

* [test] fix indent

* [test] adds fixit check

* [test] re-indent some enums

* [sema] [csapply] extract code into a separate function

* [sema] [csapply] remove redundant vardecl check

* [sema] [csapply] reindent

* [sema] [csapply] removes extra line

* [sema] [csapply] use cantype & check for extension on Optional

* [diag] update diagnostic message

* [sema] [csapply] fix ident

* [test] update tests

* [sema] [csapply] fix typo and remove redundant isOptional check

* [sema] [csapply] update var name & comments

* [sema] [csapply] bring back isOptional check

* [test] add expected-note for fix-its

* [sema] [csapply] fix a crash

* [sema] [csapply] move isOptional check outside

* [test] fix indent

* [test] fix typo

* [sema] [csapply] use baseTyUnwrapped for fixit

* [test] fix columns for fixits

* [test] update column numbers

* [sema] [csapply] move code out of for loop
2019-01-07 20:56:14 -05:00
Robert Widmann
2efbeb3912 Merge pull request #21451 from CodaFi/logicd
[SR-8272] Drop the last remnants of LogicValue
2018-12-20 23:33:20 -05:00
Robert Widmann
426fe886dc [SR-8272] Drop the last remnants of LogicValue
Removes the _getBuiltinLogicValue intrinsic in favor of an open-coded
struct_extract in SIL.  This removes Sema's last non-literal use of builtin
integer types and unblocks a bunch of cleanup.

This patch would be NFC, but it improves line information for conditional expression codegen.
2018-12-19 23:14:59 -05:00
Pavel Yaskevich
20b7c2a4ba [CodeSynthesis] Make sure that LazyInitializerExpr has a type
Currently when `LazyInitializerExpr` is added to the AST it's not
given an explicit type. Because of that constraint solver silently
fails in contraint generator without diagnostic. But since
sub-expression associated with `LazyInitializerExpr` is already
type-checked it makes sense to set its type explicitly.
2018-12-19 14:04:32 -08:00
Slava Pestov
2f61230627 Sema: Remove vestigial rvalue varargs code path 2018-12-16 01:02:56 -05:00
Slava Pestov
06f6621cdc Sema: Clean up ExprRewriter::visitParenExpr()
simplifyType() preserves ParenType sugar so we don't have to jump through
hoops here.
2018-12-16 01:02:56 -05:00
Slava Pestov
b463f52a79 Merge pull request #21343 from slavapestov/one-element-tuple
Ban one element tuples harder
2018-12-15 07:50:44 -05:00
Slava Pestov
48e89f40d7 Sema: Fix applying solution for OptionalTryExpr
In Swift 5 mode, CSGen generates a conversion constraint from
the type of OptionalTryExpr's subexpression, call it T, and
Optional<$tv> for a new type variable $tv.

When applying the solution, we would coerce the sub-expression
to T? if T was not optional, or T otherwise. This was wrong
because there's no reason that $tv is actually equal to T.

Instead, we must coerce the sub-expression to Optional<$tv>,
which is the type of the overall OptionalTryExpr.

Fixes <rdar://problem/46742002>.
2018-12-15 01:29:07 -05:00
Slava Pestov
9c1bb7189b Sema: Don't type check one-element labeled tuples when forming dynamic member lookups
These are going to be banned shortly, so construct them by other means.
2018-12-15 00:07:04 -05:00
Joe Groff
89979137fc Push ArchetypeType's API down to subclasses.
And clean up code that conditionally works only with certain kinds of archetype along the way.
2018-12-12 19:45:40 -08:00
Suyash Srijan
fa82965a7d [CSApply] Fix an issue with the @objc selector fix-it incorrectly being applied (#20949)
Special-case the diagnostic for members of non-@objc protocols that are referenced in #selector expressions.
2018-12-11 13:19:46 -05:00
Slava Pestov
392071e702 Sema: Peel off more code from the TypeChecker instance 2018-12-10 00:00:49 -05:00
Slava Pestov
06c2e980cb Merge pull request #21133 from slavapestov/lazy-implicit-inits
Lazy synthesis of implicit constructors in non-primary files
2018-12-07 22:45:40 -05:00
Slava Pestov
6c012b2aec AST: Remove some unnecessary LazyResolver * parameters from ASTContext methods 2018-12-07 20:39:27 -05:00
Slava Pestov
aa747dcd81 Remove property behaviors 2018-12-07 20:38:33 -05:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Azoy
65217a71de Cache bool builtin init
remove newline
2018-12-02 22:38:24 -06:00
Azoy
28fc1ca296 tc.Context is shorter 2018-12-01 18:57:02 -06:00
Azoy
a2dafc3d26 Remove _getBool 2018-12-01 18:51:25 -06:00
Slava Pestov
0a8ee10621 AST: Refactor AbstractStorageDecl::getAccessStrategy() for keypath resilience
Instead of passing in a DeclContext, which we don't have when emitting a keypath
accessor, pass in a ModuleDecl and ResilienceExpansion.

Keypaths now work well enough in inlinable contexts that we can check in an
end-to-end resilience test.
2018-11-16 23:18:30 -05:00
Mark Lacey
018498fb2f Merge pull request #16942 from bjhomer/bjhomer/optional-try-flattening
Flatten nested optionals resulting from try? and optional sub-expressions
2018-11-16 11:14:05 -08:00
John McCall
05c951fdc9 Compute layout when emitting an other-constructor reference.
Fixes SE-9233, a crash due to a failure to validate a storage
declaration within a protocol when generating a call to an
initiializer.
2018-11-13 16:35:26 -05:00
Dan Zheng
2863b6cc64 Gardening for @dynamicMemberLookup. (#20498)
* Gardening for `@dynamicMemberLookup`.

- Unify code style of `@dynamicMemberLookup` and `@dynamicCallable` implementations.
  - Use consistent variable names, diagnostic messages, doc comments, etc.
- Move `@dynamicMemberLookup` test to `test/attr` directory.
2018-11-11 21:54:19 -05:00
Pavel Yaskevich
e50537d66a [CSApply] Handle @autoclosure arg/param conversions in Swift versions < 5
Since it was allowed to pass function types to @autoclosure
parameters in Swift versions < 5, it has to be handled as
a regular function coversion by `coerceToType`.
2018-11-10 11:59:29 -08:00
Pavel Yaskevich
d113b4ae1f [CSApply] Refactor ExprRewriter to handle @autoclosure from parameter flag 2018-11-10 11:59:29 -08:00
Pavel Yaskevich
d9504ef919 [TypeChecker] Add typeCheckParameterDefault method
`typeCheckParameterDefault` is used to type-check default value
expression associated with a given parameter. It makes it possible
to look-through `@autoclosure` function to use its result as
contextual type, and later build implicit autoclosure expression
if needed.
2018-11-10 11:59:28 -08:00
Dan Zheng
2a4e1b83fd Implement @dynamicCallable. (#20305)
* Implement dynamically callable types (`@dynamicCallable`).

- Implement dynamically callable types as proposed in SE-0216.
  - Dynamic calls are resolved based on call-site syntax.
  - Use the `withArguments:` method if it's defined and there are no
    keyword arguments.
  - Otherwise, use the `withKeywordArguments:` method.
- Support multiple `dynamicallyCall` methods.
  - This enables two scenarios:
    - Overloaded `dynamicallyCall` methods on a single
      `@dynamicCallable` type.
    - Multiple `dynamicallyCall` methods from a `@dynamicCallable`
      superclass or from `@dynamicCallable` protocols.
  - Add `DynamicCallableApplicableFunction` constraint. This, used with
    an overload set, is necessary to support multiple `dynamicallyCall`
    methods.
2018-11-09 09:49:14 -08:00