Commit Graph

950 Commits

Author SHA1 Message Date
Kavon Farvardin
9a18422647 statically prevent runtime casts of move-only type
It appears that conditional casts require a check of the
type's metadata at runtime. There's no reason for us to
permit that at this time, since all such conditional
casts are going to fail, unless its the identity cast.

That is, a move-only type is currently only a subtype
of itself.

So for now, `is`, `as?`, `as!` casts from or to a
move-only type are now an error. The `as` casts
are permitted only if the two move-only types are
equal.
2023-02-07 16:51:46 -07:00
Hamish Knight
a40f1abaff Introduce if/switch expressions
Introduce SingleValueStmtExpr, which allows the
embedding of a statement in an expression context.
This then allows us to parse and type-check `if`
and `switch` statements as expressions, gated
behind the `IfSwitchExpression` experimental
feature for now. In the future,
SingleValueStmtExpr could also be used for e.g
`do` expressions.

For now, only single expression branches are
supported for producing a value from an
`if`/`switch` expression, and each branch is
type-checked independently. A multi-statement
branch may only appear if it ends with a `throw`,
and it may not `break`, `continue`, or `return`.

The placement of `if`/`switch` expressions is also
currently limited by a syntactic use diagnostic.
Currently they're only allowed in bindings,
assignments, throws, and returns. But this could
be lifted in the future if desired.
2023-02-01 15:30:18 +00:00
Ellie Shin
37af51dc4c Merge branch 'main' into es-pkg-acl 2023-01-19 16:18:17 -08:00
Ellie Shin
1c66d02f92 Add package access level to enum AccessLevel
Resolves rdar://104198440
2023-01-19 15:54:18 -08:00
Pavel Yaskevich
3c03449797 [MiscDiagnostics] OpaqueTypes: Handle unrelated #available conditions gracefully
`OpaqueUnderlyingTypeChecker` should skip conditional availability
blocks if none of the conditions restrict the current target.

Resolves: rdar://103445432
2023-01-04 15:36:46 -08:00
Michael Gottesman
b95fe48a90 Merge pull request #62742 from gottesmm/pr-947919228bebf49e02eae5aedb1efee8038c2b4b
[borrow-expr] Add simple support for borrow-expr and wire it up to SILGenApply
2023-01-03 08:04:33 -08:00
Doug Gregor
0c5436211c Merge pull request #62768 from DougGregor/macro-omnibus 2023-01-03 07:13:16 -08:00
Doug Gregor
af49a90dde Customize diagnostic text for extra/missing/mislabeled arguments for callee kind
Most of the diagnostics for extra/missing/mislabeled arguments refer
to argument to a "call". Some (but not call) would substitute in
"subscript". None would refer to an argument to a macro expansion
properly.

Rework all of these to refer to the argument in a call, subscript, or
macro expansion as appropriate. Fix up lots of tests that now say
"subscript" instead, and add tests for macro expansions.
2023-01-02 21:22:04 -08:00
Michael Gottesman
94f1391fbe [borrow-expr] Wire up borrow expr to SILGenApply. 2022-12-22 13:02:04 -08:00
Allan Shortlidge
d480410931 Sema: Diagnose unsupported clang decls for #_hasSymbol.
Progress towards rdar://103408651.
2022-12-22 10:55:29 -05:00
Holly Borla
42020792c6 [Sema] Represent pack element references in pack expansion patterns as
PackElementExpr.
2022-12-14 20:45:52 -08:00
Allan Shortlidge
164ac421d6 Sema: Lift restriction preventing use of #_hasSymbol on non-Darwin platforms.
An early approach to codegen for `#_hasSymbol` relied on the Darwin platfom SDK, but now that the feature lowers directly to NULL checks in LLVM IR a platform restriction is no longer needed.

