Skip to content

Commit d1b8084

Browse files
committed
[Sema] Avoid resolving placeholders for protocol requirement params
We can't ever have default arguments in protocols anyway.
1 parent 8102e39 commit d1b8084

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2317,9 +2317,17 @@ static Type validateParameterType(ParamDecl *decl) {
23172317
: TypeResolverContext::FunctionInput);
23182318
options |= TypeResolutionFlags::Direct;
23192319

2320+
// We allow placeholders in parameter types to improve recovery since if a
2321+
// default argument is present we can suggest the inferred type. Avoid doing
2322+
// this for protocol requirements though since those can't ever have default
2323+
// arguments anyway.
2324+
HandlePlaceholderTypeReprFn placeholderOpener;
2325+
if (!isa<ProtocolDecl>(dc->getParent()))
2326+
placeholderOpener = PlaceholderType::get;
2327+
23202328
const auto resolution =
23212329
TypeResolution::forInterface(dc, options, unboundTyOpener,
2322-
PlaceholderType::get,
2330+
placeholderOpener,
23232331
/*packElementOpener*/ nullptr);
23242332

23252333
if (isa<VarargTypeRepr>(nestedRepr)) {

test/Sema/placeholder_type.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,12 @@ do {
294294

295295
// Make sure we reject placeholders here.
296296
protocol TestPlaceholderRequirement {
297-
func foo(_:_) // expected-error {{type placeholder may not appear in top-level parameter}}
297+
func foo(_:_) // expected-error {{type placeholder not allowed here}}
298298
func bar() -> _ // expected-error {{type placeholder not allowed here}}
299299
func baz() -> [_] // expected-error {{type placeholder not allowed here}}
300-
func qux(_: [_]) // expected-error {{type placeholder may not appear in top-level parameter}}
300+
func qux(_: [_]) // expected-error {{type placeholder not allowed here}}
301301

302-
// FIXME: Shouldn't diagnose twice
303-
subscript(_: _) -> Void { get } // expected-error 2{{type placeholder may not appear in top-level parameter}}
302+
subscript(_: _) -> Void { get } // expected-error {{type placeholder not allowed here}}
304303
subscript() -> _ { get } // expected-error {{type placeholder not allowed here}}
305304
}
306305

@@ -311,6 +310,7 @@ var testPlaceholderComputed1: _ { 0 } // expected-error {{type placeholder not a
311310
var testPlaceholderComputed2: [_] { [0] } // expected-error {{type placeholder not allowed here}}
312311

313312
struct TestPlaceholderSubscript {
313+
// FIXME: Shouldn't diagnose twice.
314314
subscript(_: _) -> Void { () } // expected-error 2{{type placeholder may not appear in top-level parameter}}
315315
subscript(_: [_]) -> Void { () } // expected-error 2{{type placeholder may not appear in top-level parameter}}
316316
subscript() -> _ { () } // expected-error {{type placeholder not allowed here}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// {"signature":"swift::CanTypeVisitor<swift::TypeMatcher<(anonymous namespace)::AssociatedTypeInference::getPotentialTypeWitnessesByMatchingTypes(swift::ValueDecl*, swift::ValueDecl*)::MatchVisitor>::MatchVisitor, bool, swift::Type, swift::Type>::visit(swift::CanType, swift::Type, swift::Type)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
protocol a { associatedtype b func c(_ : _ d: b }
44
extension a { c(_ : _ d: b struct e : a

0 commit comments

Comments
 (0)