Commit Graph

7723 Commits

Author SHA1 Message Date
Hamish Knight
5cbdcaf655 Merge pull request #82834 from hamishknight/fuzzy
[test] Add some more known crashers
2025-07-08 08:32:52 +01:00
Hamish Knight
8b3cb86d7f Merge pull request #82802 from hamishknight/context-fail
[Completion] Map failable initializer result type into context
2025-07-08 06:20:17 +01:00
Slava Pestov
4dc0db31f9 Sema: Improve the 'protocol method can't impose new requirements on Self' check
This check had two problems. First, it would assert upon encountering
a layout requirement, due to an unimplemented code path.

A more fundamental issue is that the logic wasn't fully sound, because
it would miss certain cases, for example:

    protocol P {
      associatedtype A

      func run<B: Equatable>(_: B) where B == Self.A
    }

Here, the reduced type of `Self.A` is `B`, and at first glance, the
requirement `B: Equatable` appears to be fine. However, this
is actually a new requirement on `Self`, and the protocol be rejected.

Now that we can change the reduction order by assigning weights to
generic parameters, this check can be implemented in a better way,
by building a new generic signature first, where all generic
parameters introduced by the protocol method, like 'B' above, are
assigned a non-zero weight.

With this reduction order, any type that is equivalent to
a member type of `Self` will have a reduced type rooted in `Self`,
at which point the previous syntactic check becomes sound.

Since this may cause us to reject code we accepted previously,
the type checker now performs the check once: first on the original
signature, which may miss certain cases, and then again on the new
signature built with the weighted reduction order.

If the first check fails, we diagnose an error. If the second
check fails, we only diagnose a warning.

However, this warning will become an error soon, and it really
can cause compiler crashes and miscompiles to have a malformed
protocol like this.

Fixes rdar://116938972.
2025-07-07 12:23:58 -04:00
Hamish Knight
5730eb2e10 [test] Add some more known crashers 2025-07-07 11:12:36 +01:00
Hamish Knight
3a8bf75951 Merge pull request #82761 from hamishknight/req-assert
[test] Require asserts for IDE crashers
2025-07-04 14:10:10 +01:00
Hamish Knight
e7bf62dd81 [Completion] Map failable initializer result type into context
Make sure we don't pass an interface type to `setTypeContext`.

rdar://155038769
2025-07-04 13:55:40 +01:00
Hamish Knight
d6037049b1 [CS] Ensure type variables are eliminated by Solution::simplifyType
`TypeSimplifier` may not eliminate type variables from e.g the
pattern types of pattern expansion types since they can remain
unresolved due to e.g having a placeholder count type. Make sure we
eliminate any remaining type variables along with the placeholders.
There's probably a more principled fix here, but this is a quick and
low risk fix we can hopefully take for 6.2.

rdar://154954995
2025-07-03 16:24:31 +01:00
Hamish Knight
4aee90318a [test] Require asserts for IDE crashers 2025-07-03 10:53:27 +01:00
Hamish Knight
3daae5955e [AST] Use ErrorType for invalid value generic parameter
If we fail to resolve the value type for a value generic parameter,
previously we would have returned a null Type, causing crashes
downstream. Instead, return an ErrorType, leaving a null Type for
cases where the generic parameter isn't a value generic at all.

