Skip to content

[ClangImporter] Don't crash when a bad override affects NSErrors. #7936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1771,15 +1771,28 @@ adjustResultTypeForThrowingFunction(ForeignErrorConvention::Info errorInfo,
switch (errorInfo.TheKind) {
case ForeignErrorConvention::ZeroResult:
case ForeignErrorConvention::NonZeroResult:
// Check for a bad override.
if (resultTy->isVoid())
return Type();
return TupleType::getEmpty(resultTy->getASTContext());

case ForeignErrorConvention::NilResult:
resultTy = resultTy->getAnyOptionalObjectType();
assert(resultTy &&
"result type of NilResult convention was not imported as optional");
if (Type unwrappedTy = resultTy->getAnyOptionalObjectType())
return unwrappedTy;
// Check for a bad override.
if (resultTy->isVoid())
return Type();
// It's possible an Objective-C method overrides the base method to never
// fail, and marks the method _Nonnull to indicate that. Swift can't
// represent that, but it shouldn't fall over either.
return resultTy;

case ForeignErrorConvention::ZeroPreservedResult:
// Check for a bad override.
if (resultTy->isVoid())
return Type();
return resultTy;

case ForeignErrorConvention::NonNilError:
return resultTy;
}
Expand Down
26 changes: 26 additions & 0 deletions test/ClangImporter/foreign_errors.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-silgen -parse-as-library -verify %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -O -parse-as-library -DEMIT_SIL %s

// REQUIRES: objc_interop

import Foundation
import errors

#if !EMIT_SIL
func test0() {
try ErrorProne.fail() // expected-error {{errors thrown from here are not handled}}
}
#endif

// Test "AndReturnError" stripping.
// rdar://20722195
Expand Down Expand Up @@ -71,10 +74,12 @@ func testBlockFinal() throws {
try ErrorProne.runSwiftly(5000, callback: {})
}

#if !EMIT_SIL
func testNonBlockFinal() throws {
ErrorProne.runWithError(count: 0) // expected-error {{missing argument for parameter #1 in call}}
ErrorProne.run(count: 0) // expected-error {{incorrect argument label in call (have 'count:', expected 'callback:')}}
}
#endif

class VeryErrorProne : ErrorProne {
override class func fail() throws {}
Expand Down Expand Up @@ -108,3 +113,24 @@ func testNSErrorExhaustive() {
}
}
}

func testBadOverrides(obj: FoolishErrorSub) throws {
try obj.performRiskyOperation()
let _: FoolishErrorSub = try obj.produceRiskyOutput()
let _: String = try obj.produceRiskyString()

let _: NSObject = try obj.badNullResult()
let _: CInt = try obj.badNullResult2() // This is unfortunate but consistent.
let _: CInt = try obj.badZeroResult()
try obj.badNonzeroResult() as Void

let base = obj as SensibleErrorBase
try base.performRiskyOperation()
let _: NSObject = try base.produceRiskyOutput()
let _: String = try base.produceRiskyString()

let _: NSObject = try base.badNullResult()
let _: NSObject = try base.badNullResult2()
let _: CInt = try base.badZeroResult()
try base.badNonzeroResult() as Void
}
25 changes: 25 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,28 @@
- (BOOL) obliterate: (NSError**) error;
- (BOOL) invigorate: (NSError**) error callback: (void(^)(void)) block;
@end


@interface SensibleErrorBase: NSObject
- (BOOL)performRiskyOperationAndReturnError:(NSError **)error;
- (nullable NSObject *)produceRiskyOutputAndReturnError:(NSError **)error;
- (nullable NSString *)produceRiskyStringAndReturnError:(NSError **)error;

- (nullable NSObject *)badNullResult:(NSError **)err __attribute__((swift_error(null_result)));
- (nullable NSObject *)badNullResult2:(NSError **)err __attribute__((swift_error(null_result)));
- (int)badZeroResult:(NSError **)err __attribute__((swift_error(zero_result)));
- (int)badNonzeroResult:(NSError **)err __attribute__((swift_error(nonzero_result)));
@end

@interface FoolishErrorSub : SensibleErrorBase
// This is invalid, but Swift shouldn't crash when it sees it.
- (void)performRiskyOperationAndReturnError:(NSError **)error;
// This should technically be valid in Objective-C as a covariant return.
- (nonnull FoolishErrorSub *)produceRiskyOutputAndReturnError:(NSError **)error;
- (nonnull NSString *)produceRiskyStringAndReturnError:(NSError **)error;

- (void)badNullResult:(NSError **)err;
- (int)badNullResult2:(NSError **)err;
- (void)badZeroResult:(NSError **)err;
- (void)badNonzeroResult:(NSError **)err;
@end