Skip to content

[Sema] Propagate invalid bit during default param checking #28106

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

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 9 additions & 5 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,8 @@ static void checkInheritedDefaultValueRestrictions(ParamDecl *PD) {
}

/// Check the default arguments that occur within this pattern.
static void checkDefaultArguments(TypeChecker &tc, ParameterList *params) {
static void checkDefaultArguments(TypeChecker &tc, Decl *D,
ParameterList *params) {
for (auto *param : *params) {
checkInheritedDefaultValueRestrictions(param);
if (!param->getDefaultValue() ||
Expand All @@ -2051,6 +2052,9 @@ static void checkDefaultArguments(TypeChecker &tc, ParameterList *params) {

if (resultTy) {
param->setDefaultValue(e);
} else {
param->setInvalid();
D->setInvalid();
}

TypeChecker::checkInitializerErrorHandling(initContext, e);
Expand Down Expand Up @@ -2660,7 +2664,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
(void) SD->getImplInfo();

TypeChecker::checkParameterAttributes(SD->getIndices());
checkDefaultArguments(TC, SD->getIndices());
checkDefaultArguments(TC, SD, SD->getIndices());

if (SD->getDeclContext()->getSelfClassDecl()) {
checkDynamicSelfType(SD, SD->getValueInterfaceType());
Expand Down Expand Up @@ -3280,7 +3284,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
if (FD->getDeclContext()->getSelfClassDecl())
checkDynamicSelfType(FD, FD->getResultInterfaceType());

checkDefaultArguments(TC, FD->getParameters());
checkDefaultArguments(TC, FD, FD->getParameters());

// Validate 'static'/'class' on functions in extensions.
auto StaticSpelling = FD->getStaticSpelling();
Expand Down Expand Up @@ -3338,7 +3342,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

if (auto *PL = EED->getParameterList()) {
TypeChecker::checkParameterAttributes(PL);
checkDefaultArguments(TC, PL);
checkDefaultArguments(TC, EED, PL);
}

// We don't yet support raw values on payload cases.
Expand Down Expand Up @@ -3598,7 +3602,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
TC.definedFunctions.push_back(CD);
}

checkDefaultArguments(TC, CD->getParameters());
checkDefaultArguments(TC, CD, CD->getParameters());
}

void visitDestructorDecl(DestructorDecl *DD) {
Expand Down
4 changes: 2 additions & 2 deletions test/Generics/invalid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct X<A> : Hashable {
class Bar {}
}
// expected-note@-4 3 {{arguments to generic parameter 'A' ('Int' and 'Bool') are expected to be equal}}
// expected-note@-5 2 {{arguments to generic parameter 'A' ('Bool' and 'Int') are expected to be equal}}
// expected-note@-5 1 {{arguments to generic parameter 'A' ('Bool' and 'Int') are expected to be equal}}
// expected-note@-6 4 {{arguments to generic parameter 'A' ('Int' and 'Bool') are expected to be equal}}

struct Y<A, B, C>{} // expected-note {{arguments to generic parameter 'A' ('Int' and 'Bool') are expected to be equal}}
Expand All @@ -153,7 +153,7 @@ func multipleArguments(y: Y<Int, Int, Int>) {
func errorMessageVariants(x: X<Int>, x2: X<Bool> = X<Int>()) -> X<Bool> {
// expected-error@-1 {{default argument value of type 'X<Int>' cannot be converted to type 'X<Bool>'}}
let _: X<Bool> = x // expected-error {{cannot assign value of type 'X<Int>' to type 'X<Bool>'}}
errorMessageVariants(x: x2, x2: x2) // expected-error {{cannot convert value of type 'X<Bool>' to expected argument type 'X<Int>'}}
errorMessageVariants(x: x2, x2: x2) // no call site error because the decl is invalid
let _: X<Bool> = { return x }() // expected-error {{cannot convert value of type 'X<Int>' to closure result type 'X<Bool>'}}
let _: [X<Bool>] = [x] // expected-error {{cannot convert value of type 'X<Int>' to expected element type 'X<Bool>'}}
let _ = x as X<Bool> // expected-error {{cannot convert value of type 'X<Int>' to type 'X<Bool>' in coercion}}
Expand Down
6 changes: 0 additions & 6 deletions validation-test/compiler_crashers_2/sr11085.swift

This file was deleted.

6 changes: 6 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr11085.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: not %target-swift-frontend -typecheck %s


func foo(x: Int) {}
func foo(line: String = #line) {}
foo()