Skip to content

Commit eaf0532

Browse files
committed
[Diagnostics] Relax contextual type presence from assertion to a check
In some situations `getContextualType` for a contextual type locator is going to return then empty type. This happens because e.g. optional-some patterns and patterns with incorrect type don't have a contextual type for initialization expression but use a conversion with contextual locator nevertheless to indicate the purpose. This doesn't affect non-ambiguity diagnostics because mismatches carry both `from` and `to` types. Resolves: rdar://problem/103739206
1 parent 60952b8 commit eaf0532

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,11 @@ class Solution {
16761676
Type getContextualType(ASTNode anchor) const {
16771677
for (const auto &entry : contextualTypes) {
16781678
if (entry.first == anchor) {
1679-
return simplifyType(entry.second.getType());
1679+
// The contextual information record could contain the purpose
1680+
// without a type i.e. when the context is an optional-some or
1681+
// an invalid pattern binding.
1682+
if (auto contextualTy = entry.second.getType())
1683+
return simplifyType(contextualTy);
16801684
}
16811685
}
16821686
return Type();

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4342,7 +4342,15 @@ static bool diagnoseAmbiguityWithContextualType(
43424342
auto name = result->choices.front().getName();
43434343
auto contextualTy = solution.getContextualType(anchor);
43444344

4345-
assert(contextualTy);
4345+
// In some situations `getContextualType` for a contextual type
4346+
// locator is going to return then empty type. This happens because
4347+
// e.g. optional-some patterns and patterns with incorrect type don't
4348+
// have a contextual type for initialization expression but use
4349+
// a conversion with contextual locator nevertheless to indicate
4350+
// the purpose. This doesn't affect non-ambiguity diagnostics
4351+
// because mismatches carry both `from` and `to` types.
4352+
if (!contextualTy)
4353+
return false;
43464354

43474355
DE.diagnose(getLoc(anchor),
43484356
contextualTy->is<ProtocolType>()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: not %target-swift-frontend %s -typecheck
2+
3+
protocol RawTokenKindSubset {}
4+
5+
struct Parser {
6+
func canRecoverTo<Subset: RawTokenKindSubset>(anyIn subset: Subset.Type) {
7+
if let (kind, handle) = self.at(anyIn: subset) {
8+
}
9+
}
10+
11+
func at(_ keyword: Int) -> Bool {}
12+
13+
func at(
14+
<<<<<<< HEAD (Note: diff markers are required for reproduction of the crash)
15+
) -> Bool {
16+
=======
17+
) -> Bool {
18+
>>>>>>> My commit message (don't remove)
19+
}
20+
}

0 commit comments

Comments
 (0)