Commit Graph

28850 Commits

Author SHA1 Message Date
Hamish Knight
2e0be5afd7 [CS] Open generic requirements for types with unbound + placeholder types
- Introduce a generic requirements opening function for type resolution,
  which is used by the constraint system in cases where we need to
  impose requirements on opened type variables.
- Refactor `replaceInferableTypesWithTypeVars` to use this function
  when opening generic types that contain either placeholder or unbound
  generic types.

Together these changes ensure that we don't drop generic requirements
when an unbound generic or placeholder type is used as a generic
argument.
2025-08-26 12:16:54 +01:00
Hamish Knight
52bb8b4495 [CS] Record opened types early in getTypeOfMemberReference
Requirement opening relies on being able to query the opened type to
allow requirement fix coalescing to work. Record the opened type before
we open requirements to ensure we don't duplicate requirement fixes on
the base type with requirement fixes for the member.
2025-08-26 11:07:35 +01:00
Allan Shortlidge
6c1a9770dc Sema: Move attached macro availability checking to DeclAvailabilityChecker.
This allows the `ExportContext` that is pre-computed for availability checking
to be reused when checking attached macro availability. Also, it just makes
more sense.

NFC.
2025-08-25 21:42:56 -07:00
Slava Pestov
3728c77bb3 Merge pull request #83905 from slavapestov/cache-unavailable-check
Sema: Cache result of getUnsatisfiedAvailabilityConstraint()
2025-08-25 22:57:13 -04:00
fahadnayyar
720406fdd6 Diagnose unannotated C++ APIs returning SWIFT_SHARED_REFERENCE at Swift call sites (#83025)
This patch improves the warning for C++ APIs returning
`SWIFT_SHARED_REFERENCE` types but not annotated with either
`SWIFT_RETURNS_RETAINED` or `SWIFT_RETURNS_UNRETAINED` in the following
ways:

1. The warning for missing `SWIFT_RETURNS_(UN)RETAINED` annotations is
now emitted on Swift use sites, rather than while importing the API
(func/method decls).
- This logic is now implemented as a Misl Diagnostic in function
`diagnoseCxxFunctionCalls` in file lib/Sema/MiscDiagnostics.cpp.
- The warning is now triggered only when the API is actually used, which
reduces noise in large C++ headers.
- These warnings are still gated behind experimental-feature-flag `WarnUnannotatedReturnOfCxxFrt`

rdar://150800115
2025-08-25 14:44:25 -07:00
Slava Pestov
1972c4845f Sema: Cache result of getUnsatisfiedAvailabilityConstraint()
This query is expensive and we call it a lot.
2025-08-25 17:14:35 -04:00
Pavel Yaskevich
81de8859fb Merge pull request #83862 from xedin/rdar-158629300
[CSSimplify] SE-0324: Use correct conversion when converting array types
2025-08-25 00:05:25 -07:00
Hamish Knight
0c16c00bb4 [Sema] Resolve interface type in ExtendedTypeRequest
Resolving only a structural type meant we weren't checking generic
constraints, allowing invalid extensions to get past the type-checker.
Change to resolve an interface type.
2025-08-24 11:49:14 +01:00
Allan Shortlidge
e5adbe9910 Sema: Simplify recording required import access levels.
To reduce code duplication, implement a convenience version of
`swift::recordRequiredImportAccessLevelForDecl()` that handles the common case
of recording required import access for a reference to a `ValueDecl`.

NFC.
2025-08-21 18:24:30 -07:00
Pavel Yaskevich
29b04f4a63 [CSSimplify] SE-0324: Use correct conversion when converting array types
If the argument type is an array and it's passed to an imported declaration
that accepts a raw pointer, the solver should use an "array-to-c-pointer"
conversion instead of the one for pointers.

Resolves: rdar://158629300
2025-08-21 15:38:17 -07:00
Hamish Knight
303c1ee951 Merge pull request #83803 from hamishknight/err-fallback 2025-08-20 10:13:23 +01:00
Hamish Knight
5c3f6703a0 Remove diag::type_of_expression_is_ambiguous
This is a diagnostic that is only really emitted as a fallback when
the constraint system isn't able to better diagnose the expression.
It's not particulary helpful for the user, and can be often be
misleading since the underlying issue might not actually be an
ambiguity, and the user may well already have a type annotation. Let's
instead just emit the fallback diagnostic that we emit in all other
cases, asking the user to file a bug.
2025-08-19 17:14:23 +01:00
Egor Zhdan
3d3331f1c6 Merge pull request #83787 from egorzhdan/egorzhdan/weak-reference
[cxx-interop] Prohibit weak references to foreign reference types
2025-08-19 12:15:38 +01:00
Egor Zhdan
9abadf5483 [cxx-interop] Prohibit weak references to foreign reference types
This fixes a runtime crash when a `weak` reference to a C++ foreign reference type is used.

Instead of a runtime crash, Swift would now emit a compiler error saying that `weak` keyword is incompatible with foreign reference types.

rdar://124040825 / resolves https://github.com/swiftlang/swift/issues/83080
2025-08-18 18:08:43 +01:00
Doug Gregor
4ff65cc244 Merge pull request #83784 from DougGregor/never-emit-into-client
Add @_neverEmitIntoClient to prohibit SIL serialization for a function
2025-08-18 04:19:59 -07:00
Pavel Yaskevich
a5649a12e5 Merge pull request #83735 from xedin/rdar-158159462
[CSOptimizer] Add support for opened existential arguments
2025-08-18 00:16:24 -07:00
Doug Gregor
2f60d729f0 Add @_neverEmitIntoClient to prohibit SIL serialization for a function
Part of the Embedded Swift linkage model, this attribute ensures that
the function it applies to has a strong definition in its owning
module, and that its SIL is never serialized. That way, other modules
will not have access to its definition.

Implements rdar://158364184.
2025-08-17 23:21:57 -07:00
Hamish Knight
89feb8fabb [Diags] Remove some now unnecessary calls to flush 2025-08-17 11:48:21 +01:00
Slava Pestov
a983323508 Merge pull request #83768 from slavapestov/replace-covariant-result-type
Sema: Remove a bunch of usages of replaceCovariantResultType()
2025-08-16 08:56:12 -04:00
Hamish Knight
89787e24b6 Merge pull request #83766 from hamishknight/fifty-thousand-ants
[CS] Use apply component locator for `verifyThatArgumentIsHashable`
2025-08-16 05:20:51 +01:00
Slava Pestov
d93b120e24 Sema: Remove usage of replaceCovariantResultType() in CSApply 2025-08-15 18:37:15 -04:00
Slava Pestov
1fbdc20be1 Sema: Remove usage of replaceCovariantResultType() in CSApply 2025-08-15 18:37:15 -04:00
Slava Pestov
9be81d0302 Sema: Clean up ConstraintSystem::getTypeOfReference() 2025-08-15 18:37:12 -04:00
Slava Pestov
eb56862e66 Sema: Clean up getMemberReferenceTypeFromOpenedType() 2025-08-15 18:37:12 -04:00
Hamish Knight
4423399fce [CS] Use apply component locator for verifyThatArgumentIsHashable
For a method key path use the locator for the apply itself rather
than the member, ensuring we handle invalid cases where the apply is
the first component, and providing more accurate location info.
2025-08-15 21:30:22 +01:00
Hamish Knight
61d0486d12 [CS] NFC: Factor out hasKeyPathComponent 2025-08-15 21:30:22 +01:00
Slava Pestov
a399c46db7 Sema: Pull preconcurrency adjustment out of getUnopenedTypeOfReference() 2025-08-15 14:39:14 -04:00
Pavel Yaskevich
0d33af78ca [CSOptimizer] Add support for opened existential arguments
Check whether it would be possible to match a parameter type
by opening an existential type of the candidate argument.

Resolves: rdar://158159462
2025-08-15 00:14:51 -07:00
Slava Pestov
4146a7f935 Merge pull request #83736 from slavapestov/fix-rdar157329046
Sema: New opaque return type circularity check that doesn't trigger lazy type checking of bodies
2025-08-14 23:46:01 -04:00
Mykola (Nickolas) Pokhylets
31470bbd1e Merge pull request #82732 from nickolas-pohilets/mpokhylets/weak-let-feature
Wrap SE-0481 into an upcoming feature until source incompatibilities are resolved
2025-08-14 23:59:28 +02:00
Slava Pestov
501b21d749 Sema: Micro-optimize adjustFunctionTypeForConcurrency() 2025-08-14 17:01:47 -04:00
Slava Pestov
e26034b7ac Sema: New opaque return type circularity check that doesn't trigger lazy type checking of bodies
Commit b70f8a82b1 introduced a usage of
ReplaceOpaqueTypesWithUnderlyingTypes in Sema. Previously this was only
called from SILGen.

The problem was that ReplaceOpaqueTypesWithUnderlyingTypes would call
getUniqueUnderlyingTypeSubstitutions(), which triggers a request to
type check the body of the referenced function.

While this didn't result in unnecessary type checking work, because
UniqueUnderlyingTypeSubstitutionsRequest::evaluate() would skip bodies
in secondary files, it did change declaration checking order.

The specific issue we saw was a bad interaction with associated type
inference and unqualified lookup in a WMO build, and a complete test
case is hard to reduce here.

However, no behavior change is intended with this change, modulo bugs
elsewhere related to declaration checking order, so I feel OK not adding
a test case.

I'll hopefully address the unqualified lookup issue exposed in the
radar soon; it has a reproducer independent of opaque return types.

Fixes rdar://157329046.
2025-08-14 17:01:47 -04:00
Pavel Yaskevich
90224bf927 [Typechecker/OpenedExistentials] NFC: Extract some checks from canOpenExistentialCallArgument into a separate function
Some of the logic of `canOpenExistentialCallArgument` has to be
shared with constraint optimizer which does it's own validation
and most importantly - doesn't open overload choice type.

In order to share the logic between the solver and the optimizer,
let's extract checking that determines whether it's possible to
open existential argument into `canOpenExistentialAt`.
2025-08-14 13:58:01 -07:00
Pavel Yaskevich
030564b9db Merge pull request #83703 from xedin/rdar-157644867
[CSOptimizer] All the candidates should match `Any` parameter type
2025-08-14 13:20:12 -07:00
Hamish Knight
af73ca374d Merge pull request #83704 from hamishknight/fix-stress-tester-regression
[CS] Record `SkipUnhandledConstructInResultBuilder` for completion
2025-08-14 20:39:36 +01:00
Allan Shortlidge
eea4189f99 Merge pull request #83718 from tshortli/dont-automatically-apply-dynamic-member-lookup-subscript-access-fix-it 2025-08-14 07:17:13 -07:00
Allan Shortlidge
5e2dfdf4e6 Merge pull request #83714 from tshortli/allow-obsolete-in-swift-overrides 2025-08-14 07:16:50 -07:00
Hamish Knight
eb7aa6281c [CS] Record SkipUnhandledConstructInResultBuilder for completion
We still want to record this fix when falling back to type-checking
as a regular function body to ensure that in cases where there is a
disjunction of multiple result builders we favor the one that actually
supports the body.
2025-08-14 11:57:11 +01:00
Allan Shortlidge
db117bfefe Sema: Don't apply access level fix-it to @dynamicMemberLookup subscript.
The adjusted access level for the subscript shouldn't be fixed automatically
since sometimes the diagnostic is a warning and other times automatically
applying the fix-it immediately causes other errors, which can be confusing.

Resolves rdar://158261884.
2025-08-13 23:13:07 -07:00
Allan Shortlidge
550d7bb754 Sema: Allow overrides to be unavailable in the current Swift language mode.
Fixes a regression from https://github.com/swiftlang/swift/pull/83354.

Resolves rdar://158262522
2025-08-13 17:57:05 -07:00
Pavel Yaskevich
c46f9e4f45 [CSOptimizer] All the candidates should match Any parameter type
If the parameter is `Any` we assume that all candidates are
convertible to it, which makes it a perfect match. The solver
would then decide whether erasing to an existential is preferable.

Resolves: rdar://157644867
2025-08-13 14:59:54 -07:00
Hamish Knight
5e27e83456 Merge pull request #83613 from hamishknight/next-step
[IDE] Perform extension binding after AST mutation
2025-08-13 22:37:56 +01:00
Pavel Yaskevich
32b97d0e2a [TypeCheckEffects] AbstractFunction: Parameter types should be mapped into context
Parameter type could be represented by an associated type which is
bound to a concrete type by an extension, `AbstractFunction::getType()`
should map it into context before returning because the construct is
that it always produces a function type.

Resolves: rdar://156955193
2025-08-12 17:29:07 -07:00
Hamish Knight
ead2c0c43a Merge pull request #83634 from hamishknight/invalid-check 2025-08-12 09:36:22 +01:00
Hamish Knight
d1fb307c6d Merge pull request #83632 from hamishknight/complete-result
[CS] Better handle unsupported result builder elements for completion
2025-08-11 22:33:14 +01:00
Hamish Knight
e7098a34b2 [Sema] Remove isInvalid checks from checkAndContextualizePatternBindingInit
The invalid bit on PatternBindingDecl is very coarse grained, it
means any of the possible binding entries could be invalid.
Contextualization can handle invalid code anyway (e.g this is what we
do for `typeCheckBody`), so let's just avoid checking.
2025-08-11 12:55:36 +01:00
Hamish Knight
dad7c26bcd [CS] Always set ErrorType for exprs in markInvalid
I don't recall why I made this conditional, let's just be consistent
and always set ErrorType for invalid AST. This should only affect
things like implicit nodes.
2025-08-11 12:05:10 +01:00
Hamish Knight
9d9b4c90c6 [Sema] Sink Pattern invalidation into SyntacticElementTarget::markInvalid
Rather than handling this in the callers of `typeCheckExpression`,
handle it in the constraint system's invalidation logic. This also
ensures we set a type for sub-pattern nodes.
2025-08-11 12:05:10 +01:00
Hamish Knight
ce9028f816 [Sema] Set pattern on failure in typeCheckBinding
I don't think this currently affects anything, but match what we do
for the expression and update the pattern if type-checking the
initialization fails.
2025-08-11 12:05:10 +01:00
Hamish Knight
27e2514eff [CS] Better handle unsupported result builder elements for completion
Instead of failing, fall back to solving as a regular function body.

rdar://144303920
2025-08-11 11:19:14 +01:00