Skip to content

Commit 3562dfd

Browse files
committed
---
yaml --- r: 294875 b: refs/heads/swift-5.1-branch c: 3a68c81 h: refs/heads/master i: 294873: 31dc3b4 294871: 6d89610
1 parent c93518c commit 3562dfd

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
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: c4ec484dae5c337d004e9a542b18481e579eb524
1245+
refs/heads/swift-5.1-branch: 3a68c81e08296ba9754286f3c8085d9176c494ef
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/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/test/type/opaque.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,36 +318,40 @@ func diagnose_requirement_failures() {
318318
struct S {
319319
var foo: some P { return S() } // expected-note {{declared here}}
320320
// expected-error@-1 {{return type of property 'foo' requires that 'S' conform to 'P'}}
321-
// expected-error@-2 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
322321

323322
subscript(_: Int) -> some P { // expected-note {{declared here}}
324-
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
325323
return S()
326324
// expected-error@-1 {{return type of subscript 'subscript(_:)' requires that 'S' conform to 'P'}}
327325
}
328326

329327
func bar() -> some P { // expected-note {{declared here}}
330-
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
331328
return S()
332329
// expected-error@-1 {{return type of instance method 'bar()' requires that 'S' conform to 'P'}}
333330
}
334331

335332
static func baz(x: String) -> some P { // expected-note {{declared here}}
336-
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
337333
return S()
338334
// expected-error@-1 {{return type of static method 'baz(x:)' requires that 'S' conform to 'P'}}
339335
}
340336
}
341337

342338
func fn() -> some P { // expected-note {{declared here}}
343-
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
344339
return S()
345340
// expected-error@-1 {{return type of local function 'fn()' requires that 'S' conform to 'P'}}
346341
}
347342
}
348343

349344
func global_function_with_requirement_failure() -> some P { // expected-note {{declared here}}
350-
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
351345
return 42 as Double
352346
// expected-error@-1 {{return type of global function 'global_function_with_requirement_failure()' requires that 'Double' conform to 'P'}}
353347
}
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)