Skip to content

[TypeChecker] Fix a crash in inherited default argument type-checking #42264

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
merged 1 commit into from
Apr 9, 2022
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
9 changes: 8 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7099,12 +7099,19 @@ void ParamDecl::setDefaultExpr(Expr *E, bool isTypeChecked) {
"Can't overwrite type-checked default with un-type-checked default");
}
defaultInfo->DefaultArg = E;
defaultInfo->ExprType = E->getType();
// `Inherited` default arguments do not have an expression,
// so if the storage has been pre-allocated already we need
// to be careful requesting type here.
defaultInfo->ExprType = E ? E->getType() : Type();
defaultInfo->InitContextAndIsTypeChecked.setInt(isTypeChecked);
}

void ParamDecl::setDefaultExprType(Type type) {
if (!DefaultValueAndFlags.getPointer()) {
// If there is no type, let's not allocate storage.
if (!type)
return;

DefaultValueAndFlags.setPointer(
getASTContext().Allocate<StoredDefaultArgument>());
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,10 @@ Expr *DefaultArgumentExprRequest::evaluate(Evaluator &evaluator,

Type DefaultArgumentTypeRequest::evaluate(Evaluator &evaluator,
ParamDecl *param) const {
if (auto expr = param->getTypeCheckedDefaultExpr())
return expr->getType();
if (auto *expr = param->getTypeCheckedDefaultExpr()) {
// If the request failed, let's not propagate ErrorType up.
return isa<ErrorExpr>(expr) ? Type() : expr->getType();
}
return Type();
}

Expand Down
32 changes: 32 additions & 0 deletions test/Constraints/type_inference_from_default_exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,35 @@ func test_allow_same_type_between_dependent_types() {
s.test() // expected-error {{instance method 'test' requires the types 'String' and 'Default.X' (aka 'Int') be equivalent}}
}
}

// Crash when default type is requested before inherited constructor is type-checked

protocol StorageType {
var identifier: String { get }
}

class Storage {
}

extension Storage {
struct Test {
static let test = CustomStorage<String>("") // triggers default type request
}
}

class BaseStorage<T> : Storage, StorageType {
enum StorageType {
case standard
}

let identifier: String
let type: StorageType

init(_ id: String, type: StorageType = .standard) {
self.identifier = id
self.type = type
}
}

final class CustomStorage<T>: BaseStorage<T> { // Ok - no crash typechecking inherited init
}