Commit Graph

1757 Commits

Author SHA1 Message Date
Hamish Knight
7b729933f6 [CS] Add for loop compatibility hack for makeIterator/next ranking
Still record overload choices for `makeIterator` and `next` when
solving a `ForEachElement` constraint. This maintains compatibility
with the previous behavior where we would solve these in the constraint
system, since the choices can affect ranking if e.g Collection's
default `makeIterator` is compared with a concrete `makeIterator`
overload. This is a complete hack though and we ought to rip this out
as soon as we can.

rdar://168840696
2026-01-27 23:30:43 +00:00
Pavel Yaskevich
956ffe08ca Merge pull request #86250 from AnirudhMathur12/fix-crash-85942
[ConstraintSystem] Fix crash when diagnosing missing args in KeyPath application
2026-01-27 07:25:23 -08:00
Slava Pestov
ee0f6dd681 Sema: Narrow down the skipProtocolSelfConstraint hack
We actually have to check this constraint, otherwise we accept invalid
code where we're referencing a protocol requirement with a concrete
base type which conditionally conforms to the protocol, but the
conditional requirements are unsatisfied.

However, we still need to skip it for AnyObject lookup, Distributed
Actors, and some weird edge case in conformance checking for isolated
conformances.

We should clean this up further later, but for now this closes a soundness
hole.

Fixes rdar://168129757.
2026-01-25 21:11:59 +00:00
Slava Pestov
d860523944 Merge pull request #86760 from slavapestov/solver-shuffle
Sema: Add -solver-shuffle-disjunctions= and -solver-shuffle-choices= flags for debugging
2026-01-24 12:30:43 -05:00
Slava Pestov
30f1b0187a Sema: Add -solver-shuffle-disjunctions= and -solver-shuffle-choices= flags for debugging 2026-01-23 20:02:21 -05:00
Slava Pestov
f3cfd65f3b Sema: Remove some dead code related to favoring 2026-01-23 20:02:21 -05:00
Hamish Knight
0a3d11722a [AST] Add flag to isRefutablePattern to determine IsPattern behavior
IsPattern coercions aren't properly implemented since they rely on
doing a runtime cast, which can fail for things like function subtyping.
Make clients choose what behavior they want. Unfortunately we need to
preserve the current behavior in places since it's relied upon for
things like exhaustivity checking. We ought to properly implement
coercion handling, then we should be able to remove this.
2026-01-23 15:17:28 +00:00
AnirudhMathur12
bf0e8835b6 [ConstraintSystem] Fix callee locator for KeyPath application components
Previously, `getCalleeLocator` handled `ComponentKind::Apply` by returning a locator for the current component index. This was incorrect because the callee for an application is the member being applied (the previous component), not the application itself.
This patch updates `getCalleeLocator` to use `componentElt->getIndex() - 1` for `Apply` and `UnresolvedApply` components. This ensures that the constraint system can correctly identify the callee when diagnosing argument mismatches in KeyPath expressions (e.g., `\.split(...)`), leading to correct diagnostics instead of fallback errors.

Removed null check
2026-01-23 02:27:53 +05:30
AnirudhMathur12
56cc7aeff1 [ConstraintSystem] Fix crash when diagnosing arguments in KeyPath application
The compiler previously crashed (Signal 11 or assertion failure) when diagnosing missing or mismatched arguments in KeyPath expressions (e.g., `\.split(separator: ",")`). This occurred because `getCalleeLocator` did not correctly identify the underlying function (callee) for a KeyPath application component.

This change updates `getCalleeLocator` to correctly resolve the callee to the preceding component (index - 1) in the KeyPath, as suggested during review.

Additionally, this change fixes a latent crash in `getFunctionArgApplyInfo` exposed by the locator fix. Previously, that function blindly cast the anchor to `CallExpr` when no overload was found. It now safely checks via `getAsExpr<CallExpr>` and returns `nullopt` if the anchor is a `KeyPathExpr`, preventing assertion failures.

Resolves: https://github.com/swiftlang/swift/issues/85942
2026-01-10 22:46:21 +05:30
AnirudhMathur12
0eb7804be6 [ConstraintSystem] Fix crash when diagnosing arguments in KeyPath application
The compiler previously crashed (Signal 11 or assertion failure) when diagnosing missing or mismatched arguments in KeyPath expressions (e.g., `\.split(separator: ",")`). This occurred because `getCalleeLocator` did not correctly identify the underlying function (callee) for a KeyPath application component.