However, the tests for `#_hasSymbol` remain unsupported on Windows since that OS does not support weak linking.
2022-11-30 14:58:28 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Allan Shortlidge
3da18b6e68 SIL: Serialize and deserialize has_symbol instructions. 2022-11-16 16:07:59 -08:00
Jonathan Grynspan
f3bcef92a5 Augment maybeDiagnoseCallToKeyValueObserveMethod() and add support for a @_semantics attribute to trigger it rather than just hard-coding the name of the Foundation method. 2022-11-14 17:18:32 -05:00
Michael Gottesman
b95b19be44 [move-only] Ban move only types being stored properties of copyable types.
rdar://101650982
2022-11-04 11:56:31 -07:00
Pavel Yaskevich
a784c00da6 Merge pull request #61520 from calda/cal--SE-0365-swift-5
Enable SE-0365 behavior in Swift 5 mode
2022-10-31 17:50:02 -07:00
Cal Stephens
8528021f0d Change lookup behavior in ASTScope::lookupUnqualified when self has been unwrapped 2022-10-26 20:38:43 -07:00
Cal Stephens
ebcc9c2b8b Revert "Disable SE-0365 behavior for escaping closues in Swift 5 mode"
This reverts commit f8ca2937a8.
2022-10-26 17:38:01 -07:00
Holly Borla
d864cebef3 [TypeResolution] Enable local variable packs. 2022-10-26 17:04:23 -07:00
Cal Stephens
f8ca2937a8 Disable SE-0365 behavior for escaping closues in Swift 5 mode 2022-10-22 09:15:09 -07:00
Cal Stephens
e7512dcdcf Update method names and comments following PR feedback 2022-10-22 09:15:03 -07:00
Slava Pestov
fff224e2e7 Sema: Fix opening an existential argument to a 'try self.init()' delegation
Fixes rdar://problem/101098777.
2022-10-18 12:10:24 -04:00
Allan Shortlidge
bec9e54b3b Merge pull request #61584 from tshortli/diagnose-availability-macros-in-inlinable-closures
Sema: Ensure that use of availability macros in closures in fragile functions is diagnosed
2022-10-17 10:56:00 -07:00
Slava Pestov
a5453f1649 AST: Add new variant of swift::getParameterAt() which takes pack expansions into account 2022-10-16 21:37:25 -04:00
Allan Shortlidge
01a32639f0 Sema: Ensure that use of availability macros in closures in fragile functions is diagnosed.
Type checking of closure bodies does not invoke `TypeChecker::typeCheckStmtConditionElement()` so use of availability macros inside closure bodies written in fragile (inlinable) functions was undiagnosed. Move the diagnostic logic to `MiscDiagnostics.cpp` which implements diagnostics that are emitted regardless of the typechecking model used.

Additionally, remove the `DC->getAsDecl() != nullptr` check from `diagnoseHasSymbolCondition()` which was preventing similar fragile function diagnostics for `if #_hasSymbol` from being emitted when a closure was involved.

Resolves rdar://100581013
2022-10-14 15:33:27 -07:00
Allan Shortlidge
8a9eb40965 Sema: Reject #_hasSymbol conditions in inlinable code. 2022-10-13 21:00:24 -07:00
Cal Stephens
22e43136d9 Enable SE-0365 behavior in Swift 5.8 2022-10-10 08:03:03 -07:00
Allan Shortlidge
64e8a0334d Sema: Diagnose if #_hasSymbol as unsupported on non-Darwin targets. 2022-10-08 00:09:34 -07:00
Andrew Trick
19175926da Disable '_move' contextual keyword absent -enable-experimental-move-only
Fixes rdar://100872195 ( error: 'move' can only be applied to lvalues
  error: Can not use feature when experimental move only is disabled!)

Identifiers with a single underscore are not reserved for use by the
language implementation. It is perfectly valid for a library to define
its own '_move'.

