Skip to content

Commit ba412a9

Browse files
authored
Merge pull request #25348 from xedin/rdar-50007727
[ConstraintSystem] Use lightweight conformance check in determining w…
2 parents 5a2fd74 + a80fa91 commit ba412a9

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,15 +1972,21 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
19721972
Type type;
19731973
if (typeExpr->getTypeLoc().wasValidated()) {
19741974
type = typeExpr->getTypeLoc().getType();
1975-
} else if (auto *rep = typeExpr->getTypeRepr()) {
1975+
} else {
19761976
TypeResolutionOptions options(TypeResolverContext::InExpression);
19771977
options |= TypeResolutionFlags::AllowUnboundGenerics;
1978-
auto resolution = TypeResolution::forContextual(DC);
1979-
type = resolution.resolveType(rep, options);
1980-
typeExpr->getTypeLoc().setType(type);
1978+
1979+
auto &typeLoc = typeExpr->getTypeLoc();
1980+
bool hadError =
1981+
TC.validateType(typeLoc, TypeResolution::forContextual(DC), options);
1982+
1983+
if (hadError)
1984+
return nullptr;
1985+
1986+
type = typeLoc.getType();
19811987
}
19821988

1983-
if (!type)
1989+
if (!type || !type->getAnyNominal())
19841990
return nullptr;
19851991

19861992
// Don't bother to convert deprecated selector syntax.
@@ -1989,16 +1995,13 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
19891995
return nullptr;
19901996
}
19911997

1992-
ConformanceCheckOptions options;
1993-
options |= ConformanceCheckFlags::InExpression;
1994-
options |= ConformanceCheckFlags::SkipConditionalRequirements;
1995-
1996-
auto result = TypeChecker::conformsToProtocol(type, protocol, DC, options);
1997-
if (!result || !result->isConcrete())
1998-
return nullptr;
1999-
2000-
return CoerceExpr::forLiteralInit(TC.Context, argExpr, call->getSourceRange(),
2001-
typeExpr->getTypeLoc());
1998+
auto *NTD = type->getAnyNominal();
1999+
SmallVector<ProtocolConformance *, 2> conformances;
2000+
return NTD->lookupConformance(DC->getParentModule(), protocol, conformances)
2001+
? CoerceExpr::forLiteralInit(TC.Context, argExpr,
2002+
call->getSourceRange(),
2003+
typeExpr->getTypeLoc())
2004+
: nullptr;
20022005
}
20032006

20042007
/// Pre-check the expression, validating any types that occur in the

test/Constraints/generics.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,16 @@ func test_generic_subscript_with_missing_arg() {
757757
_ = s[0] // expected-error {{generic parameter 'U' could not be inferred}}
758758
}
759759
}
760+
761+
func rdar_50007727() {
762+
struct A<T> { // expected-note {{'T' declared as parameter to type 'A'}}
763+
struct B<U> : ExpressibleByStringLiteral {
764+
init(stringLiteral value: String) {}
765+
}
766+
}
767+
768+
struct S {}
769+
let _ = A.B<S>("hello")
770+
// expected-error@-1 {{generic parameter 'T' could not be inferred in cast to 'A.B'}}
771+
// expected-note@-2 {{explicitly specify the generic arguments to fix this issue}} {{12-12=<Any>}}
772+
}

0 commit comments

Comments
 (0)