Skip to content

Commit c7cd497

Browse files
authored
[AutoDiff] Improve @derivative and @transpose diagnostics. (#32717)
Improve `@derivative` and `@transpose` type-checking diagnostics for resolving the referenced original declaration. Previously, an error was produced on one invalid candidate at the attribute's location. This did not indicate the invalid candidate's location or the total number of invalid candidates. Now: - Diagnostic notes are produced on all invalid candidates at their location. Invalid candidates' descriptive declaration kind are shown for clarity. - Derivative registration for protocol requirements (not yet supported, TF-982) now has a clear, dedicated diagnostic. - The "original declaration type mismatch" diagnostic is improved for expected original function types with generic signatures. The message now accurately reads "candidate does not have type equal to *or less constrained than* ...", instead of "candidate does not have expected type ...". Resolves SR-13150. Paves the way for future diagnostic improvements: SR-13151, SR-13152.
1 parent 9c20198 commit c7cd497

File tree

4 files changed

+326
-211
lines changed

4 files changed

+326
-211
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,10 +3095,16 @@ ERROR(derivative_attr_class_member_dynamic_self_result_unsupported,none,
30953095
ERROR(derivative_attr_nonfinal_class_init_unsupported,none,
30963096
"cannot register derivative for 'init' in a non-final class; consider "
30973097
"making %0 final", (Type))
3098+
ERROR(derivative_attr_unsupported_accessor_kind,none,
3099+
"cannot register derivative for %0", (/*accessorKind*/ DescriptiveDeclKind))
30983100
// TODO(SR-13096): Remove this temporary diagnostic.
30993101
ERROR(derivative_attr_class_setter_unsupported,none,
31003102
"cannot yet register derivative for class property or subscript setters",
31013103
())
3104+
// TODO(TF-982): Remove this temporary diagnostic.
3105+
NOTE(derivative_attr_protocol_requirement_unsupported,none,
3106+
"cannot yet register derivative default implementation for protocol "
3107+
"requirements", ())
31023108
ERROR(derivative_attr_original_already_has_derivative,none,
31033109
"a derivative already exists for %0", (DeclName))
31043110
NOTE(derivative_attr_duplicate_note,none,
@@ -3134,15 +3140,25 @@ NOTE(transpose_attr_wrt_self_self_type_mismatch_note,none,
31343140
"%1", (Type, Type))
31353141

31363142
// Automatic differentiation attributes
3137-
ERROR(autodiff_attr_original_decl_invalid_kind,none,
3138-
"%0 is not a 'func', 'init', 'subscript', or 'var' computed property "
3139-
"declaration", (DeclNameRef))
3140-
ERROR(autodiff_attr_accessor_not_found,none,
3141-
"%0 does not have a '%1' accessor", (DeclNameRef, StringRef))
3142-
ERROR(autodiff_attr_original_decl_none_valid_found,none,
3143-
"could not find function %0 with expected type %1", (DeclNameRef, Type))
3144-
ERROR(autodiff_attr_original_decl_not_same_type_context,none,
3145-
"%0 is not defined in the current type context", (DeclNameRef))
3143+
ERROR(autodiff_attr_original_decl_ambiguous,none,
3144+
"referenced declaration %0 is ambiguous", (DeclNameRef))
3145+
NOTE(autodiff_attr_original_decl_ambiguous_candidate,none,
3146+
"candidate %0 found here", (DescriptiveDeclKind))
3147+
ERROR(autodiff_attr_original_decl_none_valid,none,
3148+
"referenced declaration %0 could not be resolved", (DeclNameRef))
3149+
NOTE(autodiff_attr_original_decl_invalid_kind,none,
3150+
"candidate %0 is not a 'func', 'init', 'subscript', or 'var' computed "
3151+
"property declaration", (DescriptiveDeclKind))
3152+
NOTE(autodiff_attr_original_decl_missing_accessor,none,
3153+
"candidate %0 does not have a %1",
3154+
(DescriptiveDeclKind, /*accessorDeclKind*/ DescriptiveDeclKind))
3155+
NOTE(autodiff_attr_original_decl_type_mismatch,none,
3156+
"candidate %0 does not have "
3157+
"%select{expected type|type equal to or less constrained than}2 %1",
3158+
(DescriptiveDeclKind, Type, /*hasGenericSignature*/ bool))
3159+
NOTE(autodiff_attr_original_decl_not_same_type_context,none,
3160+
"candidate %0 is not defined in the current type context",
3161+
(DescriptiveDeclKind))
31463162
ERROR(autodiff_attr_original_void_result,none,
31473163
"cannot differentiate void function %0", (DeclName))
31483164
ERROR(autodiff_attr_original_multiple_semantic_results,none,

0 commit comments

Comments
 (0)