Skip to content

Diagnose differentiable functions returning Void w/o inout arguments. #63080

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
Jan 18, 2023
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
6 changes: 6 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5334,6 +5334,12 @@ ERROR(differentiable_function_type_invalid_result,none,
"%select{| and satisfy '%0 == %0.TangentVector'}1, but the enclosing "
"function type is '@differentiable%select{|(_linear)}1'",
(StringRef, bool))
ERROR(differentiable_function_type_void_result,
none,
"'@differentiable' function returning Void must have at least one "
"differentiable inout parameter, i.e. a non-'@noDerivative' parameter "
"whose type conforms to 'Differentiable'",
())
ERROR(differentiable_function_type_no_differentiability_parameters,
none,
"'@differentiable' function type requires at least one differentiability "
Expand Down
22 changes: 22 additions & 0 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,28 @@ bool TypeChecker::diagnoseInvalidFunctionType(FunctionType *fnTy, SourceLoc loc,
diag.highlight((*repr)->getResultTypeRepr()->getSourceRange());
}
}

// If the result type is void, we need to have at least one differentiable
// inout argument
if (result->isVoid() &&
llvm::find_if(params,
[&](AnyFunctionType::Param param) {
if (param.isNoDerivative())
return false;
return param.isInOut() &&
TypeChecker::isDifferentiable(param.getPlainType(),
/*tangentVectorEqualsSelf*/ isLinear,
dc, stage);
}) == params.end()) {
auto diagLoc = repr ? (*repr)->getResultTypeRepr()->getLoc() : loc;
auto resultStr = fnTy->getResult()->getString();
auto diag = ctx.Diags.diagnose(
diagLoc, diag::differentiable_function_type_void_result);
hadAnyError = true;

if (repr)
diag.highlight((*repr)->getResultTypeRepr()->getSourceRange());
}
}

return hadAnyError;
Expand Down
4 changes: 4 additions & 0 deletions test/AutoDiff/Sema/differentiable_func_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ let _: @differentiable(_linear) (Float) -> NonDiffType

let _: @differentiable(_linear) (Float) -> Float

// expected-error @+1 {{'@differentiable' function returning Void must have at least one differentiable inout parameter, i.e. a non-'@noDerivative' parameter whose type conforms to 'Differentiable'}}
let _: @differentiable(reverse) (Float) -> Void
let _: @differentiable(reverse) (inout Float) -> Void // okay

// expected-error @+1 {{result type '@differentiable(reverse) (U) -> Float' does not conform to 'Differentiable', but the enclosing function type is '@differentiable'}}
func test1<T: Differentiable, U: Differentiable>(_: @differentiable(reverse) (T) -> @differentiable(reverse) (U) -> Float) {}
// expected-error @+1 {{result type '(U) -> Float' does not conform to 'Differentiable', but the enclosing function type is '@differentiable'}}
Expand Down