Previously, `getCalleeLocator` failed to resolve the `Apply` component to its corresponding member. This caused `getFunctionArgApplyInfo` to fail when attempting to retrieve the argument list and overload choice, leading to a crash during diagnostic generation.

This change updates `getCalleeLocator` to specifically handle `KeyPathComponent::Apply` and `UnresolvedApply`. It now correctly resolves the callee to the *preceding* component (index - 1) in the KeyPath, ensuring the correct overload and argument list are retrieved without collisions.

Resolves: https://github.com/swiftlang/swift/issues/85942
2026-01-07 19:56:15 +05:30
AnirudhMathur12
874b9b9d28 [Constraints] Fix crash in getFunctionArgApplyInfo for KeyPath Apply components
Fix `getCalleeLocator` to correctly return the previous component
(the callee) for KeyPath Apply components, rather than the Apply
component itself. This allows `getFunctionArgApplyInfo` to properly
retrieve overload information and avoid crashing on invalid KeyPath
expressions.

Update test expectations to match errors from the new parser.
2026-01-03 00:48:48 +05:30
AnirudhMathur12
1077e09e52 Fix crash in missing argument diagnostics for KeyPath applications
The compiler previously crashed when diagnosing missing arguments in KeyPathExpr (e.g., \.split(separator: ",")). This occurred because getFunctionArgApplyInfo failed to handle KeyPathExpr anchors, and CSDiagnostics.cpp blindly unwrapped the result.
This change fixes the crash by:
 - Retrieving KeyPath Overloads: Updating ConstraintSystem::getFunctionArgApplyInfo to scan the locator path for the KeyPathComponent. This allows us to retrieve the correct argument list and overload choice (function type) when available.
 - Adding Safety Check: Updating MissingArgumentsFailure::diagnoseAsError to safely check if FunctionArgApplyInfo was successfully created before accessing it. This ensures graceful failure if the solver cannot determine the overload for the malformed component.
2026-01-02 04:50:50 +05:30
AnirudhMathur12
6b3b540f24 [ConstraintSystem] Fix crash when diagnosing missing args in KeyPath application
This patch fixes a compiler crash (Signal 11) where `getFunctionArgApplyInfo`
blindly cast an expression to `CallExpr` without verification, and
`CSDiagnostics` subsequently dereferenced a null result.

This occurred specifically when using a KeyPath expression (e.g. inside `.map`)
that had argument labels missing. The diagnostic logic triggered on the KeyPath
node, which is not a `CallExpr`, causing an assertion failure in debug builds
and a segfault in release builds.

Resolves: https://github.com/swiftlang/swift/issues/85942
2026-01-02 00:39:34 +05:30
Slava Pestov
d795c185b5 Sema: Remove BindingSet::getConstraintSystem()
We shouldn't store a pointer to the ConstraintSystem inside every
BindingSet, but there are some annoying things to untangle before
we can do that.

As a starting point toward that, remove the getConstraintSystem()
getter so that at least we can't reach up to the ConstraintSystem
from the outside.
2025-12-13 21:16:44 -05:00
Slava Pestov
74fa1e7f5e Sema: BindingSet::Literals can just be a vector and not a map 2025-12-13 21:16:43 -05:00
Slava Pestov
6384c27e8e Sema: BindingSet::Defaults can just be a vector and not a map
The only place where we did a lookup, we also iterated over it anyway,
and all remaining usages are simplified by downgrading it to a vector.
2025-12-13 21:16:43 -05:00
Slava Pestov
ca12185204 Merge pull request #85967 from slavapestov/dont-copy-that-floppy
Sema: Avoid copying BindingSets
2025-12-12 00:18:47 -05:00
Slava Pestov
ba7df4013e Sema: Avoid copying BindingSets
They're stored inside the ConstraintGraphNode now, so returning a
`const BindingSet *` from within determineBestBindings() should
be safe.
2025-12-10 18:48:00 -05:00
Egor Zhdan
f995144c1d Merge pull request #85906 from egorzhdan/egorzhdan/reland-cxx-string-cs
Reapply "[ConstraintSystem] C++ Interop: Binding a string literal to `std.string` shouldn't increase the score"
2025-12-10 21:04:15 +00:00
Egor Zhdan
84e7f82363 Reapply "[ConstraintSystem] C++ Interop: Binding a string literal to std.string shouldn't increase the score"
This reverts commit 6852bc9834.

