Skip to content

Commit 3a68c81

Browse files
committed
[MiscDiagnostics] Run opaque return checker only if all of the return expressions are correct
(cherry picked from commit caad4a8)
1 parent c4ec484 commit 3a68c81

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

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

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)