The contextual _move keyword should only be parse when it is followed by an lvalue, so should *not* conflict with user-defined '_move' functions.
https://github.com/apple/swift-evolution/blob/main/proposals/0366-move-function.md#source-compatibility
2022-10-06 16:33:26 -07:00
Pavel Yaskevich
1a79d93121 Merge pull request #40702 from calda/cal--implicit-weak-self
[SE-0365] Allow implicit self for `weak self` captures
2022-09-30 10:30:57 -07:00
Allan Shortlidge
f810eb0818 Merge pull request #61316 from tshortli/accept-has-symbol-in-closures
Sema: Accept `if #_hasSymbol()` conditions in closure contexts
2022-09-30 09:02:35 -07:00
Anthony Latsis
2843e0c871 Gardening: Migrate compiler sources to GitHub issues 2022-09-29 23:58:55 +03:00
Allan Shortlidge
8729801eb4 Sema: Store an 'invalid' bit in PoundHasSymbolInfo to avoid repeated if #_hasSymbol diagnostics. 2022-09-29 11:47:30 -07:00
Allan Shortlidge
034e53ad20 Sema: Accept if #_hasSymbol() conditions in closure contexts.
Resolves rdar://100129165
2022-09-29 10:55:08 -07:00
Cal Stephens
5bfdfd822c Only enable SE-0365 in Swift 6 2022-09-28 19:35:26 -07:00
Cal Stephens
cc92f76a9e Revert changes for handling SE-0365 in Swift 5
Revert "Remove properties from AST nodes"

This reverts commit e4b8a829fe.

Revert "Suppress more false-positive 'self is unused' warnings"

This reverts commit 35e028e5c2.

Revert "fix warning annotation in test"

This reverts commit dfa1fda3d3.

Revert "Permit implicit self for weak self captures in nonescaping closures in Swift 5 (this is an error in Swift 6)"

This reverts commit 94ef6c4ab4.
2022-09-28 17:44:02 -07:00
Cal Stephens
e4b8a829fe Remove properties from AST nodes 2022-09-28 06:24:42 -07:00
Cal Stephens
35e028e5c2 Suppress more false-positive 'self is unused' warnings 2022-09-26 09:28:22 -07:00
Cal Stephens
94ef6c4ab4 Permit implicit self for weak self captures in nonescaping closures in Swift 5 (this is an error in Swift 6) 2022-09-25 14:32:19 -07:00
Pavel Yaskevich
1c0e887e5f [MiscDiagnostics] OpaqueChecker: Double-check validity of last expression
It's possible that TypeChecker::typeCheckStmt has did not propagate
`HadError` properly while checking the body of a function, so opaque
result checker has to be careful about requesting types from expressions.

Resolves: rdar://100066109
2022-09-23 09:36:23 -07:00
Cal Stephens
5946c66962 Style updates 2022-09-23 07:09:52 -07:00
Cal Stephens
9dd56f9daf Move remaining logic in LookupResultEntry::getBaseDecl() to ASTScope::lookupUnqualified impl, add more extensive tests, fix failing tests 2022-09-22 11:04:08 -07:00
Cal Stephens
4abca2bc9e Merge tag 'swift-DEVELOPMENT-SNAPSHOT-2022-09-12-a' of github.com:apple/swift into cal--implicit-weak-self
Tag build swift-DEVELOPMENT-SNAPSHOT-2022-09-12-a
2022-09-20 06:58:38 -07:00
Cal Stephens
84896edaf2 Use Ctx.Id_self and update comments 2022-09-15 20:01:40 -07:00
Hamish Knight
4716f61fba [AST] Introduce explicit actions for ASTWalker
Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
2022-09-13 10:35:29 +01:00
Pavel Yaskevich
149e57a0cb Merge pull request #60893 from calda/cal--warn-redundant-comparison-to-optional.none-case
Add warning when comparing a non-optional value to `Optional.none`
2022-09-12 00:36:24 -07:00
Cal Stephens
9167a177cc Merge branch 'main' into cal--implicit-weak-self 2022-09-11 10:24:55 -07:00
Slava Pestov
da76e1ee71 Sema: Allow one-element tuples when -enable-experimental-variadic-generics is on 2022-09-10 00:26:43 -04:00
Cal Stephens
d9be4a7213 Update check for Optional.none 2022-09-09 08:41:31 -07:00