rdar://154856417
2025-07-02 14:46:55 +01:00
Hamish Knight
beb2b58d62 [test] Mark a couple of IDE crashers as fixed (#82685)
Seems like these may have been fixed by #82574, unfortunately there was
a race with #82619
2025-07-01 11:00:29 -07:00
Hamish Knight
dc880588ff Merge pull request #82619 from hamishknight/fuzzy
[test] Add some more known crashers
2025-07-01 13:24:42 +01:00
Pavel Yaskevich
bec4ebd126 Merge pull request #82574 from xedin/solver-perf-behind-a-flag
[ConstraintSystem] Implement disjunction favoring algorithm behind a flag
2025-06-30 16:27:01 -07:00
Pavel Yaskevich
d3f86b4efd Merge pull request #82593 from xedin/rdar-148076903
[Concurrency] Fix a crash caused by misuse of `isolated` modifier
2025-06-30 13:06:17 -07:00
Hamish Knight
b9afc87c28 [test] Add some more known crashers 2025-06-30 20:52:06 +01:00
QuietMisdreavus
1948907eb3 use RespectOriginallyDefinedIn when mangling extension contexts (#82348)
Resolves rdar://152598492

Consider the following Swift, adapted from a real-world framework:

```swift
@available(macOS 10.8, *)
@_originallyDefinedIn(module: "another", macOS 11.0)
public struct SimpleStruct {}

@available(macOS 12.0, iOS 13.0, *)
public extension SimpleStruct {
    struct InnerStruct {}
}
```

In this scenario, `SimpleStruct` was originally in a module called
`another`, but was migrated to this module around the time of macOS
11.0. Since then, the module was ported to iOS and gained a nested type
`SimpleStruct.InnerStruct`. When mangling USRs for this nested type, the
result differs depending on whether we're targeting macOS or iOS.
They're mostly the same, but the macOS build yields a USR with an `AAE`
infix, designating that the `InnerStruct` was defined in an extension
from a module with the name of the base module. On iOS, this infix does
not exist.

The reason this is happening is because of the implementation of
`getAlternateModuleName` checking the availability spec in the
`@_originallyDefinedIn` attribute against the currently active target.
If the target matches the spec, then the alternate module name is
reported, otherwise the real module name is. Since the iOS build reports
the real module name, the mangling code doesn't bother including the
extension-context infix, instead just opting to include the parent
type's name and moving on.

This PR routes around this issue by passing the
`RespectOriginallyDefinedIn` variable to the
`ExtensionDecl::isInSameDefiningModule` method, and using that to skip
the alternate module name entirely. It also sets
`RespectOriginallyDefinedIn` to `false` in more places when mangling
USRs, but i'm not 100% confident that it was all necessary. The goal was
to make USRs more consistent across platforms, regardless of the
surrounding context.
2025-06-30 12:57:12 -06:00
Hamish Knight
e912eeb2f4 [CS] Use the resolved type in NonOptionalUnwrapFailure::diagnoseAsError
Diagnostics can outlive the ConstraintSystem itself if we have a
diagnostic transaction for e.g `typeCheckParameterDefault`, make sure
we don't try to use a solver-allocated type as an argument.
2025-06-28 12:13:19 +01:00
Hamish Knight
2b05212ff5 [CS] Avoid escaping solver-allocated types in computeSubstitutions
Make sure we call `simplifyType` for the opened type bindings to
ensure holes get converted to UnresolvedType.

rdar://154553285
2025-06-28 11:19:13 +01:00
Pavel Yaskevich
358067917e [Concurrency] Fix a crash caused by misuse of isolated modifier
Adjust isolation checking to handle misused `isolated` attribute
and let attribute checker property diagnose it.

Resolves: rdar://148076903
Resolves: https://github.com/swiftlang/swift/issues/80363
2025-06-27 23:45:53 -07:00
Pavel Yaskevich
eee40b4ecc [CSGen] Make collection subscript result type inference more principled
Infer result type of a subscript with Array or Dictionary base type
if argument type matches the key type exactly or it's a supported
literal type.

This helps to maintain the existing behavior without having to resort
to "favored type" computation.
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
b90fc2b8c6 [CSOptimizer] Don't attempt to walk into literals while analyzing operator chains
Most of them don't have any children but string interpolation does
and that marks whole chain as unsupported.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
e280569988 [CSOptimizer] Fix scoring while matching against partially resolved parameter types
When matching candidate like `[Int]` against `Array<Element>`
we need to conservatively assume that if the nominals match
the argument is a viable exact match because otherwise it's
possible to skip some of the valid matches when other overload
choice have generic parameters at the same parameter position.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
1dab584f29 [CSOptimizer] Account for speculative scores only when matching operators vs. non-operators
The problem this is trying to solve is eager selection of operators
over unsupported disjunctions, when matching operators let's take
speculative information into account because it helps to make better
choices in this case.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
a953dfe0c0 [Tests] NFC: Adjust scope threshold for rdar://rdar31742586 test-case
The test was slow with hacks but now it's much faster - takes about
63k scopes to solve, it could be improved by introducing new overloads
of prefix `-` to stdlib.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
e2fe558956 [CSOptimizer] Introduce a way to preference disjunctions before scores are considered
Some of the disjunctions are not supported by the optimizers but
could still be a better choice than an operator. Using a non-score
based preference mechanism first allows us to make sure that
operator disjunctions are not selected too eagerly in some situations
when i.e. a member (supported or not) could be a better choice.

`isPreferable` currently targets only operators in result builder
contexts but it could be expanded to more uses in the future.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
bcc749fa1a [CSOptimizer] Reset the overall score of operator disjunctions that is based speculation
New ranking + selection algorithm suffered from over-eagerly selecting
operator disjunctions vs. unsupported non-operator ones even if the
ranking was based purely on literal candidates.

This change introduces a notion of a speculative candidate - one which
has a type inferred from a literal or an initializer call that has
failable overloads and/or implicit conversions (i.e. Double/CGFloat).

`determineBestChoicesInContext` would reset the score of an operator
disjunction which was computed based on speculative candidates alone
but would preserve favoring information. This way selection algorithm
would not be skewed towards operators and at the same time if there
is no no choice by to select one we'd still have favoring information
available which is important for operator chains that consist purely
of literals.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
0525818a33 [CSOptimizer] Allow matching candidates with optional types against generic paameter types
For example passing `Int?` to `T` should be considered a match
if `T` doesn't have any requirements that block it.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
b936900eb8 [CSOptimizer] Support non-operator generic choices with simple signatures
If there are no-same type requirements and parameters use
either concrete types or generic parameter types directly,
the optimizer should be able to handle ranking. Currently
candidate arguments are considered in isolation which makes
it impossible to deal with same-type requirements and
complex generic signatures.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
26b86c2962 [Tests] NFC: Add a test-case with literal chain passed as an argument to a constructor 2025-06-27 23:43:10 -07:00
Pavel Yaskevich
666aa24224 [CSOptimizer] Add support for ?? operator 2025-06-27 23:43:10 -07:00
Pavel Yaskevich
899b2bc1e9 [CSOptimizer] Allow matching against CGFloat as a contextual result type 2025-06-27 23:43:10 -07:00
Pavel Yaskevich
2f8a343aef [CSOptimizer] Infer result types through ternary expressions 2025-06-27 23:43:09 -07:00
Pavel Yaskevich
2957da3591 [ConstraintSystem] Disable performance hacks by default
This also changes the flag to `-enable-constraint-solver-performance-hacks`.
2025-06-27 23:43:09 -07:00
Nate Chandler
a3c460988c [Test] Remove old test.
This test was never well-targeted and has been disabled for years.  We
have better coverage elsewhere.

rdar://98890125
2025-06-25 11:42:34 -07:00
Hamish Knight
aec446521a [test] Add some more known type-checker crashers 2025-06-23 10:05:22 +01:00
Hamish Knight
19af3bc295 Merge pull request #82332 from hamishknight/fuzzy
[test] Add some more known type-checker crashers
2025-06-20 18:35:14 +01:00
Hamish Knight
3c8a8a860a Merge pull request #82292 from hamishknight/bigger-hammer
[AST] Replace type variables and placeholders in original ErrorTypes
2025-06-20 10:59:24 +01:00
Pavel Yaskevich
286f975c29 Merge pull request #82320 from xedin/hide-solver-hacks-behind-a-flag
[ConstraintSystem] Guard all the performance hacks with a flag
2025-06-20 00:12:16 -07:00
Dario Rexin
6db11b60a3 Merge pull request #82347 from drexin/wip-153681688
[IRGen] Fix placeholder logic for emission of conditionally inverted …
2025-06-19 09:05:48 -07:00
Hamish Knight
c3fc76fb7a [test] Add a few more known type-checker crashers 2025-06-19 14:47:52 +01:00
Hamish Knight
61aa5a0b07 [test] Add some more known type-checker crashers 2025-06-19 14:42:54 +01:00
Hamish Knight
f62cb8f91a [test] Use guard malloc in 4c84d3d852cdd1d4.swift
This should hopefully make it crash reliably.
2025-06-19 14:42:54 +01:00
Hamish Knight
5276f2e1ef Merge pull request #82334 from hamishknight/record-opened 2025-06-19 08:18:49 +01:00
Hamish Knight
9b21efdf7d Merge pull request #82316 from hamishknight/keyed 2025-06-19 08:18:29 +01:00
Slava Pestov
5987bbf966 Merge pull request #82321 from slavapestov/rqm-fixes
RequirementMachine: Add more limits to catch runaway computation, and fix a bug
2025-06-18 23:00:38 -04:00
Dario Rexin
5ec58209dc [IRGen] Fix placeholder logic for emission of conditionally inverted protocols
rdar://153681688

Instead fo counting the actual conformances, the logic took the size of the bit field, i.e. used the highest set bit, so when a type had a conditional conformance only on ~Escapable, but not on ~Copyable, it would still add 2 placeholders, but only fill one.
2025-06-18 11:28:13 -07:00
Hamish Knight
c6ec3156f7 [CS] Distinguish locators for generic args in addSpecializationConstraint
Make sure we give each argument a different locator to ensure we
can correctly record any opened types.

rdar://153674889
2025-06-18 12:19:12 +01:00
Hamish Knight
3d9e49038b Merge pull request #82295 from hamishknight/wrapping-paper 2025-06-18 08:50:57 +01:00
Hamish Knight
b37e34ea8d [Sema] Tighten up function call check in resolveKeyPathExpr
Check for `CallExpr` instead of `ApplyExpr`, we don't support
arbitrary postfix operators in key paths.
2025-06-17 23:07:46 +01:00
Slava Pestov
bf3f4a6d79 AST: Fix lost GenericSignatureErrors in getPlaceholderRequirementSignature() 2025-06-17 17:51:25 -04:00
Pavel Yaskevich
833b6b19e9 [Frontend] Remove -disable-solver-shrink option
It's folded into a more general "disable performance hacks" one.
2025-06-17 13:52:28 -07:00