Skip to content

Commit ab59425

Browse files
committed
Fix the TypeChecker's omitNeedlessWords to use interface types. (#7193)
...avoiding a crash when trying to detect near misses of protocol requirements. Unfortunately I can't come up with a test case for the VarDecl changes; everything I try seems to already work. But using interface types is more correct anyway. https://bugs.swift.org/browse/SR-3812
1 parent 16fb513 commit ab59425

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,7 +3548,7 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
35483548

35493549
// Always look at the parameters in the last parameter list.
35503550
for (auto param : *afd->getParameterLists().back()) {
3551-
paramTypes.push_back(getTypeNameForOmission(param->getType())
3551+
paramTypes.push_back(getTypeNameForOmission(param->getInterfaceType())
35523552
.withDefaultArgument(param->isDefaultArgument()));
35533553
}
35543554

@@ -3616,10 +3616,10 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
36163616
Optional<Identifier> TypeChecker::omitNeedlessWords(VarDecl *var) {
36173617
auto &Context = var->getASTContext();
36183618

3619-
if (!var->hasType())
3619+
if (!var->hasInterfaceType())
36203620
validateDecl(var);
36213621

3622-
if (var->isInvalid() || !var->hasType())
3622+
if (var->isInvalid() || !var->hasInterfaceType())
36233623
return None;
36243624

36253625
if (var->getName().empty())
@@ -3650,7 +3650,7 @@ Optional<Identifier> TypeChecker::omitNeedlessWords(VarDecl *var) {
36503650

36513651
// Omit needless words.
36523652
StringScratchSpace scratch;
3653-
OmissionTypeName typeName = getTypeNameForOmission(var->getType());
3653+
OmissionTypeName typeName = getTypeNameForOmission(var->getInterfaceType());
36543654
OmissionTypeName contextTypeName = getTypeNameForOmission(contextType);
36553655
if (::omitNeedlessWords(name, { }, "", typeName, contextTypeName, { },
36563656
/*returnsSelf=*/false, true, allPropertyNames,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
// RUN: %target-swift-frontend -emit-module -D LIB %s -o %t/Lib.swiftmodule
3+
// RUN: %target-swift-frontend -I %t -typecheck %s -verify
4+
// REQUIRES: objc_interop
5+
6+
#if LIB
7+
8+
import Foundation
9+
10+
@objc public protocol Proto {
11+
@objc optional func method(_: Int, for object: NSObject, dividing double: Double)
12+
}
13+
14+
#else
15+
16+
import Foundation
17+
import Lib
18+
19+
class Impl: Proto {
20+
func methodWithInt(_: Int, forObject object: NSObject, dividingDouble: Double) { }
21+
// expected-warning@-1 {{instance method 'methodWithInt(_:forObject:dividingDouble:)' nearly matches optional requirement 'method(_:for:dividing:)' of protocol 'Proto'}}
22+
// expected-note@-2{{rename to 'method(_:for:dividing:)' to satisfy this requirement}}{{8-21=method}}{{30-39=for}}{{58-58=dividing }}{{none}}
23+
// expected-note@-3{{move 'methodWithInt(_:forObject:dividingDouble:)' to an extension to silence this warning}}
24+
// expected-note@-4{{make 'methodWithInt(_:forObject:dividingDouble:)' private to silence this warning}}{{3-3=private }}
25+
}
26+
27+
#endif

0 commit comments

Comments
 (0)