Skip to content

[5.8][Diagnostics] Relax contextual type presence from assertion to a check #62843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,11 @@ class Solution {
Type getContextualType(ASTNode anchor) const {
for (const auto &entry : contextualTypes) {
if (entry.first == anchor) {
return simplifyType(entry.second.getType());
// The contextual information record could contain the purpose
// without a type i.e. when the context is an optional-some or
// an invalid pattern binding.
if (auto contextualTy = entry.second.getType())
return simplifyType(contextualTy);
}
}
return Type();
Expand Down
10 changes: 9 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4335,7 +4335,15 @@ static bool diagnoseAmbiguityWithContextualType(
auto name = result->choices.front().getName();
auto contextualTy = solution.getContextualType(anchor);

assert(contextualTy);
// 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.
if (!contextualTy)
return false;

DE.diagnose(getLoc(anchor),
contextualTy->is<ProtocolType>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: not %target-swift-frontend %s -typecheck

protocol RawTokenKindSubset {}

struct Parser {
func canRecoverTo<Subset: RawTokenKindSubset>(anyIn subset: Subset.Type) {
if let (kind, handle) = self.at(anyIn: subset) {
}
}

func at(_ keyword: Int) -> Bool {}

func at(
<<<<<<< HEAD (Note: diff markers are required for reproduction of the crash)
) -> Bool {
=======
) -> Bool {
>>>>>>> My commit message (don't remove)
}
}