In addition to the original change, this makes sure that C++ `std::string` and Swift `String` are given distinct score, in order to prevent ambiguity which was causing build failures in some projects.

rdar://158439395
2025-12-09 12:55:18 +00:00
Slava Pestov
8da3b62d7b Merge pull request #85293 from slavapestov/too-complex-source-loc
Sema: Fix source location bookkeeping for 'reasonable time' diagnostic
2025-12-07 01:26:04 -05:00
Slava Pestov
d8cf185a13 Sema: Fix source location bookkeeping for 'reasonable time' diagnostic
We already had bookkeeping to track which statement in a multi-statement
closure we were looking at, but this was only used for the 'reasonable time'
diagnostic in the case that we hit the expression timer, which was almost
never hit, and is now off by default. The scope, memory, and trial limits
couldn't use this information, so they would always diagnose the entire
target being type checked.

Move it up from ExpressionTimer to ConstraintSystem, so that we get the
right source location there too. Also, factor out some code duplication
in BuilderTransform to ensure we get the same benefit for result builders
applied to function bodies too.
2025-12-06 20:31:08 -05:00
Slava Pestov
d57a38be0a Sema: Fix latent bug due to unhandled cases in constraints::simplifyLocator() 2025-12-05 15:05:58 -05:00
Anthony Latsis
88220a33c3 [NFC] "SwiftVersion" → "LanguageMode" in DiagnosticEngine::warnUntilSwiftVersion, etc. 2025-12-04 15:11:07 +00:00
Egor Zhdan
4234fb7358 Merge pull request #85340 from egorzhdan/egorzhdan/revert-cs-std-string
Revert "[ConstraintSystem] C++ Interop: Binding a string literal to `std.string` shouldn't increase the score"
2025-11-18 18:51:12 +00:00
Hamish Knight
1f32f7f5ac Merge pull request #85525 from hamishknight/yeet
[CS] Remove `getImplicitValueConversionLocator` & `ImplicitConversion`
2025-11-17 14:03:13 +00:00
Hamish Knight
61a5ae8e01 [CS] Eagerly bind hole in recordInvalidNode
We know this is where the issue is so we can immediately bind to a hole,
ensuring we don't produce unnecessary downstream diagnostics from
things we can't infer.
2025-11-16 11:47:21 +00:00
Hamish Knight
6dfff1ce9c [CS] Remove getImplicitValueConversionLocator & ImplicitConversion
These are now unused.
2025-11-15 19:26:46 +00:00
Slava Pestov
819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Anthony Latsis
bda6edb85c AST: Rename GenericContext::isGeneric to hasGenericParamList
`isGeneric` is a misleading name because this method checks for the
existence of a `GenericParamList`, which is not implied by genericity.
2025-11-11 15:55:16 +00:00
Egor Zhdan
6852bc9834 Revert "[ConstraintSystem] C++ Interop: Binding a string literal to std.string shouldn't increase the score"
This reverts commit cd9c37ca

This is causing build failures for some projects. We need more time to investigate. Reverting this temporarily in the meantime.

