-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Diagnostics] When building a subscript don't assume that overload is always present #5819
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1233,11 +1233,32 @@ namespace { | |
ConstraintLocatorBuilder locator, | ||
bool isImplicit, AccessSemantics semantics) { | ||
// Determine the declaration selected for this subscript operation. | ||
auto selected = getOverloadChoice( | ||
auto selected = getOverloadChoiceIfAvailable( | ||
cs.getConstraintLocator( | ||
locator.withPathElement( | ||
ConstraintLocator::SubscriptMember))); | ||
auto choice = selected.choice; | ||
|
||
// Handles situation where there was a solution available but it didn't | ||
// have a proper overload selected from subscript call, might be because | ||
// solver was allowed to return free or unresolved types, which can | ||
// happen while running diagnostics on one of the expressions. | ||
if (!selected.hasValue()) { | ||
auto &tc = cs.TC; | ||
auto baseType = base->getType(); | ||
|
||
if (auto errorType = baseType->getAs<ErrorType>()) { | ||
tc.diagnose(base->getLoc(), diag::cannot_subscript_base, | ||
errorType->getOriginalType()) | ||
.highlight(base->getSourceRange()); | ||
} else { | ||
tc.diagnose(base->getLoc(), diag::cannot_subscript_ambiguous_base) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we assert that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think depending if free type variables are allowed it might be a type variable as well, not sure how much sense would it make it add assertion here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I brought it up because I saw similar assertions in other places that used It'd be fine to do this as a follow-up. |
||
.highlight(base->getSourceRange()); | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
auto choice = selected->choice; | ||
auto subscript = cast<SubscriptDecl>(choice.getDecl()); | ||
|
||
auto &tc = cs.getTypeChecker(); | ||
|
@@ -1255,7 +1276,7 @@ namespace { | |
// Figure out the index and result types. | ||
auto containerTy | ||
= subscript->getDeclContext()->getDeclaredTypeOfContext(); | ||
auto subscriptTy = simplifyType(selected.openedType); | ||
auto subscriptTy = simplifyType(selected->openedType); | ||
auto indexTy = subscriptTy->castTo<AnyFunctionType>()->getInput(); | ||
auto resultTy = subscriptTy->castTo<AnyFunctionType>()->getResult(); | ||
|
||
|
@@ -1282,7 +1303,7 @@ namespace { | |
// Form the subscript expression. | ||
|
||
// Handle dynamic lookup. | ||
if (selected.choice.getKind() == OverloadChoiceKind::DeclViaDynamic || | ||
if (choice.getKind() == OverloadChoiceKind::DeclViaDynamic || | ||
subscript->getAttrs().hasAttribute<OptionalAttr>()) { | ||
base = coerceObjectArgumentToType(base, baseTy, subscript, | ||
AccessSemantics::Ordinary, locator); | ||
|
@@ -1308,13 +1329,13 @@ namespace { | |
solution.computeSubstitutions( | ||
subscript->getInterfaceType(), | ||
dc, | ||
selected.openedFullType, | ||
selected->openedFullType, | ||
getConstraintSystem().getConstraintLocator( | ||
locator.withPathElement(ConstraintLocator::SubscriptMember)), | ||
substitutions); | ||
|
||
// Convert the base. | ||
auto openedFullFnType = selected.openedFullType->castTo<FunctionType>(); | ||
auto openedFullFnType = selected->openedFullType->castTo<FunctionType>(); | ||
auto openedBaseType = openedFullFnType->getInput(); | ||
containerTy = solution.simplifyType(tc, openedBaseType); | ||
base = coerceObjectArgumentToType( | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// RUN: not %target-swift-frontend %s -typecheck | ||
|
||
_ = try [ { return .D($0[0]) } ] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// RUN: not %target-swift-frontend %s -typecheck | ||
|
||
_ = [1].reduce([:]) { $0[$1] } |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// RUN: not %target-swift-frontend %s -emit-silgen | ||
|
||
enum X { | ||
init?(a: Int) { | ||
.p[a] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, this makes me wonder: are all direct uses of
getOverloadChoice
withinCSApply.cpp
suspect because of this issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They might be, I saw couple of valid ones too, do you want to be to double-check and maybe create separate PR to address any issues I find?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be greatly appreciated!