Skip to content

Commit 4be0aad

Browse files
authored
Merge pull request #17280 from xedin/rdar-41141944-4.2
[4.2][TypeChecker] When formatting witness type for diagnostics don't assu…
2 parents d055bdf + 88ad74b commit 4be0aad

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,13 @@ static void checkRedeclaration(TypeChecker &tc, ValueDecl *current) {
937937
if (!other->isAccessibleFrom(currentDC))
938938
continue;
939939

940-
const auto markInvalid = [&current, &tc]() {
940+
const auto markInvalid = [&current]() {
941941
current->setInvalid();
942942
if (auto *varDecl = dyn_cast<VarDecl>(current))
943943
if (varDecl->hasType())
944-
varDecl->setType(ErrorType::get(tc.Context));
944+
varDecl->setType(ErrorType::get(varDecl->getType()));
945945
if (current->hasInterfaceType())
946-
current->setInterfaceType(ErrorType::get(tc.Context));
946+
current->setInterfaceType(ErrorType::get(current->getInterfaceType()));
947947
};
948948

949949
// Thwart attempts to override the same declaration more than once.

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,17 @@ static Type getTypeForDisplay(ModuleDecl *module, ValueDecl *decl) {
16661666
}
16671667
}
16681668

1669+
// Redeclaration checking might mark a candidate as `invalid` and
1670+
// reset it's type to ErrorType, let's dig out original type to
1671+
// make the diagnostic better.
1672+
if (auto errorType = type->getAs<ErrorType>()) {
1673+
auto originalType = errorType->getOriginalType();
1674+
if (!originalType || !originalType->is<AnyFunctionType>())
1675+
return type;
1676+
1677+
type = originalType;
1678+
}
1679+
16691680
return type->castTo<AnyFunctionType>()->getResult();
16701681
}
16711682

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
6+
@objc protocol P {
7+
func foo(a arg: Int) // expected-note {{protocol requires function 'foo(a:)' with type '(Int) -> ()'; do you want to add a stub?}}
8+
}
9+
10+
class C : P {
11+
// expected-error@-1 {{type 'C' does not conform to protocol 'P'}}
12+
var foo: Float = 0.75
13+
// expected-note@-1 {{'foo' previously declared here}}
14+
// expected-note@-2 {{candidate is not a function}}
15+
func foo() {}
16+
// expected-error@-1 {{invalid redeclaration of 'foo()'}}
17+
// expected-note@-2 {{candidate has non-matching type '() -> ()'}}
18+
}

0 commit comments

Comments
 (0)