rdar://158439395
2025-11-05 11:19:11 -08:00
Hamish Knight
260d10f6b8 [CS] Fix a couple of fixPropertyWrapperFailure crashers
Make sure we query the constraint system for a type if we have a local
property wrapper in a closure to avoid kicking interface type
computation outside the closure, and make sure we map into context if
we need to.
2025-11-04 00:56:01 +00:00
Hamish Knight
3b57a7cd91 [CS] Clean up some property wrapper logic
These methods can be simplified a bunch since the returned decl is
always the input decl and we can refactor the lambdas to just return
the auxiliary variable and have the type computation in the caller.
2025-11-04 00:56:01 +00:00
Slava Pestov
69c8d15570 Sema: Fix a fuzzer crash
This test case crashes when prepared overloads are disabled, but passes
when enabled. To avoid messing up tests if we have to turn the flag on
and off, fix the crash.
2025-10-24 16:20:21 -04:00
Hamish Knight
977246e0da [CS] Use inSalvageMode instead of phase in a couple of places
This seems to better match what the conditions are checking for.
2025-10-19 13:14:00 +01:00
Tim Kientzle
41f680cb93 Merge pull request #84826 from tbkka/tbkka-revert-revert-floatingpointdescription
Re-land new Floating-poing `debugDescription`
2025-10-12 20:19:12 -07:00
Tim Kientzle
4c0b58096e Make _InlineArray subject to the same optimizations as InlineArray 2025-10-10 14:06:14 -07:00
Slava Pestov
8600cdcc2a Sema: Remove ConstraintSystemPhase::{Diagnostics,Finalization} 2025-10-10 16:06:42 -04:00
Hamish Knight
48ac5126d9 [AST] Fold away nested ErrorTypes
Remove the now-unnecessary hack where we could build recursive
ErrorTypes.
2025-10-08 21:16:02 +01:00
Hamish Knight
9e4208b69b [CS] Remove custom logic from simplifyTypeForCodeCompletion
We ought to be able to just use `simplifyType` with an additional
parameter to tell it to produce archetypes.
2025-10-04 12:56:52 +01:00
Hamish Knight
669a2ce9b0 [CS] Sink placeholder handling logic into Solution::simplifyType
Move the logic from `FailureDiagnostic::resolveType` into
`Solution::simplifyType` to allow completion to use it too. While
here, also handle cases where the placeholder is from a different
member of the equivalence class to the generic parameter.
2025-10-04 12:56:52 +01:00
Hamish Knight
5171b84dba [CS] Replace UnresolvedType with ErrorType in simplifyType/resolveType
This means we now either produce a bare ErrorType, or an ErrorType
with a generic parameter original type for a generic parameter hole.
We ought to further consolidate this logic by sinking the generic
parameter original type replacement into `simplifyType` itself, but
I'm leaving that for a future patch since it affects completion
results and I want to try keep this close to NFC.
2025-10-03 09:50:42 +01:00
Allan Shortlidge
2467b931a7 ConstraintSystem: Move key path type utilities to AST. 2025-09-25 07:13:17 -07:00
Slava Pestov
7f8c4e08a8 Sema: Correctly thread PreparedOverloadBuilder through InferableTypeOpener 2025-09-18 14:54:16 -04:00
Slava Pestov
9dea72579a Sema: Record ASTNode type changes in PreparedOverload
Also, move some methods from ConstraintSystem.h to ConstraintSystem.cpp.
2025-09-18 14:54:16 -04:00
Slava Pestov
4ea2f1706b Sema: Simplify getConcreteReplacementForProtocolSelfType() 2025-09-18 11:31:59 -04:00
Doug Gregor
7d21bc332a [Embedded] Diagnose untyped throws as an Embedded Swift restriction
Untyped throws depends on existentials (`any Error`), and is therefore
not available in Embedded Swift. Introduce a diagnostic that diagnoses
any use of untyped throws, suggesting that one use typed throws
instead.

Make this an opt-in diagnostic enabled with `-Wwarning
EmbeddedRestrictions`, whether in Embedded Swift or not, using the
"default ignore" flag on these new warnings. Document this new
diagnostic group, and put the existing Embedded Swift error about
weak/unowned references in it as well.

Part of the general push to have the type checker identify code that
will not compile as Embedded Swift earlier, rdar://133874555.
2025-09-14 21:48:50 -07:00
Slava Pestov
13b4b03aca Sema: Fix crash in IgnoreAssignmentDestinationType::diagnoseForAmbiguity() 2025-09-09 18:58:10 -04:00
Hamish Knight
5b8dfc70ab [CS] Emit fallback diagnostic if needed for IgnoreInvalidASTNode
If no other error has been emitted, make sure we emit a fallback
diagnostic rather than crashing in the ASTVerifier or SILGen.
2025-08-28 15:31:48 +01:00
Hamish Knight
9c898206e8 Merge pull request #83870 from hamishknight/overrun
[CS] Open generic requirements for types with unbound + placeholder types
2025-08-27 23:20:33 +01:00