Commit Graph

1030 Commits

Author SHA1 Message Date
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Kavon Farvardin
485bdc7918 describe more cases of noncopyable in a tuple; also improve existing message a bit. 2023-06-26 16:55:06 -07:00
Holly Borla
cd752cca22 [NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup
option to exclude macro expansions when the name lookup request is constructed.
Currently, the source location argument is unused.
2023-06-11 23:09:47 -07:00
Holly Borla
684ef9c482 [AST] Add a new accessor kind for init accessors. 2023-06-06 18:57:31 -07:00
Joe Groff
359e045192 Require switch on a noncopyable-type binding to be explicitly consume-d.
Pattern matching as currently implemented is consuming, but that's not
necessarily what we want to be the default behavior when borrowing pattern
matching is implemented. When a binding of noncopyable type is pattern-matched,
require it to be annotated with the `consume` operator explicitly. That way,
when we introduce borrowing pattern matching later, we have the option to make
`switch x` do the right thing without subtly changing the behavior of existing
code. rdar://110073984
2023-06-02 18:14:37 -07:00
Kavon Farvardin
31aa2f77e3 polish noncopyable types diagnostic wordings
- replaces "move-only" terminology with "noncopyable"
- replaces compiler jargon like "guaranteed parameters"
  and "lvalue" with corresponding language-level notions
- simplifies diagnostics about closures.

and probably more.

rdar://109281444
2023-05-24 20:56:36 -07:00
Michael Gottesman
67fcb1c86e [consume-operator] Change consume so that its result is not considered an lvalue.
Before this change it was possible to:

1. Call mutating methods on a consume result.
2. assign into a consume (e.x.: var x = ...; (consume x) = value.

From an implementation perspective, this involved just taking the logic I
already used for the CopyExpr and reusing it for ConsumeExpr with some small
tweaks.

rdar://109479440
2023-05-17 22:42:42 -07:00
Michael Gottesman
fd25cf379a Rename MoveExpr -> ConsumeExpr to reflect the final name.
NFC.
2023-05-17 22:42:42 -07:00
Michael Gottesman
e6f1691122 [copy-operator] Add support for the copy operator in preparation for making consuming and borrowing no implicit copy.
Some notes:

1. I implemented this as a contextual keyword that can only apply directly to
lvalues. This ensures that we can still call functions called copy, define
variables named copy, etc. I added tests for both the c++ and swift-syntax based
parsers to validate this. So there shouldn't be any source breaks.

2. I did a little bit of type checker work to ensure that we do not treat
copy_expr's result as an lvalue. Otherwise, one could call mutating functions on
it or assign to it, which we do not want since the result of copy_value is

3. As expected, by creating a specific expr, I was able to have much greater
control of the SILGen codegen and thus eliminate extraneous copies and other
weirdness than if we used a function and had to go through SILGenApply.

rdar://101862423
2023-05-17 22:42:42 -07:00
Hamish Knight
4cafd90c01 [Sema] Allow if/switch expressions in optional chain assignments
Make sure we look through a wrapping
`OptionalEvaluationExpr` and its injections when
looking for an assignment to mark a valid if/switch
source expression.

rdar://109305454
2023-05-15 11:58:28 +01:00
swift-ci
2ad15893eb Merge pull request #65434 from jreference/61440-no-generic-env-for-type-param
Sema: fix for 61440 (lib/AST/GenericEnvironment) no generic environme…
2023-04-26 12:01:17 -07:00
jreference
e18b40b743 Sema: fix for 61440 (lib/AST/GenericEnvironment) no generic environment provided for type with type parameter 2023-04-26 22:02:41 +08:00
Pavel Yaskevich
1c3667b52a Merge pull request #65310 from calda/cal--SE-3065-bad-pattern-check
[SE-0365] Fix issue where boolean condition before `let self` condition would prevent using implicit self
2023-04-21 09:54:27 -07:00
Hamish Knight
017794a7d7 Merge pull request #65098 from hamishknight/poundland 2023-04-21 09:26:10 +01:00
Anthony Latsis
d224efc8a8 Merge pull request #65318 from AnthonyLatsis/revert-f608802cfe498b8684e4ea63603fe99073102ce6
Revert "Sema: Emit diagnostics when walking into collection literals with defaulted types"
2023-04-21 01:26:03 +03:00
Cal Stephens
ee906f7ba5 Merge branch 'main' into cal--SE-3065-bad-pattern-check 2023-04-20 13:41:48 -07:00
Cal Stephens
870dbccf88 Consolidate LabeledConditionalStmt::rebindsSelf logic into shared helper 2023-04-20 13:39:05 -07:00
Anthony Latsis
f76f6acbf0 Revert "Sema: Emit diagnostics when walking into collection literals with defaulted types"
This reverts commit f608802cfe.
2023-04-20 21:55:40 +03:00
Cal Stephens
3e140286e8 Fix issue where non-pattern-binding condition would break SE-0365 unwrapped self check 2023-04-19 19:17:35 -07:00
Joe Groff
8ea5ab84d3 Merge pull request #65173 from jckarter/noncopyable-tuples-not-supported
Diagnose attempts to use tuples with noncopyable elements.
2023-04-17 10:49:53 -07:00
Holly Borla
0ed4b3325f [SE-0393] Enable parameter packs for generic functions.
The VariadicGenerics flag still exists and is required for generic types
with parameter packs
2023-04-15 15:40:05 -07:00
Joe Groff
52e97d6544 Diagnose attempts to use tuples with noncopyable elements.
These aren't fully supported yet. rdar://108024586
2023-04-14 16:49:30 -07:00
Hamish Knight
bca451baa0 [Sema] Improve diagnostic for empty if expr branch
Previously we would say that it must end with a
`throw`, but a more useful diagnostic is that
there is an expression missing.
2023-04-12 14:54:22 +01:00
Cal Stephens
10878e8aba Tweak ClosureExpr inference instead 2023-04-03 20:53:21 -07:00
Cal Stephens
8b193a2f9f Also walk to Stmts and Decls, since those can contain Exprs 2023-04-03 18:21:02 -07:00
Cal Stephens
1b5dab00eb Fix issue where closure implicit self check was being run on expr that wasn't member of closure 2023-04-03 18:21:02 -07:00
Kavon Farvardin
76107ed931 expose more of SILGen to move-only types by default
Also removes some redundant checks in SILGen for
the flag in order to process `_move` and `_borrow`
that are already checked for in Sema. We will keep
those behind the feature flag for now.
2023-03-13 22:39:31 -07:00
Anthony Latsis
4d2b2f6fee Sema: Allow member type expressions rooted on non-identifier qualifiers to omit .self until Swift 6 2023-03-09 04:55:25 +03:00
Richard Wei
98c2a837d2 [Macros] Always visit macro-produced decls as auxiliary decls
Always use `Decl::visitAuxiliaryDecls` to visit decls produced by macros, including peer macros and declaration macros. Use name-driven expansion for peer macros. Remove `MacroExpansionDecl::getRewritten()`.

Also make `ExpandMacroExpansionDeclRequest` cache the buffer ID (similar to other macros) instead of an array of decls.
2023-03-04 23:48:21 -08:00
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
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