Skip to content

Commit 1f1cc10

Browse files
---
yaml --- r: 294911 b: refs/heads/swift-5.1-branch c: 488aebf h: refs/heads/master i: 294909: 92580f5 294907: fc7947e 294903: e4076b3 294895: b766fda 294879: d5e7aad 294847: 52d6c7e 294783: 7fd385b 294655: 3ce9fa3 294399: 1cbca49 293887: bed83aa 292863: 24b455d 290815: 153f385 286719: 533980e 278527: e4902cf 262143: ddec100
1 parent 18cbd6b commit 1f1cc10

File tree

14 files changed

+129
-28
lines changed

14 files changed

+129
-28
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ refs/heads/marcrasi-astverifier-disable: 3fac766a23a77ebd0640296bfd7fc116ea60a4e
12421242
refs/heads/revert-22227-a-tall-white-fountain-played: adfce60b2eaa54903ea189bed8a783bca609fa53
12431243
refs/heads/revert-22300-revert-22227-a-tall-white-fountain-played: 5f92040224df7dd4e618fdfb367349df64d8acad
12441244
refs/heads/swift-5.1-old-llvm-branch: 9cef8175146f25b72806154b8a0f4a3f52e3e400
1245-
refs/heads/swift-5.1-branch: 662238d7b0d1538eb760e2837d25cf7e68cedd17
1245+
refs/heads/swift-5.1-branch: 488aebff01e49aa5309be23fdb2fcd19863790af
12461246
refs/tags/swift-4.2.2-RELEASE: e429d1f1aaf59e69d38207a96e56265c7f6fccec
12471247
refs/tags/swift-5.0-DEVELOPMENT-SNAPSHOT-2019-02-02-a: 3e5a03d32ff3b1e9af90d6c1198c14f938379a6e
12481248
refs/tags/swift-5.0-DEVELOPMENT-SNAPSHOT-2019-02-03-a: 4591c933063ddcb0d6cd6d0cdd01086b2f9b244d

