Commit Graph

3754 Commits

Author SHA1 Message Date
Hamish Knight
54fd18e5b5 Merge pull request #86011 from hamishknight/dyn-apply
[CS] Do member lookup last when binding dynamic member overload
2025-12-13 23:45:15 +00:00
Hamish Knight
4b25dc528d [CS] Do member lookup last when binding dynamic member overload
Otherwise if the member lookup gets simplified immediately and we 
have a recursive dynamic member lookup we will crash since we wouldn't
have introduced the corresponding applicable function constraint.

rdar://164321858
2025-12-12 16:39:02 +00:00
Kathy Gray
472937e57d Merge pull request #85591 from kathygray-pl/kathy/diagnosticProperty
[Diagnostics] Increase possibility for missed property diagnostic
2025-12-12 16:09:12 +00:00
Chris Williams
fe0191c62c [CSSimplify] Parameter pack wrapping logic incorrectly considers tuple LValueTypes to not be tuples (#85962)
In #65125 (and beyond) `matchTypes`, has logic to attempt to wrap an
incoming parameter in a tuple under certain conditions that might help
with type expansion.

In the case the incoming type was backed by a `var`, it would be wrapped
by an `LValueType` then be subsequently mis-diagnosed as not-a-tuple.

More details in #85924 , this this is also the cause of (and fix for)
#85837 as well...
2025-12-11 22:29:42 -08:00
Kathy Gray
fde49b8847 Diagnostics : Increase possibility for missed property diagnostic
Impact for an unknown property access was frequently higher than other options
on ambiguous selections, by 3 to 5 points, causing fix selections that were
farther away and frequently noted to be in accurate. This commit lowers the
impact to be in a similar range to other fixes and this causes property accesses
to be selected more proprotionaly.

In the existing test suite, this changed the diagnostic only in the case of
protocol composition, which was also discovered to be a flawed binding lookup.

Tests added for the property lookup, tests updated for protocol composition
(Including correcting a likely error in a test specification)
2025-12-11 16:08:22 +00:00
Pavel Yaskevich
cb8d672511 Merge pull request #85873 from xedin/rdar-165862285
[CSRanking] Disambiguate static vs. instance method that used to be s…
2025-12-10 06:48:20 -08:00
Slava Pestov
3dcd9bb011 Merge pull request #85938 from slavapestov/12-year-old-fixme-strikes-back
Sema: Tighten ridiculous disambiguation hack for overloaded type declarations
2025-12-10 06:53:43 -05:00
Slava Pestov
e7d2c163be Merge pull request #85933 from slavapestov/fix-ben-regression
Sema: Disambiguate some more bidirectional conversions
2025-12-10 06:53:01 -05:00
Slava Pestov
6701e5b39b Sema: Tighten ridiculous disambiguation hack for overloaded type declarations
We noticed that a project failed to build with prepared overloads enabled,
but built successfully with prepared overloads disabled.

It turns out that the code clearly should never have been accepted in the
first place, because the type checker was making an arbitrary choice between
two nominal type declarations with the same name.

Further inspection revealed this was because of a FIXME added in 2013 which
was far too broad. Tighten up the logic here to only disambiguate if at least
one of the two declarations is a type alias, and it has the same underlying
type as the other one. A choice between unrelated nominal type declarations
should always be ambiguous.

This still isn't perfect, because we might have two generic type aliases
that are referenced in both solutions with different substitution maps,
in which case we will still erroneously pick the first one. But this
is better than the old logic, at least.

Fixes rdar://165863775.
2025-12-09 22:48:12 -05:00
Slava Pestov
f3e6b4ceda Sema: Disambiguate some more bidirectional conversions
This fixes an ambiguity introduced by the stdlib change in
0f99458900.

Since (borrowing T) -> () and (T) -> () both convert to
each other, we could end up with ambiguous solutions where
neither one was better than the other. Generalize the
existing trick we use for labeled vs unlabeled tuples to
also strip off ownership specifiers and @convention(...)
from function types. This fixes the regression, as well
an existing FIXME in a test I added a while ago where
the same problem arises with @convention(block).
2025-12-09 18:31:59 -05:00
Slava Pestov
1e9f8826a7 Sema: Split off Apple-specific parts of bidirectional_conversions.swift test 2025-12-09 17:35:04 -05:00
Ben Cohen
58f661cfba Allow Equatable: ~Escapable (#85854)
Adds `~Escapable` to #85746
2025-12-08 09:50:50 -08: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
Pavel Yaskevich
7c32c2a21b [CSRanking] Disambiguate static vs. instance method that used to be supported by a performance hack
Handle the following situation:

```swift
struct S {
   func test() {}
   static func test(_: S) {}
}
```

Calling `S.test(s)` where `s` has a type `S` without any other context
should prefer a complete call to a static member over a partial
application of an instance once based on the choice of the base type.

The behavior is consistent for double-applies as well i.e.
`S.test(s)()` if static method produced a function type it would be
preferred.

Resolves: rdar://165862285
2025-12-05 13:27:53 -08:00
Ben Cohen
0f99458900 Allow Comparable: ~Copyable (#85747)
Builds on #85746 which covers Equatable
2025-12-05 08:37:36 -08:00
Ben Cohen
fbb420f38d Allow Equatable: ~Copyable (#85746)
Under review
[here](https://forums.swift.org/t/se-0499-support-copyable-escapable-in-simple-standard-library-protocols/83297).
Multi-part version of #85079.
2025-12-03 05:45:38 -08: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
c853d81d37 [CS] Always eagerly bind member type var to hole for missing member
We know this is where the issue is so we ought to always produce a
concrete hole.
2025-11-16 11:47:21 +00:00
Pavel Yaskevich
4bd370fe46 Merge pull request #85451 from xedin/rdar-164201746
[CSOptimizer] Skip `??` if it's involved in optional chain with unres…
2025-11-12 17:30:51 -08:00
Pavel Yaskevich
a01692ace1 [CSOptimizer] Skip ?? if it's involved in optional chain with unresolved result type
`??` operator is overloaded on optionality of its result. When the
first argument matches exactly, the ranking is going to be skewed
towards selecting an overload choice that returns a non-optional type.
This is not always correct i.e. when operator is involved in optional
chaining. To avoid producing an incorrect favoring, let's skip the this
disjunction when constraints associated with result type indicate
that it should be optional.

Simply adding it as a binding won't work because if the second argument
is non-optional the overload that returns `T?` would still have a lower
score.

Resolves: rdar://164201746
2025-11-11 15:21:33 -08:00
Hamish Knight
de51fed13d Merge pull request #85410 from hamishknight/pack-fix
[CS] Handle packs in `increaseScoreForGenericParamPointerConversion`
2025-11-11 09:07:55 +00:00
Hamish Knight
ded97baea1 [CS] Handle packs in increaseScoreForGenericParamPointerConversion
Missed this in my original patch, handle pack parameters the same as
regular generic parameters, ensuring we don't prefer a pack overload
over a generic overload just because there are pointer conversions
involved. Note this doesn't fix the wider issue of rdar://122011759,
I'm planning on looking into that in a follow-up.
2025-11-10 14:26:25 +00:00
Pavel Yaskevich
513caf2c9b [CSOptimizer] Avoid favoring overloads that mismatch context on async
This is a fix for the ported "calls with a single unlabeled argument"
hack. If overload doesn't match context on async effect, let's not favor
it because that is more important than defaulted parameters.

Resolves: rdar://164269641
2025-11-07 14:15:45 -08:00
Hamish Knight
00d8774032 [Sema] Upgrade tuple shuffle warning to error in future lang mode
This has been deprecated for a while now, flip it to an error for a
future language mode.
2025-10-29 15:14:23 +00:00
Hamish Knight
24347812f5 [Diag] Reword the tuple shuffle diagnostic
"reorder" seems a bit less jargony than "shuffle", and include the 
labels that are being reordered.
2025-10-29 15:14:23 +00:00
Hamish Knight
6510eb024a [test] Fix indentation in tuple_shuffle.swift 2025-10-29 15:14:23 +00:00
Hamish Knight
1781faba1a [CS] Upgrade tuple label mismatch warning to error for future lang mode
I missed upgrading this to an error for Swift 6 mode, let's upgrade it
to an error for a future language mode. It's important we reject these
cases since we're otherwise allowing subtyping to be a weaker constraint
than conversion.
2025-10-29 15:14:23 +00:00
Pavel Yaskevich
e1a6077117 [CSOptimizer] Update unary call favoring to include resolved member references
Update special favoring logic for unlabeled unary calls to support
non-overloads member references in argument positions.

The original hack missed a case where a type of a member is known
in advance (i.e. a property without overloads) because there as
another hack (shrink) for that.

This helps in situations like `Double(x)` where `x` is a property
of some type that is referenced using an implicit `self.` injected
by the compiler.

Resolves: rdar://161419917
2025-10-27 18:22:37 +09:00
Hamish Knight
f15fae4737 Merge pull request #84822 from hamishknight/just-a-phase
[CS] Remove `ConstraintSystemPhase`
2025-10-24 16:05:08 +01:00
Hamish Knight
8397a7a61d Merge pull request #84907 from hamishknight/fixed-point
[CS] Penalize implicit pointer conversions to generic parameter types
2025-10-24 15:56:07 +01:00
Slava Pestov
1b6ac2c1d8 Sema: Clean up leading dot fix logic in simplifyConformsToConstraint()
Tests still pass without this code, including the tests I just
added in the previous commit.
2025-10-20 21:50:41 -04:00
Slava Pestov
8d247100d8 Sema: Add a couple of passing diagnostic tests to static_members_on_protocol_in_generic_context.swift
It looks like we didn't test leading dot with instance members.
2025-10-20 21:50:41 -04:00
Hamish Knight
0a1905acba [CS] Only use unsolved applicable fn for @dynamicMemberLookup cases
This works around the fact that existential opening does not currently
work correctly in cases where the argument isn't resolved before the
applicable fn is solved.
2025-10-19 13:14:00 +01:00
Hamish Knight
f1823786b6 [CS] Re-order subscript constraint generation
Add the application constraint before the member constraint, which
allows us to get rid of the special-cased delaying logic for dynamic
member subscripts. The diagnostic change here is due to the fact that
we no longer have a simplified type for the result in CSGen, which
would also be the case if we had a disjunction for the member.
2025-10-19 13:14:00 +01:00
Pavel Yaskevich
2b7adbc4ff Merge pull request #84800 from xedin/remove-csapply-operator-devirt
[CSApply] Don't attempt operator devirtualization
2025-10-18 23:09:23 +09:00
Hamish Knight
77a59383f7 [CS] Penalize implicit pointer conversions to generic parameter types
We ought to consider outright banning these conversions if the
destination is a generic parameter type, but for now let's penalize
them such that we don't end up with ambiguities if you're doing an
implicit pointer conversion through an identity generic function.

rdar://161205293
2025-10-16 14:14:53 +01:00
Hamish Knight
1f5a0b4c88 [CS] Use simplifyType in isDependentMemberTypeWithBaseThatContainsUnresolvedPackExpansions
The pack expansion type variable may be a nested in the fixed type of
another type variable, and as such we unfortunately need to fully
`simplifyType` here.

rdar://162545380
2025-10-14 15:13:05 +01:00
Pavel Yaskevich
9d00c7bd18 [CSApply] Don't attempt operator devirtualization
This appears to only support synthesized conformances because
operators in such cases use different names - `__derived_*`.

These days devirtualization like this is performed as part of
mandatory inlining. And this approach doesn't stack well with
features like `MemberImportVisibility` because there is change
to check whether witness is available or not.
2025-10-10 16:31:11 -07:00
Hamish Knight
a83ea3c8bb Merge pull request #84745 from hamishknight/fishmonger
[Evaluator] Enforce consistent results for cyclic requests
2025-10-09 17:08:04 +01:00
Pavel Yaskevich
1d8536eed2 Merge pull request #84729 from xedin/rdar-161207705
[CSSimplify] Avoid simplifying dependent members until pack expansion…
2025-10-08 16:19:00 -07:00
Hamish Knight
e7c7239587 [Sema] Insert ErrorType same-type constraints for placeholder signatures
This helps avoid producing more downstream errors. This changes
`GenericSignature::forInvalid` to produce the same signature as e.g
`<T where T == Undefined>`. This subsumes the need to introduce
conformance requirements for invertible protocols.
2025-10-08 21:16:02 +01:00
Pavel Yaskevich
b0a2bd6578 Merge pull request #84715 from xedin/rdar-159401910
[Diagnostics] Fix a few issues with existential type mismatches
2025-10-07 09:39:36 -07:00
Pavel Yaskevich
c278717887 [CSSimplify] Avoid simplifying dependent members until pack expansions in the based are bound
Dependent members cannot be simplified if base type contains unresolved
pack expansion type variables because they don't give enough information
to substitution logic to form a correct type. For example:

```
protocol P { associatedtype V }
struct S<each T> : P { typealias V = (repeat (each T)?) }
```

If pack expansion is represented as `$T1` and its pattern is `$T2`, a
reference to `V` would get a type `S<Pack{$T}>.V` and simplified version
would be `Optional<Pack{$T1}>` instead of `Pack{repeat Optional<$T2>}`
because `$T1` is treated as a substitution for `each T` until bound.

Resolves: rdar://161207705
2025-10-07 00:20:51 -07:00
Hamish Knight
9883477c02 Merge pull request #84699 from hamishknight/meta-error
[CS] Improve diagnostics for non-metatype `type(of:)` contextual type
2025-10-07 02:49:10 +01:00
Henrik G. Olsson
d3214de950 Merge pull request #84685 from hnrklssn/verify-all
This adds the -verify-ignore-unrelated flag. When -verify is used without -verify-ignore-unrelated, diagnostics emitted in buffers other than the main file and those passed with -verify-additional-file (except diagnostics emitted at <unknown>:0) will now result in an error. They were previously ignored. The old behaviour is still available as opt-in using -verify-ignore-unrelated, but by being strict by default it should make it harder to accidentally miss diagnostics.

To avoid unnecessary performance overhead, -verify-additional-file is still required to parse the expected-* directives in files other than the main file.
2025-10-06 18:01:47 -07:00
Pavel Yaskevich
15c56627b8 [CSSimplify] Remove all InstanceType at the end of a locator to diagnose mismatched existential conversion 2025-10-06 14:01:28 -07:00
Hamish Knight
d65f28984a [CS] Improve diagnostics for non-metatype type(of:) contextual type
Emit a custom diagnostic for this case, and handle holes.
2025-10-06 21:45:48 +01:00
Pavel Yaskevich
36fc1e6c43 [CSSimplify] Diagnose an attempt to match non-existential type to an existential one
Resolves: rdar://159401910
2025-10-06 09:51:34 -07:00
Hamish Knight
6d7d1d78d6 Merge pull request #84695 from hamishknight/typewriter
[AST] Canonicalize original type for ErrorType
2025-10-06 17:23:55 +01:00