Skip to content

Commit 3a043a2

Browse files
committed
Sema: Hacky fix for a crash in CSDiag
It looks like the mapTypeIntoContext() call is using the wrong 'dc', because in fact we have no way of knowing what the original 'dc' was which produced the type parameters in question. Really the problem is we're picking apart SubstitutedType here, which is almost always the wrong thing to do. I don't think SubstitutedType should exist at all. To avoid the crash, just bail-out if 'dc' is not generic. I'm intentionally avoiding a principled fix here to hasten the apocalypse when all of CSDiag.cpp will collapse under its own weight and get rewritten in a sane way.
1 parent e52857a commit 3a043a2

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

Diff for: lib/Sema/CSDiag.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ static bool findGenericSubstitutions(DeclContext *dc, Type paramType,
10431043

10441044
bool mismatch(SubstitutableType *paramType, TypeBase *argType) {
10451045
Type type = paramType;
1046-
if (dc && type->isTypeParameter())
1046+
if (dc && dc->getGenericParamsOfContext() && type->isTypeParameter())
10471047
type = ArchetypeBuilder::mapTypeIntoContext(dc, paramType);
10481048

10491049
if (auto archetype = type->getAs<ArchetypeType>()) {
@@ -1062,7 +1062,7 @@ static bool findGenericSubstitutions(DeclContext *dc, Type paramType,
10621062
paramType.findIf([&](Type type) -> bool {
10631063
if (auto substitution = dyn_cast<SubstitutedType>(type.getPointer())) {
10641064
Type original = substitution->getOriginal();
1065-
if (dc && original->isTypeParameter())
1065+
if (dc && dc->getGenericParamsOfContext() && original->isTypeParameter())
10661066
original = ArchetypeBuilder::mapTypeIntoContext(dc, original);
10671067

10681068
Type replacement = substitution->getReplacementType();

Diff for: test/Generics/invalid.swift

+61-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-parse-verify-swift
1+
// RUN: %target-parse-verify-swift -enable-experimental-nested-generic-types
22

33
func bet() where A : B {} // expected-error {{'where' clause cannot be attached to a non-generic declaration}}
44

@@ -11,3 +11,63 @@ protocol he where A : B { // expected-error {{'where' clause cannot be attached
1111

1212
associatedtype vav where A : B // expected-error {{'where' clause cannot be attached to an associated type declaration}}
1313
}
14+
15+
16+
struct Lunch<T> {
17+
struct Dinner<U> {
18+
19+
var leftovers: T
20+
var transformation: (T) -> U
21+
}
22+
}
23+
24+
class Deli<Spices> {
25+
26+
class Pepperoni {}
27+
struct Sausage {}
28+
}
29+
30+
struct Pizzas<Spices> {
31+
class NewYork {
32+
}
33+
34+
class DeepDish {
35+
}
36+
}
37+
38+
class HotDog {
39+
}
40+
41+
struct Pepper {}
42+
struct ChiliFlakes {}
43+
44+
func eatDinnerConcrete(d: Pizzas<ChiliFlakes>.NewYork,
45+
t: Deli<ChiliFlakes>.Pepperoni) {
46+
}
47+
48+
func eatDinnerConcrete(d: Pizzas<Pepper>.DeepDish,
49+
t: Deli<Pepper>.Pepperoni) {
50+
}
51+
52+
func badDiagnostic1() {
53+
54+
_ = Lunch<Pizzas<Pepper>.NewYork>.Dinner<HotDog>( // expected-error {{expression type 'Lunch<Pizzas<Pepper>.NewYork>.Dinner<HotDog>' is ambiguous without more context}}
55+
leftovers: Pizzas<ChiliFlakes>.NewYork(),
56+
transformation: { _ in HotDog() })
57+
}
58+
59+
func badDiagnostic2() {
60+
61+
let firstCourse = Pizzas<ChiliFlakes>.NewYork()
62+
63+
var dinner = Lunch<Pizzas<ChiliFlakes>.NewYork>.Dinner<HotDog>(
64+
leftovers: firstCourse,
65+
transformation: { _ in HotDog() })
66+
67+
let topping = Deli<Pepper>.Pepperoni()
68+
69+
eatDinnerConcrete(d: firstCourse, t: topping)
70+
// expected-error@-1 {{cannot invoke 'eatDinnerConcrete' with an argument list of type '(d: Pizzas<ChiliFlakes>.NewYork, t: Deli<Pepper>.Pepperoni)'}}
71+
// expected-note@-2 {{overloads for 'eatDinnerConcrete' exist with these partially matching parameter lists: (d: Pizzas<ChiliFlakes>.NewYork, t: Deli<ChiliFlakes>.Pepperoni), (d: Pizzas<Pepper>.DeepDish, t: Deli<Pepper>.Pepperoni)}}
72+
73+
}

0 commit comments

Comments
 (0)