Skip to content

[5.9][CSSimplify] Add special handling if specialized type comes from TypeExpr #66885

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
Jun 24, 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
9 changes: 9 additions & 0 deletions include/swift/Sema/ConstraintLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,15 @@ class ConstraintLocatorBuilder {
return ConstraintLocatorBuilder(this, newElt, newFlags);
}

/// Determine whether this locator builder points directly to a
/// given expression.
template <typename E>
bool directlyAt() const {
if (auto *expr = getAnchor().dyn_cast<Expr *>())
return isa<E>(expr) && hasEmptyPath();
return false;
}

/// Determine whether this builder has an empty path.
bool hasEmptyPath() const {
return !element;
Expand Down
32 changes: 32 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13547,6 +13547,38 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
auto *genericParam = typeVar->getImpl().getGenericParameter();
openedTypes.push_back({genericParam, typeVar});
}
} else if (locator.directlyAt<TypeExpr>()) {
auto *BGT = type1->getAs<BoundGenericType>();
if (!BGT)
return SolutionKind::Error;

decl = BGT->getDecl();

auto genericParams = BGT->getDecl()->getInnermostGenericParamTypes();
if (genericParams.size() != BGT->getGenericArgs().size())
return SolutionKind::Error;

for (unsigned i = 0, n = genericParams.size(); i != n; ++i) {
auto argType = BGT->getGenericArgs()[i];
if (auto *typeVar = argType->getAs<TypeVariableType>()) {
openedTypes.push_back({genericParams[i], typeVar});
} else {
// If we have a concrete substitution then we need to create
// a new type variable to be able to add it to the list as-if
// it is opened generic parameter type.
auto *GP = genericParams[i];

unsigned options = TVO_CanBindToNoEscape;
if (GP->isParameterPack())
options |= TVO_CanBindToPack;

auto *argVar = createTypeVariable(
getConstraintLocator(locator, LocatorPathElt::GenericArgument(i)),
options);
addConstraint(ConstraintKind::Bind, argVar, argType, locator);
openedTypes.push_back({GP, argVar});
}
}
} else {
// If the overload hasn't been resolved, we can't simplify this constraint.
auto overloadLocator = getCalleeLocator(getConstraintLocator(locator));
Expand Down
12 changes: 12 additions & 0 deletions test/decl/ext/specialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ func testNestedExtensions() {

Tree<Int>.Branch<String>.Nest<Void>.Egg.twite()
}

// rdar://111059036 - failed to produce a diagnostic in specialized extension
struct Test {
struct Key<Value> {}
}

class State {
}

extension Test.Key<State> {
static let state = Self<State>()
}