branches/swift-5.1-branch/cmake/modules/SwiftSource.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function(_compile_swift_files
265265

266266
# Force swift 4 compatibility mode for overlays.
267267
if (SWIFTFILE_IS_SDK_OVERLAY)
268-
list(APPEND swift_flags "-swift-version" "4")
268+
list(APPEND swift_flags "-swift-version" "5")
269269
endif()
270270

271271
if(SWIFTFILE_IS_SDK_OVERLAY)

branches/swift-5.1-branch/include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ NOTE(implicit_member_declared_here,none,
5151
"%1 '%0' is implicitly declared", (StringRef, StringRef))
5252
NOTE(extended_type_declared_here,none,
5353
"extended type declared here", ())
54+
NOTE(opaque_return_type_declared_here,none,
55+
"opaque return type declared here", ())
5456

5557
//------------------------------------------------------------------------------
5658
// MARK: Constraint solver diagnostics
@@ -1585,6 +1587,9 @@ ERROR(type_does_not_conform_in_decl_ref,none,
15851587
ERROR(type_does_not_conform_decl_owner,none,
15861588
"%0 %1 requires that %2 conform to %3",
15871589
(DescriptiveDeclKind, DeclName, Type, Type))
1590+
ERROR(type_does_not_conform_in_opaque_return,none,
1591+
"return type of %0 %1 requires that %2 conform to %3",
1592+
(DescriptiveDeclKind, DeclName, Type, Type))
15881593
ERROR(types_not_equal_decl,none,
15891594
"%0 %1 requires the types %2 and %3 be equivalent",
15901595
(DescriptiveDeclKind, DeclName, Type, Type))

branches/swift-5.1-branch/lib/Sema/CSDiagnostics.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,25 @@ ValueDecl *RequirementFailure::getDeclRef() const {
155155
auto *anchor = getRawAnchor();
156156
auto *locator = cs.getConstraintLocator(anchor);
157157

158-
if (isFromContextualType()) {
159-
auto type = cs.getContextualType();
158+
// Get a declaration associated with given type (if any).
159+
// This is used to retrieve affected declaration when
160+
// failure is in any way contextual, and declaration can't
161+
// be fetched directly from constraint system.
162+
auto getAffectedDeclFromType = [](Type type) -> ValueDecl * {
160163
assert(type);
161-
auto *alias = dyn_cast<TypeAliasType>(type.getPointer());
162-
return alias ? alias->getDecl() : type->getAnyGeneric();
163-
}
164+
// If problem is related to a typealias, let's point this
165+
// diagnostic directly to its declaration without desugaring.
166+
if (auto *alias = dyn_cast<TypeAliasType>(type.getPointer()))
167+
return alias->getDecl();
168+
169+
if (auto *opaque = type->getAs<OpaqueTypeArchetypeType>())
170+
return opaque->getDecl();
171+
172+
return type->getAnyGeneric();
173+
};
174+
175+
if (isFromContextualType())
176+
return getAffectedDeclFromType(cs.getContextualType());
164177

165178
if (auto *AE = dyn_cast<CallExpr>(anchor)) {
166179
// NOTE: In valid code, the function can only be a TypeExpr
@@ -196,11 +209,7 @@ ValueDecl *RequirementFailure::getDeclRef() const {
196209
if (overload)
197210
return overload->choice.getDecl();
198211

199-
auto ownerType = getOwnerType();
200-
if (auto *NA = dyn_cast<TypeAliasType>(ownerType.getPointer()))
201-
return NA->getDecl();
202-
203-
return ownerType->getAnyGeneric();
212+
return getAffectedDeclFromType(getOwnerType());
204213
}
205214

206215
GenericSignature *RequirementFailure::getSignature(ConstraintLocator *locator) {
@@ -263,6 +272,28 @@ bool RequirementFailure::diagnoseAsError() {
263272
auto lhs = resolveType(getLHS());
264273
auto rhs = resolveType(getRHS());
265274

275+
if (auto *OTD = dyn_cast<OpaqueTypeDecl>(AffectedDecl)) {
276+
auto *namingDecl = OTD->getNamingDecl();
277+
emitDiagnostic(
278+
anchor->getLoc(), diag::type_does_not_conform_in_opaque_return,
279+
namingDecl->getDescriptiveKind(), namingDecl->getFullName(), lhs, rhs);
280+
281+
TypeLoc returnLoc;
282+
if (auto *VD = dyn_cast<VarDecl>(namingDecl)) {
283+
returnLoc = VD->getTypeLoc();
284+
} else if (auto *FD = dyn_cast<FuncDecl>(namingDecl)) {
285+
returnLoc = FD->getBodyResultTypeLoc();
286+
} else if (auto *SD = dyn_cast<SubscriptDecl>(namingDecl)) {
287+
returnLoc = SD->getElementTypeLoc();
288+
}
289+
290+
if (returnLoc.hasLocation()) {
291+
emitDiagnostic(returnLoc.getLoc(), diag::opaque_return_type_declared_here)
292+
.highlight(returnLoc.getSourceRange());
293+
}
294+
return true;
295+
}
296+
266297
if (genericCtx != reqDC && (genericCtx->isChildContextOf(reqDC) ||
267298
isStaticOrInstanceMember(AffectedDecl))) {
268299
auto *NTD = reqDC->getSelfNominalTypeDecl();

branches/swift-5.1-branch/lib/Sema/MiscDiagnostics.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,9 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
22572257
AbstractFunctionDecl *Implementation;
22582258
OpaqueTypeDecl *OpaqueDecl;
22592259
SmallVector<std::pair<Expr*, Type>, 4> Candidates;
2260+
2261+
bool HasInvalidReturn = false;
2262+
22602263
public:
22612264
OpaqueUnderlyingTypeChecker(TypeChecker &TC,
22622265
AbstractFunctionDecl *Implementation,
@@ -2270,7 +2273,13 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
22702273

22712274
void check() {
22722275
Implementation->getBody()->walk(*this);
2273-
2276+
2277+
// If given function has any invalid returns in the body
2278+
// let's not try to validate the types, since it wouldn't
2279+
// be accurate.
2280+
if (HasInvalidReturn)
2281+
return;
2282+
22742283
// If there are no candidates, then the body has no return statements, and
22752284
// we have nothing to infer the underlying type from.
22762285
if (Candidates.empty()) {
@@ -2349,6 +2358,20 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
23492358
}
23502359
return std::make_pair(false, E);
23512360
}
2361+
2362+
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
2363+
if (auto *RS = dyn_cast<ReturnStmt>(S)) {
2364+
if (RS->hasResult()) {
2365+
auto resultTy = RS->getResult()->getType();
2366+
// If expression associated with return statement doesn't have
2367+
// a type or type has an error, checking opaque types is going
2368+
// to produce incorrect diagnostics.
2369+
HasInvalidReturn |= resultTy.isNull() || resultTy->hasError();
2370+
}
2371+
}
2372+
2373+
return {true, S};
2374+
}
23522375
};
23532376

23542377
} // end anonymous namespace

branches/swift-5.1-branch/stdlib/public/Darwin/AppKit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ add_swift_target_library(swiftAppKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS
99
NSGraphics.swift
1010
NSOpenGL.swift
1111

12-
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} -swift-version 4
12+
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
1313
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
1414
TARGET_SDKS OSX
1515
SWIFT_MODULE_DEPENDS_OSX Darwin CoreData CoreGraphics CoreImage Dispatch Foundation IOKit ObjectiveC QuartzCore XPC # auto-updated

branches/swift-5.1-branch/stdlib/public/Darwin/CloudKit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_swift_target_library(swiftCloudKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES}
1616
CKRecordValue.swift
1717
DefaultParameters.swift
1818

19-
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}" "-swift-version" "4.2"
19+
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
2020
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
2121
TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR WATCHOS WATCHOS_SIMULATOR
2222

branches/swift-5.1-branch/stdlib/public/Darwin/CoreImage/CoreImage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension CIFilter {
3838
for (key, value) in elements {
3939
dict[key] = value
4040
}
41-
self.init(name: name, withInputParameters: dict)
41+
self.init(name: name, parameters: dict)
4242
}
4343
}
4444

branches/swift-5.1-branch/stdlib/public/Darwin/CoreMedia/CMTime.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
extension CMTime {
1616
public init(seconds: Double, preferredTimescale: CMTimeScale) {
17-
self = CMTimeMakeWithSeconds(seconds, preferredTimescale)
17+
self = CMTimeMakeWithSeconds(seconds, preferredTimescale: preferredTimescale)
1818
}
1919

2020
public init(value: CMTimeValue, timescale: CMTimeScale) {
21-
self = CMTimeMake(value, timescale)
21+
self = CMTimeMake(value: value, timescale: timescale)
2222
}
2323
}
2424

@@ -67,7 +67,7 @@ extension CMTime {
6767

6868
public func convertScale(_ newTimescale: Int32, method: CMTimeRoundingMethod)
6969
-> CMTime {
70-
return CMTimeConvertScale(self, newTimescale, method)
70+
return CMTimeConvertScale(self, timescale: newTimescale, method: method)
7171
}
7272
}
7373

branches/swift-5.1-branch/stdlib/public/Darwin/CoreMedia/CMTimeRange.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// CMTimeRangeFromTimeToTime
2525
extension CMTimeRange {
2626
public init(start: CMTime, end: CMTime) {
27-
self = CMTimeRangeFromTimeToTime(start, end)
27+
self = CMTimeRangeFromTimeToTime(start: start, end: end)
2828
}
2929

3030
public var isValid: Bool {
@@ -40,24 +40,24 @@ extension CMTimeRange {
4040
}
4141

4242
public var isEmpty: Bool {
43-
return self.isValid && (self.duration == kCMTimeZero)
43+
return self.isValid && (self.duration == .zero)
4444
}
4545

4646
public var end: CMTime {
4747
return CMTimeRangeGetEnd(self)
4848
}
4949

5050
public func union(_ otherRange: CMTimeRange) -> CMTimeRange {
51-
return CMTimeRangeGetUnion(self, otherRange)
51+
return CMTimeRangeGetUnion(self, otherRange: otherRange)
5252
}
5353
public func intersection(_ otherRange: CMTimeRange) -> CMTimeRange {
54-
return CMTimeRangeGetIntersection(self, otherRange)
54+
return CMTimeRangeGetIntersection(self, otherRange: otherRange)
5555
}
5656
public func containsTime(_ time: CMTime) -> Bool {
57-
return CMTimeRangeContainsTime(self, time)
57+
return CMTimeRangeContainsTime(self, time: time)
5858
}
5959
public func containsTimeRange(_ range: CMTimeRange) -> Bool {
60-
return CMTimeRangeContainsTimeRange(self, range)
60+
return CMTimeRangeContainsTimeRange(self, otherRange: range)
6161
}
6262
}
6363

branches/swift-5.1-branch/stdlib/public/Darwin/Foundation/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ add_swift_target_library(swiftFoundation ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES
6464
UUID.swift
6565
CheckClass.mm
6666

67-
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}" "-Xllvm" "-sil-inline-generics" "-Xllvm" "-sil-partial-specialization" "-swift-version" "5"
67+
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}" "-Xllvm" "-sil-inline-generics" "-Xllvm" "-sil-partial-specialization"
6868
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
6969

7070
SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics Dispatch IOKit ObjectiveC # auto-updated

branches/swift-5.1-branch/stdlib/public/Darwin/UIKit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_swift_target_library(swiftUIKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_
66
UIKit.swift
77
UIKit_FoundationExtensions.swift.gyb
88

9-
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} -swift-version 4.2
9+
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
1010
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
1111
TARGET_SDKS IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR WATCHOS WATCHOS_SIMULATOR
1212
SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics CoreImage Dispatch Foundation ObjectiveC QuartzCore os # auto-updated

branches/swift-5.1-branch/stdlib/public/Darwin/UIKit/UIKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ internal struct _UIViewQuickLookState {
298298
static var views = Set<UIView>()
299299
}
300300

301-
extension UIView : _DefaultCustomPlaygroundQuickLookable {
301+
extension UIView : __DefaultCustomPlaygroundQuickLookable {
302302
@available(*, deprecated, message: "UIView._defaultCustomPlaygroundQuickLook will be removed in a future Swift version")
303303
public var _defaultCustomPlaygroundQuickLook: PlaygroundQuickLook {
304304
if _UIViewQuickLookState.views.contains(self) {

branches/swift-5.1-branch/test/type/opaque.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,45 @@ struct RedeclarationTest {
313313
subscript(redeclared _: Int) -> some Q { return 0 }
314314
subscript(redeclared _: Int) -> P { return 0 }
315315
}
316+
317+
func diagnose_requirement_failures() {
318+
struct S {
319+
var foo: some P { return S() } // expected-note {{declared here}}
320+
// expected-error@-1 {{return type of property 'foo' requires that 'S' conform to 'P'}}
321+
322+
subscript(_: Int) -> some P { // expected-note {{declared here}}
323+
return S()
324+
// expected-error@-1 {{return type of subscript 'subscript(_:)' requires that 'S' conform to 'P'}}
325+
}
326+
327+
func bar() -> some P { // expected-note {{declared here}}
328+
return S()
329+
// expected-error@-1 {{return type of instance method 'bar()' requires that 'S' conform to 'P'}}
330+
}
331+
332+
static func baz(x: String) -> some P { // expected-note {{declared here}}
333+
return S()
334+
// expected-error@-1 {{return type of static method 'baz(x:)' requires that 'S' conform to 'P'}}
335+
}
336+
}
337+
338+
func fn() -> some P { // expected-note {{declared here}}
339+
return S()
340+
// expected-error@-1 {{return type of local function 'fn()' requires that 'S' conform to 'P'}}
341+
}
342+
}
343+
344+
func global_function_with_requirement_failure() -> some P { // expected-note {{declared here}}
345+
return 42 as Double
346+
// expected-error@-1 {{return type of global function 'global_function_with_requirement_failure()' requires that 'Double' conform to 'P'}}
347+
}
348+
349+
func recursive_func_is_invalid_opaque() {
350+
func rec(x: Int) -> some P {
351+
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
352+
if x == 0 {
353+
return rec(x: 0)
354+
}
355+
return rec(x: x - 1)
356+
}
357+
}

0 commit comments

Comments
 (0)