Commit Graph

347 Commits

Author SHA1 Message Date
Anthony Latsis
5e41794680 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-23 19:18:08 +01:00
Doug Gregor
b182c96bd7 Print diagnostic group names by default
Print diagnostic groups as part of the LLVM printer in the same manner as the
Swift one does, always. Make `-print-diagnostic-groups` an inert option, since we
always print diagnostic group names with the `[#GroupName]` syntax.

As part of this, we no longer render the diagnostic group name as part
of the diagnostic *text*, instead leaving it up to the diagnostic
renderer to handle the category appropriately. Update all of the tests
that were depending on `-print-diagnostic-groups` putting it into the
text to instead use the `{{documentation-file=<file name>}}`
diagnostic verification syntax.
2025-03-29 15:40:56 -07:00
Allan Shortlidge
0fd2f3fc1c Sema: Diagnose @_spi_available on declarations that cannot be unavailable.
The attribute makes the declaration unavailable from the perspective of clients
of the module's public interface and was creating a loophole that admitted
inappropriate unavailability.
2025-03-07 19:44:48 -08:00
Allan Shortlidge
949a6c68d7 AST/Sema: Retire SemanticAvailableAttr::getVersionAvailability().
Query for availability constraints instead of calling getVersionAvailability().
2025-02-16 07:44:45 -08:00
Allan Shortlidge
fdcf53cca6 Tests: Test conformance checker with witnesses in unavailable extensions.
A protocol conformance witness must be as available as its requirement. In
Swift 6.1 and earlier, the conformance checker failed to note that witnesses in
unavailable extensions are unavailable. That bug was fixed by a previous
change, but there was no test case covering it so the difference in behavior
was not acknowledged.

Related to rdar://143466010.
2025-01-23 21:13:24 -08:00
Doug Gregor
cd6864a5f3 Merge pull request #76454 from DmT021/wp/deprecated-diagnostic-groups
[Diagnostics] Add new groups to the 'deprecated' diagnostic group
2024-09-30 09:05:16 -07:00
Dmitrii Galimzianov
aa5e10f8d2 [Diagnostics] DeprecatedDeclaration group 2024-09-25 23:18:25 +02:00
Slava Pestov
f35c90a6b7 Merge pull request #76445 from slavapestov/maptypeintocontext
Overhaul mapTypeIntoContext()
2024-09-14 08:45:20 -04:00
Slava Pestov
40641f5e91 Sema: Fix dodgy logic in findMissingGenericRequirementForSolutionFix()
'type' and 'missingType' are contextual types in the generic environment
of the witness thunk. We cannot simply map them into the environment of
the conformance, because if the conforming type is a class, the witness
thunk has an extra generic parameter at depth=0, index=0 and all other
generic parameters are shifted down by one depth.
2024-09-13 08:12:51 -04:00
Allan Shortlidge
a860b02360 Sema: Diagnose unavailability of associated type decls.
It should not be possible to mark an associated type declaration unavailable
since doing so does not have an effect on how a conformance interacts with its
associated types.
2024-09-11 16:42:47 -07:00
Alex Hoppen
66104395d7 [Sema/SourceKit] Emit same diagnostics for missing protocol requirements on the command line and in SourceKit
Some editors use diagnostics from SourceKit to replace build issues. This causes issues if the diagnostics from SourceKit are formatted differently than the build issues. Make sure they are rendered the same way, removing most uses of `DiagnosticsEditorMode`.

To do so, always emit the `add stubs for conformance` note (which previously was only emitted in editor mode) and remove all `; add <something>` suffixes from notes that state which requirements are missing.

rdar://129283608
2024-08-07 14:01:30 -07:00
Slava Pestov
557300ba48 Move associated type inference tests into their own directory 2024-06-28 17:50:24 -04:00
Slava Pestov
005d83f8d2 Sema: Fix incorrect use of STL iterator in associated type inference
Fixes rdar://problem/127575477.
2024-06-27 18:21:14 -04:00
Holly Borla
005b45c1cc [Sema] Diagnose deprecated default implementations in the witness checker.
If a protocol provides a deprecated default implementation for a requirement
that is not deprecated, the compiler should emit a warning so the programmer
can provide an explicit implementation of the requirement. This is helpful
for staging in new protocol requirements that should be implemented in
conforming types.
2024-06-10 07:10:38 -07:00
Doug Gregor
96fd043638 Add tests for multiple @_implements on associated types 2024-03-25 17:13:33 -07:00
Slava Pestov
5afdb987e2 Merge pull request #72146 from slavapestov/ncgenerics-fixes-9
Non-copyable generics fixes, part 9
2024-03-07 22:14:33 -05:00
Slava Pestov
af8c8f1671 Sema: Remove non-experimental associated type inference 2024-03-07 17:30:17 -05:00
Slava Pestov
41df661160 AST: Use a builtin conformance for unconditional Copyable/Escapable
This generalizes what we were already doing for classes.
2024-03-07 15:07:47 -05:00
Slava Pestov
10a2ddb95e RequirementMachine: Fix MaxConcreteNesting check to take initial rules into account
Fixes rdar://123357717.
2024-03-06 21:42:49 -05:00
Slava Pestov
1b99dd2451 RequirementMachine: Tweak RewriteSystem::recordConflict() heuristic again 2024-02-29 18:13:28 -05:00
Pavel Yaskevich
4e7425c03d [Tests] NFC: Add a test-case for rdar://123357062 2024-02-24 23:18:27 -08:00
Pavel Yaskevich
0c17b41d1e [Tests] NFC: Add a test-case for rdar://123336457 2024-02-24 23:15:36 -08:00
Slava Pestov
bf2a77ee86 Merge pull request #71865 from slavapestov/fix-rdar123543200
Sema: Fix checkTypeWitness() for Objective-C protocol compositions
2024-02-24 15:26:38 -05:00
Slava Pestov
6ac91cde5d Merge pull request #71821 from slavapestov/inheritance-clause-fixes
Refactor protocol inheritance clauses and existential layout for non-copyable generics
2024-02-24 11:58:03 -05:00
Slava Pestov
236d22ca8f Add regression test for bug already fixed 2024-02-24 07:25:59 -05:00
Slava Pestov
2befb08ed0 Sema: Fix checkTypeWitness() for Objective-C protocol compositions
I forgot that a protocol composition (C & P) can satisfy a superclass
requirement [T : D] in one narrow case implemented in
TypeBase::isExactSuperclassOf():

- D is a superclass of C
- P is an @objc protocol
- C is declared in Objective-C

This case was ruled out here because the code assumed the type witness
had to be a concrete class or archetype to satisfy a superclass
requirement.

Fixes rdar://problem/123543200.
2024-02-24 06:39:52 -05:00
Slava Pestov
14b110d2e8 Sema: Associated type inference optimization
This addresses a performance regression from 83cb420ee4.

In the old associated type inference implementation, we used to
fold valid solutions by comparing type witnesses, but this was
not correct as described in the commit message there.

After my fix we started to record more valid solutions from the
same search space, and this caused a performance regression
because we were already doing exponential work.

However in the program in question, each possible choice of witness
for a requirement would introduce the same bindings, so there was
nothing to gain from trying them all.

Since ranking only compares pairs of witnesses for the same
requirement, we can optimize this problem another way: by folding
identical terms in a disjunction, but only if *all* terms are
identical, which avoids the correctness issue in the old search
strategy.

Fixes rdar://problem/123334433.
2024-02-23 22:13:39 -05:00
Slava Pestov
ba90861602 Sema: Try a little harder to infer associated types to generic parameters if all else fails
If we have an abstract witness, we don't attempt a generic parameter
binding at all. But if simplifying the abstract witness failed, we
should still attempt it.

This would be cleaner as a disjunction in the solver but I want
to change behavior as little as possible, so this adds a new fallback
that we run when all else fails.

Fixes rdar://problem/123345520.
2024-02-21 05:42:39 -05:00
Slava Pestov
29b08a92a5 Add two test cases that regressed after a previous attempt at fixing a bug 2024-02-21 05:42:39 -05:00
Slava Pestov
b489baac95 Add two regression tests for bugs already fixed 2024-02-21 05:42:14 -05:00
Slava Pestov
0b2e168c56 Sema: Tweak abstract witness inference priority again
We used to do attempt things in this order:

- abstract witnesses, defaults, generic parameters

I tried this but it broke things:

- generic parameters, abstract witnesses, defaults

Hoping this sticks:

- abstract witnesses, generic parameters, defaults

Fixes rdar://123262178.
2024-02-21 05:42:14 -05:00
Slava Pestov
1c24b880ec Merge pull request #70467 from AnthonyLatsis/recursive-member-typerepr
AST: Remodel `MemberTypeRepr` to be recursive
2024-02-16 11:36:31 -05:00
Slava Pestov
70c9f8a47e RequirementMachine: Leave behind conflicting requirements in the minimized signature
Requirement lowering only expects that it won't see two requirements
of the same kind (except for conformance requirements). So only mark
those as conflicting.

This addresses a crash-on-invalid and improves diagnostics for
move-only generics, because a conflict won't drop the copyability
of a generic parameter and expose a move-only-naive user to
confusing error messages.

Fixes #61031.
Fixes #63997.
Fixes rdar://problem/111991454.
2024-02-15 14:32:31 -05:00
Slava Pestov
cdd6f8d510 Add a test case 2024-02-14 10:36:42 -05:00
Slava Pestov
c34a74c6d9 Sema: More conservative 'tautological binding' check
Instead of computing the reduced type of the witness upfront and
then checking for canonical equality in type matching, check for
reduced equality in type matching.

This restores the old behavior and prevents us from considering too
many protocol extension witnesses, while fixing the request cycle
that motivated the change to instead match against the reduced type
of the witness.

Fixes rdar://problem/122589094, rdar://problem/122596633.
2024-02-14 06:34:52 -05:00
Slava Pestov
cfc684c00e Sema: Fix recent regression with -disable-experimental-associated-type-inference
This just brings back a bunch of old brittle logic conditionalized on the
negation of the flag.

Fixes rdar://problem/122810266.
2024-02-14 06:33:32 -05:00
Slava Pestov
f95109d364 Sema: Restore old behavior of generic parameters with associated type inference
This effectively reverts d0bd026077, so
we now look for abstract type witnesses before generic parameters.

In particular, this means we again prefer the default type witness
over a generic parameter if nothing else forces it to be a generic
parameter:

    protocol P { associatedtype A = Int }
    struct S<T>: P {}
    // S.T is always Int

Fixing this properly requires modeling generic parameter bindings as
disjunctions, which is a more disruptive change than I want to take
for now.

Fixes rdar://problem/122587432.
2024-02-11 21:17:09 -05:00
Slava Pestov
23ef89322d Sema: New strategy for dealing with completely fixed associated types
If a protocol fixes an associated type of another protocol via a
concrete same-type requirement, we should make use of this fact
immediately when building the associated type inference constraint
system.

Previously we only introduced fixed type bindings at the end, and
only in the 'abstract type witness' inference path, which we
reach if a type witness cannot be resolved by looking at value
witnesses alone.

This meant that associated type inference would find valid or
ambiguous solutions which would then be contradicted by the
ensureRequirementsAreSatisfied() check.

Longer term, what I want to do is actually build the type witness
system upfront, and also teach the constraint solver here about
merging equivalence classes, so that we can split them again when
backtracking. This will combine the best of both approaches.

Fixes rdar://problem/122586992.
2024-02-10 23:56:02 -05:00
Slava Pestov
84921dbc36 Sema: Prefer abstract witnesses from the current protocol
A minimal Sequence conformance only needs to define an Iterator type,
with the Element type witness inferred from the Element of the iterator.

This trick didn't always work if the conforming type conformed to other
protocols with declared same-type requirements involving Self.Element.

Refine the heuristic introduced in 23599b667b
to prefer abstract type witnesses in the current protocol, even if there
is a shorter one in another protocol.

Fixes rdar://problem/122574126, rdar://problem/122588328.
2024-02-10 23:55:54 -05:00
Slava Pestov
2ed50ec276 Sema: If the structural occurs check skips a witness, treat it as tautological
Fixes rdar://problem/122584912.
2024-02-10 17:20:14 -05:00
Slava Pestov
f92a67d56e Check type witnesses in a couple of associated type inference tests 2024-02-10 17:20:14 -05:00
Slava Pestov
615f68664f Sema: Fix logic error in sanitizeProtocolRequirements()
If P is our protocol and it has an associated type A, we can't just
fold any dependent member type T.[Q]A to T.[P]A; this only makes
sense if T conforms to P.

Fixes rdar://problem/122587920.
2024-02-10 12:08:44 -05:00
Slava Pestov
8acd573598 Sema: Fix two problems in the solver's witness type matching logic
If we have an inner generic parameter of the requirement on the LHS,
it can't match anything other than an inner generic parameter on the RHS.

If we have the protocol Self type on the LHS, it can only match
something with the same nominal on the RHS. This should be an exact
match but ideally we would also recursively solve dependnet member
types, etc. Skip this all for now.
2024-02-10 12:08:44 -05:00
Slava Pestov
bfdafe6fcc Sema: Fix logic error in associated type inference solver
If matching a candidate value witness against a protocol requirement produced
non-viable bindings, then don't consider the witness in the solver; it can
never lead to a valid solution.
2024-02-10 12:08:44 -05:00
Slava Pestov
c999a87d98 Sema: Fold identical solutions at the end 2024-02-10 12:08:44 -05:00
Slava Pestov
83cb420ee4 Sema: Fix associated type solution ranking
We folded away viable solutions with identical type witnesses;
the first one "wins". However, solutions also store the value
witnesses from which those type witnesses were derived, and
this determines their ranking.

Suppose we have three solutions S_1, S_2, S_3 ranked as follows:

    S_1 < S_2 < S_3

If S_1 and S_3 have identical type witnesses, then one of two
things would happen:

Scenario A:
- we find S_1, and record it.
- we find S_2, and record it.
- we find S_3; it's identical to S_1, so we drop it.

Scenario B:
- we find S_3, and record it.
- we find S_2, and record it.
- we find S_1; it's identical to S_3, so we drop it.

Now, we the best solution Scenario A is S_1, and the best
solution in Scenario B is S_3.

To fix this and ensure we always end up with S_1, remove this
folding of solutions, except for invalid solutions where it
doesn't matter.

To avoid recording too many viable solutions, instead prune the
solution list every time we add a new solution. This maintains the
invariant that no solution is clearly worse than the others; when
we get to the end, we just check if we have exactly one solution,
in which case we know it's the best one.

Fixes rdar://problem/122586685.
2024-02-10 09:36:37 -05:00
Slava Pestov
06b1aee360 Evaluator: Cache circular evaluation to avoid redundant diagnostics
Previously, if a request R evaluated itself N times, we would emit N
"circular reference" diagnostics. These add no value, so instead let's
cache the user-provided default value on the first circular evaluation.

This changes things slightly so that instead of returning an
llvm::Expected<Request::OutputType>, various evaluator methods take
a callback which can produce the default value.

The existing evaluateOrDefault() interface is unchanged, and a new
evaluateOrFatal() entry point replaces
llvm::cantFail(ctx.evaluator(...)).

Direct callers of the evaluator's operator() were updated to pass in
the callback. The benefit of the callback over evaluateOrDefault() is
that if the default value is expensive to constuct, like a dummy
generic signature, we will only construct it in the case where a
cycle actually happened, otherwise we just delete the callback.

(cherry picked from commit b8fcf1c709efa6cd28e1217bd0efe876f7c0d2b7)
2024-02-09 16:02:24 -08:00
Anthony Latsis
ef931342ad AST/ASTWalker: Refactor for recursive MemberTypeRepr representation 2024-02-09 17:33:21 +03:00
Kavon Farvardin
9f3743dec0 NCGenerics: mark hanging tests as unsupported 2024-02-04 19:14:53 -08:00
Slava Pestov
387156ae11 Merge pull request #71354 from slavapestov/check-type-witness-fix
Sema: Fix problems with checkTypeWitness()
2024-02-03 08:28:14 -05:00